Repository navigation
Conversation
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.
There was a problem hiding this comment.
🟢 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
approved these changes
Oct 24, 2026
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:gives
"caf\xe9.txt"in the output, andjson.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()intojsonescape()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.