For Object, stringify mapping key as YAML rather than JSON #104
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.
With this change, mapping keys are stringified during
YAML.parse()
orYAML.Document#toJSON()
as YAML rather than JSON representations:The reason for this change is this npm advisory, which is related to this issue: nodeca/js-yaml#475 -- which also affects this library. Essentially, it's possible to abuse aliases to create an exponential tree of nodes consisting of collections that each have multiple references. When such a tree is used as the key of a mapping that is then converted to a JS object,
JSON.stringify()
was previously called on it, and the process runs out of memory creating the string.Stringifying the key as YAML rather than JSON allows for the exponential explosion to be averted, as the repeated references use the same anchors as in the original input.
This change will be a part of the next minor release. It could arguably require a major version bump, but I figure that if you're working with structures that may include collections or aliases as keys and you care about the contents of said keys, you're almost certainly already using
mapAsMap: true
to avoid the stringification, and so this change should not break any reasonable implementations.While this fix patches the library's own vulnerability to this type of attack, I'm considering whether I need to do something more when parsing this type of input, as the library user may not expect it to be able to return structures that are not safe to e.g. stringify as JSON.