Repository navigation
Description
Description
YAML tag names are fully user-controlled, via verbatim tags (!) or a %TAG shorthand. js-yaml 5.x resolves them by indexing a plain {} object, so any name that exists on Object.prototype — constructor, toString, proto, valueOf, and 8 others — resolves to an inherited member instead of falling through to the intended "unknown tag" error.
The inherited value is truthy, so it is accepted as a tag definition and dereferenced as one. This produces type confusion and throws a raw TypeError. js-yaml documents and otherwise consistently throws YAMLException for malformed input, so this error escapes the error handling of every consumer that follows the documented contract.
Details
src/schema.ts:41 builds the tag lookup tables as plain objects, which inherit from Object.prototype:
function createTagDefinitionMap (): TagDefinitionMap {
return {
scalar: {}, // <-- inherits from Object.prototype
sequence: {},
mapping: {}
}
}src/parser/constructor.ts:139 resolves document tag names against them:
function lookupTag (exact, prefix, tagName) {
const exactTag = exact[tagName] // <-- unguarded; tagName comes from the document
if (exactTag) return exactTag
for (const tag of prefix) {
if (tagName.startsWith(tag.tagName)) return tag
}
return undefined // the intended "unknown tag" path
}return undefined is what drives the unknown ${nodeKind} tag error in findExplicitTag. Inherited members never reach it, because exact['constructor'] is already truthy.
The confused value is then used as a tag definition:
- scalar path →
scalarTag.resolve(...)
→TypeError: scalarTag.resolve is not a function - collection path →
definition.tag.create(definition.tagName)
(constructor.ts:399,:421)
The collection path is the clearer illustration. For !<constructor>, exact.mapping['constructor'] is the Object constructor, so definition.tag.create(definition.tagName) evaluates to:
Object.create('constructor')
// TypeError: Object prototype may only be an Object or null: constructorPoS
import { load, YAMLException } from 'js-yaml'
const check = (doc) => {
try { load(doc); return 'loaded (no error)' } catch (e) {
return `${e.constructor.name} | instanceof YAMLException: ${e instanceof YAMLException} | ${e.message.split('\n')[0]}`
}
}
// a tag name that is not registered and not on Object.prototype.
console.log('!<nope> ', check('!<nope> x'))
//a tag name that IS on Object.prototype.
console.log('!<constructor>', check('!<constructor> x'))
console.log('!<toString> ', check('!<toString>\n a: 1\n'))
Activity
fix: prevent prototype fallback in tag and harden object lookups, #782 …
puzrin commentedon Jul 31, 2026
Thanks. Fixed the reported places and hardened the checks in the others for sure.
For all: while bug is not nice, it does not leave try/catch guard, if you follow safety recommendations
Update dependency js-yaml to v5 (#516)