Skip to content

fix: use strict UTF-8 decoding in json() to reject invalid bytes#1879

Open
kshyatisekhar-panda wants to merge 1 commit intonode-fetch:mainfrom
kshyatisekhar-panda:fix/json-strict-utf8-decoding
Open

fix: use strict UTF-8 decoding in json() to reject invalid bytes#1879
kshyatisekhar-panda wants to merge 1 commit intonode-fetch:mainfrom
kshyatisekhar-panda:fix/json-strict-utf8-decoding

Conversation

@kshyatisekhar-panda
Copy link
Copy Markdown

Fixes #1853

Summary

  • response.json() was silently accepting invalid JSON with malformed UTF-8 (e.g. lone surrogate pairs like \ud83d)
  • Root cause: json() called this.text(), which uses TextDecoder with default replacement mode (fatal: false). This silently replaces invalid UTF-8 bytes with U+FFFD before JSON.parse() sees them, so invalid data parses without error.
  • Fix: json() now calls consumeBody() directly and decodes with new TextDecoder('utf-8', {fatal: true}), so invalid UTF-8 in JSON responses throws a TypeError instead of being silently corrupted.
  • text() remains unchanged — replacement mode is correct for plain text per the Fetch spec.

Test plan

  • All 388 existing tests pass
  • Invalid UTF-8 in JSON responses now correctly throws a TypeError

Fixes node-fetch#1853. response.json() was using TextDecoder with default replacementmode, which silently replaced invalid UTF-8 (e.g. lone surrogates) withU+FFFD, allowing invalid JSON to be parsed without error. This changesjson() to use fatal: true, so invalid UTF-8 throws instead of beingsilently corrupted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

response.json() allows invalid JSON through

1 participant