Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(javascript): keep literal-method and URL-literal requests on aliased receivers

Iwakurakaoru0822e3e76646657e
diff --cc src/domain/javascriptStaticAnalysisHelpers.ts
index d13d2a7ac,0109dead9..c7085e2af
--- a/src/domain/javascriptStaticAnalysisHelpers.ts
+++ b/src/domain/javascriptStaticAnalysisHelpers.ts
@@@ -356,39 -356,56 +356,82 @@@ const XHR_METHODS = new Set(
"TRACE",
]);
+/** An absolute HTTP(S)/WebSocket URL, or an absolute or relative path. */
+const isRequestUrlLiteral = (value: string): boolean =>
+ /^(?:https?|wss?):\/\//iu.test(value) ||
+ value.startsWith("/") ||
+ value.startsWith("./") ||
+ value.startsWith("../");
+
+ /**
- * Require a literal HTTP method: `fs.open(path, "r")` and
- * `window.open(url, "_blank")` share the callee name but take no URL second.
- * XHR normalizes the standard methods' case; extension methods such as
- * WebDAV `PROPFIND` are conventionally uppercase tokens.
++ * Browsing-context and document `open` take a URL or type first and a target
++ * name second. The name is only a spelling, since `parent` or `self` may be a
++ * local XHR binding, so it never overrides a literal HTTP method.
+ */
-const isXhrMethodArgument = (node: t.Node | null | undefined): boolean => {
- const value = stringValue(node);
- return (
- value !== undefined &&
- (XHR_METHODS.has(value.toUpperCase()) || /^[A-Z][A-Z-]*$/u.test(value))
- );
++const WINDOW_OPEN_CALL =
++ /(?:^|\.)(?:window|self|globalThis|top|parent|opener|frames|document)\.open$/u;
++
+/**
+ * Read `XMLHttpRequest.open(method, url)`. `fs.open(path, "r")` and
+ * `window.open(url, "_blank")` share the callee name, so a literal method
- * must be an HTTP method, and a computed method needs a request URL literal.
- * XHR normalizes the standard methods' case; extension methods such as
- * WebDAV `PROPFIND` are conventionally uppercase tokens.
++ * must be an HTTP method, and a computed method needs a request URL literal
++ * on a receiver not spelled as a browsing context. XHR normalizes the
++ * standard methods' case; extension methods such as WebDAV `PROPFIND` are
++ * conventionally uppercase tokens.
+ */
+const xhrOpenUrl = (
++ name: string,
+ methodNode: t.Node | null | undefined,
+ urlNode: t.Node | null | undefined,
+): string | undefined => {
+ const url = stringValue(urlNode);
+ if (url === undefined) return undefined;
+ const method = stringValue(methodNode);
- if (method === undefined) return isRequestUrlLiteral(url) ? url : undefined;
++ if (method === undefined)
++ return !WINDOW_OPEN_CALL.test(name) && isRequestUrlLiteral(url)
++ ? url
++ : undefined;
+ return XHR_METHODS.has(method.toUpperCase()) || /^[A-Z][A-Z-]*$/u.test(method)
+ ? url
+ : undefined;
};
+ const KEYED_COLLECTION_CONSTRUCTORS = new Set([
+ "FormData",
+ "Headers",
+ "Map",
+ "Set",
+ "URLSearchParams",
+ "WeakMap",
+ "WeakSet",
+ ]);
+
/**
- * Browsing-context and document `open` take a URL or type first and a target
- * name second, never an XHR method and request URL.
- * Whether a get/delete receiver is provably a keyed collection rather than an
- * HTTP client: a constructed Map, Set, Headers, FormData, or URLSearchParams,
- * or the platform `headers`/`searchParams` properties of a request, response,
- * or URL. Other receivers stay possible clients.
++ * Whether a get/delete receiver is spelled as a keyed collection: a
++ * constructed Map, Set, Headers, FormData, or URLSearchParams, or a
++ * `headers`/`searchParams` property. Spelling alone does not prove the
++ * binding, so callers keep URL and path literals as requests.
*/
- const WINDOW_OPEN_CALL =
- /(?:^|\.)(?:window|self|globalThis|top|parent|opener|frames|document)\.open$/u;
+ const isKeyedCollectionReceiver = (callee: t.Node): boolean => {
+ if (!t.isMemberExpression(callee) && !t.isOptionalMemberExpression(callee))
+ return false;
+ const receiver = callee.object;
+ if (t.isNewExpression(receiver))
+ return (
+ t.isIdentifier(receiver.callee) &&
+ KEYED_COLLECTION_CONSTRUCTORS.has(receiver.callee.name)
+ );
+ if (
+ !t.isMemberExpression(receiver) &&
+ !t.isOptionalMemberExpression(receiver)
+ )
+ return false;
+ const property = semanticStaticPropertyName(
+ receiver.property,
+ receiver.computed,
+ );
+ return property === "headers" || property === "searchParams";
+ };
/** Select the literal URL argument for recognized network callees. */
export const endpointArgument = (
@@@ -402,14 -420,18 +446,21 @@@
): string | undefined => {
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
return stringValue(args[0]);
- if (name.endsWith(".open") && !WINDOW_OPEN_CALL.test(name))
- return xhrOpenUrl(args[0], args[1]);
- if (
- ["get", "post", "put", "patch", "delete", "request"].some(
- (method) => name === method || name.endsWith(`.${method}`),
- )
- )
- return stringValue(args[0]);
- // `XMLHttpRequest.open(method, url)`.
- if (name.endsWith(".open") && isXhrMethodArgument(args[0]))
- return stringValue(args[1]);
++ if (name.endsWith(".open")) return xhrOpenUrl(name, args[0], args[1]);
+ const method = ["get", "post", "put", "patch", "delete", "request"].find(
+ (candidate) => name === candidate || name.endsWith(`.${candidate}`),
+ );
- if (method !== undefined)
- // Only get and delete also belong to the keyed-collection APIs.
- return (method === "get" || method === "delete") &&
++ if (method !== undefined) {
++ const value = stringValue(args[0]);
++ // Only get and delete also belong to the keyed-collection APIs, whose
++ // keys are header, field, and parameter names rather than URLs or paths.
++ return value !== undefined &&
++ (method === "get" || method === "delete") &&
++ !isRequestUrlLiteral(value) &&
+ isKeyedCollectionReceiver(callee)
+ ? undefined
- : stringValue(args[0]);
++ : value;
++ }
if (name.endsWith("loadURL")) return stringValue(args[0]);
return undefined;
};
diff --cc src/domain/webBundleAnalyzer.test.ts
index d4f5440be,279785546..e8f038d69
--- a/src/domain/webBundleAnalyzer.test.ts
+++ b/src/domain/webBundleAnalyzer.test.ts
@@@ -183,23 -183,41 +183,61 @@@ describe("web bundle static-analysis pa
fs.open("/tmp/data.txt", "r", done);
window.open("https://help.example.test/", "_blank");
xhr.open(method, "/dynamic-method");
+ fs.open(path, "r", done);
+ window.open(url, "_blank");
+ window.open(url, "/preview");
+ globalThis.open(url, "./frame");
+ document.open("text/html", "/replace");
++ parent.open("GET", "/parent-xhr");
++ self.open("post", "/self-xhr");
`),
);
expect(
result.observations.endpoints.map(({ value }) => value).sort(),
- ).toEqual(["/dav/", "/xhr-get", "https://xhr.example.test/submit"]);
+ ).toEqual([
+ "/dav/",
+ "/dynamic-method",
++ "/parent-xhr",
++ "/self-xhr",
+ "/xhr-get",
+ "https://xhr.example.test/submit",
+ ]);
});
+ it("keeps HTTP-method-named reads of provable keyed collections out of endpoints", () => {
+ const result = analyzeCapturedWebBundle(
+ inspection(`
+ axios.get("https://api.example.test/users");
+ api.get("users");
+ client.delete("cache-key");
+ new Map().get("map-key");
+ new URLSearchParams(location.search).get("q");
+ new Headers(init).delete("x-trace");
+ new FormData(form).get("file");
+ response.headers.get("content-type");
+ request?.headers.delete("cookie");
+ new URL(location.href).searchParams.get("page");
+ api.headers.post("/v1/headers");
+ api.searchParams.request("/search");
++ new Headers().get("/users");
++ api.headers.get("https://api.example.test/headers");
++ api.searchParams.delete("../search");
+ `),
+ );
+ expect(
+ result.observations.endpoints.map(({ value }) => value).sort(),
+ ).toEqual([
++ "../search",
+ "/search",
++ "/users",
+ "/v1/headers",
+ "cache-key",
++ "https://api.example.test/headers",
+ "https://api.example.test/users",
+ "users",
+ ]);
+ });
+
it("recognizes loadURL endpoints like static analysis", () => {
const result = analyzeCapturedWebBundle(
inspection('window.win.loadURL("https://embedded.test/app");'),