feat(browser): trace captured website source-map locations (#897)

* refactor(browser): share captured-script acquisition boundaries

Reuse stable capture/source acquisition and identity validation for module
and source-map analysis. Preserve host I/O, malformed input, unavailable
source and digest mismatch diagnostics.

Refs #851

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* feat(browser): trace captured website source-map locations

Refs #851. Preserve selected capture/map byte identity, duplicate mappings and embedded source evidence through the pinned upstream codec.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* test(browser): verify source-map artifacts and synchronize generated contracts

Cover byte-preserving map reads and actionable filesystem constraints. Refresh generated public schemas and metadata on current main.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve inert source-map URL context

The selected URL is local codec context and grants no transport authority. Preserve its reported text and verify normal/userinfo contexts through real public interfaces, alongside independent manifest byte identity.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
This commit is contained in:
ysopwnable
2026-10-07 16:21:18 +08:00
committed by GitHub
parent 4801a24089
commit f430c416c7
58 changed files with 3420 additions and 282 deletions
+1
View File
@@ -14,6 +14,7 @@ export class ArtifactOperationError extends AnalysisError {
| "inspect_keyed_archive"
| "export_web_scripts"
| "trace_web_module_imports"
| "trace_web_source_location"
| "analyze_javascript_application",
readonly reason:
| "cancelled"
+19
View File
@@ -0,0 +1,19 @@
import { expect, it } from "vitest";
import { ProviderCleanupError } from "./providerCleanupError.js";
import { projectAnalysisError } from "./analysisErrorProjection.js";
it("preserves the binary cleanup default and an explicitly owned nonbinary operation", () => {
expect(new ProviderCleanupError("ghidra", ["database"], {}).operation).toBe(
"close_binary",
);
const error = new ProviderCleanupError(
"source-map-decoder",
["runtime"],
{ reason: "cleanup failed" },
{ operation: "trace_web_source_location" },
);
expect(error.operation).toBe("trace_web_source_location");
expect(projectAnalysisError(error)).toMatchObject({
code: "cleanup_incomplete",
details: { operation: "trace_web_source_location" },
});
});
+4 -2
View File
@@ -12,9 +12,11 @@ export class ProviderCleanupError extends ProviderAdapterError {
providerId: string,
resources: readonly string[],
diagnostics: Readonly<Record<string, JsonValue>>,
options?: ErrorOptions,
/** Identify the operation owning these resources; binary callers retain their default. */
options?: ErrorOptions & { readonly operation?: string },
) {
super(providerId, "close_binary", { ...options, diagnostics });
const { operation = "close_binary", ...errorOptions } = options ?? {};
super(providerId, operation, { ...errorOptions, diagnostics });
this.cleanupResources = [...resources];
}
}
+49
View File
@@ -0,0 +1,49 @@
import { z } from "zod";
import { digestSchema } from "./digests.js";
import { webScriptExportManifestSchema } from "./webScriptExport.js";
/** Select one source from an existing captured-script export. */
export const selectedWebScriptInputSchema = z.strictObject({
manifest_path: z
.string()
.min(1)
.describe("Absolute export_web_scripts manifest path"),
script_index: z
.number()
.int()
.min(0)
.describe("Zero-based index in manifest.scripts"),
});
export type SelectedWebScriptInput = z.output<
typeof selectedWebScriptInputSchema
>;
/** Exact identity of one local file, independently read as bytes. */
export const webArtifactFileSchema = z.strictObject({
path: z.string().min(1),
sha256: digestSchema,
bytes: z.number().int().min(0),
});
/** Artifact-port report shared by capture-derived analyses. */
export const selectedWebScriptArtifactsSchema = z.strictObject({
manifest: webScriptExportManifestSchema,
manifestFile: webArtifactFileSchema,
sourceFile: webArtifactFileSchema,
source: z.string(),
});
export type SelectedWebScriptArtifacts = z.output<
typeof selectedWebScriptArtifactsSchema
>;
/** Preserve original capture authority and completeness alongside the actual manifest identity. */
export const capturedWebManifestIdentitySchema = webArtifactFileSchema.extend({
reported_output_directory: z.string(),
...webScriptExportManifestSchema.pick({
capture_path: true,
capture_sha256: true,
capture_kind: true,
capture_completeness: true,
source_evidence_id: true,
}).shape,
});
+127
View File
@@ -0,0 +1,127 @@
import { z } from "zod";
import { digestSchema } from "./digests.js";
import { exportedWebScriptSchema } from "./webScriptExport.js";
import {
capturedWebManifestIdentitySchema,
selectedWebScriptInputSchema,
webArtifactFileSchema,
} from "./webScriptArtifacts.js";
/** JavaScript source-map positions use UTF-16 columns and one-based lines. */
export const webSourcePositionSchema = z.strictObject({
line: z.number().int().min(1),
column: z.number().int().min(0),
});
/** Trace one explicit generated position through an explicitly paired local map. */
export const webSourceLocationInputSchema = selectedWebScriptInputSchema.extend(
{
source_map: z.strictObject({
path: z
.string()
.min(1)
.describe("Absolute local source-map path; no fetch"),
url: z
.string()
.min(1)
.describe(
"Explicit source-map URL context used to resolve original source names",
),
}),
generated_position: webSourcePositionSchema,
},
);
export type WebSourceLocationInput = z.output<
typeof webSourceLocationInputSchema
>;
const originalContentSchema = z.discriminatedUnion("state", [
z.strictObject({
state: z.literal("embedded"),
text: z.string(),
utf8_sha256: digestSchema,
utf8_bytes: z.number().int().min(0),
representation: z.literal("UTF-8 encoding of decoded sourcesContent text"),
}),
z.strictObject({
state: z.literal("unavailable"),
reason: z.literal("sourcesContent-not-retained"),
}),
]);
/** Keep every matching segment, including anonymous mappings and duplicate URLs. */
export const webSourceMapMatchSchema = z.discriminatedUnion("state", [
z.strictObject({ state: z.literal("generated-only") }),
z.strictObject({
state: z.literal("mapped"),
flattened_source_index: z.number().int().min(0),
section_path: z.array(z.number().int().min(0)),
source_index: z.number().int().min(0),
reported_source: z.string().nullable(),
reported_source_root: z.string().nullable(),
resolved_url: z.string().nullable(),
ignored: z.boolean(),
content: originalContentSchema,
original_position: webSourcePositionSchema.extend({
offset: z.number().int().min(0).nullable(),
content_position: z.enum([
"verified-in-embedded-text",
"outside-embedded-text",
"content-unavailable",
]),
}),
name: z.string().nullable(),
}),
]);
/** Codec observations; map association and deployment authenticity remain selected context. */
export const webSourceMapReportSchema = z.strictObject({
source_map_sha256: digestSchema,
url_context: z.string().min(1),
engine: z.strictObject({ id: z.string().min(1), version: z.string().min(1) }),
runtime: z.strictObject({
id: z.string().min(1),
version: z.string().min(1),
v8_heap_limit_bytes: z.number().int().min(1),
}),
format: z.enum(["regular", "indexed"]),
reported_file: z.string().nullable(),
lookup: z.strictObject({
requested: webSourcePositionSchema,
matched_generated_position: webSourcePositionSchema.nullable(),
semantics: z.literal(
"greatest generated position <= requested, across lines; all equal-position segments",
),
location_units: z.literal("one-based lines; zero-based UTF-16 columns"),
}),
matches: z.array(webSourceMapMatchSchema),
diagnostics: z.array(z.string()),
});
/** Bounds for complete source-map evidence and upstream decoded layouts. */
export const WEB_SOURCE_MAP_LIMITS = {
mapBytes: 4 * 1024 * 1024,
outputBytes: 32 * 1024 * 1024,
decodedRows: 262144,
decodedSegments: 262144,
sectionDepth: 64,
decodeTimeoutMs: 20000,
} as const;
export type WebSourceMapReport = z.output<typeof webSourceMapReportSchema>;
/** Selected capture/map byte identities and complete point attribution inline. */
export const webSourceLocationResultSchema = webSourceMapReportSchema.extend({
manifest: capturedWebManifestIdentitySchema,
source: webArtifactFileSchema.extend({
script_index: z.number().int().min(0),
script: exportedWebScriptSchema,
}),
source_map: webArtifactFileSchema.extend({
url: z.string().min(1),
association: z.literal("caller-selected"),
}),
generated_offset: z.number().int().min(0),
source_authenticity: z.literal("unknown"),
execution: z.literal("unknown"),
limitations: z.array(z.string()),
});
+25
View File
@@ -0,0 +1,25 @@
import { expect, it } from "vitest";
import { webSourceOffset } from "./webSourcePosition.js";
it("uses UTF-16 columns and actual JavaScript line terminators", () => {
const text = "🎋a\r\nb\rc\nd\u2028e\u2029";
expect(webSourceOffset(text, { line: 1, column: 2 })).toBe(2);
expect(webSourceOffset(text, { line: 1, column: 3 })).toBe(3);
expect(webSourceOffset(text, { line: 1, column: 4 })).toBeUndefined();
expect(webSourceOffset(text, { line: 2, column: 0 })).toBe(5);
expect(webSourceOffset(text, { line: 6, column: 0 })).toBe(13);
expect(webSourceOffset(text, { line: 7, column: 0 })).toBeUndefined();
});
it.each([
{ line: 0, column: 0 },
{ line: 1, column: -1 },
{ line: 1.5, column: 0 },
])("rejects invalid source positions: %j", (position) => {
expect(webSourceOffset("", position)).toBeUndefined();
});
it("retains the empty source and the final empty line", () => {
expect(webSourceOffset("", { line: 1, column: 0 })).toBe(0);
expect(webSourceOffset("a\r\n", { line: 2, column: 0 })).toBe(3);
});
+32
View File
@@ -0,0 +1,32 @@
/** JavaScript positions use one-based lines and zero-based UTF-16 columns. */
export interface WebSourcePosition {
readonly line: number;
readonly column: number;
}
/** Verify a selected position against actual text without splitting the whole source. */
export const webSourceOffset = (
text: string,
position: WebSourcePosition,
): number | undefined => {
if (
!Number.isSafeInteger(position.line) ||
position.line < 1 ||
!Number.isSafeInteger(position.column) ||
position.column < 0
)
return undefined;
let line = 1;
let start = 0;
for (const boundary of text.matchAll(/\r\n?|\n|\u2028|\u2029/gu)) {
if (line === position.line)
return position.column <= boundary.index - start
? start + position.column
: undefined;
line += 1;
start = boundary.index + boundary[0].length;
}
return line === position.line && position.column <= text.length - start
? start + position.column
: undefined;
};