From 559956cf6b5db2677aa4f8b4f34ee67b8038f4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mu=C3=B1oz=20Rey?= <108531295+Davmunrey@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:59:38 +0100 Subject: [PATCH] refactor(inspector): own the Node discovery path rule in one module (#10680) Node Inspector discovery reports a target as `file://` plus the pathname, with quotes and backslashes replaced by underscores. Whether such a pathname is identifiable was decided twice: inline in authorizeRuntimeTargetLocation and again as NODE_DISCOVERY_AMBIGUOUS in the createNodeDiscoveryTempDirectory fixture. The copies could drift, and the fixture could not import the product rule without loading JavaScriptRuntimeScope and its Electron file-scope chain. Move the rule to src/inspector/nodeDiscoveryPath.ts, a module with no imports, and use it from both places. Behavior is unchanged. A unit test covers the Windows branch, which Linux CI cannot reach through the boundary tests. Validation: nodeDiscoveryPath and cancellation tests 12/12; javascriptRuntimeTargetLocations and javascriptRuntimeObservation 42/42 with TMPDIR containing an underscore (exercises the /tmp fallback); check:fast passes. Refs morluto/rea#9996 --- src/inspector/JavaScriptRuntimeScope.ts | 24 ++++++++++-------------- src/inspector/nodeDiscoveryPath.test.ts | 18 ++++++++++++++++++ src/inspector/nodeDiscoveryPath.ts | 14 ++++++++++++++ tests/fixtures/temporaryDirectory.ts | 12 +++++------- 4 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 src/inspector/nodeDiscoveryPath.test.ts create mode 100644 src/inspector/nodeDiscoveryPath.ts diff --git a/src/inspector/JavaScriptRuntimeScope.ts b/src/inspector/JavaScriptRuntimeScope.ts index 10689f8613..ffe367f7b1 100644 --- a/src/inspector/JavaScriptRuntimeScope.ts +++ b/src/inspector/JavaScriptRuntimeScope.ts @@ -15,6 +15,7 @@ import type { JavaScriptRuntimeTargetLocation, } from "../domain/javascript/javascriptRuntimeObservation.js"; import { cancelledBy } from "../domain/analysisErrorCore.js"; +import { isLosslessNodeDiscoveryPath } from "./nodeDiscoveryPath.js"; import { authorizedElectronFile, type ElectronFileScope, @@ -150,20 +151,15 @@ export const authorizeRuntimeTargetLocation = async ( files, ); - // Node concatenates the pathname, then replaces quotes and backslashes with - // underscores. A remaining underscore cannot prove which pathname was used. - // The runtime runs on this host and reports one of this host's paths. - if (HOST_PLATFORM.platform !== "win32" && !/[_"\\]/u.test(reportedPath)) { - if (reportedPath.startsWith("/")) { - const decision = await authorizeRuntimeLocation( - pathToFileURL(reportedPath).href, - signal, - operation, - asarHeaderAdmission, - files, - ); - if (decision.allowed) return decision; - } + if (isLosslessNodeDiscoveryPath(reportedPath, HOST_PLATFORM.platform)) { + const decision = await authorizeRuntimeLocation( + pathToFileURL(reportedPath).href, + signal, + operation, + asarHeaderAdmission, + files, + ); + if (decision.allowed) return decision; } return { allowed: true, diff --git a/src/inspector/nodeDiscoveryPath.test.ts b/src/inspector/nodeDiscoveryPath.test.ts new file mode 100644 index 0000000000..3ab3be64d5 --- /dev/null +++ b/src/inspector/nodeDiscoveryPath.test.ts @@ -0,0 +1,18 @@ +import { expect, it } from "vitest"; + +import { isLosslessNodeDiscoveryPath } from "./nodeDiscoveryPath.js"; + +it.for([ + ["/tmp/rea/entry.js", "linux", true], + ["/tmp/space root/entry#hash.js", "darwin", true], + ["/tmp/bad_root/entry.js", "linux", false], + ['/tmp/quote"d/entry.js', "linux", false], + ["/tmp/back\\slash/entry.js", "linux", false], + ["relative/entry.js", "linux", false], + ["/tmp/rea/entry.js", "win32", false], +] as const)( + "reports whether Node discovery identifies %s on %s exactly", + ([path, platform, expected]) => { + expect(isLosslessNodeDiscoveryPath(path, platform)).toBe(expected); + }, +); diff --git a/src/inspector/nodeDiscoveryPath.ts b/src/inspector/nodeDiscoveryPath.ts new file mode 100644 index 0000000000..0532703352 --- /dev/null +++ b/src/inspector/nodeDiscoveryPath.ts @@ -0,0 +1,14 @@ +/** + * Whether Node Inspector discovery metadata identifies this pathname exactly. + * + * Node reports a target URL as `file://` plus the pathname, with quotes and + * backslashes replaced by underscores. A remaining underscore cannot prove + * which pathname was used, and Windows pathnames always lose their separators. + * This module has no imports so that fixtures can share the rule cheaply; + * callers pass the platform the Node process runs on. + */ +export const isLosslessNodeDiscoveryPath = ( + path: string, + platform: NodeJS.Platform, +): boolean => + platform !== "win32" && path.startsWith("/") && !/[_"\\]/u.test(path); diff --git a/tests/fixtures/temporaryDirectory.ts b/tests/fixtures/temporaryDirectory.ts index 4fc3bbba1b..e6ce1def9a 100644 --- a/tests/fixtures/temporaryDirectory.ts +++ b/tests/fixtures/temporaryDirectory.ts @@ -5,6 +5,7 @@ import { join } from "node:path"; import { onTestFinished } from "vitest"; import { normalizeTestTemporaryPrefix } from "../../scripts/lib/test-temporary-ownership.mjs"; +import { isLosslessNodeDiscoveryPath } from "../../src/inspector/nodeDiscoveryPath.js"; import { removeTestWorkspace } from "../support/workspace/workspaceFixture.js"; /** @@ -32,9 +33,6 @@ export const createTestTempDirectory = async ( return canonicalDirectory; }; -/** Characters Node's Inspector discovery metadata cannot carry losslessly. */ -const NODE_DISCOVERY_AMBIGUOUS = /[_"\\]/u; - /** * Like `createTestTempDirectory`, for fixtures whose paths Node reports through * Inspector discovery metadata. @@ -53,16 +51,16 @@ export const createNodeDiscoveryTempDirectory = async ( ): Promise => { if (process.platform === "win32") return createTestTempDirectory(prefix); const primary = await realpath(tmpdir()); - const base = NODE_DISCOVERY_AMBIGUOUS.test(primary) - ? await realpath("/tmp") - : primary; + const base = isLosslessNodeDiscoveryPath(primary, process.platform) + ? primary + : await realpath("/tmp"); const canonicalDirectory = await realpath( await mkdtemp(join(base, normalizeTestTemporaryPrefix(prefix))), ); onTestFinished(async () => { await removeTestWorkspace(canonicalDirectory); }); - if (NODE_DISCOVERY_AMBIGUOUS.test(canonicalDirectory)) + if (!isLosslessNodeDiscoveryPath(canonicalDirectory, process.platform)) throw new Error( `No temporary directory free of '_', '"' and '\\' for Node discovery fixtures: ${canonicalDirectory}`, );