Skip to content

Commit e1a1a77

Browse files
committed
fix: Handle invalid unicode escapes
1 parent a163ea0 commit e1a1a77

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/compose/resolve-flow-scalar.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function doubleQuotedValue(source: string, onError: FlowScalarErrorHandler) {
164164
next = source[++i + 1]
165165
while (next === ' ' || next === '\t') next = source[++i + 1]
166166
} else if (next === 'x' || next === 'u' || next === 'U') {
167-
const length = { x: 2, u: 4, U: 8 }[next]
167+
const length = next === 'x' ? 2 : next === 'u' ? 4 : 8
168168
res += parseCharCode(source, i + 1, length, onError)
169169
i += length
170170
} else {
@@ -235,10 +235,11 @@ function parseCharCode(
235235
const cc = source.substr(offset, length)
236236
const ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc)
237237
const code = ok ? parseInt(cc, 16) : NaN
238-
if (isNaN(code)) {
238+
try {
239+
return String.fromCodePoint(code)
240+
} catch {
239241
const raw = source.substr(offset - 2, length + 2)
240242
onError(offset - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`)
241243
return raw
242244
}
243-
return String.fromCodePoint(code)
244245
}

tests/doc/errors.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ describe('block scalars', () => {
5858
})
5959
})
6060

61+
describe('flow scalars', () => {
62+
test('invalid hex escapes', () => {
63+
const doc = YAML.parseDocument('"\\x0"')
64+
expect(doc.errors).toMatchObject([{ code: 'BAD_DQ_ESCAPE' }])
65+
})
66+
67+
test('invalid unicode escapes', () => {
68+
const doc = YAML.parseDocument('"\\U00110000"')
69+
expect(doc.errors).toMatchObject([{ code: 'BAD_DQ_ESCAPE' }])
70+
})
71+
})
72+
6173
describe('block collections', () => {
6274
test('mapping with bad indentation', () => {
6375
const src = 'foo: "1"\n bar: 2\n'

0 commit comments

Comments
 (0)