Skip to content

Commit 3105455

Browse files
committed
Replace maxMergeSeqLengthoption with maxTotalMergeKeys (more robust)
1 parent 39d00d6 commit 3105455

6 files changed

Lines changed: 76 additions & 76 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## [Unreleased]
10+
### Added
11+
- Added `maxTotalMergeKeys` (10000) loader option to limit the total number of
12+
keys processed by YAML merge (`<<`) across one `load()` / `loadAll()` call.
13+
14+
### Changed
15+
- `maxMergeSeqLength` replaced with `maxTotalMergeKeys` for limiting YAML merge
16+
processing.
17+
18+
919
## [5.1.0] - 2026-06-23
1020
### Added
1121
- Collection tags can finalize an incrementally populated carrier into a
@@ -75,8 +85,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7585
- Added `docs/safety.md` with notes about processing untrusted YAML.
7686
- Added `maxDepth` (100) loader option. Not a problem, but gives a better
7787
exception instead of RangeError on stack overflow.
78-
- Added `maxMergeSeqLength` (20) loader option. Not a problem after `merge` fix,
79-
but an additional restriction for safety.
88+
- Added a loader option limiting merge sequence length. Not a problem after
89+
`merge` fix, but an additional restriction for safety.
8090
- Added sourcemaps to `dist/` builds.
8191

8292
### Changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ options:
6969
error.
7070
- `maxDepth` _(default: 100)_ - limits the nesting depth for collections (does
7171
not take aliases into account).
72-
- `maxMergeSeqLength` _(default: 20)_ - limits the number of items in merge
73-
(`<<`) sequences.
72+
- `maxTotalMergeKeys` _(default: 10000)_ - limits the total number of keys
73+
processed by merge (`<<`) across one `load()` / `loadAll()` call. Set to `-1`
74+
to disable.
7475

7576
> [!NOTE]
7677
>

‎src/parser/constructor.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ interface ConstructorOptions {
8080
filename?: string
8181
schema?: Schema
8282
json?: boolean
83-
maxMergeSeqLength?: number
83+
maxTotalMergeKeys?: number
8484
}
8585

8686
// `source` is input data, not config — so it has no default here.
8787
const DEFAULT_CONSTRUCTOR_OPTIONS: Required<Omit<ConstructorOptions, 'source'>> = {
8888
filename: '',
8989
schema: CORE_SCHEMA,
9090
json: false,
91-
maxMergeSeqLength: 20
91+
maxTotalMergeKeys: 10000
9292
}
9393

9494
interface ConstructorState extends Required<ConstructorOptions> {
@@ -99,6 +99,7 @@ interface ConstructorState extends Required<ConstructorOptions> {
9999
frames: Frame[]
100100
anchors: Map<string, Anchor>
101101
tagHandlers: TagHandlers
102+
totalMergeKeys: number
102103
}
103104

104105
function eventPosition (event: Event) {
@@ -252,6 +253,10 @@ function isMappingTag (tag: AnyTag): tag is MappingTagDefinition<any, any> {
252253
// precedence: an already-present key (explicit or from an earlier source) wins.
253254
function mergeKeys (state: ConstructorState, frame: MappingFrame, source: unknown, sourceTag: MappingTagDefinition<any, any>) {
254255
for (const sourceKey of sourceTag.keys(source)) {
256+
if (state.maxTotalMergeKeys !== -1 && ++state.totalMergeKeys > state.maxTotalMergeKeys) {
257+
throwError(state, `merge keys exceeded maxTotalMergeKeys (${state.maxTotalMergeKeys})`)
258+
}
259+
255260
if (frame.tag.has(frame.value, sourceKey)) continue
256261

257262
const err = frame.tag.addPair(frame.value, sourceKey, sourceTag.get(source, sourceKey))
@@ -270,11 +275,7 @@ function mergeSource (state: ConstructorState, frame: MappingFrame, source: unkn
270275
if (isMappingTag(sourceTag)) {
271276
mergeKeys(state, frame, source, sourceTag)
272277
} else if (sourceTag.nodeKind === 'sequence' && Array.isArray(source)) {
273-
const seen = new Set<unknown>()
274278
for (const element of source) {
275-
// Dedup identical sources (`<<: [*a, *a]`); the first one wins anyway.
276-
if (seen.has(element)) continue
277-
seen.add(element)
278279
mergeKeys(state, frame, element, frame.tag)
279280
}
280281
} else {
@@ -308,14 +309,11 @@ function addValue (state: ConstructorState, value: unknown, tag: AnyTag) {
308309
frame.hasValue = true
309310
} else if (frame.kind === 'sequence') {
310311
if (frame.merge) {
311-
// Element of a `<<: [...]` list: validate it is a mapping and cap the
312-
// length, then collect it like any other item for the target to fold in.
312+
// Element of a `<<: [...]` list: validate it is a mapping, then collect
313+
// it like any other item for the target to fold in.
313314
if (!isMappingTag(tag)) {
314315
throwError(state, 'cannot merge mappings; the provided source object is unacceptable')
315316
}
316-
if (frame.index >= state.maxMergeSeqLength) {
317-
throwError(state, `merge sequence length exceeded maxMergeSeqLength (${state.maxMergeSeqLength})`)
318-
}
319317
}
320318
const err = frame.tag.addItem(frame.value, value, frame.index++)
321319
if (err) throwError(state, err)
@@ -361,7 +359,8 @@ function constructFromEvents (events: Event[], options: ConstructorOptions): unk
361359
position: 0,
362360
frames: [],
363361
anchors: new Map(),
364-
tagHandlers: Object.create(null)
362+
tagHandlers: Object.create(null),
363+
totalMergeKeys: 0
365364
}
366365

367366
while (state.eventIndex < state.events.length) {

‎test/core/pathological.test.mjs‎

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
import { describe, it } from 'node:test'
2-
import assert from 'node:assert'
32
import { load, YAMLException, YAML11_SCHEMA } from 'js-yaml'
4-
import workerpool from 'workerpool'
5-
6-
// Resolved in the main thread; passed into the worker since the worker's
7-
// eval scope has no `require`. `import()` is syntax, so it survives eval.
8-
const yamlUrl = import.meta.resolve('js-yaml')
9-
10-
async function loadYamlInWorker (doc, url, options) {
11-
const mod = await import(url)
12-
mod.load(doc, options)
13-
}
143

154
function assertYamlException (fn, pattern) {
165
try {
176
fn()
187
} catch (error) {
19-
assert(
20-
error instanceof YAMLException,
21-
`expected YAMLException, got ${error.name}`
22-
)
23-
if (pattern) assert.match(error.message, pattern)
8+
if (!(error instanceof YAMLException)) {
9+
throw new Error(`expected YAMLException, got ${error.name}`)
10+
}
11+
if (pattern && !pattern.test(error.message)) {
12+
throw new Error(`expected ${error.message} to match ${pattern}`)
13+
}
2414
return
2515
}
2616

27-
assert.fail('expected YAMLException')
17+
throw new Error('expected YAMLException')
2818
}
2919

30-
function createRepeatedMergeAliasPattern (repetitions, keys) {
31-
const src = Array.from({ length: keys }, (_, i) => `k${i}: 0`).join(', ')
20+
function createMergeChain (count) {
21+
const lines = ['a0: &a0 { k0: 0 }']
3222

33-
return `
34-
a: &a {${src}}
35-
b: { <<: [ ${'*a, '.repeat(repetitions - 1)}*a ] }
36-
`
23+
for (let i = 1; i < count; i++) {
24+
lines.push(`a${i}: &a${i} { <<: *a${i - 1}, k${i}: ${i} }`)
25+
}
26+
27+
lines.push(`b: *a${count - 1}`)
28+
return `${lines.join('\n')}\n`
3729
}
3830

3931
describe('Pathological tests', () => {
@@ -50,24 +42,10 @@ describe('Pathological tests', () => {
5042
})
5143

5244
describe('Merge aliases', () => {
53-
it('loads repeated merge aliases with many keys', async () => {
54-
const doc = createRepeatedMergeAliasPattern(100000, 100000)
55-
const pool = workerpool.pool()
56-
try {
57-
await pool.exec(loadYamlInWorker, [doc, yamlUrl, { maxMergeSeqLength: 1000000 }])
58-
.timeout(10 * 1000)
59-
} finally {
60-
await pool.terminate()
61-
}
62-
})
63-
64-
it('throws YAMLException on long merge sequence (over maxMergeSeqLength)', () => {
45+
it('throws YAMLException when merge chain exceeds maxTotalMergeKeys', () => {
6546
assertYamlException(() => {
66-
load(`
67-
a: &a { k: 0 }
68-
b: { <<: [ ${'*a, '.repeat(20)}*a ] }
69-
`, { schema: YAML11_SCHEMA })
70-
}, /merge sequence length exceeded maxMergeSeqLength/)
47+
load(createMergeChain(100000), { schema: YAML11_SCHEMA })
48+
}, /merge keys exceeded maxTotalMergeKeys/)
7149
})
7250
})
7351
})

‎test/core/tags/merge.test.mjs‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,6 @@ foo: bar
5858
)
5959
})
6060

61-
it('deduplicates repeated merge sequence sources', () => {
62-
// This test is coverage only, to toggle optional deduplication branch
63-
const src = `
64-
base: &b { x: 1 }
65-
merged: { <<: [*b, *b], y: 2 }
66-
`
67-
const expected = {
68-
base: { x: 1 },
69-
merged: { x: 1, y: 2 }
70-
}
71-
72-
assert.deepStrictEqual(
73-
load(src, { schema: CORE_SCHEMA.withTags(mergeTag) }),
74-
expected
75-
)
76-
})
77-
7861
it('throws when the target mapping tag rejects a merged pair', () => {
7962
const src = `
8063
--- !!set

‎test/core/units/load-options.test.mjs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { describe, it } from 'node:test'
22
import assert from 'node:assert/strict'
33
import { load, loadAll, FAILSAFE_SCHEMA, YAML11_SCHEMA, YAMLException } from 'js-yaml'
44

5+
function createMergeChain (count) {
6+
const lines = ['a0: &a0 { k0: 0 }']
7+
8+
for (let i = 1; i < count; i++) {
9+
lines.push(`a${i}: &a${i} { <<: *a${i - 1}, k${i}: ${i} }`)
10+
}
11+
12+
lines.push(`b: *a${count - 1}`)
13+
return `${lines.join('\n')}\n`
14+
}
15+
516
describe('load options', () => {
617
it('filename — included in error messages', () => {
718
assert.throws(() => load('@', { filename: 'my.yml' }), /my\.yml/)
@@ -25,13 +36,31 @@ describe('load options', () => {
2536
assert.throws(() => load(nested, { maxDepth: 5 }), /maxDepth/)
2637
})
2738

28-
it('maxMergeSeqLength — caps merge sequence length', () => {
39+
it('maxTotalMergeKeys — caps total merge keys', () => {
2940
const merge = (n) =>
3041
Array.from({ length: n }, (_, i) => `- &x${i} {a${i}: ${i}}`).join('\n') +
3142
'\n- <<: [' + Array.from({ length: n }, (_, i) => `*x${i}`).join(', ') + ']\n'
3243

33-
assert.doesNotThrow(() => load(merge(3), { schema: YAML11_SCHEMA, maxMergeSeqLength: 5 }))
34-
assert.throws(() => load(merge(3), { schema: YAML11_SCHEMA, maxMergeSeqLength: 2 }), /maxMergeSeqLength/)
44+
assert.doesNotThrow(() => load(merge(3), { schema: YAML11_SCHEMA, maxTotalMergeKeys: 5 }))
45+
assert.throws(() => load(merge(3), { schema: YAML11_SCHEMA, maxTotalMergeKeys: 2 }), /maxTotalMergeKeys/)
46+
assert.doesNotThrow(() => load(merge(3), { schema: YAML11_SCHEMA, maxTotalMergeKeys: -1 }))
47+
48+
const result = load(createMergeChain(150), { schema: YAML11_SCHEMA, maxTotalMergeKeys: -1 })
49+
assert.equal(Object.keys(result.b).length, 150)
50+
})
51+
52+
it('loadAll — maxTotalMergeKeys is shared across all documents', () => {
53+
const src = `
54+
---
55+
a: &a { k1: 1, k2: 2 }
56+
b: { <<: *a }
57+
---
58+
a: &a { k1: 1, k2: 2 }
59+
b: { <<: *a }
60+
`
61+
62+
assert.doesNotThrow(() => loadAll(src, { schema: YAML11_SCHEMA, maxTotalMergeKeys: 4 }))
63+
assert.throws(() => loadAll(src, { schema: YAML11_SCHEMA, maxTotalMergeKeys: 3 }), /maxTotalMergeKeys/)
3564
})
3665

3766
it('loadAll — options reach every argument form', () => {

0 commit comments

Comments
 (0)