From 0a9f22847d2cdec2fcfa77f3486faefd19f1ec14 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:40:59 -0500 Subject: [PATCH] fix(browser): recover literal Fetch Request inputs (#12356) --- .../javascript/javascriptNetworkRequests.ts | 20 +++++++++- .../javascriptStaticAnalysisCalls.ts | 27 ++++--------- src/domain/webBundleAnalyzer.test.ts | 38 ++++++++++++++++++- src/domain/webBundleAnalyzerInspection.ts | 18 +++++++-- src/domain/webBundleAnalyzerResult.ts | 2 +- src/domain/webBundleAnalyzerState.ts | 2 +- src/domain/webBundleScriptAnalysis.ts | 2 +- 7 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/domain/javascript/javascriptNetworkRequests.ts b/src/domain/javascript/javascriptNetworkRequests.ts index f93e654bb0..b7a77ab936 100644 --- a/src/domain/javascript/javascriptNetworkRequests.ts +++ b/src/domain/javascript/javascriptNetworkRequests.ts @@ -4,16 +4,34 @@ import type { JavaScriptSemanticAnalysisState, } from "./javascriptSemanticState.js"; import * as t from "@babel/types"; +import { unwrapJavaScriptExpression } from "./javascriptAstValues.js"; import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; import { isIntrinsicInstanceMethodCall } from "./javascriptSemanticIntrinsicInstances.js"; +import { stringValue } from "./javascriptStaticAnalysisHelpers.js"; -export const isFetchCall = ( +const isFetchCall = ( node: t.Node, bindings: JavaScriptSemanticAnalysisState, ): boolean => (t.isCallExpression(node) || t.isOptionalCallExpression(node)) && isUnmodifiedGlobalReference(node.callee, bindings, "fetch"); +/** Read a proven Fetch Request input's source literal without constructing it. */ +export const literalFetchRequestConstructorUrl = ( + call: t.Node, + input: t.Node, + bindings: JavaScriptSemanticAnalysisState, +): string | undefined => { + const target = unwrapJavaScriptExpression(input).node; + if ( + t.isNewExpression(target) && + isFetchCall(call, bindings) && + isUnmodifiedGlobalReference(target.callee, bindings, "Request") + ) + return stringValue(target.arguments[0]); + return undefined; +}; + /** Browser request identity requires an intact intrinsic, not a method spelling. */ export const isBrowserNetworkRequest = ( node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, diff --git a/src/domain/javascript/javascriptStaticAnalysisCalls.ts b/src/domain/javascript/javascriptStaticAnalysisCalls.ts index a52cb8b991..938521a695 100644 --- a/src/domain/javascript/javascriptStaticAnalysisCalls.ts +++ b/src/domain/javascript/javascriptStaticAnalysisCalls.ts @@ -1,6 +1,6 @@ import { isBrowserNetworkRequest, - isFetchCall, + literalFetchRequestConstructorUrl, } from "./javascriptNetworkRequests.js"; import * as t from "@babel/types"; import { @@ -588,19 +588,6 @@ export const indexVitePreloadDependencyTable = ( }; }; -const requestConstructorUrl = ( - node: t.Node, - bindings: JavaScriptSemanticAnalysisState, -): string | undefined => { - const target = unwrapJavaScriptExpression(node).node; - if ( - t.isNewExpression(target) && - isUnmodifiedGlobalReference(target.callee, bindings, "Request") - ) - return stringValue(target.arguments[0]); - return undefined; -}; - const inspectEndpointCall = ( node: JavaScriptCallLike, name: string, @@ -614,9 +601,7 @@ const inspectEndpointCall = ( if (target === undefined) return; const literal = stringValue(target) ?? - (isFetchCall(node, bindings) - ? requestConstructorUrl(target, bindings) - : undefined); + literalFetchRequestConstructorUrl(node, target, bindings); if (literal !== undefined) { addEndpoint(context, { node, @@ -879,8 +864,12 @@ const urlObjectRequestTarget = ( seen, isToString, ); - if (!isToString && isFetchCall(site, bindings)) { - const requestUrl = requestConstructorUrl(initialized, bindings); + if (!isToString) { + const requestUrl = literalFetchRequestConstructorUrl( + site, + initialized, + bindings, + ); if (requestUrl !== undefined) return requestUrl; } if (!t.isNewExpression(initialized)) return undefined; diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts index 830c31851b..abb0ef82c8 100644 --- a/src/domain/webBundleAnalyzer.test.ts +++ b/src/domain/webBundleAnalyzer.test.ts @@ -98,7 +98,7 @@ it("reports a browser request whose URL is not a literal as an unknown endpoint 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.", + "A browser request's endpoint could not be resolved from its URL argument.", affected_script_keys: [scriptKey], call_sites: [ { @@ -113,6 +113,42 @@ it("reports a browser request whose URL is not a literal as an unknown endpoint }); }); +it("recovers inline Request inputs only for proven fetch calls", () => { + const source = [ + 'fetch(new Request("/bare"));', + "window.fetch(new globalThis.Request(`/qualified`));", + 'function local(Request) { fetch(new Request("/shadowed")); }', + "fetch(new Request(dynamic));", + 'new WebSocket(new Request("wss://app.example.test/coerced"));', + ].join("\n"); + const result = analyzeWebBundleFixture(inspection(source)); + expect( + result.observations.endpoints.map(({ value, mechanism }) => ({ + value, + mechanism, + })), + ).toEqual([ + { value: "/bare", mechanism: "call:fetch" }, + { value: "/qualified", mechanism: "call:window.fetch" }, + ]); + expect(result.unknowns).toContainEqual( + expect.objectContaining({ + dimension: "endpoints", + call_sites: [ + expect.objectContaining({ + location: expect.objectContaining({ line: 3 }), + }), + expect.objectContaining({ + location: expect.objectContaining({ line: 4 }), + }), + expect.objectContaining({ + location: expect.objectContaining({ line: 5 }), + }), + ], + }), + ); +}); + it("reads an XMLHttpRequest URL from open's second argument on an inline receiver", () => { const scriptKey = `scr_${"1".repeat(64)}`; const result = analyzeWebBundleFixture( diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts index 1fa025daf7..008c330181 100644 --- a/src/domain/webBundleAnalyzerInspection.ts +++ b/src/domain/webBundleAnalyzerInspection.ts @@ -3,6 +3,7 @@ import type { JavaScriptSemanticPrecisionInput } from "./javascript/javascriptSe import { isBrowserNetworkRequest, createXhrOpenRequestQuery, + literalFetchRequestConstructorUrl, } from "./javascript/javascriptNetworkRequests.js"; import type { JavaScriptSemanticAnalysisState } from "./javascript/javascriptSemanticState.js"; import { @@ -288,7 +289,14 @@ const inspectCall = ( ? { ...endpoint, kind: knownRequest ? "endpoint" : endpoint.kind } : undefined; if (endpoint === undefined && knownRequest && !mapLookup) - addProvenRequestEndpoint(script, node, name, xhrRequest, accumulator); + addProvenRequestEndpoint( + script, + node, + name, + xhrRequest, + bindings, + accumulator, + ); if (networkEndpoint?.kind === "candidate") addEndpointCandidate( script, @@ -395,20 +403,22 @@ 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. + * A recoverable URL literal is an endpoint. Other present arguments retain + * an unknown endpoint at their call site. */ const addProvenRequestEndpoint = ( script: IncludedScript, node: t.CallExpression | t.OptionalCallExpression | t.NewExpression, name: string, xhrRequest: boolean, + bindings: JavaScriptSemanticAnalysisState, accumulator: AnalysisAccumulator, ): void => { const url = xhrRequest ? node.arguments[1] : node.arguments[0]; if (url === undefined) return; const mechanism = `call:${name}`; - const literal = stringValue(url); + const literal = + stringValue(url) ?? literalFetchRequestConstructorUrl(node, url, bindings); if (literal !== undefined) { addFinding({ collection: accumulator.endpoints, diff --git a/src/domain/webBundleAnalyzerResult.ts b/src/domain/webBundleAnalyzerResult.ts index b7633e85d2..373bf99722 100644 --- a/src/domain/webBundleAnalyzerResult.ts +++ b/src/domain/webBundleAnalyzerResult.ts @@ -368,7 +368,7 @@ const buildUnknowns = (input: UnknownsInput): WebBundleAnalysis["unknowns"] => [ { dimension: "endpoints" as const, reason: - "A browser request's URL argument is not a string literal, so the endpoint it reaches is unknown.", + "A browser request's endpoint could not be resolved from its URL argument.", affected_script_keys: [ ...new Set( input.unresolvedEndpoints.map( diff --git a/src/domain/webBundleAnalyzerState.ts b/src/domain/webBundleAnalyzerState.ts index 52f204274e..feda96b671 100644 --- a/src/domain/webBundleAnalyzerState.ts +++ b/src/domain/webBundleAnalyzerState.ts @@ -18,7 +18,7 @@ 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. */ + /** Browser requests whose URL argument could not be resolved statically. */ readonly unresolvedEndpoints: WebBundleCallSite[]; readonly webMcp: BundleObservations["webmcp_declarations"]; readonly inferences: WebBundleAnalysis["inferences"]; diff --git a/src/domain/webBundleScriptAnalysis.ts b/src/domain/webBundleScriptAnalysis.ts index 65e5b936b9..87f8665856 100644 --- a/src/domain/webBundleScriptAnalysis.ts +++ b/src/domain/webBundleScriptAnalysis.ts @@ -20,7 +20,7 @@ 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. */ + /** Browser requests whose URL argument could not be resolved statically. */ unresolved_endpoints: z.array(webBundleCallSiteSchema), });