mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
fix(javascript): keep CommonJS wrapper redeclarations from inventing application owners (#12084)
fix(javascript): keep wrapper redeclarations from inventing application owners A top-level lexical declaration that reuses a CommonJS wrapper parameter cannot evaluate, and its recovered syntax is complete. Counting that diagnostic as dropped syntax marked the entrypoint as an unexamined loader and erased another package's proven loadFile root. Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <morluto@users.noreply.github.com>
This commit is contained in:
7 files changed
+188
-5
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { ArtifactInventorySnapshot } from "../../domain/artifactInventorySnapshot.js";
|
||||
@@ -423,6 +424,127 @@ it("reports partial coverage when a successful static pass stopped at a limit",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not treat a CommonJS wrapper redeclaration as another application owner", async () => {
|
||||
const digest = (value: string) =>
|
||||
createHash("sha256").update(value).digest("hex");
|
||||
const textFile = (
|
||||
path: string,
|
||||
source: string,
|
||||
kind: JavaScriptArtifactFile["kind"],
|
||||
): JavaScriptArtifactFile => ({
|
||||
path,
|
||||
container_sha256: SHA256,
|
||||
sha256: digest(path + source),
|
||||
bytes: source.length,
|
||||
inventory_artifact_id: `art_${digest(path)}`,
|
||||
kind,
|
||||
unpacked: false,
|
||||
text: { included: true, value: source },
|
||||
});
|
||||
const files = [
|
||||
textFile(
|
||||
"a/package.json",
|
||||
JSON.stringify({ main: "main.cjs" }),
|
||||
"package-json",
|
||||
),
|
||||
textFile(
|
||||
"a/main.cjs",
|
||||
"const require={main:{require(){}}};require.main.require('../shared/window.cjs');",
|
||||
"javascript",
|
||||
),
|
||||
textFile("b/package.json", '{"main":"main.cjs"}', "package-json"),
|
||||
textFile("b/main.cjs", "require('../shared/window.cjs');", "javascript"),
|
||||
textFile(
|
||||
"shared/window.cjs",
|
||||
"const {BrowserWindow}=require('electron');new BrowserWindow().loadFile('index.html');",
|
||||
"javascript",
|
||||
),
|
||||
textFile("a/index.html", "possible page", "html"),
|
||||
textFile("b/index.html", "proven page", "html"),
|
||||
textFile("shared/index.html", "source-relative decoy", "html"),
|
||||
textFile("index.html", "artifact-root decoy", "html"),
|
||||
];
|
||||
const analyzed = await Promise.all(
|
||||
files
|
||||
.filter((file) => file.kind === "javascript")
|
||||
.map((file) =>
|
||||
analyzedEntry(
|
||||
file.text.included ? file.text.value : "",
|
||||
file.path,
|
||||
).then((entry) => ({ ...entry, file })),
|
||||
),
|
||||
);
|
||||
const local = analyzed.find((entry) => entry.file.path === "a/main.cjs");
|
||||
expect(local?.javascript).toMatchObject({
|
||||
parse_status: "complete",
|
||||
parse_error_count: 0,
|
||||
references: [],
|
||||
analysis_coverage: {
|
||||
syntax: { status: "complete", error_count: 0 },
|
||||
semantics: { uncertainty: ["commonjs-wrapper-redeclaration"] },
|
||||
},
|
||||
});
|
||||
const packageObservation = (path: string, main: string) => ({
|
||||
path,
|
||||
sha256: digest(path),
|
||||
status: "included" as const,
|
||||
name: null,
|
||||
version: null,
|
||||
main,
|
||||
main_status: "declared" as const,
|
||||
main_entrypoint: main,
|
||||
renderer_declarations: {},
|
||||
renderer_entrypoint: null,
|
||||
limitation: null,
|
||||
});
|
||||
const analysis: JavaScriptArtifactAnalysis = {
|
||||
jsonDocuments: new JavaScriptJsonDocuments(),
|
||||
files: analyzed,
|
||||
packages: [
|
||||
packageObservation("a/package.json", "main.cjs"),
|
||||
packageObservation("b/package.json", "main.cjs"),
|
||||
],
|
||||
json_modules: [],
|
||||
html_scripts: [],
|
||||
html_inline_scripts: [],
|
||||
source_maps: [],
|
||||
visited_ast_nodes: 0,
|
||||
findings: 0,
|
||||
modules: 0,
|
||||
parse_failures: 0,
|
||||
limitations: [],
|
||||
};
|
||||
const steps = buildGraph(
|
||||
snapshot,
|
||||
{
|
||||
files,
|
||||
unavailable_relevant_files: [],
|
||||
containers: [],
|
||||
text_bytes_read: files.reduce((total, file) => total + file.bytes, 0),
|
||||
invalid_utf8_files: 0,
|
||||
},
|
||||
analysis,
|
||||
);
|
||||
let next = steps.next();
|
||||
while (next.done !== true) next = steps.next();
|
||||
const roles = next.value.graph.nodes.filter(
|
||||
(node) => node.kind === "electron-renderer",
|
||||
);
|
||||
expect(roles).toHaveLength(1);
|
||||
expect(roles[0]?.observations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
declared_path: "index.html",
|
||||
resolution_status: "resolved",
|
||||
resolved_path: "b/index.html",
|
||||
limitations: [],
|
||||
}),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps recovered Electron facts partial after syntax recovery", async () => {
|
||||
const source = `import { BrowserWindow } from "electron"; new BrowserWindow({webPreferences: {sandbox: true}}); const duplicate = 1; const duplicate = 2;`;
|
||||
const entry = await analyzedEntry(source, "main.mjs");
|
||||
|
||||
Reference in new issue
Block a user