feat: decoder hardening with proper errors#1
Merged
Merged
Conversation
Decoder hardening for untrusted JSON input: - Cap Struct/Value/ListValue recursion at 100 (matching protobuf's CodedInputStream / JsonFormat limit) so deeply nested input fails with a clean JSONException instead of StackOverflowError. - Normalize decode errors to fastjson2's native JSONException with position context (offset/line/column): malformed int64/uint64/float/double, timestamp, duration, base64, enum names, and numeric/bool map keys. Keep internal/config errors (missing TypeRegistry, bad target Class, unreachable invariants) as IllegalState/IllegalArgumentException so a config bug never masquerades as bad input. For Any, a missing registry is a config error (IllegalStateException) while a client-submitted unresolvable @type is user-facing (JSONException + position). - Any @type-first fast path: resolve the content descriptor up front and decode the remaining fields straight off the live reader, skipping the LinkedHashMap buffer + JSON.toJSONString + re-parse the old path required. - Validate bool map keys ("true"/"false" only) instead of silently coercing any string to false (which also collided keys). - Guard a null/missing packed-WKT "value" in an Any (previously an uncaught NullPointerException for Timestamp/Duration/BytesValue); google.protobuf.Value with null becomes NullValue, mirroring the top-level field rule. - Route generated decoders' bytes/enum/numeric+bool map-key parsing through public FieldReader helpers so codegen shares the identical JSONException contract (no inline BASE64.decode / Enum.valueOf / Long.parseLong). Add BuffJsonHardeningTest (recursion depth, parse-error offsets, Any field-order parity, native error types, packed-WKT null value, bool map keys), validating codegen and runtime paths. Full suite green; decode throughput on par with prior baselines and encoder allocation within committed budgets.
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.
Decoder hardening for untrusted JSON input:
Add BuffJsonHardeningTest (recursion depth, parse-error offsets, Any field-order parity, native error types, packed-WKT null value, bool map keys), validating codegen and runtime paths. Full suite green; decode throughput on par with prior baselines and encoder allocation within committed budgets.