mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
fix(javascript): read the target of method-first request(method, url) calls
endpointArgument takes the first literal of a get/post/put/patch/delete/request
call as its endpoint candidate. Method-first clients (superagent's
request(method, url), needle.request, Angular HttpClient.request) put the HTTP
method there, so Api.request("get", "/api/v1/profile") reported the candidate
"get" in both analyze_javascript_application and analyze_web_bundle, and the
path was lost.
For a request callee whose first literal is a standard HTTP method (the
XHR_METHODS set that xhrOpenUrl uses, compared after toUpperCase), an exact
second string is now the candidate. A computed target keeps the method token,
so request("POST", target) is unchanged, as are RPC names, Octokit route
strings and URL-first calls.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fe8e571f78
commit
73bc6f48c7
4 files changed
+100
-1
No files matched your search
@@ -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`:
|
||||
|
||||
@@ -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]));
|
||||
|
||||
@@ -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 = `
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
Reference in new issue
Block a user