mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
fix(browser): recover literal Fetch Request inputs (#12356)
This commit is contained in:
1 parent
1530507a7e
commit
0a9f22847d
7 files changed
+81
-28
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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"];
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user