From 7b500d78339901a16db2626ae3822cb15de89dac Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:12:44 +0800 Subject: [PATCH] fix(browser): report non-literal request URLs as unknown endpoints instead of dropping them (#11630) * fix(browser): report non-literal request URLs as unknown endpoints instead of dropping them * fix(browser): read XHR URLs from the proven request kind and bound view call sites --- src/domain/analysisView/analysisViewResult.ts | 5 +- src/domain/analysisView/webBundleView.test.ts | 29 +++++++++ src/domain/analysisView/webBundleView.ts | 31 +++++++++- src/domain/webBundleAnalysis.ts | 9 +++ src/domain/webBundleAnalyzer.test.ts | 59 +++++++++++++++++++ src/domain/webBundleAnalyzer.ts | 1 + src/domain/webBundleAnalyzerInspection.ts | 48 ++++++++++++++- src/domain/webBundleAnalyzerResult.ts | 24 ++++++++ src/domain/webBundleAnalyzerState.ts | 8 ++- src/domain/webBundleScriptAnalysis.ts | 14 ++++- .../browser/webBundleResultCapacity.test.ts | 1 + 11 files changed, 221 insertions(+), 8 deletions(-) diff --git a/src/domain/analysisView/analysisViewResult.ts b/src/domain/analysisView/analysisViewResult.ts index 2cd07ebfa7..4f14e5aba8 100644 --- a/src/domain/analysisView/analysisViewResult.ts +++ b/src/domain/analysisView/analysisViewResult.ts @@ -90,6 +90,7 @@ import { javaScriptRoutesViewSummarySchema, } from "./javascriptRoutesView.js"; import { + webBundleViewSourceUnknownsSchema, webBundleViewRequestSchema, webBundleViewItemsSchema, webBundleSummarySchema, @@ -849,7 +850,7 @@ export const webBundleSummaryViewResultSchema = z.strictObject({ parent_operation: z.literal("analyze_web_bundle"), target_url: z.string(), target_origin: recordedBrowserOriginSchema.nullable(), - source_unknowns: webBundleAnalysisSchema.shape.unknowns, + source_unknowns: webBundleViewSourceUnknownsSchema, summary: webBundleSummarySchema, }); @@ -860,7 +861,7 @@ export const webBundlePageViewResultSchema = z.strictObject({ parent_operation: z.literal("analyze_web_bundle"), target_url: z.string(), target_origin: recordedBrowserOriginSchema.nullable(), - source_unknowns: webBundleAnalysisSchema.shape.unknowns, + source_unknowns: webBundleViewSourceUnknownsSchema, view: webBundleViewRequestSchema, completeness: webBundleAnalysisSchema.shape.completeness, items: webBundleViewItemsSchema, diff --git a/src/domain/analysisView/webBundleView.test.ts b/src/domain/analysisView/webBundleView.test.ts index 401b70adf7..f7a07e403d 100644 --- a/src/domain/analysisView/webBundleView.test.ts +++ b/src/domain/analysisView/webBundleView.test.ts @@ -87,6 +87,35 @@ it("counts the distinct full parent/report limitation union before bounding", () } }); +it("bounds repeated call sites of a source unknown in every view while the parent keeps them all", () => { + const callSites = Array.from({ length: 12_000 }, (_, index) => ({ + mechanism: "call:fetch", + location: { script_key: "app.js", line: index + 1, column: 0 }, + })); + const unknown = { + dimension: "endpoints", + reason: "A browser request's URL argument is not a string literal.", + affected_script_keys: ["app.js"], + call_sites: callSites, + }; + const parent = makeParent({ unknowns: [unknown] }); + for (const selection of [ + { kind: "summary" }, + { kind: "web-bundle", collection: "endpoints", fields: ["value"] }, + ]) { + const view = query(parent, selection); + const [projected] = ( + view as { + source_unknowns: (typeof unknown & { + call_sites_omitted?: number; + })[]; + } + ).source_unknowns; + expect(projected?.call_sites).toEqual(callSites.slice(0, 20)); + expect(projected?.call_sites_omitted).toBe(callSites.length - 20); + } +}); + it("retains distinct structured unknown scopes on filtered pages", () => { const unknowns = [ { diff --git a/src/domain/analysisView/webBundleView.ts b/src/domain/analysisView/webBundleView.ts index ef07dbe45d..31433beed3 100644 --- a/src/domain/analysisView/webBundleView.ts +++ b/src/domain/analysisView/webBundleView.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { boundedList } from "../boundedList.js"; import type { AnalysisError } from "../analysisErrorBase.js"; import { AnalysisResourceConstraintError } from "../analysisErrorCore.js"; import { jsonValueByteLength } from "../jsonValueByteLength.js"; @@ -117,6 +118,34 @@ const choose = ( return out; }; +/** + * Call sites each source unknown repeats on every summary and page. The + * shared fields are copied into every view before the page budget applies, + * so per-call detail stays a small prefix; the parent keeps every call site. + */ +const VIEW_CALL_SITES = 20; + +/** + * A web bundle's unknowns as views repeat them: per-call detail is a prefix, + * with the count of call sites only the parent Evidence lists. + */ +export const webBundleViewSourceUnknownsSchema = z.array( + webBundleAnalysisSchema.shape.unknowns.element.extend({ + call_sites_omitted: z.int().min(1).exactOptional(), + }), +); + +const viewSourceUnknowns = ( + unknowns: WebBundleAnalysis["unknowns"], +): z.output => + unknowns.map((unknown) => { + if (unknown.call_sites === undefined) return unknown; + const { items, omitted } = boundedList(unknown.call_sites, VIEW_CALL_SITES); + return omitted === 0 + ? unknown + : { ...unknown, call_sites: [...items], call_sites_omitted: omitted }; + }); + /** Historical projection of completed analyze_web_bundle Evidence. */ export const projectWebBundleView = ( parent: AnalysisViewParent, @@ -162,7 +191,7 @@ export const projectWebBundleView = ( artifact: parent.artifact ?? null, target_url: report.capture.target_url, target_origin: report.capture.target_origin, - source_unknowns: report.unknowns, + source_unknowns: viewSourceUnknowns(report.unknowns), limitations: boundedViewMessages( sourceLimitations, parent.evidenceId, diff --git a/src/domain/webBundleAnalysis.ts b/src/domain/webBundleAnalysis.ts index bfac18cbf5..af57133c8b 100644 --- a/src/domain/webBundleAnalysis.ts +++ b/src/domain/webBundleAnalysis.ts @@ -290,6 +290,13 @@ const sourceLocationSchema = z.object({ column: z.int().min(0).nullable(), }); +/** One source call whose endpoint is unknown, and the API it calls. */ +export const webBundleCallSiteSchema = z.object({ + mechanism: z.string(), + location: sourceLocationSchema, +}); +export type WebBundleCallSite = z.infer; + const basisSchema = z.object({ script_key: z.string(), artifact_sha256: z.string().regex(/^[a-f0-9]{64}$/u), @@ -557,6 +564,8 @@ export const webBundleAnalysisSchema = z.object({ dimension: z.string(), reason: z.string(), affected_script_keys: z.array(z.string()), + /** The source calls this unknown is about, when it is about calls. */ + call_sites: z.array(webBundleCallSiteSchema).exactOptional(), /** The setting or request parameter that bounded these scripts, and how to change it. */ limit: analysisResultLimitSchema.exactOptional(), remediation: z.string().exactOptional(), diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts index a7f717c489..e09aa69015 100644 --- a/src/domain/webBundleAnalyzer.test.ts +++ b/src/domain/webBundleAnalyzer.test.ts @@ -53,6 +53,65 @@ it("retains standalone GraphQL Source operations in captured scripts", () => { ]); }); +it("reports a browser request whose URL is not a literal as an unknown endpoint at its call site", () => { + const scriptKey = `scr_${"1".repeat(64)}`; + const result = analyzeWebBundleFixture( + inspection( + [ + "fetch(flag ? '/a' : '/b');", + "new WebSocket(base + '/socket');", + "fetch('/literal');", + "const lookup = new Map(); lookup.get(key);", + ].join("\n"), + ), + ); + expect(result.observations.endpoints.map(({ value }) => value)).toEqual([ + "/literal", + ]); + expect(result.unknowns).toContainEqual({ + dimension: "endpoints", + reason: + "A browser request's URL argument is not a string literal, so the endpoint it reaches is unknown.", + affected_script_keys: [scriptKey], + call_sites: [ + { + mechanism: "call:fetch", + location: { script_key: scriptKey, line: 1, column: 0 }, + }, + { + mechanism: "call:WebSocket", + location: { script_key: scriptKey, line: 2, column: 0 }, + }, + ], + }); +}); + +it("reads an XMLHttpRequest URL from open's second argument on an inline receiver", () => { + const scriptKey = `scr_${"1".repeat(64)}`; + const result = analyzeWebBundleFixture( + inspection( + [ + "new XMLHttpRequest().open('GET', url);", + "new XMLHttpRequest().open('POST', '/inline-xhr');", + ].join("\n"), + ), + ); + expect(result.observations.endpoints).toContainEqual( + expect.objectContaining({ value: "/inline-xhr" }), + ); + expect(result.unknowns).toContainEqual( + expect.objectContaining({ + dimension: "endpoints", + call_sites: [ + { + mechanism: "call:open", + location: { script_key: scriptKey, line: 1, column: 0 }, + }, + ], + }), + ); +}); + it.each([ { kind: "recovered", diff --git a/src/domain/webBundleAnalyzer.ts b/src/domain/webBundleAnalyzer.ts index bd7f7ce74e..77d05f3a20 100644 --- a/src/domain/webBundleAnalyzer.ts +++ b/src/domain/webBundleAnalyzer.ts @@ -65,6 +65,7 @@ export const analyzeCapturedWebScript = ( routes: accumulator.routes, endpoints: accumulator.endpoints, webmcp_declarations: accumulator.webMcp, + unresolved_endpoints: accumulator.unresolvedEndpoints, }, inferences: accumulator.inferences, }); diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts index 4b5e041fda..66ebcbaf2d 100644 --- a/src/domain/webBundleAnalyzerInspection.ts +++ b/src/domain/webBundleAnalyzerInspection.ts @@ -242,12 +242,14 @@ const inspectCall = ( openReceiverFacts.get(node.start ?? -1), ); const mapLookup = isMapLookup(node); - const knownRequest = - isBrowserNetworkRequest(node, bindings) || isXhrRequest(node); + const xhrRequest = isXhrRequest(node); + const knownRequest = isBrowserNetworkRequest(node, bindings) || xhrRequest; const networkEndpoint = endpoint !== undefined && !mapLookup ? { ...endpoint, kind: knownRequest ? "endpoint" : endpoint.kind } : undefined; + if (endpoint === undefined && knownRequest && !mapLookup) + addProvenRequestEndpoint(script, node, name, xhrRequest, accumulator); if (networkEndpoint?.kind === "candidate") addEndpointCandidate( script, @@ -351,6 +353,48 @@ const addEndpointCandidate = ( ); }; +/** + * A proven browser request whose URL the callee spelling did not classify: + * the URL is XMLHttpRequest `open`'s second argument, otherwise the first. + * A literal URL is an endpoint; any other present argument is kept as an + * unknown endpoint at its call site rather than dropped. + */ +const addProvenRequestEndpoint = ( + script: IncludedScript, + node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, + name: string, + xhrRequest: boolean, + accumulator: AnalysisAccumulator, +): void => { + const url = xhrRequest ? node.arguments[1] : node.arguments[0]; + if (url === undefined) return; + const mechanism = `call:${name}`; + const literal = stringValue(url); + if (literal !== undefined) { + addFinding({ + collection: accumulator.endpoints, + script, + rawValue: literal, + mechanism, + node, + accumulator, + }); + return; + } + const site = location(script.script_key, node); + addUnique( + accumulator, + compositeKey([ + "unresolved-endpoint", + mechanism, + site.script_key, + String(site.line), + String(site.column), + ]), + () => accumulator.unresolvedEndpoints.push({ mechanism, location: site }), + ); +}; + const addFinding = (context: AddFindingContext): void => { const value = sanitizeEndpointCandidate(context.rawValue); const key = `finding\0${context.mechanism}\0${context.script.script_key}\0${value}`; diff --git a/src/domain/webBundleAnalyzerResult.ts b/src/domain/webBundleAnalyzerResult.ts index f5361bf297..304f0e27b3 100644 --- a/src/domain/webBundleAnalyzerResult.ts +++ b/src/domain/webBundleAnalyzerResult.ts @@ -6,6 +6,7 @@ import { browserOriginSchema } from "./browserObservation.js"; import { webBundleAnalysisSchema, type WebBundleAnalysis, + type WebBundleCallSite, } from "./webBundleAnalysis.js"; import { createWebBundleAnalysisAccumulator, @@ -177,6 +178,7 @@ export const buildCapturedWebBundleAnalysis = ( failedScripts: accumulator.failedScripts, recoveredScripts: accumulator.recoveredScripts, widenedScripts: accumulator.widenedScripts, + unresolvedEndpoints: accumulator.unresolvedEndpoints, unavailableAnalyses, sourceMaps, omittedScripts, @@ -220,6 +222,10 @@ const mergeScriptResults = ( appendAll(accumulator.edges, result.observations.edges); appendAll(accumulator.routes, result.observations.routes); appendAll(accumulator.endpoints, result.observations.endpoints); + appendAll( + accumulator.unresolvedEndpoints, + result.observations.unresolved_endpoints, + ); appendAll(accumulator.webMcp, result.observations.webmcp_declarations); appendAll(accumulator.inferences, result.inferences); } @@ -266,6 +272,7 @@ interface UnknownsInput { readonly failedScripts: readonly string[]; readonly recoveredScripts: readonly string[]; readonly widenedScripts: readonly string[]; + readonly unresolvedEndpoints: readonly WebBundleCallSite[]; readonly unavailableAnalyses: readonly Extract< WebBundleScriptAnalysis, { readonly status: "unavailable" } @@ -340,6 +347,23 @@ const buildUnknowns = (input: UnknownsInput): WebBundleAnalysis["unknowns"] => [ : { refused_phase: refusedPhase }), }), )), + ...(input.unresolvedEndpoints.length === 0 + ? [] + : [ + { + dimension: "endpoints" as const, + reason: + "A browser request's URL argument is not a string literal, so the endpoint it reaches is unknown.", + affected_script_keys: [ + ...new Set( + input.unresolvedEndpoints.map( + ({ location }) => location.script_key, + ), + ), + ], + call_sites: [...input.unresolvedEndpoints], + }, + ]), ...(input.widenedScripts.length === 0 ? [] : [ diff --git a/src/domain/webBundleAnalyzerState.ts b/src/domain/webBundleAnalyzerState.ts index f24c813680..52f204274e 100644 --- a/src/domain/webBundleAnalyzerState.ts +++ b/src/domain/webBundleAnalyzerState.ts @@ -1,5 +1,8 @@ import type { WebPageInspection } from "./browserObservationSchemas.js"; -import type { WebBundleAnalysis } from "./webBundleAnalysis.js"; +import type { + WebBundleAnalysis, + WebBundleCallSite, +} from "./webBundleAnalysis.js"; type BundleObservations = WebBundleAnalysis["observations"]; type BrowserScript = WebPageInspection["scripts"]["items"][number]; @@ -15,6 +18,8 @@ export interface AnalysisAccumulator { readonly edges: BundleObservations["chunks"]["edges"]; readonly routes: BundleObservations["routes"]; readonly endpoints: BundleObservations["endpoints"]; + /** Browser requests whose URL argument is not a string literal. */ + readonly unresolvedEndpoints: WebBundleCallSite[]; readonly webMcp: BundleObservations["webmcp_declarations"]; readonly inferences: WebBundleAnalysis["inferences"]; readonly seen: Set; @@ -29,6 +34,7 @@ export const createWebBundleAnalysisAccumulator = (): AnalysisAccumulator => ({ edges: [], routes: [], endpoints: [], + unresolvedEndpoints: [], webMcp: [], inferences: [], seen: new Set(), diff --git a/src/domain/webBundleScriptAnalysis.ts b/src/domain/webBundleScriptAnalysis.ts index 04f9b83468..15145b63aa 100644 --- a/src/domain/webBundleScriptAnalysis.ts +++ b/src/domain/webBundleScriptAnalysis.ts @@ -1,6 +1,9 @@ import { z } from "zod"; -import { webBundleAnalysisSchema } from "./webBundleAnalysis.js"; +import { + webBundleAnalysisSchema, + webBundleCallSiteSchema, +} from "./webBundleAnalysis.js"; import { analysisResultLimitSchema } from "./analysisSettingDefinitions.js"; import { javaScriptRouteSourceFactsSchema } from "./javascript/javascriptRouteFacts.js"; import { javaScriptRefusedPhaseSchema } from "./javascript/javascriptSemanticWorkAllowance.js"; @@ -14,6 +17,8 @@ const observationsSchema = z.strictObject({ endpoints: webBundleAnalysisSchema.shape.observations.shape.endpoints, webmcp_declarations: webBundleAnalysisSchema.shape.observations.shape.webmcp_declarations, + /** Browser requests whose URL argument is not a string literal. */ + unresolved_endpoints: z.array(webBundleCallSiteSchema), }); /** @@ -112,7 +117,11 @@ export const webBundleScriptAnalysisSchema = z "script_key", ]); }); - for (const collection of ["routes", "endpoints"] as const) + for (const collection of [ + "routes", + "endpoints", + "unresolved_endpoints", + ] as const) result.observations[collection].forEach((finding, index) => checkScriptKey(finding.location.script_key, [ "observations", @@ -138,6 +147,7 @@ export const webBundleScriptAnalysisSchema = z result.observations.edges.length > 0 || result.observations.routes.length > 0 || result.observations.endpoints.length > 0 || + result.observations.unresolved_endpoints.length > 0 || result.observations.webmcp_declarations.length > 0 || result.inferences.length > 0; if ( diff --git a/tests/boundary/browser/webBundleResultCapacity.test.ts b/tests/boundary/browser/webBundleResultCapacity.test.ts index 6dbabc81b6..6f505e7310 100644 --- a/tests/boundary/browser/webBundleResultCapacity.test.ts +++ b/tests/boundary/browser/webBundleResultCapacity.test.ts @@ -111,6 +111,7 @@ const buildWideScriptResult = ( makeWebMcp("lastTool"), ) : [], + unresolved_endpoints: [], }, inferences: collection === "inferences"