Skip to content

Commit 125cd5a

Browse files
committed
Add maxAliases option
1 parent 3105455 commit 125cd5a

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111
- Added `maxTotalMergeKeys` (10000) loader option to limit the total number of
1212
keys processed by YAML merge (`<<`) across one `load()` / `loadAll()` call.
13+
- Added `maxAliases` (-1) loader option to limit the number of YAML aliases per
14+
document.
1315

1416
### Changed
1517
- `maxMergeSeqLength` replaced with `maxTotalMergeKeys` for limiting YAML merge

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ options:
7272
- `maxTotalMergeKeys` _(default: 10000)_ - limits the total number of keys
7373
processed by merge (`<<`) across one `load()` / `loadAll()` call. Set to `-1`
7474
to disable.
75+
- `maxAliases` _(default: -1)_ - limits the number of alias nodes (`*ref`) per
76+
document. Set to `0` to reject all aliases, or to `-1` for no limit.
7577

7678
> [!NOTE]
7779
>

‎src/parser/constructor.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ interface ConstructorOptions {
8181
schema?: Schema
8282
json?: boolean
8383
maxTotalMergeKeys?: number
84+
maxAliases?: number
8485
}
8586

8687
// `source` is input data, not config — so it has no default here.
8788
const DEFAULT_CONSTRUCTOR_OPTIONS: Required<Omit<ConstructorOptions, 'source'>> = {
8889
filename: '',
8990
schema: CORE_SCHEMA,
9091
json: false,
91-
maxTotalMergeKeys: 10000
92+
maxTotalMergeKeys: 10000,
93+
maxAliases: -1
9294
}
9395

9496
interface ConstructorState extends Required<ConstructorOptions> {
@@ -100,6 +102,7 @@ interface ConstructorState extends Required<ConstructorOptions> {
100102
anchors: Map<string, Anchor>
101103
tagHandlers: TagHandlers
102104
totalMergeKeys: number
105+
aliasCount: number
103106
}
104107

105108
function eventPosition (event: Event) {
@@ -360,7 +363,8 @@ function constructFromEvents (events: Event[], options: ConstructorOptions): unk
360363
frames: [],
361364
anchors: new Map(),
362365
tagHandlers: Object.create(null),
363-
totalMergeKeys: 0
366+
totalMergeKeys: 0,
367+
aliasCount: 0
364368
}
365369

366370
while (state.eventIndex < state.events.length) {
@@ -370,6 +374,7 @@ function constructFromEvents (events: Event[], options: ConstructorOptions): unk
370374
switch (event.type) {
371375
case EVENT_DOCUMENT:
372376
state.anchors = new Map()
377+
state.aliasCount = 0
373378
state.tagHandlers = Object.create(null)
374379
for (const directive of event.directives) {
375380
if (directive.kind === 'tag') state.tagHandlers[directive.handle] = directive.prefix
@@ -435,6 +440,10 @@ function constructFromEvents (events: Event[], options: ConstructorOptions): unk
435440
}
436441

437442
case EVENT_ALIAS: {
443+
if (state.maxAliases !== -1 && ++state.aliasCount > state.maxAliases) {
444+
throwError(state, `aliases exceeded maxAliases (${state.maxAliases})`)
445+
}
446+
438447
const name = state.source.slice(event.anchorStart, event.anchorEnd)
439448
const anchor = state.anchors.get(name)
440449
if (!anchor) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,36 @@ b: { <<: *a }
5858
a: &a { k1: 1, k2: 2 }
5959
b: { <<: *a }
6060
`
61-
6261
assert.doesNotThrow(() => loadAll(src, { schema: YAML11_SCHEMA, maxTotalMergeKeys: 4 }))
6362
assert.throws(() => loadAll(src, { schema: YAML11_SCHEMA, maxTotalMergeKeys: 3 }), /maxTotalMergeKeys/)
6463
})
6564

65+
it('maxAliases — caps alias count', () => {
66+
const src = `
67+
base: &base { a: 1 }
68+
one: *base
69+
two: *base
70+
`
71+
assert.deepEqual(load(src).one, { a: 1 })
72+
assert.doesNotThrow(() => load(src, { maxAliases: 2 }))
73+
assert.throws(() => load(src, { maxAliases: 1 }), /maxAliases/)
74+
assert.throws(() => load(src, { maxAliases: 0 }), /maxAliases/)
75+
assert.doesNotThrow(() => load(src, { maxAliases: -1 }))
76+
})
77+
78+
it('loadAll — maxAliases is applied per document', () => {
79+
const src = `
80+
---
81+
a: &a 1
82+
b: *a
83+
---
84+
a: &a 1
85+
b: *a
86+
`
87+
assert.doesNotThrow(() => loadAll(src, { maxAliases: 1 }))
88+
assert.throws(() => loadAll(src, { maxAliases: 0 }), /maxAliases/)
89+
})
90+
6691
it('loadAll — options reach every argument form', () => {
6792
const src = 'test: 1\ntest: 2'
6893
const expected = [{ test: 2 }]

0 commit comments

Comments
 (0)