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:
N0zoM1z0
2026-10-05 16:55:42 +08:00
committed by GitHub
parent 05bb630a00
commit 84e4361c09
22 changed files with 654 additions and 106 deletions
+44 -3
View File
@@ -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",
},
};
};
+7 -1
View File
@@ -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 -2
View File
@@ -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;
}
+14 -3
View File
@@ -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 ||
@@ -16,7 +16,7 @@ export const JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS = [
name: "list_javascript_runtime_targets",
...toolContractMetadata("list_javascript_runtime_targets"),
description:
"List every attachable Node/Electron V8 Inspector target from an explicit literal-loopback endpoint. Every returned target and script location exposed by that Inspector endpoint is eligible for Evidence.",
"List every eligible Node/Electron V8 Inspector target from an explicit literal-loopback endpoint. Node discovery file locations that cannot be verified are preserved as unresolved reported URLs; they do not prove filesystem identity. Listing does not attach to targets.",
kind: "runtime-provider",
inputSchema: listJavaScriptRuntimeTargetsInputSchema,
outputSchema: evidenceResultOf(javascriptRuntimeTargetListSchema),
@@ -33,7 +33,7 @@ export const JAVASCRIPT_RUNTIME_OBSERVATION_TOOL_CONTRACTS = [
name: "observe_javascript_runtime",
...toolContractMetadata("observe_javascript_runtime"),
description:
"Attach passively to one Node/Electron V8 Inspector target by supplying its loopback Inspector endpoint and target ID. The provider rechecks that the target currently exists at that endpoint and is an attachable file or URL target. Captures Debugger.scriptParsed and Runtime execution-context events. REA never evaluates, pauses, resumes, reads source, or instruments the target; require/import edges, EventEmitter activity, and Electron IPC remain explicit unknowns. Reconcile the result with static Application Graph Evidence using reconcile_javascript_runtime. The selected endpoint exposes every target it serves to this tool.",
"Attach passively to one Node/Electron V8 Inspector target by supplying its loopback Inspector endpoint and target ID. The provider rechecks target presence and attachability; a Node discovery file location may remain unresolved. Loaded script locations are validated independently from Debugger.scriptParsed. Captures script and Runtime execution-context metadata. REA never evaluates, pauses, resumes, reads source, or instruments the target; require/import edges, EventEmitter activity, and Electron IPC remain explicit unknowns. Reconcile the result with static Application Graph Evidence using reconcile_javascript_runtime. The selected endpoint exposes every target it serves to this tool.",
kind: "runtime-provider",
inputSchema: observeJavaScriptRuntimeInputSchema,
outputSchema: evidenceResultOf(javascriptRuntimeObservationSchema),
+17 -1
View File
@@ -64,11 +64,27 @@ export type JavaScriptRuntimeLocation = z.infer<
typeof javascriptRuntimeLocationSchema
>;
/** Discovery metadata can identify an endpoint target without verifying its file location. */
export const javascriptRuntimeUnresolvedLocationSchema = z.strictObject({
kind: z.literal("unresolved"),
reported_url: z.string().min(1),
reason: z.literal("unverifiable-file-location"),
});
/** Verified locations and explicitly unresolved discovery metadata. */
export const javascriptRuntimeTargetLocationSchema = z.union([
javascriptRuntimeLocationSchema,
javascriptRuntimeUnresolvedLocationSchema,
]);
export type JavaScriptRuntimeTargetLocation = z.infer<
typeof javascriptRuntimeTargetLocationSchema
>;
const javascriptRuntimeTargetSchema = z.strictObject({
target_id: z.string().min(1),
protocol_type: z.string().min(1),
attached: z.boolean(),
location: javascriptRuntimeLocationSchema,
location: javascriptRuntimeTargetLocationSchema,
});
/** Complete V8 Inspector target inventory exposed by the selected endpoint. */
@@ -66,6 +66,14 @@ const reconcileEntity = (
index: CandidateIndex,
layers: readonly ParsedStaticLayer[],
): EvaluatedMatch => {
if (entity.location.kind === "unresolved")
return unresolved(entity, {
status: "unknown",
basis: "none",
confidence: "unknown",
reason: "runtime-location-unresolved",
candidates: [],
});
const locations = candidatesWithLocation(entity, index, layers);
if (entity.sourceSha256 !== null) {
const content =
@@ -20,6 +20,7 @@ import {
javascriptRuntimeObservationSchema,
javascriptRuntimeKindSchema,
type JavaScriptRuntimeObservation,
type JavaScriptRuntimeLocation,
} from "./javascriptRuntimeObservation.js";
import { parseEvidence, type Evidence } from "./evidence.js";
import {
@@ -67,6 +68,16 @@ interface NormalizedV8Inspection {
readonly title: string;
readonly attached: boolean;
readonly url: string;
}
| {
readonly target_id: string;
readonly type: string;
readonly title: string;
readonly attached: boolean;
readonly unresolved_location: Extract<
JavaScriptRuntimeObservation["target"]["location"],
{ kind: "unresolved" }
>;
};
readonly frames: readonly [];
readonly scripts: {
@@ -305,7 +316,9 @@ const normalizeV8Inspection = (
type: result.target.protocol_type,
title: result.target.runtime_kind,
attached: result.target.attached,
...runtimeLocation(result.target.location),
...(result.target.location.kind === "unresolved"
? { unresolved_location: result.target.location }
: runtimeLocation(result.target.location)),
},
frames: [],
scripts: {
@@ -359,7 +372,7 @@ const normalizeV8Inspection = (
});
const runtimeLocation = (
location: JavaScriptRuntimeObservation["target"]["location"],
location: JavaScriptRuntimeLocation,
): { readonly file_path: string } | { readonly url: string } => {
if (location.kind === "file") return { file_path: location.file_path };
if (location.kind === "url") return { url: location.sanitized_url };
@@ -87,9 +87,11 @@ const captureSummaries = (
target_node_id: targetNode.node_id,
target_key: capture.inspection.target.target_id,
target_location:
"url" in capture.inspection.target
? capture.inspection.target.url
: capture.inspection.target.file_path,
"unresolved_location" in capture.inspection.target
? capture.inspection.target.unresolved_location
: "url" in capture.inspection.target
? capture.inspection.target.url
: capture.inspection.target.file_path,
frames: capture.inspection.frames.length,
scripts: capture.inspection.scripts.items.length,
workers: capture.inspection.workers.length,
@@ -14,7 +14,10 @@ export interface RuntimeReconciliationEntity {
readonly node: ApplicationNode;
readonly capture: ParsedRuntimeCapture;
readonly runtimeKey: string;
readonly location: { readonly kind: "file" | "url"; readonly value: string };
readonly location: {
readonly kind: "file" | "url" | "unresolved";
readonly value: string;
};
readonly frameKey: string | null;
readonly sourceSha256: string | null;
readonly staticKind: "renderer" | "javascript" | "worker" | "service-worker";
@@ -73,9 +76,14 @@ const targetEntity = (
): RuntimeReconciliationEntity => {
const target = capture.inspection.target;
const location =
"url" in target
? ({ kind: "url", value: target.url } as const)
: ({ kind: "file", value: target.file_path } as const);
"unresolved_location" in target
? ({
kind: "unresolved",
value: target.unresolved_location.reported_url,
} as const)
: "url" in target
? ({ kind: "url", value: target.url } as const)
: ({ kind: "file", value: target.file_path } as const);
return runtimeEntity(capture, {
kind: "target",
nodeKind: "target",
@@ -89,7 +97,13 @@ const targetEntity = (
label: target.title,
properties: {
runtime_type: target.type,
location: location.value,
...(location.kind === "unresolved"
? {
reported_url: location.value,
location_kind: "unresolved",
location_reason: "unverifiable-file-location",
}
: { location: location.value }),
attached: target.attached,
},
section: "target",
@@ -3,6 +3,7 @@ import { isAbsolute, resolve } from "node:path";
import { z } from "zod";
import { evidenceSchema } from "./evidence.js";
import { javascriptRuntimeUnresolvedLocationSchema } from "./javascriptRuntimeObservation.js";
import {
compareCodePoints,
javascriptApplicationGraphSchema,
@@ -152,7 +153,10 @@ const captureSummarySchema = z.strictObject({
kind: z.enum(["browser", "electron", "v8-inspector", "electron-active"]),
target_node_id: nodeIdSchema,
target_key: boundedTextSchema,
target_location: z.string().min(1),
target_location: z
.string()
.min(1)
.or(javascriptRuntimeUnresolvedLocationSchema),
frames: z.number().int().min(0),
scripts: z.number().int().min(0),
workers: z.number().int().min(0),
@@ -167,6 +171,7 @@ const reconciliationReasonSchema = z.enum([
"captured-content-disagrees-with-static-location",
"no-static-content-match",
"no-authorized-location-mapping",
"runtime-location-unresolved",
"runtime-frame-unattributed",
"static-or-runtime-coverage-incomplete",
"reconciliation-limit-reached",
File diff suppressed because one or more lines are too long