mirror of
https://github.com/morluto/rea
synced 2026-10-28 06:46:47 -04:00
7e1c560dd6
Decode known literal file URLs with explicit path semantics before assigning filesystem-expression context. Preserve unknown drive, UNC, dynamic and malformed inputs and existing source-relative projection. Validation: 64 path-owner and 26 filesystem application tests pass. Fixes #2561. Based on #3147. Co-authored-by: João Pedro de Faria <jpdefo7@hotmail.com>
320 lines
9.9 KiB
TypeScript
320 lines
9.9 KiB
TypeScript
import { posix } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { parseExpression } from "@babel/parser";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { analyzeJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
|
import {
|
|
staticPath,
|
|
staticPathResolutionContext,
|
|
} from "./javascriptStaticAnalysisHelpers.js";
|
|
import type { JavaScriptStaticPathContext } from "./javascriptStaticAnalysisTypes.js";
|
|
|
|
const absoluteParts = [
|
|
["/app", "/shared/preload.js"],
|
|
["ignored", "/app", "preload.js"],
|
|
["/app", "first", "/shared", "nested", "../preload.js"],
|
|
["/app", "preload///"],
|
|
["/app", "../../.."],
|
|
["///"],
|
|
["", "/app", "", "preload.js", ""],
|
|
];
|
|
|
|
const anchoredExpressions = [
|
|
['path.resolve(__dirname, "preload.cjs")', "preload.cjs"],
|
|
['path.posix.resolve("ignored", __dirname, "preload.js")', "preload.js"],
|
|
['path.resolve("/ignored", __dirname, "preload.js")', "preload.js"],
|
|
[
|
|
'path.posix.resolve("ignored", dirname(fileURLToPath(import.meta.url)), "preload.js")',
|
|
"preload.js",
|
|
],
|
|
[
|
|
'path.resolve("ignored", path.dirname(__filename), "preload.js")',
|
|
"preload.js",
|
|
],
|
|
['path.resolve(__dirname, "../preload.js")', "../preload.js"],
|
|
['path.resolve(__dirname, "sub", __dirname, "preload.js")', "preload.js"],
|
|
['path.resolve(__dirname, "/shared", "preload.js")', "/shared/preload.js"],
|
|
["path.resolve(__dirname)", "."],
|
|
['path.posix.resolve(`/app`, `/shared`, "preload.js")', "/shared/preload.js"],
|
|
['path.posix.resolve("" + "/app", "preload.js")', "/app/preload.js"],
|
|
['__dirname + "/preload.js"', "preload.js"],
|
|
['__dirname + "/" + "preload.js"', "preload.js"],
|
|
['path.dirname(__filename) + "/lib" + "/preload.js"', "lib/preload.js"],
|
|
] as const;
|
|
|
|
const unknownExpressions = [
|
|
"path.resolve()",
|
|
"path.posix.resolve()",
|
|
'path.resolve("sub", "../preload.js")',
|
|
'path.resolve("", "")',
|
|
'path.resolve(runtimeBase, "/app/preload.js")',
|
|
'path.resolve("/app", runtimeFile)',
|
|
'path.resolve(runtimeBase, __dirname, "preload.js")',
|
|
"path.resolve(__dirname, runtimeFile)",
|
|
'path.resolve(...parts, "/app/preload.js")',
|
|
'path.resolve("/app", ...parts)',
|
|
'path.resolve(42, "/app/preload.js")',
|
|
'path.resolve("/app", null)',
|
|
"path.resolve(`/app/${runtimeFile}`)",
|
|
'path.win32.resolve("/app", "/shared/preload.js")',
|
|
'path.win32.resolve(__dirname, "preload.js")',
|
|
'win32.resolve("/app", "preload.js")',
|
|
'resolve("/app", "preload.js")',
|
|
'__dirname + "preload.js"',
|
|
'path.dirname(__filename) + "preload.js"',
|
|
];
|
|
|
|
describe("inert JavaScript path resolution", () => {
|
|
it.each(absoluteParts)(
|
|
"matches the POSIX resolve oracle for %j",
|
|
(...parts) => {
|
|
assertPathProjection(
|
|
`path.posix.resolve(${parts.map((part) => JSON.stringify(part)).join(", ")})`,
|
|
posix.resolve(...parts),
|
|
"filesystem-expression",
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each(anchoredExpressions)(
|
|
"projects the source anchor in %s",
|
|
(expression, path) => {
|
|
assertPathProjection(expression, path, "filesystem-expression");
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
[
|
|
'path.join("/app", "/shared/preload.js")',
|
|
posix.join("/app", "/shared/preload.js"),
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.posix.join("/app", "preload/")',
|
|
posix.join("/app", "preload/"),
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.join("sub", "../preload.js")',
|
|
posix.join("sub", "../preload.js"),
|
|
"module-specifier",
|
|
],
|
|
[
|
|
'path.join(__dirname, "preload.js")',
|
|
"preload.js",
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.join(__dirname, "/preload.js")',
|
|
"preload.js",
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.join(dirname(fileURLToPath(import.meta.url)), "/lib", "/preload.js")',
|
|
"lib/preload.js",
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.join("ignored", __dirname, "preload.js")',
|
|
"ignored/preload.js",
|
|
"filesystem-expression",
|
|
],
|
|
[
|
|
'path.join(dirname(fileURLToPath(import.meta.url)), "preload.js")',
|
|
"preload.js",
|
|
"filesystem-expression",
|
|
],
|
|
['join("sub", "preload.js")', "sub/preload.js", "module-specifier"],
|
|
["path.join(__dirname)", ".", "filesystem-expression"],
|
|
] as const)("preserves join behavior in %s", (expression, path, context) => {
|
|
assertPathProjection(expression, path, context);
|
|
});
|
|
|
|
it.each(unknownExpressions)("keeps unsupported %s unknown", (expression) => {
|
|
expect(
|
|
staticPath(parseExpression(expression, { sourceType: "module" })),
|
|
).toBeUndefined();
|
|
const { analysis, windowExpression, utilityExpression } =
|
|
analyzePathConsumers(expression);
|
|
expect(analysis.parse_status).toBe("partial");
|
|
expect(analysis.parse_error_count).toBe(0);
|
|
expect(analysis.role_paths).toEqual([]);
|
|
expect(analysis.electron.browser_windows).toEqual([
|
|
expect.objectContaining({
|
|
preload_path: null,
|
|
preload_resolution_context: null,
|
|
web_preferences: [
|
|
{
|
|
name: "preload",
|
|
value: { status: "dynamic", value: null, expression },
|
|
},
|
|
],
|
|
location: lineRange(2, 0, windowExpression.length),
|
|
}),
|
|
]);
|
|
expect(analysis.electron.utility_processes).toEqual([
|
|
{
|
|
module_path: null,
|
|
module_resolution_context: null,
|
|
module_expression: expression,
|
|
service_name: null,
|
|
module_key: null,
|
|
location: lineRange(3, 0, utilityExpression.length),
|
|
},
|
|
]);
|
|
expect(analysis.limitations).toContain(
|
|
"One or more static keys, expressions, or Electron boundary values were dynamic and remain unknown.",
|
|
);
|
|
});
|
|
|
|
it("keeps recovered templates with invalid escapes unknown as paths", () => {
|
|
const expression = ["`", String.raw`\unicode`, "`"].join("");
|
|
const { analysis } = analyzePathConsumers(expression);
|
|
|
|
expect(analysis.parse_status).toBe("partial");
|
|
expect(analysis.parse_error_count).toBe(3);
|
|
expect(analysis.role_paths).toEqual([]);
|
|
expect(analysis.electron.browser_windows).toMatchObject([
|
|
{
|
|
preload_path: null,
|
|
preload_resolution_context: null,
|
|
web_preferences: [
|
|
{
|
|
name: "preload",
|
|
value: { status: "dynamic", value: null, expression },
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
expect(analysis.electron.utility_processes).toEqual([
|
|
expect.objectContaining({
|
|
module_path: null,
|
|
module_resolution_context: null,
|
|
module_expression: expression,
|
|
}),
|
|
]);
|
|
});
|
|
|
|
it.each([
|
|
"path.join()",
|
|
'path.join("/app", runtimeFile)',
|
|
"path.join(...parts)",
|
|
])("preserves unresolved join syntax in %s", (expression) =>
|
|
expect(
|
|
staticPath(parseExpression(expression, { sourceType: "module" })),
|
|
).toBeUndefined(),
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
"file:///app/preload%20file.js",
|
|
"file:///app/%E2%9C%93.js",
|
|
"file:///app/preload.js?query=1#fragment",
|
|
])("decodes literal file URL %s as a filesystem expression", (url) => {
|
|
assertPathProjection(
|
|
`fileURLToPath(${JSON.stringify(url)})`,
|
|
fileURLToPath(url, { windows: false }),
|
|
"filesystem-expression",
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
'fileURLToPath("file:///app/%2Fpreload.js")',
|
|
'fileURLToPath("file:///app/%5Cpreload.js")',
|
|
'fileURLToPath("file:///app/%")',
|
|
'fileURLToPath("file:///C:/app/preload.js")',
|
|
'fileURLToPath("file://remote/share/preload.js")',
|
|
'fileURLToPath("file:///app/preload.js", options)',
|
|
"fileURLToPath(selectedUrl)",
|
|
])("leaves platform-dependent or invalid %s unknown", (expression) =>
|
|
expect(
|
|
staticPath(parseExpression(expression, { sourceType: "module" })),
|
|
).toBeUndefined(),
|
|
);
|
|
|
|
const analyzePathConsumers = (expression: string) => {
|
|
const property = `preload: ${expression}`;
|
|
const genericPrefix = "const options = { ";
|
|
const windowPrefix = "new BrowserWindow({ webPreferences: { ";
|
|
const windowExpression = `${windowPrefix}${property} } })`;
|
|
const utilityExpression = `utilityProcess.fork(${expression})`;
|
|
return {
|
|
property,
|
|
genericPrefix,
|
|
windowPrefix,
|
|
windowExpression,
|
|
utilityExpression,
|
|
analysis: analyzeJavaScriptStaticSource(
|
|
`${genericPrefix}${property} }; const { BrowserWindow, utilityProcess } = require("electron");\n${windowExpression};\n${utilityExpression};`,
|
|
),
|
|
};
|
|
};
|
|
|
|
const assertPathProjection = (
|
|
expression: string,
|
|
path: string,
|
|
context: JavaScriptStaticPathContext,
|
|
) => {
|
|
const node = parseExpression(expression, { sourceType: "module" });
|
|
expect(staticPath(node)).toBe(path);
|
|
expect(staticPathResolutionContext(node)).toBe(context);
|
|
const {
|
|
analysis,
|
|
property,
|
|
genericPrefix,
|
|
windowPrefix,
|
|
windowExpression,
|
|
utilityExpression,
|
|
} = analyzePathConsumers(expression);
|
|
expect(analysis.parse_status).toBe("complete");
|
|
expect(analysis.parse_error_count).toBe(0);
|
|
expect(analysis.role_paths).toEqual(
|
|
(
|
|
[
|
|
[1, genericPrefix.length],
|
|
[2, windowPrefix.length],
|
|
] as const
|
|
).map(([line, column]) => ({
|
|
role: "preload",
|
|
path,
|
|
resolution_context: context,
|
|
mechanism: "property:preload",
|
|
module_key: null,
|
|
location: lineRange(line, column, column + property.length),
|
|
})),
|
|
);
|
|
expect(analysis.electron.browser_windows).toEqual([
|
|
{
|
|
options_status: "object-literal",
|
|
web_preferences_status: "object-literal",
|
|
web_preferences: [
|
|
{
|
|
name: "preload",
|
|
value: { status: "literal", value: path, expression: null },
|
|
},
|
|
],
|
|
preload_path: path,
|
|
preload_resolution_context: context,
|
|
module_key: null,
|
|
location: lineRange(2, 0, windowExpression.length),
|
|
},
|
|
]);
|
|
expect(analysis.electron.utility_processes).toEqual([
|
|
{
|
|
module_path: path,
|
|
module_resolution_context: context,
|
|
module_expression: null,
|
|
service_name: null,
|
|
module_key: null,
|
|
location: lineRange(3, 0, utilityExpression.length),
|
|
},
|
|
]);
|
|
};
|
|
|
|
const lineRange = (line: number, start: number, end: number) => ({
|
|
start: { line, column: start },
|
|
end: { line, column: end },
|
|
});
|