Files
reverse-engineering-anything/tests/boundary/cli/javascriptRendererEndpoints.test.ts
T
pedrocormannandClaude Opus 5.5 73bc6f48c7 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>
2026-11-21 22:29:49 +01:00

178 lines
7.0 KiB
TypeScript

import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
cliTest(
"preserves renderer endpoint certainty through CLI graph delivery",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-renderer-endpoints-");
await writeFixtureFiles(root, {
"main.cjs": `
const { BrowserWindow } = require("electron");
const replaced = new BrowserWindow();
replaced.loadURL = () => {};
replaced.loadURL("https://replaced.test");
const local = { loadURL(value) { return value; } };
local.loadURL("https://local.test");
const intact = new BrowserWindow();
intact.loadURL("https://intact.test");
new BrowserWindow().loadURL(runtimeUrl);
new BrowserWindow().loadFile("page.html");
`,
"page.html": "<p>Local renderer</p>",
});
const result = await analyzeJavaScriptDirectory("CLI", cli, root);
const endpoints = result.graph.nodes.filter(
({ kind }) => kind === "endpoint",
);
expect(endpoints).toHaveLength(4);
for (const value of ["https://replaced.test", "https://local.test"]) {
const endpoint = endpoints.find(({ observations }) =>
observations.some(({ properties }) => properties.value === value),
);
expect(endpoint?.observations[0]).toMatchObject({
properties: { value, interpretation: "candidate" },
evidence: { state: "inferred", confidence: "low" },
});
expect(endpoint?.observations[0]?.evidence.limitations).toEqual(
expect.arrayContaining([
expect.stringContaining("identity is unproven"),
]),
);
expect(
result.graph.edges.find(
({ target_node_id }) => target_node_id === endpoint?.node_id,
),
).toMatchObject({
relation: "calls",
properties: { interpretation: "candidate" },
evidence: { state: "inferred", confidence: "low" },
});
}
const intact = endpoints.find(({ observations }) =>
observations.some(
({ properties }) => properties.value === "https://intact.test",
),
);
expect(intact?.observations[0]).toMatchObject({
properties: { value: "https://intact.test" },
evidence: { state: "observed", confidence: "exact" },
});
expect(intact?.observations[0]?.properties).not.toHaveProperty(
"interpretation",
);
const dynamic = endpoints.find(({ observations }) =>
observations.some(({ properties }) => properties.value === "runtimeUrl"),
);
expect(dynamic?.observations[0]).toMatchObject({
properties: { value: "runtimeUrl", resolution: "partial" },
evidence: { state: "observed" },
});
},
);
cliTest(
"qualifies local browser request spellings through CLI Evidence",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-network-spellings-");
await writeFixtureFiles(root, {
"index.js": `
const local = { fetch(x) { return x; }, sendBeacon(x) { return x; }, EventSource(x) { return x; } };
local.fetch('/local-fetch'); local.sendBeacon('/local-beacon'); local.EventSource('/local-events');
const other = { open(a,b) { return b; }, post(x) { return x; }, put(x) { return x; }, patch(x) { return x; }, request(x) { return x; } };
other.open('GET', '/local-open'); other.post('/local-post'); other.put('/local-put'); other.patch('/local-patch'); other.request('/local-request');
fetch('/intrinsic-fetch'); navigator.sendBeacon('/intrinsic-beacon'); new EventSource('/intrinsic-events');
`,
});
const result = await analyzeJavaScriptDirectory("CLI", cli, root);
const endpoints = result.graph.nodes.filter(
({ kind }) => kind === "endpoint",
);
expect(endpoints).toHaveLength(11);
for (const endpoint of endpoints) {
const observation = endpoint.observations[0]!;
const local = String(observation.properties.value).startsWith("/local-");
expect(observation.evidence).toMatchObject(
local
? { state: "inferred", confidence: "low" }
: { state: "observed", confidence: "exact" },
);
expect(observation.properties.interpretation).toBe(
local ? "candidate" : undefined,
);
if (local)
expect(
result.graph.edges.find(
({ target_node_id }) => target_node_id === endpoint.node_id,
)?.evidence,
).toMatchObject({ state: "inferred", confidence: "low" });
}
},
);
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 }) => {
const root = await createTestTempDirectory("rea-xhr-identity-");
await writeFixtureFiles(root, {
"index.js": `
const xhr = new XMLHttpRequest();
xhr.open('GET', '/intact-xhr');
const replaced = new XMLHttpRequest();
replaced.open = (method, value) => value;
replaced.open('GET', '/replaced-xhr');
{ const XMLHttpRequest = class {}; const local = new XMLHttpRequest(); local.open('GET', '/local-xhr'); }
`,
});
const result = await analyzeJavaScriptDirectory("CLI", cli, root);
const endpoints = result.graph.nodes.filter(
({ kind }) => kind === "endpoint",
);
expect(endpoints).toHaveLength(3);
for (const endpoint of endpoints) {
const observation = endpoint.observations[0]!;
expect(observation.evidence).toMatchObject(
observation.properties.value === "/intact-xhr"
? { state: "observed", confidence: "exact" }
: { state: "inferred", confidence: "low" },
);
}
},
);