3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 22:55:44 +01:00

Answer architecture round 1: the reader returns its refusals and owns its stack, a decision and a test keep JSON.parse out, item 83 filed, the scoring run recorded

This commit is contained in:
2026-10-08 12:37:30 +02:00
committed by Lillem4n
parent d7ba149b83
commit d7f2384876
9 changed files with 118 additions and 74 deletions
+25 -12
View File
@@ -1,5 +1,8 @@
import assert from 'node:assert/strict'
import { dirname, join } from 'node:path'
import fc from 'fast-check'
import { fileURLToPath } from 'node:url'
import { readFileSync, readdirSync } from 'node:fs'
import test from 'node:test'
import type { JsonSpelling } from './canonical-json.ts'
@@ -13,48 +16,50 @@ test('reads every JSON value kind', () => {
assert.deepEqual(parseJsonText('[ ]'), { value: [] })
assert.deepEqual(parseJsonText('{ }'), { value: {} })
assert.deepEqual(parseJsonText('[[],{}]'), { value: [[], {}] })
assert.ok(Object.is(parseJsonText('-0')?.value, -0))
assert.ok(Object.is(parseJsonText('-0').value, -0))
})
test('decodes every string escape', () => {
assert.deepEqual(parseJsonText('"\\"\\\\\\/\\b\\f\\n\\r\\t\\u00e9\\uD83C\\uDF89\\ud800"'), { value: '"\\/\b\f\n\r\té\u{1f389}\ud800' })
})
test('keys stay the keys the text spells after V8 has read a colliding key (https://issues.chromium.org/issues/521080746)', () => {
test('keys stay the keys the text spells after JSON.parse has read a key holding a backslash (https://issues.chromium.org/issues/521080746)', () => {
const canary = '{"\\r":1,"\\f":2}'
JSON.parse(canary)
JSON.parse('{"\\r":3,"\\\\":4}')
assert.deepEqual(Object.keys(parseJsonText(canary)?.value ?? {}), ['\r', '\f'])
assert.deepEqual(Object.keys(parseJsonText(canary).value ?? {}), ['\r', '\f'])
})
test('a __proto__ key is an own key', () => {
const read = parseJsonText('{"__proto__":{"a":1}}')?.value
const read = parseJsonText('{"__proto__":{"a":1}}').value
assert.deepEqual(Object.keys(read ?? {}), ['__proto__'])
assert.equal(Object.getPrototypeOf(read), Object.prototype)
})
test('a repeated key keeps its first position and its last value, as JSON.parse does', () => {
const read = parseJsonText('{"a":1,"b":2,"a":3}')?.value
const read = parseJsonText('{"a":1,"b":2,"a":3}').value
assert.deepEqual(read, { a: 3, b: 2 })
assert.deepEqual(Object.keys(read ?? {}), ['a', 'b'])
})
test('a number past the double range reads as JSON.parse reads it', () => {
assert.deepEqual(parseJsonText('1e999'), { value: Infinity })
test('refuses a number past the double range, which JSON.parse reads as Infinity', () => {
assert.deepEqual(parseJsonText('1e999'), { refusal: 'non-finite' })
assert.deepEqual(parseJsonText('[-1e999]'), { refusal: 'non-finite' })
assert.deepEqual(parseJsonText('[1e999,'), { refusal: 'syntax' })
})
test('refuses text JSON.parse refuses', () => {
const refused = ['', ' ', '{', '[', '[1', '[1,', '[1,]', '{"a"}', '{"a":}', '{"a":1,}', '{a:1}', '{"a" 1}', '{,}', '01', '1.', '.5', '-', '1e', '+1', '"a', '"\u0001"', '"\\x"', '"\\u12"', '"\\u12g4"', 'tru', 'nul', '1 2', '[1]]', '}', "'a'", 'NaN', ' 1']
const refused = ['', ' ', '{', '[', '[1', '[1,', '[1,]', '{"a"}', '{"a":}', '{"a":1,}', '{a:1}', '{"a" 1}', '{,}', '01', '1.', '.5', '-', '1e', '+1', '"a', '"\u0001"', '"\\x"', '"\\u12"', '"\\u12g4"', 'tru', 'nul', '1 2', '[1]]', '}', "'a'", 'NaN', '\u00a01']
for (const text of refused) {
assert.throws(() => JSON.parse(text), SyntaxError, text)
assert.equal(parseJsonText(text), undefined, text)
assert.deepEqual(parseJsonText(text), { refusal: 'syntax' }, text)
}
})
test('reads a value nested far past the call stack without recursing', () => {
const depth = 100_000
assert.ok(parseJsonText(`${'['.repeat(depth)}${']'.repeat(depth)}`) !== undefined)
assert.ok(parseJsonText(`${'{"a":'.repeat(depth)}1${'}'.repeat(depth)}`) !== undefined)
assert.ok(parseJsonText(`${'['.repeat(depth)}${']'.repeat(depth)}`).value !== undefined)
assert.ok(parseJsonText(`${'{"a":'.repeat(depth)}1${'}'.repeat(depth)}`).value !== undefined)
})
test('reads back every value the canonical serializer writes', () => {
@@ -76,8 +81,16 @@ test('accepts exactly the text JSON.parse accepts', () => {
} catch {
accepted = false
}
assert.equal(parseJsonText(text) !== undefined, accepted)
assert.equal(parseJsonText(text).refusal !== 'syntax', accepted)
}),
propertyRuns(2000),
)
})
test('no shipped file reads JSON with JSON.parse, which parseJsonText stands in for', () => {
const sourceRoot = dirname(fileURLToPath(import.meta.url))
const readers = readdirSync(sourceRoot, { encoding: 'utf8', recursive: true }).filter(
(name) => name.endsWith('.ts') && !name.endsWith('.test.ts') && !name.startsWith('conformance') && readFileSync(join(sourceRoot, name), 'utf8').includes('JSON.parse('),
)
assert.deepEqual(readers, [])
})
+47 -41
View File
@@ -2,8 +2,16 @@ import type { JsonValue } from './json-value.ts'
type Frame = { entries: [string, JsonValue][]; key: string; kind: 'object' } | { items: JsonValue[]; kind: 'array' }
type Item = { end: number; frame: Frame; kind: 'opened' } | { end: number; kind: 'value'; value: JsonValue } | { kind: 'refused' }
export type JsonReading = { refusal: JsonRefusal; value?: undefined } | { refusal?: undefined; value: JsonValue }
type JsonRefusal = 'non-finite' | 'syntax'
type Scanned<T> = { end: number; value: T } | undefined
type State = { expect: 'separator'; index: number; value: JsonValue } | { expect: 'value'; index: number }
const escapes = new Map([
['"', '"'],
['/', '/'],
@@ -21,57 +29,52 @@ const literals = new Map<string, JsonValue>([
['true', true],
])
const numberSyntax = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[Ee][+-]?[0-9]+)?/y
const syntaxRefusal: Item = { kind: 'refused' }
// In place of JSON.parse: V8 reads an escaped key against keys earlier parses saw (https://issues.chromium.org/issues/521080746).
export function parseJsonText(text: string): { value: JsonValue } | undefined {
// In place of JSON.parse: V8 can read an escaped key as another key it read before (https://issues.chromium.org/issues/521080746).
export function parseJsonText(text: string): JsonReading {
const frames: Frame[] = []
let index = skipWhitespace(text, 0)
let overflowed = false
let state: State = { expect: 'value', index: skipWhitespace(text, 0) }
for (;;) {
const opened = openValue(text, index, frames)
if (opened === undefined) return undefined
let { end, value } = opened
if (value === undefined) {
index = end
if (state.expect === 'value') {
const item = readItem(text, state.index)
if (item.kind === 'refused') return { refusal: 'syntax' }
if (item.kind === 'opened') frames.push(item.frame)
else if (typeof item.value === 'number' && !Number.isFinite(item.value)) overflowed = true
state = item.kind === 'opened' ? { expect: 'value', index: item.end } : { expect: 'separator', index: skipWhitespace(text, item.end), value: item.value }
continue
}
for (;;) {
index = skipWhitespace(text, end)
const frame = frames[frames.length - 1]
if (frame === undefined) return index === text.length ? { value } : undefined
if (frame.kind === 'array') frame.items.push(value)
else frame.entries.push([frame.key, value])
const separator = text.charAt(index)
if (separator === ',') break
if (separator !== (frame.kind === 'array' ? ']' : '}')) return undefined
frames.pop()
end = index + 1
value = frame.kind === 'array' ? frame.items : Object.fromEntries(frame.entries)
}
const index: number = state.index
const value = state.value
const frame = frames[frames.length - 1]
index = skipWhitespace(text, index + 1)
if (frame?.kind === 'object') {
const key = readKey(text, index)
if (key === undefined) return undefined
frame.key = key.value
index = key.end
if (frame === undefined) return index !== text.length ? { refusal: 'syntax' } : overflowed ? { refusal: 'non-finite' } : { value }
if (frame.kind === 'array') frame.items.push(value)
else frame.entries.push([frame.key, value])
const separator = text.charAt(index)
if (separator === (frame.kind === 'array' ? ']' : '}')) {
frames.pop()
state = { expect: 'separator', index: skipWhitespace(text, index + 1), value: frame.kind === 'array' ? frame.items : Object.fromEntries(frame.entries) }
continue
}
if (separator !== ',') return { refusal: 'syntax' }
state = { expect: 'value', index: skipWhitespace(text, index + 1) }
if (frame.kind === 'array') continue
const key = readKey(text, state.index)
if (key === undefined) return { refusal: 'syntax' }
frame.key = key.value
state = { expect: 'value', index: key.end }
}
}
// A value whose `value` is undefined opened a container, and `end` is where its first member starts.
function openValue(text: string, index: number, frames: Frame[]): Scanned<JsonValue | undefined> {
function readItem(text: string, index: number): Item {
const opener = text.charAt(index)
if (opener !== '[' && opener !== '{') return readScalar(text, index)
const first = skipWhitespace(text, index + 1)
if (text.charAt(first) === (opener === '[' ? ']' : '}')) return { end: first + 1, value: opener === '[' ? [] : {} }
if (opener === '[') {
frames.push({ items: [], kind: 'array' })
return { end: first, value: undefined }
}
if (text.charAt(first) === (opener === '[' ? ']' : '}')) return { end: first + 1, kind: 'value', value: opener === '[' ? [] : {} }
if (opener === '[') return { end: first, frame: { items: [], kind: 'array' }, kind: 'opened' }
const key = readKey(text, first)
if (key === undefined) return undefined
frames.push({ entries: [], key: key.value, kind: 'object' })
return { end: key.end, value: undefined }
return key === undefined ? syntaxRefusal : { end: key.end, frame: { entries: [], key: key.value, kind: 'object' }, kind: 'opened' }
}
function readKey(text: string, index: number): Scanned<string> {
@@ -81,12 +84,15 @@ function readKey(text: string, index: number): Scanned<string> {
return text.charAt(colon) === ':' ? { end: skipWhitespace(text, colon + 1), value: key.value } : undefined
}
function readScalar(text: string, index: number): Scanned<JsonValue> {
if (text.charAt(index) === '"') return readString(text, index)
for (const [spelling, value] of literals) if (text.startsWith(spelling, index)) return { end: index + spelling.length, value }
function readScalar(text: string, index: number): Item {
if (text.charAt(index) === '"') {
const string = readString(text, index)
return string === undefined ? syntaxRefusal : { end: string.end, kind: 'value', value: string.value }
}
for (const [spelling, value] of literals) if (text.startsWith(spelling, index)) return { end: index + spelling.length, kind: 'value', value }
numberSyntax.lastIndex = index
const number = numberSyntax.exec(text)?.[0]
return number === undefined ? undefined : { end: index + number.length, value: Number(number) }
return number === undefined ? syntaxRefusal : { end: index + number.length, kind: 'value', value: Number(number) }
}
function readString(text: string, start: number): Scanned<string> {
+3 -7
View File
@@ -3,7 +3,7 @@ import type { ConvertFault } from '../result.ts'
import type { JsonValue } from '../json-value.ts'
import { backslashEscape, holdsNullCharacter } from './commonmark/grammar.ts'
import { backtickRun, closingBacktickRun } from './commonmark/backtick-runs.ts'
import { isJsonValue, overNested } from '../json-value.ts'
import { overNested } from '../json-value.ts'
import { largestNesting } from '../nesting.ts'
import { parseJsonText } from '../json-text.ts'
import { serializeCanonicalJson } from '../canonical-json.ts'
@@ -61,7 +61,7 @@ const prefixFault = `an unescaped ${directivePrefix} completes no directive; ${d
export function attributeValue(text: string, kind: AttributeKind): AttributeReading {
if (kind === 'string') return { value: { kind, value: text } }
if (kind === 'boolean') return text === 'true' || text === 'false' ? { value: { kind, value: text === 'true' } } : { refusal: 'kind' }
const parsed = parseJson(text)
const parsed = parseJsonText(text).value
if (parsed === undefined) return { refusal: 'kind' }
if (kind === 'number') return typeof parsed === 'number' ? { value: { kind, value: parsed } } : { refusal: 'kind' }
return overNested(parsed) ? { refusal: 'nesting' } : { value: { kind, value: parsed } }
@@ -325,12 +325,8 @@ function readQuotedValue(text: string, index: number): Read<{ end: number; value
if (character !== undefined) {
return { fault: malformedDirective(`a raw ${character} inside {attrs} breaks the directive: spell it \\u${escapeDigits(character)}`) }
}
const parsed = parseJson(spelling)
const parsed = parseJsonText(spelling).value
if (typeof parsed !== 'string') return { fault: malformedDirective('the {attrs} quoted value is not a JSON string') }
return { value: { end: cursor + 1, value: { decoded: parsed, spelling } } }
}
function parseJson(raw: string): JsonValue | undefined {
const value = parseJsonText(raw)?.value
return value !== undefined && isJsonValue(value) ? value : undefined
}
+3 -2
View File
@@ -65,9 +65,10 @@ function carriedJson(node: AdfNode, spelled: object, spelling: JsonSpelling, pat
function readCarriedJson(raw: string, spelling: JsonSpelling, levels: number, type?: string): Read<AdfNode> {
const wayOut = type === undefined ? directiveEscape : fenceEscape(type)
const parsed = parseJsonText(raw)
if (parsed === undefined) return { fault: malformedDirective(`the opaque carry holds invalid JSON; ${wayOut}`) }
if (parsed.refusal !== undefined) {
return { fault: parsed.refusal === 'syntax' ? malformedDirective(`the opaque carry holds invalid JSON; ${wayOut}`) : unsupportedNodeShape('the opaque carry holds a number JSON cannot spell') }
}
const { value } = parsed
if (!isJsonValue(value)) return { fault: unsupportedNodeShape('the opaque carry holds a number JSON cannot spell') }
const typed = type === undefined || type === '' ? { value } : typedValue(value, type)
if (typed.fault !== undefined) return typed
const held = typed.value
@@ -405,6 +405,13 @@ test('reads the inline carry back to the node its json attribute holds', () => {
])
})
test('reads an escaped JSON key as the key it spells after reading a key holding a backslash (https://issues.chromium.org/issues/521080746)', () => {
const extension = (parameters: string): string => `!adf:extension {extensionKey=toc extensionType=x parameters=${JSON.stringify(parameters)}}\n`
const read = content(markdownToAdf([extension('{"\\b":1,"\\f":2}'), extension('{"\\b":3,"\\\\":4}'), extension('{"\\b":1,"\\f":2}')].join('\n')))
const node = (parameters: AdfAttributes): AdfNode => ({ attrs: { extensionKey: 'toc', extensionType: 'x', parameters }, type: 'extension' })
assert.deepEqual(read, [node({ '\b': 1, '\f': 2 }), node({ '\b': 3, '\\': 4 }), node({ '\b': 1, '\f': 2 })])
})
test('names the invalid JSON no opaque carry holds, and the spelling that keeps it literal', () => {
const invalid = 'malformed-directive: the opaque carry holds invalid JSON; '
const fence = `${invalid}!adf:codeBlock {language="adf:x"} around a bare fence keeps it a code block`