Record the guards' input boundary and Goal 8's size as JSON, and drop the hardening past it

This commit is contained in:
2026-10-04 19:18:55 +02:00
parent fb0d2b4eac
commit 58a9c89dfc
6 changed files with 19 additions and 35 deletions
+1 -21
View File
@@ -83,7 +83,7 @@ test("reads a node's siblings as a walk rather than as one call's arguments", ()
assert.equal(fault(wide), 'accepted')
})
test('refuses an object holding itself or a key it does not own, and accepts one shared', () => {
test('refuses an object holding itself, and accepts one shared', () => {
const holdsItself = 'an ADF document is a tree: an object in it holds itself'
const paragraph: { content: unknown[]; type: string } = { content: [], type: 'paragraph' }
paragraph.content.push(paragraph)
@@ -96,26 +96,6 @@ test('refuses an object holding itself or a key it does not own, and accepts one
assert.equal(fault({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), holdsItself)
assert.equal(fault({ content: [{ marks: [{ attrs, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 }), holdsItself)
assert.equal(isAdfDocument({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), false)
const notANode = "an ADF document's content holds ADF nodes: one of them is not"
const inherits: { content?: unknown[] } = {}
const heir: Record<string, unknown> = Object.create(inherits)
heir['type'] = 'paragraph'
inherits.content = [heir]
assert.equal(fault({ content: [heir], type: 'doc', version: 1 }), notANode)
const hidden: Record<string, unknown> = { type: 'paragraph' }
Object.defineProperty(hidden, 'content', { value: [hidden] })
assert.equal(fault({ content: [hidden], type: 'doc', version: 1 }), notANode)
const link: Record<string, unknown> = Object.create({ attrs })
link['type'] = 'link'
assert.equal(fault({ content: [{ marks: [link], text: 'x', type: 'text' }], type: 'doc', version: 1 }), notANode)
const indexed: unknown[] = []
Object.defineProperty(indexed, 0, { value: indexed })
assert.equal(fault({ content: [{ attrs: { a: indexed }, type: 'paragraph' }], type: 'doc', version: 1 }), holdsItself)
const inheritedIndex: { content?: unknown[]; type: string } = { type: 'paragraph' }
const heirs: unknown[] = Object.setPrototypeOf([], [inheritedIndex])
heirs.length = 1
inheritedIndex.content = heirs
assert.equal(fault({ content: [inheritedIndex], type: 'doc', version: 1 }), holdsItself)
const bold = { type: 'strong' }
const text = { marks: [bold], text: 'x', type: 'text' }
const shared = { content: [text, text], type: 'paragraph' }
+3 -8
View File
@@ -106,7 +106,7 @@ export function isAdfNode(value: unknown): value is AdfNode {
}
export function isAdfMark(value: unknown): value is AdfMark {
if (!isRecord(value) || !holdsOnly(value, markKeys) || !ownsWhatItHolds(value, markKeys)) return false
if (!isRecord(value) || !holdsOnly(value, markKeys)) return false
if (typeof value['type'] !== 'string') return false
return !('attrs' in value) || isAttributes(value['attrs'])
}
@@ -135,7 +135,7 @@ function holdsItself(root: object): boolean {
if (onPath.has(entry.item)) return true
ancestors.push(entry.item)
onPath.add(entry.item)
for (const child of Array.isArray(entry.item) ? [...entry.item] : Object.values(entry.item)) if (typeof child === 'object' && child !== null) pending.push({ depth: entry.depth + 1, item: child })
for (const child of Object.values(entry.item)) if (typeof child === 'object' && child !== null) pending.push({ depth: entry.depth + 1, item: child })
}
return false
}
@@ -144,7 +144,7 @@ function isNodeArray(value: readonly unknown[]): value is readonly AdfNode[] {
const pending: unknown[] = [...value]
while (pending.length > 0) {
const node = pending.pop()
if (!isRecord(node) || !holdsOnly(node, nodeKeys) || !ownsWhatItHolds(node, nodeKeys)) return false
if (!isRecord(node) || !holdsOnly(node, nodeKeys)) return false
if (typeof node['type'] !== 'string') return false
if ('attrs' in node && !isAttributes(node['attrs'])) return false
if ('marks' in node && !isArrayOf(node['marks'], isAdfMark)) return false
@@ -219,11 +219,6 @@ function extraKey(value: Record<string, unknown>, keys: readonly string[]): stri
return Object.keys(value).find((key) => !keys.includes(key))
}
// holdsItself walks own enumerable keys alone, so a guard reading any other key could walk a cycle forever.
function ownsWhatItHolds(value: Record<string, unknown>, keys: readonly string[]): boolean {
return keys.every((key) => !(key in value) || Object.prototype.propertyIsEnumerable.call(value, key))
}
function holdsOnly(value: Record<string, unknown>, keys: readonly string[]): boolean {
return extraKey(value, keys) === undefined
}