diff --git a/docs/guides/javascript/javascript-artifact-reconstruction.md b/docs/guides/javascript/javascript-artifact-reconstruction.md index 67c6463094..166320d606 100644 --- a/docs/guides/javascript/javascript-artifact-reconstruction.md +++ b/docs/guides/javascript/javascript-artifact-reconstruction.md @@ -466,6 +466,12 @@ while locally shadowed receivers can still contribute network endpoint candidates. A template recovered with a parser error and no cooked value stays dynamic; its raw spelling is not treated as a valid JavaScript string. +Method-first `request` calls, as in superagent, needle, and Angular +`HttpClient`, pass a standard HTTP method, in any letter case, before the +target. In both `analyze_javascript_application` and `analyze_web_bundle`, +`request("get", "/api/users")` contributes `/api/users` as the network endpoint +candidate. When the target is computed, the method token remains the candidate. + RPC client syntax also contributes network endpoint candidates, in both `analyze_javascript_application` and `analyze_web_bundle`, distinguished by `mechanism`: diff --git a/src/domain/javascript/javascriptStaticAnalysisHelpers.ts b/src/domain/javascript/javascriptStaticAnalysisHelpers.ts index 5e3b9ebbea..c8624c69b5 100644 --- a/src/domain/javascript/javascriptStaticAnalysisHelpers.ts +++ b/src/domain/javascript/javascriptStaticAnalysisHelpers.ts @@ -696,7 +696,15 @@ export const endpointArgument = ( receiverFact === "extension-state"))) ) return undefined; - return { kind: "candidate", value }; + // Method-first clients such as superagent, needle, and Angular + // `HttpClient` call `request(method, url)`. When a standard method token + // precedes an exact string, that string is the candidate target. With a + // computed target, the token itself remains the candidate. + const target = + method === "request" && XHR_METHODS.has(value.toUpperCase()) + ? stringValue(args[1]) + : undefined; + return { kind: "candidate", value: target ?? value }; } if (name === "sendBeacon" || name.endsWith(".sendBeacon")) return candidate(stringValue(args[0])); diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts index deaac1f23d..d3cf97e822 100644 --- a/src/domain/webBundleAnalyzer.test.ts +++ b/src/domain/webBundleAnalyzer.test.ts @@ -1063,6 +1063,58 @@ describe("web bundle endpoint candidate classification", () => { }); }); +describe("web bundle method-first request candidates", () => { + it("reads the exact target after an HTTP method token in both analyzers", () => { + const source = ` + Api.request("get", "/api/v1/profile"); + Api.request("POST", "/api/v1/items", item); + client.request("Patch", \`/api/v1/items/1\`); + http.request("DELETE", "https://api.example.test/items/1"); + request("options", "users"); + Api.request("delete", \`/api/v1/items/\${id}\`); + http.request("POST", target); + rpc.request("initialize", "/session"); + octokit.request("GET /repos/{owner}/{repo}"); + request("https://example.test/health", "GET"); + `; + const expectedCandidates = [ + // An exact string after a standard method token, in any case. + "/api/v1/profile", + "/api/v1/items", + "/api/v1/items/1", + "https://api.example.test/items/1", + "users", + // A computed target leaves the method token as the candidate. + "delete", + "POST", + // Any other first argument is still the candidate. + "initialize", + "GET /repos/{owner}/{repo}", + "https://example.test/health", + ].sort(); + const result = analyzeWebBundleFixture(inspection(source)); + expect(result.observations.endpoints).toEqual([]); + expect( + result.inferences + .filter(({ kind }) => kind === "endpoint_candidate") + .map(({ value }) => value) + .sort(), + ).toEqual(expectedCandidates); + const staticEndpoints = analyzeJavaScriptStaticSource(source).endpoints; + expect( + staticEndpoints + .filter(({ interpretation }) => interpretation === "candidate") + .map(({ value }) => value) + .sort(), + ).toEqual(expectedCandidates); + expect( + staticEndpoints.filter( + ({ interpretation }) => interpretation === undefined, + ), + ).toEqual([]); + }); +}); + describe("web bundle configuration and navigation parity", () => { it("keeps configuration setting reads out of endpoints in both analyzers", () => { const source = ` diff --git a/tests/boundary/cli/javascriptRendererEndpoints.test.ts b/tests/boundary/cli/javascriptRendererEndpoints.test.ts index 53f25b8be4..5cc28fd4cc 100644 --- a/tests/boundary/cli/javascriptRendererEndpoints.test.ts +++ b/tests/boundary/cli/javascriptRendererEndpoints.test.ts @@ -113,6 +113,39 @@ cliTest( }, ); +cliTest.for(["CLI", "stdio MCP"] as const)( + "reports the exact target of method-first request calls through %s", + async (surface, { cli }) => { + const root = await createTestTempDirectory("rea-method-first-request-"); + await writeFixtureFiles(root, { + "api.js": ` + const BASE = "https://api.example.test"; + class Api { + static request(method, path, body) { + return fetch(BASE + path, { method, body: JSON.stringify(body) }); + } + } + Api.request("get", "/api/v1/profile"); + Api.request("POST", "/api/v1/items", { name: "item" }); + Api.request("delete", \`/api/v1/items/\${id}\`); + `, + }); + const result = await analyzeJavaScriptDirectory(surface, cli, root); + const requests = result.graph.nodes + .filter(({ kind }) => kind === "endpoint") + .flatMap(({ observations }) => observations) + .filter(({ properties }) => properties.mechanism === "call:Api.request"); + expect( + requests.map(({ properties }) => String(properties.value)).sort(), + ).toEqual(["/api/v1/items", "/api/v1/profile", "delete"]); + for (const observation of requests) + expect(observation).toMatchObject({ + properties: { interpretation: "candidate" }, + evidence: { state: "inferred", confidence: "low" }, + }); + }, +); + cliTest( "preserves binding-backed XHR identity and qualifies replaced open methods", async ({ cli }) => {