Reject unpaired JSON surrogate escapes#1085
Draft
He-Pin wants to merge 1 commit into
Draft
Conversation
6aee07c to
8974a1e
Compare
Motivation: std.parseJson and strict .json import fast paths accepted or normalized JSON strings containing unpaired UTF-16 surrogate escapes. That allowed invalid JSON Unicode to become Jsonnet strings, diverging from cpp-jsonnet and jrsonnet behavior and from the intent of rejecting malformed JSON input. Modification: Add a shared surrogate validator for JSON visitors, enable it for std.parseJson, and convert strict .json import surrogate failures into normal Jsonnet errors instead of falling back or producing raw invalid strings. Keep the import fast path allocation-conscious with a private nullable return contract, leaving any reusable OptionVal abstraction for a separate PR. Result: Lone high/low surrogate values and keys now fail with Invalid JSON: unpaired surrogate in string, valid surrogate pairs still materialize as codepoint strings, escaped literal text such as \\uD800 remains ordinary text, and loose Jsonnet .json files can still fall back through the Jsonnet parser. References: https://jsonnet.org/ref/stdlib.html#std-parseJson google/go-jsonnet#885 com-lihaoyi/upickle#722
8974a1e to
07ed891
Compare
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.
Motivation
Jsonnet strings are defined in Unicode-codepoint terms, and sjsonnet already rejects unpaired surrogate escapes in Jsonnet source strings.
std.parseJsonand the strict.jsonimport fast path could still let JSON strings containing lone UTF-16 surrogate halves through as Jsonnet strings or defer the failure until a later string operation.The better long-term fix belongs in ujson, where surrogate-pair validation can happen while decoding
\uXXXXescapes without a downstream pass. This PR stays draft as a downstream guard while the upstream issue is tracked.Modification
ValVisitor.rejectUnpairedSurrogatesfor decoded JSON string values and object keys.std.parseJson, returning a normalInvalid JSON: unpaired surrogate in stringerror..jsonimport fast path and preloader so imported JSON cannot expose malformed strings."\\uD800".parseJsonImportOrNullcontract; a reusableOptionValabstraction is intentionally left for a separate PR.Result
std.parseJsonnow aligns with cpp-jsonnet and jrsonnet by rejecting unpaired surrogate escapes, while still accepting valid surrogate pairs. go-jsonnet currently normalizes these cases to U+FFFD.std.codepoint(std.parseJson("\"\\uD800\""))5529665533std.codepoint(std.parseJson("\"\\uDC00\""))5632065533std.codepoint(std.parseJson("\"\\uD83D\\uDE00\""))128512128512128512128512128512std.codepoint(std.objectFields(std.parseJson("{\"\\uD800\":1}"))[0])5529665533.jsonimport{"s":"\\uD800"}thenstd.codepoint((import "surrogate.json").s)std.codepointerrors on a malformed stringInvalid JSON: unpaired surrogate in string.jsonimport{"s":"\\uD83D\\uDE00"}thenstd.codepoint((import "pair.json").s)128512128512Import rows are sjsonnet-specific because this PR changes sjsonnet's
.jsonimport fast path.References
std.parseJsondocumentation: https://jsonnet.org/ref/stdlib.html#std-parseJson