Skip to content

Commit

Permalink
fix: Require indentation for ? explicit-key contents (fixes #551)
Browse files Browse the repository at this point in the history
Adds `explicitKey?: true` to CST BlockMap items
  • Loading branch information
eemeli committed Jun 8, 2024
1 parent 22f2c6f commit 5096f83
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/parse/cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ export interface BlockMap {
offset: number
indent: number
items: Array<
| { start: SourceToken[]; key?: never; sep?: never; value?: never }
| {
start: SourceToken[]
explicitKey?: true
key?: never
sep?: never
value?: never
}
| {
start: SourceToken[]
explicitKey?: true
key: Token | null
sep: SourceToken[]
value?: Token
Expand Down
21 changes: 9 additions & 12 deletions src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class Parser {
it.value = token
} else {
Object.assign(it, { key: token, sep: [] })
this.onKeyLine = !includesToken(it.start, 'explicit-key-ind')
this.onKeyLine = !it.explicitKey
return
}
break
Expand Down Expand Up @@ -532,7 +532,7 @@ export class Parser {
const atNextItem =
!this.onKeyLine &&
this.indent === map.indent &&
it.sep &&
(it.sep || it.explicitKey) &&
this.type !== 'seq-item-ind'

// For empty nodes, assign newline-separated not indented empty tokens to following node
Expand Down Expand Up @@ -572,24 +572,25 @@ export class Parser {
return

case 'explicit-key-ind':
if (!it.sep && !includesToken(it.start, 'explicit-key-ind')) {
if (!it.sep && !it.explicitKey) {
it.start.push(this.sourceToken)
it.explicitKey = true
} else if (atNextItem || it.value) {
start.push(this.sourceToken)
map.items.push({ start })
map.items.push({ start, explicitKey: true })
} else {
this.stack.push({
type: 'block-map',
offset: this.offset,
indent: this.indent,
items: [{ start: [this.sourceToken] }]
items: [{ start: [this.sourceToken], explicitKey: true }]
})
}
this.onKeyLine = true
return

case 'map-value-ind':
if (includesToken(it.start, 'explicit-key-ind')) {
if (it.explicitKey) {
if (!it.sep) {
if (includesToken(it.start, 'newline')) {
Object.assign(it, { key: null, sep: [this.sourceToken] })
Expand Down Expand Up @@ -672,11 +673,7 @@ export class Parser {
default: {
const bv = this.startBlockValue(map)
if (bv) {
if (
atNextItem &&
bv.type !== 'block-seq' &&
includesToken(it.start, 'explicit-key-ind')
) {
if (atNextItem && bv.type !== 'block-seq' && it.explicitKey) {
map.items.push({ start })
}
this.stack.push(bv)
Expand Down Expand Up @@ -888,7 +885,7 @@ export class Parser {
type: 'block-map',
offset: this.offset,
indent: this.indent,
items: [{ start }]
items: [{ start, explicitKey: true }]
} as BlockMap
}
case 'map-value-ind': {
Expand Down
15 changes: 15 additions & 0 deletions tests/doc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,21 @@ describe('maps with no values', () => {
})
})

describe('odd indentations', () => {
test('Block map with empty explicit key (#551)', () => {
const doc = YAML.parseDocument<YAML.YAMLMap, false>('?\n? a')
expect(doc.contents.items).toMatchObject([
{ key: { value: null }, value: null },
{ key: { value: 'a' }, value: null }
])
})

test('Block map with unindented !!null explicit key', () => {
const doc = YAML.parseDocument<YAML.YAMLMap, false>('?\n!!null')
expect(doc.errors).not.toHaveLength(0)
})
})

describe('Excessive entity expansion attacks', () => {
const root = resolve(__dirname, '../artifacts/pr104')
const src1 = readFileSync(resolve(root, 'case1.yml'), 'utf8')
Expand Down

0 comments on commit 5096f83

Please sign in to comment.