Skip to content

Commit 2ea0aef

Browse files
committed
fix(compiler): fail closed on callback-accepting namespace method arguments
Array.from, JSON.parse, JSON.stringify, and Object.groupBy synchronously invoke a callback argument. When that callback was passed by reference rather than inline, setupCanScheduleSelfUpdate skipped it (an Identifier is not a call), so the setup checkpoint was omitted even though the callback could call a setter. Add SETUP_CALLBACK_NAMESPACE_METHODS to identify these methods. For them, the data argument (index 0) is walked normally; subsequent arguments use walkInvokedArgument, the same fail-closed check that sync-factory hooks already use. Inline arrow callbacks are still entered and classified; only opaque references now correctly trigger the checkpoint.
1 parent 81f7a79 commit 2ea0aef

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎packages/octane/src/compiler/compile.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4773,6 +4773,17 @@ const SETUP_PURE_GLOBAL_CALLEES = new Set([
47734773
'decodeURIComponent',
47744774
]);
47754775
const SETUP_PURE_GLOBAL_NAMESPACES = new Set(['Math', 'JSON', 'Number', 'Object', 'Array', 'Date']);
4776+
// Namespace methods that synchronously invoke a callback argument. The callback
4777+
// is always at argument index 1; earlier arguments are data. A mapper, reviver,
4778+
// replacer, or classifier passed by reference cannot be entered, so its body is
4779+
// opaque and must fail closed the same way sync-factory hook arguments do.
4780+
const SETUP_CALLBACK_NAMESPACE_METHODS = new Set([
4781+
'Array.from',
4782+
'Array.fromAsync',
4783+
'JSON.parse',
4784+
'JSON.stringify',
4785+
'Object.groupBy',
4786+
]);
47764787

47774788
// Every binding name the module declares anywhere. A global namespace shadowed
47784789
// by any local, parameter, or import is treated as an unknown callee.
@@ -4876,7 +4887,15 @@ function setupCanScheduleSelfUpdate(stmts, ctx) {
48764887
SETUP_PURE_GLOBAL_NAMESPACES.has(callee.object.name) &&
48774888
isGlobal(callee.object.name)
48784889
) {
4879-
walk(n.arguments, true);
4890+
if (
4891+
callee.property?.type === 'Identifier' &&
4892+
SETUP_CALLBACK_NAMESPACE_METHODS.has(callee.object.name + '.' + callee.property.name)
4893+
) {
4894+
if (n.arguments[0]) walk(n.arguments[0], true);
4895+
for (let i = 1; i < n.arguments.length; i++) walkInvokedArgument(n.arguments[i]);
4896+
} else {
4897+
walk(n.arguments, true);
4898+
}
48804899
return;
48814900
}
48824901
found = true;

‎packages/octane/tests/audit-hook-behavior.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ describe('setup checkpoint emission', () => {
581581
expect(code).not.toContain(checkpoint);
582582
});
583583

584+
it('omits the checkpoint for callback-accepting namespace methods with inline callbacks', () => {
585+
const code = compiled(`
586+
const mapped = Array.from(props.items, (x: any) => x.id);
587+
const data = JSON.parse(props.text, (_k: string, v: any) => v);
588+
`);
589+
expect(code).not.toContain(checkpoint);
590+
});
591+
584592
it.each([
585593
[
586594
'a setter called in setup',
@@ -605,6 +613,22 @@ describe('setup checkpoint emission', () => {
605613
'a shadowed global namespace',
606614
`const Math = props.math;\nconst value = Math.max(props.n, 1);`,
607615
],
616+
[
617+
'Array.from with a mapper passed by reference',
618+
`const mapped = Array.from(props.items, props.mapper);`,
619+
],
620+
[
621+
'JSON.parse with a reviver passed by reference',
622+
`const data = JSON.parse(props.text, props.reviver);`,
623+
],
624+
[
625+
'JSON.stringify with a replacer passed by reference',
626+
`const text = JSON.stringify(props.data, props.replacer);`,
627+
],
628+
[
629+
'Object.groupBy with a classifier passed by reference',
630+
`const groups = Object.groupBy(props.items, props.classify);`,
631+
],
608632
])('keeps the checkpoint for %s', (_, body) => {
609633
expect(compiled(body)).toContain(checkpoint);
610634
});

0 commit comments

Comments
 (0)