Substitute invalid UTF-8 in JSON error renderer output - #3465
Conversation
json_encode() returns false for strings with invalid UTF-8 sequences, so exception messages or titles carrying raw binary data produced an empty response body under a JSON content type. Pass JSON_INVALID_UTF8_SUBSTITUTE so invalid sequences are replaced with the UTF-8 replacement character instead of discarding the whole payload.
|
I changed line 41 of JsonErrorRenderer back to There's a couple of problems with the test. First, the which doesn't include the message we're looking for. Secondly asserting that the key Can you update the test to prove that the change works? |
The previous test invoked the renderer with displayErrorDetails=false, so only the title was encoded and the invalid exception message never reached json_encode. Assert the decoded exception message contains U+FFFD so the test fails if JSON_INVALID_UTF8_SUBSTITUTE is removed.
|
|
json_encode()returnsfalsewhen any string in the payload contains an invalid UTF-8 sequence, so a JSON error response for an exception whose message or title carries raw binary data (invalid-UTF-8 user input echoed into exception messages is the common path) was emitted as a zero-length body underContent-Type: application/json. Strict API clients then fail to parse the empty body and the actual error information is silently discarded.This adds
JSON_INVALID_UTF8_SUBSTITUTEso invalid byte sequences are replaced with the UTF-8 replacement character instead of failing the whole encode. The renderer output stays valid JSON in all cases.Reproduction on 4.x before this change: