You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Specific use of !!binary causes TypeError: Cannot read properties of undefined (reading 'buffer') when .toJS()/.toJSON() #610
Describe the bug
The following YAML causes an unhandled error when calling parse() or parseDocument().toJS(), and the error message does not sound like it is intended behavior.
? ? !!binary ? !!binary
To Reproduce
Welcome to Node.js v22.14.0.
Type ".help" for more information.
> const {parse} = require("yaml")
undefined
> parse('? ? !!binary ? !!binary')
Uncaught TypeError: Cannot read properties of undefined (reading 'buffer')
at Object.stringify (yaml-fuzz/node_modules/yaml/dist/schema/yaml-1.1/binary.js:43:51)
at Object.stringify (yaml-fuzz/node_modules/yaml/dist/stringify/stringify.js:120:18)
at Object.stringifyPair (yaml-fuzz/node_modules/yaml/dist/stringify/stringifyPair.js:34:25)
at Pair.toString (yaml-fuzz/node_modules/yaml/dist/nodes/Pair.js:33:29)
at Object.stringify (yaml-fuzz/node_modules/yaml/dist/stringify/stringify.js:95:21)
at stringifyFlowCollection (yaml-fuzz/node_modules/yaml/dist/stringify/stringifyCollection.js:106:29)
at Object.stringifyCollection (yaml-fuzz/node_modules/yaml/dist/stringify/stringifyCollection.js:10:12)
at YAMLMap.toString (yaml-fuzz/node_modules/yaml/dist/nodes/YAMLMap.js:136:36)
at stringifyKey (yaml-fuzz/node_modules/yaml/dist/nodes/addPairToJSMap.js:51:28)
> (node:1635) [TAG_RESOLVE_FAILED] YAMLWarning: Unresolved tag: tag:yaml.org,2002:binary at line 1, column 5:
? ? !!binary ? !!binary
^^^^^^^^
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1635) Warning: Keys with collection values will be stringified due to JS Object restrictions: "". Set mapAsMap: true to use object keys.
Expected behaviour
I would expect it to return the JS/JSON representation, or throw a YAMLParseError. I'm not quite sure what the expected value is as I just bumped into this error while fuzzing yaml.
Versions (please complete the following information):
This was due to a pretty ridiculous combination of factors:
The fallback behaviour when parsing explicit tags that expect scalar values, but are given a collection value, still includes the tag name in the parsed node.
Including a valid YAML 1.1 tag includes the tag in the document's schema.
With the default option value mapAsMap: false, mappings are represented as JS objects, which require a serialization of the keys.
The serialization of !!binary and !!timestamp did not expect to be called on invalid nodes, which is what we got from step 1.
The warning here is now better, and the TypeError is avoided.
Activity
cxcorp commentedon Mar 15, 2025
This seems to be something related to using complex keys with tags, as the following also produces an error:
? ? !!timestamp ? !!timestamp 2025-03-15T15:35:58.586Ze.g. with Node REPL
eemeli commentedon Mar 20, 2025
This was due to a pretty ridiculous combination of factors:
mapAsMap: false, mappings are represented as JS objects, which require a serialization of the keys.!!binaryand!!timestampdid not expect to be called on invalid nodes, which is what we got from step 1.The warning here is now better, and the TypeError is avoided.
cxcorp commentedon Mar 20, 2025
Wow! Seems like just the kind of thing only a fuzzer would find 😅 Thank you!
fix: Improve warning & avoid TypeError on bad YAML 1.1 nodes (fixes e…