Skip to content

tool_writeout_json: escape bytes that are not UTF-8 - #23584

Closed
Marti2203 wants to merge 2 commits into
curl:masterfrom
Marti2203:header-json-utf8
Closed

Marti2203 wants to merge 2 commits into
curl:masterfrom
Marti2203:header-json-utf8

Conversation

@Marti2203

Copy link
Copy Markdown
Contributor

jsonquoted() copies every byte from 0x80 up as-is, so a header with bytes that are not valid UTF-8 makes %{header_json} output that is not valid JSON (RFC 8259 requires UTF-8). For example, a server sending a Latin-1 filename:

Content-Disposition: attachment; filename="caf\xe9.txt"

gives "caf\xe9.txt" in the output, and json.loads() fails with UnicodeDecodeError. The same applies to %{json} and to {{name:json}} with --variable.

This keeps valid UTF-8 sequences as they are and escapes any other byte as \u00XX, treating it as ISO-8859-1, so nothing is lost: the above becomes "café.txt". Replacing such bytes with U+FFFD would be the other option, but it loses data.

Test 1671 gets three more headers: a Latin-1 byte, a valid UTF-8 sequence (unchanged) and a cut UTF-8 sequence.

Note: the open #23535 (by another author) refactors jsonquoted() into jsonescape() but still passes bytes >= 0x80 through, so it does not change this. The two touch the same lines; whichever lands second can be rebased, and I am happy to rebase this onto #23535 if it goes in first.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually; the patch was manually reviewed.

jsonquoted() copied all bytes from 0x80 and up as-is, so a header value
or other string with bytes that are not valid UTF-8 made %{header_json}
and %{json} produce output that is not valid JSON (RFC 8259 requires
UTF-8). Keep valid UTF-8 sequences, and escape any other byte as
\u00XX, as if it was ISO-8859-1.

Extend test 1671 with such headers.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The bounded validation preserves existing escaping behavior, and targeted regression coverage supports the change with no unresolved findings.

No open issues 🎉

What changed in this PR

This PR makes the curl tool’s JSON escaping preserve valid UTF-8 while escaping invalid bytes as \u00XX, preventing malformed JSON in header output, write-out variables, and variable expansion.

Changes:

  • Adds bounded UTF-8 validation, rejecting truncated sequences, overlong encodings, surrogates, and out-of-range code points.
  • Extends test 1671 with Latin-1, valid UTF-8, and truncated UTF-8 header values.
File Description
tests/​data/​test1671 Adds regression coverage for valid and invalid UTF-8 headers.
src/​tool_writeout_json.c Validates UTF-8 sequences and escapes invalid bytes during JSON encoding.

Review effort: Balanced

@bagder

bagder commented Oct 24, 2026

Copy link
Copy Markdown
Member

Thanks!

@bagder bagder closed this in 2565bd1 Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants