Skip to content

Commit b91b7e0

Browse files
Facetomyselfmorluto
authored andcommitted
fix(javascript): ignore Electron callee names that only share a suffix (#2678)
loadFile, loadURL, and serviceWorker.register matched any callee ending with those spellings. uploadFile, downloadURL, and myserviceWorker.register were recorded as renderer or service-worker evidence, and the loadURL suffix also became a network endpoint.
1 parent 2895a18 commit b91b7e0

3 files changed

Lines changed: 129 additions & 4 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
4+
5+
const project = (source: string) => {
6+
const analysis = analyzeJavaScriptStaticSource(source);
7+
return {
8+
status: analysis.parse_status,
9+
roles: analysis.role_paths.map(({ role, path, mechanism }) => ({
10+
role,
11+
path,
12+
mechanism,
13+
})),
14+
references: analysis.references.map(({ kind, specifier }) => ({
15+
kind,
16+
specifier,
17+
})),
18+
endpoints: analysis.endpoints.map(({ kind, value, mechanism }) => ({
19+
kind,
20+
value,
21+
mechanism,
22+
})),
23+
};
24+
};
25+
26+
describe("Electron callee member boundaries", () => {
27+
it("keeps exact and dotted loadFile, loadURL, and serviceWorker.register calls", () => {
28+
expect(
29+
project(`
30+
loadFile("./bare.html");
31+
win.loadFile("./renderer/index.html");
32+
app.win.loadFile("./nested.html");
33+
loadURL("https://bare.example.test/");
34+
BrowserWindow.loadURL("https://app.example.test/page");
35+
win?.loadURL("https://optional.example.test/");
36+
serviceWorker.register("./bare-sw.js");
37+
navigator.serviceWorker.register("./sw.js");
38+
`),
39+
).toEqual({
40+
status: "complete",
41+
roles: [
42+
{
43+
role: "renderer",
44+
path: "./bare.html",
45+
mechanism: "call:loadFile",
46+
},
47+
{
48+
role: "renderer",
49+
path: "./renderer/index.html",
50+
mechanism: "call:win.loadFile",
51+
},
52+
{
53+
role: "renderer",
54+
path: "./nested.html",
55+
mechanism: "call:app.win.loadFile",
56+
},
57+
{
58+
role: "renderer",
59+
path: "https://bare.example.test/",
60+
mechanism: "call:loadURL",
61+
},
62+
{
63+
role: "renderer",
64+
path: "https://app.example.test/page",
65+
mechanism: "call:BrowserWindow.loadURL",
66+
},
67+
{
68+
role: "renderer",
69+
path: "https://optional.example.test/",
70+
mechanism: "call:win.loadURL",
71+
},
72+
],
73+
references: [
74+
{ kind: "service-worker", specifier: "./bare-sw.js" },
75+
{ kind: "service-worker", specifier: "./sw.js" },
76+
],
77+
endpoints: [
78+
{
79+
kind: "network",
80+
value: "https://bare.example.test/",
81+
mechanism: "call:loadURL",
82+
},
83+
{
84+
kind: "network",
85+
value: "https://app.example.test/page",
86+
mechanism: "call:BrowserWindow.loadURL",
87+
},
88+
{
89+
kind: "network",
90+
value: "https://optional.example.test/",
91+
mechanism: "call:win.loadURL",
92+
},
93+
],
94+
});
95+
});
96+
97+
it("does not treat a shared identifier suffix as an Electron callee", () => {
98+
expect(
99+
project(`
100+
uploadFile("./secret.html");
101+
preloadFile("./also.html");
102+
loadFileBackup("./nope.html");
103+
downloadURL("https://cdn.example.test/app");
104+
reloadURL("https://cdn.example.test/other");
105+
notloadURL("https://cdn.example.test/nope");
106+
myserviceWorker.register("./nope.js");
107+
serviceWorker.registerExtra("./nope.js");
108+
`),
109+
).toEqual({
110+
status: "complete",
111+
roles: [],
112+
references: [],
113+
endpoints: [],
114+
});
115+
});
116+
});

‎src/domain/javascript/javascriptStaticAnalysisCalls.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
argumentValue,
1313
calleeName,
1414
endpointArgument as endpointArgumentHelper,
15+
isDottedCallee,
1516
prefixedArgument,
1617
range,
1718
rangeForOffsets,
@@ -94,13 +95,13 @@ export const inspectCall = (
9495
? undefined
9596
: workerUrlSpecifier(worker, bindings)),
9697
});
97-
if (name.endsWith("serviceWorker.register"))
98+
if (isDottedCallee(name, "serviceWorker.register"))
9899
addReference(context, { node, kind: "service-worker", specifier: first });
99100
inspectEndpointCall(node, name, first, context);
100101
inspectStorageCall(node, name, first, context);
101102
if (first === "audio" || first === "video")
102103
noteMediaElementPreloads(node, accumulator);
103-
if (name.endsWith("loadFile") && first !== undefined)
104+
if (isDottedCallee(name, "loadFile") && first !== undefined)
104105
addLocatedFinding(context, {
105106
collection: accumulator.roles,
106107
key: `role\0renderer\0${first}`,
@@ -114,7 +115,7 @@ export const inspectCall = (
114115
location: range(node),
115116
},
116117
});
117-
if (name.endsWith("loadURL") && first !== undefined)
118+
if (isDottedCallee(name, "loadURL") && first !== undefined)
118119
addLocatedFinding(context, {
119120
collection: accumulator.roles,
120121
key: `role\0renderer\0${first}`,

‎src/domain/javascript/javascriptStaticAnalysisHelpers.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,14 @@ const isKeyedCollectionReceiver = (callee: t.Node): boolean => {
540540
return property === "headers" || property === "searchParams";
541541
};
542542

543+
/**
544+
* True when `name` is exactly `member`, or a dotted callee path ending in
545+
* `.member`. A shared suffix inside one identifier, such as `uploadFile`
546+
* for `loadFile`, does not match.
547+
*/
548+
export const isDottedCallee = (name: string, member: string): boolean =>
549+
name === member || name.endsWith(`.${member}`);
550+
543551
/** Select the literal URL argument for recognized network callees. */
544552
export const endpointArgument = (
545553
name: string,
@@ -573,7 +581,7 @@ export const endpointArgument = (
573581
? undefined
574582
: value;
575583
}
576-
if (name.endsWith("loadURL")) return stringValue(args[0]);
584+
if (isDottedCallee(name, "loadURL")) return stringValue(args[0]);
577585
return undefined;
578586
};
579587

0 commit comments

Comments
 (0)