Skip to content

Commit d8faa7a

Browse files
committed
Reject malformed UTF-8
This is common with break-in attempts and just floods the log with garbage.
1 parent 8c99de0 commit d8faa7a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

wcfsetup/install/files/lib/action/ApiAction.class.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace wcf\action;
44

55
use CuyZ\Valinor\Mapper\MappingError;
6+
use CuyZ\Valinor\Utility\String\StringFormatterError;
67
use FastRoute\ConfigureRoutes;
78
use FastRoute\Dispatcher\Result\MethodNotAllowed;
89
use FastRoute\Dispatcher\Result\NotMatched;
@@ -98,6 +99,8 @@ static function (ConfigureRoutes $r) {
9899
return $this->toErrorResponse(RequestFailure::ValidationFailed, $e->getType(), $e->getMessage(), $e->getField());
99100
} catch (IllegalLinkException) {
100101
return $this->toErrorResponse(RequestFailure::ValidationFailed, 'assertion_failed');
102+
} catch (StringFormatterError) {
103+
return $this->toErrorResponse(RequestFailure::InternalError, 'malformed_utf8');
101104
} catch (\Throwable $e) {
102105
logThrowable($e);
103106

wcfsetup/install/files/lib/http/middleware/HandleValinorMappingErrors.class.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CuyZ\Valinor\Mapper\MappingError;
66
use CuyZ\Valinor\Mapper\Tree\Message\NodeMessage;
7+
use CuyZ\Valinor\Utility\String\StringFormatterError;
78
use Laminas\Diactoros\Response\HtmlResponse;
89
use Laminas\Diactoros\Response\JsonResponse;
910
use Psr\Http\Message\ResponseInterface;
@@ -32,10 +33,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3233
{
3334
try {
3435
return $handler->handle($request);
35-
} catch (MappingError $e) {
36-
$message = "Could not map type '{$e->type()}'.";
37-
$errors = $e->messages()
38-
->formatWith(new PrependPath());
36+
} catch (MappingError | StringFormatterError $e) {
37+
if ($e instanceof MappingError) {
38+
$message = "Could not map type '{$e->type()}'.";
39+
$errors = $e->messages()
40+
->formatWith(new PrependPath());
41+
} else {
42+
$message = "Rejected malformed UTF-8 input.";
43+
$errors = [];
44+
}
3945

4046
$preferredType = Helper::getPreferredContentType($request, [
4147
'application/json',

0 commit comments

Comments
 (0)