fix(protobuf): reject cyclic collections when decoding Struct/Value - #309
Open
Amayyas wants to merge 1 commit into
Open
fix(protobuf): reject cyclic collections when decoding Struct/Value#309Amayyas wants to merge 1 commit into
Amayyas wants to merge 1 commit into
Conversation
Decoding a cyclic Dart collection recursed until the stack overflowed:
final map = <String, Object?>{};
map['x'] = map;
Value.fromJson(map); // StackOverflowError
`Value`, `Struct` and `ListValue` decode into each other, so the decoding
functions now thread the set of containers currently being decoded through
the recursion and throw a `FormatException` when one is re-entered. This
matches the existing behaviour for other undecodable input, such as
`Value.fromJson(DateTime.now())`.
Lists are affected in the same way as maps, as are cycles that span both
(a map holding a list that holds the map), so all of them are covered.
A collection that is merely repeated rather than cyclic, such as the same
map referenced by two keys, still decodes: entries are removed from the set
once decoded, so only ancestors are rejected.
Only decoding is affected. `Struct.fields` is final, so a cyclic object
graph cannot be built in the first place and encoding cannot recurse.
Fixes googleapis#285
Contributor
There was a problem hiding this comment.
Code Review
This pull request adds unit tests to verify the behavior of JSON decoding for cyclic and repeated acyclic structures in Struct, ListValue, and Value classes. Specifically, it introduces tests to ensure that cyclic maps and lists are rejected with a FormatException, while repeated but acyclic references are successfully decoded. There are no review comments, and I have no additional feedback to provide.
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.
Decoding a cyclic Dart collection recursed until the stack overflowed:
Value,StructandListValuedecode into each other, so the decoding functions now thread the set of containers currently being decoded through the recursion, and throw aFormatExceptionwhen one is re-entered.Notes
FormatExceptionwas chosen for consistency with the existing behaviour for other undecodable input —Value.fromJson(DateTime.now())already throws it. Happy to switch toArgumentErrorif you'd rather treat it as a caller-side mistake.Struct.fieldsisfinal, so a cyclic object graph cannot be constructed in the first place and encoding cannot recurse.dart format,dart analyze,dart test(180 passing) and the protojson conformance suite (211 passing) are all clean.Fixes #285