From fe1122bd4d6d0587d7f4350cf35a62242785ea2e Mon Sep 17 00:00:00 2001 From: Andre Klapper Date: Sun, 17 Dec 2023 11:41:42 +0100 Subject: [PATCH] Catch RuntimeException: mb_convert_encoding(): Illegal character encoding specified at PhabricatorTextDocumentEngine.php:73 Summary: When given `$encoding` is invalid, catch the exception to show a proper error message and make the server logs provide more hints. ``` EXCEPTION: (RuntimeException) mb_convert_encoding(): Illegal character encoding specified at [/src/error/PhutilErrorHandler.php:261] #0 <#2> PhutilErrorHandler::handleError(integer, string, string, integer, array) called at [/src/error/PhutilErrorHandler.php:261] #1 <#2> mb_convert_encoding(string, string, string) called at [/src/applications/files/document/PhabricatorTextDocumentEngine.php:73] ``` Closes T15624 Test Plan: Open a URL which passes a bogus encoding value as parameter, like `/source/somerepository/browse/master/README.md?as=source&encode=TROLOLOL` Reviewers: O1 Blessed Committers, valerio.bozzolan Reviewed By: O1 Blessed Committers, valerio.bozzolan Subscribers: Sten, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno Maniphest Tasks: T15624 Differential Revision: https://we.phorge.it/D25418 --- .../document/PhabricatorTextDocumentEngine.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/applications/files/document/PhabricatorTextDocumentEngine.php b/src/applications/files/document/PhabricatorTextDocumentEngine.php index 2fce98baf1..8f04a56751 100644 --- a/src/applications/files/document/PhabricatorTextDocumentEngine.php +++ b/src/applications/files/document/PhabricatorTextDocumentEngine.php @@ -70,10 +70,16 @@ abstract class PhabricatorTextDocumentEngine $encoding = $this->getEncodingConfiguration(); if ($encoding !== null) { if (function_exists('mb_convert_encoding')) { - $content = mb_convert_encoding($content, 'UTF-8', $encoding); - $this->encodingMessage = pht( - 'This document was converted from %s to UTF8 for display.', - $encoding); + try { + $content = mb_convert_encoding($content, 'UTF-8', $encoding); + $this->encodingMessage = pht( + 'This document was converted from %s to UTF8 for display.', + $encoding); + } catch (Throwable $ex) { + $this->encodingMessage = pht( + 'Unable to convert from requested encoding %s to UTF8.', + $encoding); + } } else { $this->encodingMessage = pht( 'Unable to perform text encoding conversion: mbstring extension '.