fix(inspector): preserve lossy Node discovery locations (#542)
Keep explicitly selected Node Inspector targets observable when discovery cannot verify their file location. Resolve literal POSIX discovery paths without URI decoding, and retain lossy Windows or underscore paths as unresolved metadata. Validate scriptParsed file URLs independently. Preserve unknown target identity through runtime reconciliation, regenerate contracts, and verify real CLI/MCP behavior on Linux and Windows. Closes #530 Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
import {
|
||||
browserOriginSchema,
|
||||
sanitizeBrowserUrl,
|
||||
} from "../domain/browserObservation.js";
|
||||
import type { JavaScriptRuntimeLocation } from "../domain/javascriptRuntimeObservation.js";
|
||||
import type {
|
||||
JavaScriptRuntimeLocation,
|
||||
JavaScriptRuntimeTargetLocation,
|
||||
} from "../domain/javascriptRuntimeObservation.js";
|
||||
import { authorizedElectronFile } from "./ElectronFileScope.js";
|
||||
|
||||
export type RuntimeLocationDecision =
|
||||
@@ -48,7 +53,43 @@ export const authorizeRuntimeLocation = async (
|
||||
};
|
||||
};
|
||||
|
||||
/** Authorize a target whose Node Inspector URL omits its main entry path. */
|
||||
/** Interpret Node's lossy discovery metadata separately from scriptParsed URLs. */
|
||||
export const authorizeRuntimeTargetLocation = async (
|
||||
value: string,
|
||||
): Promise<RuntimeLocationDecision> => authorizeRuntimeLocation(value);
|
||||
context: { readonly type: string; readonly product: string },
|
||||
): Promise<
|
||||
| {
|
||||
readonly allowed: true;
|
||||
readonly location: JavaScriptRuntimeTargetLocation;
|
||||
}
|
||||
| { readonly allowed: false; readonly reason: "unsupported_location" }
|
||||
> => {
|
||||
if (
|
||||
context.type !== "node" ||
|
||||
!/^node\.js\/v/iu.test(context.product) ||
|
||||
!value.startsWith("file://")
|
||||
)
|
||||
return authorizeRuntimeLocation(value);
|
||||
const reportedPath = value.slice("file://".length);
|
||||
if (!reportedPath.startsWith("/") && !/^[a-z]:[/\\_]/iu.test(reportedPath))
|
||||
return authorizeRuntimeLocation(value);
|
||||
|
||||
// Node concatenates the pathname, then replaces quotes and backslashes with
|
||||
// underscores. A remaining underscore cannot prove which pathname was used.
|
||||
if (process.platform !== "win32" && !/[_"\\]/u.test(reportedPath)) {
|
||||
if (reportedPath.startsWith("/")) {
|
||||
const decision = await authorizeRuntimeLocation(
|
||||
pathToFileURL(reportedPath).href,
|
||||
);
|
||||
if (decision.allowed) return decision;
|
||||
}
|
||||
}
|
||||
return {
|
||||
allowed: true,
|
||||
location: {
|
||||
kind: "unresolved",
|
||||
reported_url: value,
|
||||
reason: "unverifiable-file-location",
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -24,7 +24,8 @@ export const createInspectorExclusionCounts = (): Record<
|
||||
export const describeInspectorTargetLimitations = (): string[] => [
|
||||
"REA attaches to an already-running exact target and never launches, resumes, evaluates, pauses, or mutates it.",
|
||||
"Only Runtime.enable and Debugger.enable are sent; source text, object values, EventEmitter activity, and Electron IPC are not inspected.",
|
||||
"Target IDs and locations are authorized, but the Inspector protocol does not authenticate an operating-system process ID or Electron role.",
|
||||
"The selected endpoint and target ID authorize observation; discovery file locations can remain unresolved and do not prove filesystem identity.",
|
||||
"Node discovery reports a best-effort pathname rather than an encoded file URL; lossy Windows paths and ambiguous underscores are preserved without guessing a file path.",
|
||||
"Every target exposed by the selected Inspector endpoint is eligible for observation; target IDs and locations are not authenticated as operating-system process identity.",
|
||||
"No runtime graph depth is traversed because passive Inspector events do not establish require/import caller edges.",
|
||||
];
|
||||
@@ -120,6 +121,11 @@ export const finalizeInspectorCapture = async ({
|
||||
"script unload events",
|
||||
],
|
||||
unknowns: [
|
||||
...(target.location.kind === "unresolved"
|
||||
? [
|
||||
"The discovery-reported file location cannot be verified; loaded script locations are resolved independently from Debugger.scriptParsed.",
|
||||
]
|
||||
: []),
|
||||
"Scripts collected before attachment may have been garbage-collected and therefore omitted.",
|
||||
"A bounded observation window cannot establish that an unobserved script or behavior never occurs.",
|
||||
"The declared Node/Electron process role is not authenticated by the Inspector protocol.",
|
||||
|
||||
@@ -2,7 +2,7 @@ import { z } from "zod";
|
||||
|
||||
import type {
|
||||
JavaScriptRuntimeTargetList,
|
||||
JavaScriptRuntimeLocation,
|
||||
JavaScriptRuntimeTargetLocation,
|
||||
} from "../domain/javascriptRuntimeObservation.js";
|
||||
import {
|
||||
BrowserObservationError,
|
||||
@@ -111,5 +111,5 @@ const validatedInspectorWebSocket = (
|
||||
|
||||
/** Authorized target plus its durable location. */
|
||||
export interface AuthorizedV8InspectorTarget extends V8InspectorTarget {
|
||||
readonly location: JavaScriptRuntimeLocation;
|
||||
readonly location: JavaScriptRuntimeTargetLocation;
|
||||
}
|
||||
|
||||
@@ -90,7 +90,10 @@ export class V8InspectorProvider implements JavaScriptRuntimeObservationPort {
|
||||
const allowed: AuthorizedV8InspectorTarget[] = [];
|
||||
const excluded = createInspectorExclusionCounts();
|
||||
for (const target of discovery.targets) {
|
||||
const decision = await authorizeRuntimeTargetLocation(target.url);
|
||||
const decision = await authorizeRuntimeTargetLocation(target.url, {
|
||||
type: target.type,
|
||||
product: discovery.runtime.product,
|
||||
});
|
||||
if (!decision.allowed) {
|
||||
excluded[decision.reason] += 1;
|
||||
continue;
|
||||
@@ -128,7 +131,11 @@ export class V8InspectorProvider implements JavaScriptRuntimeObservationPort {
|
||||
"observe_javascript_runtime",
|
||||
options.signal,
|
||||
);
|
||||
const target = await authorizedTarget(discovery.targets, input);
|
||||
const target = await authorizedTarget(
|
||||
discovery.targets,
|
||||
input,
|
||||
discovery.runtime.product,
|
||||
);
|
||||
if (input.runtime_kind !== undefined)
|
||||
assertRuntimeKind(target, input.runtime_kind);
|
||||
connection = await CdpConnection.connect(
|
||||
@@ -172,6 +179,7 @@ const projectTarget = (target: AuthorizedV8InspectorTarget) => ({
|
||||
const authorizedTarget = async (
|
||||
targets: readonly V8InspectorTarget[],
|
||||
input: ObserveJavaScriptRuntimeInput,
|
||||
product: string,
|
||||
): Promise<AuthorizedV8InspectorTarget> => {
|
||||
const target = targets.find(({ id }) => id === input.target_id);
|
||||
if (target === undefined)
|
||||
@@ -179,7 +187,10 @@ const authorizedTarget = async (
|
||||
"observe_javascript_runtime",
|
||||
"target_not_found",
|
||||
);
|
||||
const decision = await authorizeRuntimeTargetLocation(target.url);
|
||||
const decision = await authorizeRuntimeTargetLocation(target.url, {
|
||||
type: target.type,
|
||||
product,
|
||||
});
|
||||
if (
|
||||
target.attached ||
|
||||
!decision.allowed ||
|
||||
|
||||
Reference in New Issue
Block a user