Files
reverse-engineering-anything/src/domain/javascript/javascriptStaticAnalysisReturnedRequire.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

286 lines
10 KiB
TypeScript

import { expect, it } from "vitest";
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
it.each([
["async function get(){return module;}(await get())", true],
["async function get(){return module;}const api=await get();api", true],
[
"async function get(){return module;}const pending=get();const api=await pending;api",
true,
],
["function get(){return module;}const api=await get();api", true],
["function* get(){yield module;}const api=await get();api", false],
["function* get(){return module;}const api=await get();api", false],
["async function* get(){yield module;}const api=await get();api", false],
[
"async function get(){return {require(){}};}const api=await get();api",
false,
],
["async function get(){return module;}const api=get();api", false],
[
"async function child(){return module;}async function get(){return {child:child()};}(await get()).child",
false,
],
[
"async function child(){return module;}function get(){return {child:child()};}(await get().child)",
true,
],
[
"async function child(){return module;}async function get(){return {child:child()};}const {child:pending}=await get();const api=await pending;api",
true,
],
[
"async function get(){return module;}const pending=get();const api=flag?pending:await pending;api",
true,
],
[
"async function get(){return module;}const pending=get();const api=flag?await pending:pending;api",
true,
],
[
"async function inner(){return module;}function get(){return inner();}(await get())",
true,
],
])(
"preserves awaited result consumption through aliases and projections: %s",
(receiver, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${receiver}.require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
},
);
it.each([
"const {make=host}={make:class{}};",
"class Local{}const {make=host}={make:Local};",
"const [make=host]=[class{}];",
])(
"excludes inactive defaults for known class constructors: %s",
(declaration) => {
const analysis = analyzeJavaScriptStaticSource(
`function host(){return module;}${declaration}new make().require=()=>{};require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual([
expect.objectContaining({ kind: "require", specifier: "./shared.cjs" }),
]);
},
);
it.each([
["const {make=host}={make(){return {};}};", false],
["const {make=host}={make:()=>({})};", false],
["const [make=host]=[()=>({})];", false],
["const {box:{make=host}={}}={box:{make(){return {};}}};", false],
["const {make=host}={};", true],
["const [make=host]=[];", true],
["const {box:{make=host}={}}={};", true],
["const {make=host}=unknown;", true],
["const [make=host]=unknown;", true],
["const {make=host}={make:unknown};", true],
["const {make=host}={make:undefined};", true],
["const [make=host]=[undefined];", true],
["const {make=host}={get make(){return undefined;}};", true],
["const {make=host}={make:flag?()=>({}):()=>({})};", false],
[
"const values=[()=>({})];values[Symbol.iterator]=function*(){yield undefined;};const [make=host]=values;",
true,
],
[
"Array.prototype[Symbol.iterator]=function*(){yield undefined;};const [make=host]=[()=>({})];",
true,
],
["const {make=host}={make:host};", true],
["const [make=host]=[host];", true],
["const make=host;", true],
["const make=()=>({});", false],
])(
"retains only applicable default callable sources: %s",
(declaration, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`function host(){return module;}${declaration}make().require=()=>{};require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references.at(-1)).toEqual(
expect.objectContaining({
kind: "require",
specifier: expected ? null : "./shared.cjs",
}),
);
},
);
it.each([
"function* get(){yield module;}",
"function* get(){return module;}",
"async function get(){return module;}",
"async function* get(){yield module;}",
"const get=async()=>module;",
])(
"distinguishes direct wrapper objects from their eventual contents: %s",
(declaration) => {
const analysis = analyzeJavaScriptStaticSource(
`${declaration}const api=get();api.require=()=>{};api.require('./unused.cjs');require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual([
expect.objectContaining({ kind: "require", specifier: "./shared.cjs" }),
]);
},
);
it.each([
"const table={x(){return{};},y(){return{};},z(){return module;}};const get=flag?table.x:table.y;",
"const table={a:{b(){return{};},d(){return module;}},c:{d(){return{};},b(){return module;}}};const get=flag?table.a.b:table.c.d;",
])("retains precise selected direct-return receivers: %s", (declaration) => {
const analysis = analyzeJavaScriptStaticSource(
`${declaration}get().require('./unused.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual([]);
});
it.each([
["Object.prototype[0]=module;", true],
["Object.prototype['0']=module;", true],
["Object.prototype.constructor.prototype[0]=module;", true],
["globalThis.Object.prototype[0]=module;", true],
["global.Object.prototype[0]=module;", true],
["window.Object.prototype[0]=module;", true],
["self.Object.prototype[0]=module;", true],
["Object.prototype['01']=module;", false],
["Object.prototype.extra=module;", false],
["Object.prototype.require=replacement;", false],
["Array.prototype[0]=module;", true],
["Array.prototype['0']=module;", true],
["Array.prototype.constructor.prototype[0]=module;", true],
["Array.prototype['01']=module;", false],
["Array.prototype.extra=module;", false],
["Array.prototype.require=replacement;", false],
])("retains inherited numeric array iteration slots: %s", (write, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${write}const [api]=[,];api.require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
});
it.each([
"function local(Object){Object.prototype[0]=module;const[api]=[,];api.require('./shared.cjs');}",
"function local(globalThis){globalThis.Object.prototype[0]=module;const[api]=[,];api.require('./shared.cjs');}",
])("retains shadowed prototype controls: %s", (source) => {
const analysis = analyzeJavaScriptStaticSource(source);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual([]);
});
it.each([
[
"Array.prototype.constructor.prototype[Symbol.iterator]=function*(){yield module;};",
true,
],
[
"Array.prototype.constructor.prototype.constructor.prototype[Symbol.iterator]=custom;",
true,
],
["Array.prototype.constructor.prototype.extra=1;", false],
["Array.prototype.constructor.prototype.require=replacement;", false],
["Array.prototype.require=replacement;", false],
])(
"tracks iterator changes through prototype constructor cycles: %s",
(write, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${write}const [selected]=[];selected.require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
},
);
it.each([
["const get=()=>module;get()", true],
["function get(){return module;}get()", true],
["const get=()=>module;const alias=get;alias()", true],
["const get=()=>({loader:module});get().loader", true],
["const get=()=>flag?module:{};get()", true],
["const inner=()=>module;const get=()=>inner();get()", true],
["(()=>module)()", true],
["const get=()=>({require(){}});get()", false],
["const get=()=>module.exports;get()", false],
["function get(module){return module;}get({require(){}})", false],
["let get=()=>module;get=()=>({require(){}});get()", false],
])("retains possible returned module receivers: %s", (receiver, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${receiver}.require('./shared.cjs');`,
);
expect(analysis.parse_error_count).toBe(0);
expect(analysis.references).toEqual(
expected
? [expect.objectContaining({ kind: "require", specifier: null })]
: [],
);
});
it.each([
[
"const [api]=[module];Array.prototype.extra=1;api.require=replacement;",
true,
],
[
"const [api]=[module];Array.prototype[Symbol.iterator]=custom;api.require=replacement;",
true,
],
["const [api]=[{}];Array.prototype.extra=1;api.require=replacement;", false],
[
"const values={*[Symbol.iterator](){yield module;}};const [api]=values;api.require=replacement;",
true,
],
[
"const values=[{}];values[Symbol.iterator]=function*(){yield module;};const [api]=values;api.require=replacement;",
true,
],
[
"Array.prototype[Symbol.iterator]=custom;const [api]=[{}];api.require=replacement;",
true,
],
["const get=()=>module;get().require=replacement;", true],
["const get=()=>({require(){}});get().require=replacement;", false],
])(
"retains host delegate uncertainty after possible receiver writes: %s",
(write, expected) => {
const analysis = analyzeJavaScriptStaticSource(
`${write}require('./shared.cjs');`,
);
expect(analysis.references).toEqual([
expect.objectContaining({
kind: "require",
specifier: expected ? null : "./shared.cjs",
}),
]);
},
);
it("preserves a require before later returned-receiver delegate writes", () => {
const analysis = analyzeJavaScriptStaticSource(
`require('./shared.cjs');const get=()=>module;get().require=replacement;`,
);
expect(analysis.references).toEqual([
expect.objectContaining({ kind: "require", specifier: "./shared.cjs" }),
]);
});