|
|
|
@@ -1,28 +1,28 @@
|
|
|
|
|
import { expect, it } from "vitest";
|
|
|
|
|
|
|
|
|
|
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
|
|
|
|
import { analyzeCommonJsSemanticFixture } from "../../../tests/fixtures/javascriptCommonJsAnalysis.js";
|
|
|
|
|
|
|
|
|
|
it("shares required process writes with globals while keeping earlier observations", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`const proc=require('process');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`const proc=require('process');
|
|
|
|
|
process.cwd(); const before=proc.env.BEFORE;
|
|
|
|
|
proc.env={}; const after=process.env.AFTER;`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["BEFORE"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not propagate replacement-object writes from import-equals direct origins", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`import fs = require('fs');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`import fs = require('fs');
|
|
|
|
|
const other=require('fs'); fs={}; fs.readFileSync=custom; other.readFileSync('kept');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains original import-equals aliases captured before reassignment", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`import fs = require('fs');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`import fs = require('fs');
|
|
|
|
|
const alias=fs; const other=require('fs'); fs={}; alias.readFileSync=custom; other.readFileSync('wrong');`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps computed namespace calls as unknown receiver escapes", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const fs=require('fs'); fs.hook=custom; fs[key](); fs.readFileSync('wrong');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
@@ -31,7 +31,8 @@ it("keeps computed namespace calls as unknown receiver escapes", () => {
|
|
|
|
|
it.each(["(0, fs.readFileSync)", "(assigned = fs.readFileSync)"])(
|
|
|
|
|
"checks captured function validity through %s",
|
|
|
|
|
(value) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`const fs=require('fs'); let assigned;
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs=require('fs'); let assigned;
|
|
|
|
|
const saved=${value}; fs.readFileSync=custom; const late=${value}; saved('kept'); late('wrong');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
|
},
|
|
|
|
@@ -41,7 +42,7 @@ it.each(['process.env={MODE:"local"};', "process.argv=[];", "patch(process);"])(
|
|
|
|
|
"shares unbound process object effects with required aliases: %s",
|
|
|
|
|
(write) => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`${write} const proc=require('process');
|
|
|
|
|
analyzeCommonJsSemanticFixture(`${write} const proc=require('process');
|
|
|
|
|
${write.includes("argv") ? "const arg=proc.argv[2];" : "const mode=proc.env.MODE;"}`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
},
|
|
|
|
@@ -51,14 +52,14 @@ it.each([
|
|
|
|
|
"function local(process) {process.env={};}",
|
|
|
|
|
"process={}; process.env={};",
|
|
|
|
|
])("keeps unrelated process replacements separate: %s", (write) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`${write} const proc=require('process'); const mode=proc.env.MODE;`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["MODE"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves exports captured before a later loader overwrite", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs=require('node:fs'); fs.readFileSync('before');
|
|
|
|
|
const read=fs.readFileSync;
|
|
|
|
|
require('fs').readFileSync('direct-before');
|
|
|
|
@@ -82,9 +83,9 @@ it.each([
|
|
|
|
|
])(
|
|
|
|
|
"keeps repeated or already replaced loader captures uncertain: %s",
|
|
|
|
|
(source) => {
|
|
|
|
|
expect(analyzeJavaScriptSemantics(source).configurationOperations).toEqual(
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations,
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
@@ -95,7 +96,7 @@ it.each([
|
|
|
|
|
])(
|
|
|
|
|
"shares require-mode import-equals effects in both directions: %s",
|
|
|
|
|
(write, call) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`import fs = require('node:fs');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`import fs = require('node:fs');
|
|
|
|
|
const other = require('fs'); other.readFileSync('before'); ${write} ${call}`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual([
|
|
|
|
|
"before",
|
|
|
|
@@ -110,7 +111,7 @@ it.each([
|
|
|
|
|
])(
|
|
|
|
|
"retains captured native function calls after argument escape: %s",
|
|
|
|
|
(capture) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`${capture} observe(read); read('kept');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
@@ -118,20 +119,20 @@ it.each([
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("distinguishes escaping a method value from escaping its namespace", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`const fs=require('fs');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`const fs=require('fs');
|
|
|
|
|
observe(fs.readFileSync); fs.readFileSync('kept');
|
|
|
|
|
observe(fs); fs.readFileSync('wrong');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("still invalidates escaped process configuration containers", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`const proc=require('process');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`const proc=require('process');
|
|
|
|
|
observe(proc.env); const mode=proc.env.MODE;`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains direct require effects without poisoning later cached receivers", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
require('node:fs').readFileSync('first');
|
|
|
|
|
const fs = require('fs'); fs.readFileSync('second');
|
|
|
|
|
require('node:fs').createReadStream('stream');
|
|
|
|
@@ -156,7 +157,7 @@ it.each([
|
|
|
|
|
"require('node:fs').promises.open = custom;",
|
|
|
|
|
"const fsp = require('fs/promises'); fsp.open = custom;",
|
|
|
|
|
])("shares fs.promises object writes with fs/promises: %s", (write) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const before = require('node:fs/promises'); before.open('before');
|
|
|
|
|
${write}
|
|
|
|
|
const after = require('node:fs/promises'); after.open('wrong');
|
|
|
|
@@ -169,7 +170,7 @@ it.each([
|
|
|
|
|
"({ loader: require } = other);",
|
|
|
|
|
"for (require of loaders) {}",
|
|
|
|
|
])("rejects provenance from an overwritten ambient loader: %s", (write) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`${write}
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`${write}
|
|
|
|
|
const fs = require('node:fs'); fs.readFileSync('wrong');
|
|
|
|
|
require('node:fs').readFileSync('also-wrong');
|
|
|
|
|
`);
|
|
|
|
@@ -180,7 +181,7 @@ it.each([
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps local loader reassignment separate from the ambient loader", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
function local(require) { require = replacement; }
|
|
|
|
|
const fs = require('node:fs'); fs.readFileSync('kept');
|
|
|
|
|
`);
|
|
|
|
@@ -188,7 +189,7 @@ it("keeps local loader reassignment separate from the ambient loader", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps wrapper require separate from global-object property writes", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
globalThis.require = replacement;
|
|
|
|
|
function polyfill(key) { globalThis[key] = replacement; }
|
|
|
|
|
require('node:fs').readFileSync('kept');
|
|
|
|
@@ -199,7 +200,7 @@ it("keeps wrapper require separate from global-object property writes", () => {
|
|
|
|
|
it.each(["import fs from 'node:fs';", "const fs = require('node:fs');"])(
|
|
|
|
|
"retains reads and saved methods before a later write: %s",
|
|
|
|
|
(declaration) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`${declaration}
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`${declaration}
|
|
|
|
|
fs.readFileSync('before');
|
|
|
|
|
const saved = fs.readFileSync;
|
|
|
|
|
const { readFileSync: destructured } = fs;
|
|
|
|
@@ -220,7 +221,7 @@ it.each([
|
|
|
|
|
"const { readFileSync: read } = require('fs');",
|
|
|
|
|
"const read = require('fs').readFileSync;",
|
|
|
|
|
])("checks cached module writes at the projected capture: %s", (capture) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
const saved = require('fs').readFileSync;
|
|
|
|
|
fs.readFileSync = custom;
|
|
|
|
@@ -231,7 +232,7 @@ it.each([
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains captured methods across later escapes without freezing a namespace alias", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('node:fs'); const saved = require('fs').readFileSync;
|
|
|
|
|
const namespace = require('fs'); patch(fs);
|
|
|
|
|
const late = require('fs').readFileSync;
|
|
|
|
@@ -241,7 +242,7 @@ it("retains captured methods across later escapes without freezing a namespace a
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps writes in repeated callable and loop activations conservative", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('node:fs');
|
|
|
|
|
function run() { fs.readFileSync('wrong'); fs.readFileSync = custom; }
|
|
|
|
|
for (;;) { const read = require('fs').readFileSync; read('wrong'); fs.readFileSync = custom; }
|
|
|
|
@@ -250,14 +251,14 @@ it("keeps writes in repeated callable and loop activations conservative", () =>
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not propagate a reassigned local object's writes into cached exports", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`let a = require('node:fs'); const b = require('node:fs'); a = {}; a.readFileSync = custom; b.readFileSync('kept');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toMatchObject([{ key: "kept" }]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("shares repeated require mutations for every built-in effect family", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('fs'); const otherFs = require('node:fs'); fs.createReadStream = custom; otherFs.createReadStream('wrong');
|
|
|
|
|
const cp = require('child_process'); const otherCp = require('node:child_process'); cp.spawn = custom; otherCp.spawn('wrong', []);
|
|
|
|
|
const timers = require('timers'); const otherTimers = require('node:timers'); timers.setTimeout = custom; otherTimers.setTimeout(callback, 1);
|
|
|
|
@@ -274,7 +275,7 @@ it("shares repeated require mutations for every built-in effect family", () => {
|
|
|
|
|
it.each(["node:fs", "fs"])(
|
|
|
|
|
"shares repeated built-in require identity across %s",
|
|
|
|
|
(specifier) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const a = require('node:fs'); const b = require('${specifier}'); const alias = b; a.readFileSync = custom; b.createReadStream('kept'); alias.readFileSync('wrong');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
@@ -285,14 +286,14 @@ it.each(["node:fs", "fs"])(
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("shares unknown escapes across repeated requires without losing earlier effects", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const a = require('node:fs'); const b = require('fs'); b.readFileSync('before'); patch(a); b.readFileSync('after');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["before"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves exact default ESM import mutation paths", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`import fs from 'node:fs'; fs.writeFileSync = custom; fs.readFileSync('kept'); fs.writeFileSync('replaced');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toMatchObject([
|
|
|
|
@@ -301,7 +302,7 @@ it("preserves exact default ESM import mutation paths", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains built-in effects before later top-level escapes", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('node:fs'); const cp = require('node:child_process'); const timers = require('node:timers'); const http = require('node:http');
|
|
|
|
|
fs.readFileSync('before'); fs.createReadStream('before'); cp.spawn('tool', []); timers.setTimeout(callback, 1); http.get('http://example.test');
|
|
|
|
|
patch(fs); patch(cp); patch(timers); patch(http);
|
|
|
|
@@ -319,7 +320,7 @@ it("retains built-in effects before later top-level escapes", () => {
|
|
|
|
|
it.each(["env", "argv"])(
|
|
|
|
|
"does not treat a replaced process %s as configuration",
|
|
|
|
|
(family) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const proc = require('node:process'); proc.${family} = replacement; const value = proc.${family}[${family === "env" ? "'MODE'" : "2"}];`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
@@ -327,14 +328,14 @@ it.each(["env", "argv"])(
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("does not infer temporal safety across repeated callable or loop activations", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const fs = require('node:fs'); function run() { fs.readFileSync('callable'); patch(fs); } for (;;) { fs.readFileSync('loop'); patch(fs); }`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not recover a replaced method through a subsequent alias", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const fs = require('node:fs'); fs.readFileSync = custom; const read = fs.readFileSync; read('config.json');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
@@ -345,7 +346,7 @@ it.each([
|
|
|
|
|
"patch(fs); patch(cp); patch(timers); patch(http);",
|
|
|
|
|
"const fsAlias = fs; fsAlias.readFileSync = custom; fsAlias.createReadStream = custom; const cpAlias = cp; cpAlias.spawn = custom; timers[key] = custom; delete http.get;",
|
|
|
|
|
])("excludes replaced or escaped CommonJS methods: %s", (mutation) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fs = require('node:fs'); const cp = require('node:child_process');
|
|
|
|
|
const timers = require('node:timers'); const http = require('node:http');
|
|
|
|
|
${mutation}
|
|
|
|
@@ -360,7 +361,7 @@ it.each([
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves unaffected CommonJS methods after sibling property mutation", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const fs = require('node:fs'); fs.writeFileSync = custom; fs.readFileSync('config.json');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toMatchObject([
|
|
|
|
@@ -378,7 +379,7 @@ it.each([
|
|
|
|
|
const { setTimeout: schedule } = require("timers");
|
|
|
|
|
const { get } = require("http");`,
|
|
|
|
|
])("recovers builtin effects from CommonJS provenance: %s", (declarations) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`${declarations}
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`${declarations}
|
|
|
|
|
const text = read("config.json", "utf8");
|
|
|
|
|
const stream = fs.createReadStream("config.json"); stream.destroy();
|
|
|
|
|
const child = launch("tool", []); child.kill("SIGTERM");
|
|
|
|
@@ -412,7 +413,7 @@ it.each([
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not promote ambiguous or non-namespace CommonJS origins to builtin effects", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
const fake = require("./fs.js"); fake.readFileSync("external");
|
|
|
|
|
const absentDefault = require("node:fs").default; absentDefault.readFileSync("default");
|
|
|
|
|
const { constants } = require("node:fs"); constants.createReadStream("nested");
|
|
|
|
@@ -425,7 +426,7 @@ it("does not promote ambiguous or non-namespace CommonJS origins to builtin effe
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not assign built-in APIs to unrelated or shadowed bindings", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`
|
|
|
|
|
import fs from "./fs.js";
|
|
|
|
|
import { constants as constantsFs } from "node:fs";
|
|
|
|
|
fs.createReadStream("config.json");
|
|
|
|
@@ -443,9 +444,9 @@ it.each([
|
|
|
|
|
`import proc from 'node:process'; proc.env = {}; const value = process.env.MODE;`,
|
|
|
|
|
`import fs from 'node:fs'; fs.readFileSync = custom; const other = require('fs'); other.readFileSync('wrong');`,
|
|
|
|
|
])("keeps delayed or replaced built-in identities unknown: %s", (source) => {
|
|
|
|
|
expect(analyzeJavaScriptSemantics(source).configurationOperations).toEqual(
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations,
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
@@ -454,7 +455,7 @@ it.each([
|
|
|
|
|
`import fs from 'node:fs'; fs.writeFileSync = custom; const other = require('fs'); other.readFileSync('kept');`,
|
|
|
|
|
])("keeps earlier and independent built-in observations: %s", (source) => {
|
|
|
|
|
expect(
|
|
|
|
|
analyzeJavaScriptSemantics(source).configurationOperations.map(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations.map(
|
|
|
|
|
({ key }) => key,
|
|
|
|
|
),
|
|
|
|
|
).toEqual(["kept"]);
|
|
|
|
@@ -469,7 +470,7 @@ it("shares equivalent alias subproblems and repeated intact receiver facts", ()
|
|
|
|
|
{ length: 128 },
|
|
|
|
|
(_, index) => `a29.readFileSync('file-${index}');`,
|
|
|
|
|
);
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
["const fs = require('fs');", ...aliases, ...calls].join("\n"),
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations).toHaveLength(128);
|
|
|
|
@@ -481,7 +482,7 @@ it.each([
|
|
|
|
|
`const tls = require('tls'); const socket = tls.connect(443, 'example.test'); socket.end();`,
|
|
|
|
|
])("retains TLS acquisitions and their release: %s", (source) => {
|
|
|
|
|
expect(
|
|
|
|
|
analyzeJavaScriptSemantics(source).resourceOperations.map(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).resourceOperations.map(
|
|
|
|
|
({ kind, method }) => ({ kind, method }),
|
|
|
|
|
),
|
|
|
|
|
).toEqual([
|
|
|
|
@@ -497,9 +498,9 @@ it.each([
|
|
|
|
|
`globalThis.process.env = { MODE: 'local' }; const proc = require('process'); const value = proc.env.MODE;`,
|
|
|
|
|
`const proc = require('process'); proc.env.MODE = 'local'; const value = proc.env.MODE;`,
|
|
|
|
|
])("shares writes through the original process object: %s", (source) => {
|
|
|
|
|
expect(analyzeJavaScriptSemantics(source).configurationOperations).toEqual(
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations,
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
@@ -519,7 +520,7 @@ it.each([
|
|
|
|
|
"retains reads before a later sequence operand: %s",
|
|
|
|
|
(source, expectedKey) => {
|
|
|
|
|
expect(
|
|
|
|
|
analyzeJavaScriptSemantics(source).configurationOperations.map(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations.map(
|
|
|
|
|
({ key }) => key,
|
|
|
|
|
),
|
|
|
|
|
).toEqual([expectedKey]);
|
|
|
|
@@ -528,21 +529,21 @@ it.each([
|
|
|
|
|
|
|
|
|
|
it("does not use source offsets for destructuring assignment evaluation order", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const fs = require('fs'); const target = {};
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs = require('fs'); const target = {};
|
|
|
|
|
[target[fs.readFileSync('wrong')]] = (fs.readFileSync = custom, []);`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps process reads after an alias is reassigned away from the global", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`let host = globalThis; host = {process: {env: {}}};
|
|
|
|
|
analyzeCommonJsSemanticFixture(`let host = globalThis; host = {process: {env: {}}};
|
|
|
|
|
host.process.env = {}; const proc = require('process'); const value = proc.env.MODE;`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["MODE"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects CommonJS require after the wrapper module replaces its delegate", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`module.require = () => ({ readFileSync() {} });
|
|
|
|
|
analyzeCommonJsSemanticFixture(`module.require = () => ({ readFileSync() {} });
|
|
|
|
|
const fs = require('node:fs'); fs.readFileSync('unknown');`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
@@ -552,7 +553,7 @@ it.each([
|
|
|
|
|
`const owner = module; owner.require = () => ({}); const fs = require('fs'); fs.readFileSync('wrong');`,
|
|
|
|
|
`Object.defineProperty(module, 'require', {value: () => ({})}); const fs = require('fs'); fs.readFileSync('wrong');`,
|
|
|
|
|
])("tracks only actual wrapper require replacements: %s", (source) => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(source);
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(source);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(
|
|
|
|
|
source.includes("extra") ? ["kept"] : [],
|
|
|
|
|
);
|
|
|
|
@@ -560,7 +561,7 @@ it.each([
|
|
|
|
|
|
|
|
|
|
it("preserves a saved promises object after deleting its parent export", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
delete fs.promises; const handle = fsp.open('kept'); handle.close();`);
|
|
|
|
|
expect(
|
|
|
|
|
ir.resourceOperations.map(({ kind, method }) => ({ kind, method })),
|
|
|
|
@@ -571,7 +572,7 @@ it("preserves a saved promises object after deleting its parent export", () => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not infer network acquisition methods from another built-in", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const { open } = require('node:net'); open();`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.resourceOperations).toEqual([]);
|
|
|
|
@@ -579,7 +580,7 @@ it("does not infer network acquisition methods from another built-in", () => {
|
|
|
|
|
|
|
|
|
|
it("recognizes nested ESM namespace receivers as original built-ins", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`import * as ns from 'node:fs'; import fs from 'node:fs';
|
|
|
|
|
analyzeCommonJsSemanticFixture(`import * as ns from 'node:fs'; import fs from 'node:fs';
|
|
|
|
|
ns.default.readFileSync('first'); fs.readFileSync('second');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual([
|
|
|
|
|
"first",
|
|
|
|
@@ -589,7 +590,7 @@ it("recognizes nested ESM namespace receivers as original built-ins", () => {
|
|
|
|
|
|
|
|
|
|
it("keeps intact fs/promises identity after deleting the fs.promises slot", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const fs=require('fs'); delete fs.promises;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs=require('fs'); delete fs.promises;
|
|
|
|
|
const fsp=require('fs/promises'); const handle=fsp.open('kept'); handle.close();`);
|
|
|
|
|
expect(
|
|
|
|
|
ir.resourceOperations.map(({ kind, method }) => ({ kind, method })),
|
|
|
|
@@ -604,11 +605,11 @@ it.each([
|
|
|
|
|
`const fs = require('fs'); patch(fs.promises); require('fs/promises').open('wrong');`,
|
|
|
|
|
`const fs = require('fs'); patch(fs.promises); require('node:fs/promises').open('wrong');`,
|
|
|
|
|
])("keeps replaced or escaped promises receivers unknown: %s", (source) => {
|
|
|
|
|
expect(analyzeJavaScriptSemantics(source).resourceOperations).toEqual([]);
|
|
|
|
|
expect(analyzeCommonJsSemanticFixture(source).resourceOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("follows final sequence expressions for direct built-in method calls", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(`const fs = require('fs');
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(`const fs = require('fs');
|
|
|
|
|
(0, fs.readFileSync)('kept'); fs.readFileSync = custom;
|
|
|
|
|
(0, fs.readFileSync)('wrong');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
@@ -620,35 +621,35 @@ it.each([
|
|
|
|
|
])(
|
|
|
|
|
"shares named process containers with their namespace aliases: %s",
|
|
|
|
|
(source) => {
|
|
|
|
|
expect(analyzeJavaScriptSemantics(source).configurationOperations).toEqual(
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
analyzeCommonJsSemanticFixture(source).configurationOperations,
|
|
|
|
|
).toEqual([]);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it("retains uncertainty after a computed write to module's require delegate", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const key = 'require'; module[key] = replacement;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const key = 'require'; module[key] = replacement;
|
|
|
|
|
const fs = require('fs'); fs.readFileSync('wrong');`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains a module namespace captured before its binding is reassigned", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`let fs = require('fs'); const saved = fs;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`let fs = require('fs'); const saved = fs;
|
|
|
|
|
fs = {}; saved.readFileSync('kept');`);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not let own fs probe calls hide later builtin reads", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`const fs=require('fs'); fs.existsSync('probe'); fs.readFileSync('kept');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("resolves nested ESM default namespace methods", () => {
|
|
|
|
|
const ir = analyzeJavaScriptSemantics(
|
|
|
|
|
const ir = analyzeCommonJsSemanticFixture(
|
|
|
|
|
`import * as ns from 'node:fs'; ns.default.readFileSync('kept');`,
|
|
|
|
|
);
|
|
|
|
|
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["kept"]);
|
|
|
|
@@ -656,21 +657,21 @@ it("resolves nested ESM default namespace methods", () => {
|
|
|
|
|
|
|
|
|
|
it("tracks process objects passed through qualified global references", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`patch(globalThis.process); const proc = require('process');
|
|
|
|
|
analyzeCommonJsSemanticFixture(`patch(globalThis.process); const proc = require('process');
|
|
|
|
|
const mode = proc.env.MODE;`);
|
|
|
|
|
expect(ir.configurationOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("checks a saved child object's method when that function is selected", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
fs.promises.open = custom; const open = fsp.open; open('wrong');`);
|
|
|
|
|
expect(ir.resourceOperations).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("retains an earlier captured function after its object method is replaced", () => {
|
|
|
|
|
const ir =
|
|
|
|
|
analyzeJavaScriptSemantics(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
analyzeCommonJsSemanticFixture(`const fs = require('fs'); const fsp = fs.promises;
|
|
|
|
|
const open = fsp.open; fs.promises.open = custom; open('kept');`);
|
|
|
|
|
expect(ir.resourceOperations).toMatchObject([
|
|
|
|
|
{ kind: "acquire", method: "open" },
|
|
|
|
|