Files
reverse-engineering-anything/src/domain/javascript/javascriptSemanticReturnedReferencesWrites.test.ts
T
ShuaixinHuang e9fc8fb30c fix(javascript): preserve runtime path bytes and Electron ownership (#4941)
* fix(javascript): preserve literal percent runtime paths

* fix(javascript): ignore resolve prefixes before the final anchor

* fix(electron): retain application roots for loadFile declarations

* fix(electron): resolve loadFile from enclosing application package

* fix(electron): retain outer application root across package scopes

* fix(electron): derive application ownership from inventoried packages

* fix(electron): retain resolved package entrypoint ownership

* fix(electron): preserve competing application ownership

* fix(electron): retain invalid and omitted runtime ownership

* fix(electron): preserve omitted module ownership and runtime paths

* fix(electron): preserve proven roots before confining page paths

* fix(electron): retain possible owners behind missing imports

* fix(electron): distinguish absent imports and owned page candidates

* fix(electron): keep unavailable JSON out of owner gaps

* fix(electron): retain exact loader formats and default entry status

* fix(electron): retain unavailable entrypoint ownership candidates

* test(ghidra): preserve Windows Node probe prerequisites

* test(ghidra): assert cancellation during profile shutdown

* fix(electron): separate examined imports from frontend ownership

* fix(electron): retain unavailable directory entrypoint owners

* test(jeb): retain Linux ownership prerequisite in isolation

* fix(javascript): retain unchecked loader ownership and literal main paths

* fix(javascript): preserve runtime paths and call-site module aliases

* fix(javascript): bound module alias traversal and preserve projections

* fix(javascript): retain host module and nested artifact path boundaries

* fix(javascript): preserve ESM and archive module loader provenance

* test(javascript): distinguish unmapped containers from missing exports

* fix: retain default module aliases and Electron builtin imports

* fix: preserve host file URL and inward ASAR ownership

* fix: preserve CommonJS file URL classification in CI fixtures

* fix: retain bound module loaders and Windows entrypoint uncertainty

* fix: retain ASAR uncertainty and invalidate changed Electron app roots

* fix(javascript): retain ownership uncertainty across loader boundaries

* fix(javascript): preserve loader sources and bound alias queries

* fix(javascript): retain destructured loader sources

* fix(javascript): preserve loader uncertainty at resolution boundaries

* fix(javascript): preserve callable and package launch ownership

* fix(javascript): preserve captured values across member reads

* test(process): assert tagged filesystem cancellation failures

* fix(javascript): preserve inventory bases and invocation read timing

* fix(javascript): preserve ownership across indirect and unread loads

* test(javascript): order fixture directory creation on Windows

* test(javascript): share ordered filesystem fixture writer

* fix(javascript): unify loader provenance and package validation

* fix(javascript): preserve captured Node require loader values

* fix(javascript): preserve loader invocation authority

* fix(javascript): verify generic loader invocation authority

* fix(javascript): preserve conditional loader method alternatives

* fix(javascript): require proven factories for source association

* fix(javascript): honor replaced Electron app path setters

* fix(javascript): prove path and assignment source ownership

* fix(javascript): retain native delegate eligibility through shared writes

* fix(process): settle stale baseline errors after confirmed absence

* fix(javascript): preserve assignment and path boundary evidence

* fix(javascript): prove source-directory resolve anchors

* fix: preserve application path provenance and portable schemas

* fix(javascript): retain CommonJS exported setter effects

* fix: isolate Linux identity observations and retain native module exports

* fix(javascript): preserve published app and helper provenance

* fix(javascript): normalize typed stores and native path fixtures
2026-10-20 17:25:19 +08:00

204 lines
6.5 KiB
TypeScript

import * as t from "@babel/types";
import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
import { indexJavaScriptBindingsWithReceiversSteps } from "./javascriptSemanticAnalysis.js";
import { createSemanticReturnedReferences } from "./javascriptSemanticReturnedReferences.js";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
it.each([
[
'const box={make(){return {};}};box.make=()=>module;box.make().require("./shared.cjs");',
true,
],
[
'const box={make(){return module;}};box.make=()=>({});box.make().require("./shared.cjs");',
false,
],
[
'const box={make(){return {};}};if(flag)box.make=()=>module;box.make().require("./shared.cjs");',
true,
],
[
'const box={make(){return module;}};if(flag)box.make=()=>({});box.make().require("./shared.cjs");',
true,
],
[
'const box={make(){return {};}};box.make().require("./shared.cjs");box.make=()=>module;',
false,
],
[
'const box={make(){return {};}};const alias=box;box.make=()=>module;alias.make().require("./shared.cjs");',
true,
],
[
'const box={make(){return module;}};const alias=box;box.make=()=>({});alias.make().require("./shared.cjs");',
false,
],
[
'const box={make(){return {};}};const make=box.make;box.make=()=>module;make().require("./shared.cjs");',
false,
],
[
'const box={make(){return module;}};const make=box.make;box.make=()=>({});make().require("./shared.cjs");',
true,
],
[
'const box={inner:{make(){return {};}}};box.inner.make=()=>module;box.inner.make().require("./shared.cjs");',
true,
],
[
'const box={inner:{make(){return module;}}};box.inner={make(){return {};}};box.inner.make().require("./shared.cjs");',
false,
],
[
'const box={};({make:box.make}={make:()=>module});box.make().require("./shared.cjs");',
true,
],
[
'const box=[()=>({})];box[0]=()=>module;box[0]().require("./shared.cjs");',
true,
],
[
'const box={make(){return {};}};box.other=()=>module;box.make().require("./shared.cjs");',
false,
],
[
'const box={make(){return {};}};box.make=async()=>module;box.make().require("./shared.cjs");',
false,
],
[
'const box={make(){return {};}};box.make=async()=>module;(await box.make()).require("./shared.cjs");',
true,
],
[
'const box={make(){return {};}};box.make=function*(){return module;};(await box.make()).require("./shared.cjs");',
false,
],
] as const)(
"selects returned references at the member read: %s",
(source, expected) => {
const analysis = analyzeJavaScriptStaticSource(source);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
},
);
it.each([
["let make=()=>module;const box={make};make=()=>({});box.make()", true],
["let make=()=>({});const box={make};make=()=>module;box.make()", false],
["let make=()=>module;const box={make:make};make=()=>({});box.make()", true],
["let make=()=>module;const box=[make];make=()=>({});box[0]()", true],
["let make=()=>({});const box=[make];make=()=>module;box[0]()", false],
[
"let make=()=>module;const box={make};const saved=box.make;make=()=>({});saved()",
true,
],
[
"let make=()=>module;const box={};box.make=make;make=()=>({});box.make()",
true,
],
[
"let make=()=>({});const box={};box.make=make;make=()=>module;box.make()",
false,
],
[
"let make=()=>module;const box={};[box.make]=[make];make=()=>({});box.make()",
true,
],
[
"let make=()=>module;const old={make};const box={...old};make=()=>({});box.make()",
true,
],
[
"const old={make:()=>module};const box={...old};old.make=()=>({});box.make()",
true,
],
[
"const old={make:()=>({})};const box={...old};old.make=()=>module;box.make()",
false,
],
[
"const old={make:()=>module};const {...box}=old;old.make=()=>({});box.make()",
true,
],
[
"const old={make:()=>({})};const {...box}=old;old.make=()=>module;box.make()",
false,
],
["const old=[()=>module];const [...box]=old;old[0]=()=>({});box[0]()", true],
["const old=[()=>({})];const [...box]=old;old[0]=()=>module;box[0]()", false],
[
"let make=()=>module;const {make:saved=make}={};make=()=>({});saved()",
true,
],
[
"let inner={make:()=>module};const box={inner};inner={make:()=>({})};inner.make=()=>({});box.inner.make()",
true,
],
[
"let inner={make:()=>({})};const box={inner};inner={make:()=>module};inner.make=()=>module;box.inner.make()",
false,
],
[
"let inner={make:()=>({})};const box={inner};inner.make=()=>module;box.inner.make()",
true,
],
[
"let inner={make:()=>({})};const saved=inner;const box={inner};inner={make:()=>({})};saved.make=()=>module;box.inner.make()",
true,
],
] as const)(
"retains captured values independently of later member reads: %s",
(expression, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${expression}.require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
},
);
it("refreshes cached returned targets after the shared member index settles", () => {
const parsed = parseJavaScriptSource(
"const box={make(){return {};}};box.make=()=>module;box.make();",
"member-writes.cjs",
);
if (parsed === null) throw new Error("Expected parsed member writes");
const steps = indexJavaScriptBindingsWithReceiversSteps(parsed.program);
let step = steps.next();
while (!step.done) step = steps.next();
const index = step.value;
let callee: t.Node | undefined;
traverseJavaScriptAst(parsed.program, {
enter(node) {
if (t.isCallExpression(node)) callee = node.callee;
},
});
if (callee === undefined) throw new Error("Expected member call");
const returned = createSemanticReturnedReferences(
index.bindings,
"direct-call",
);
expect(
returned(callee).references.some(({ node }) => t.isIdentifier(node)),
).toBe(false);
const effects = index.indexMemberMutationFactsSteps();
let effect = effects.next();
while (!effect.done) effect = effects.next();
expect(returned(callee).references).toEqual([
expect.objectContaining({
node: expect.objectContaining({ name: "module" }),
}),
]);
});