Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9d4d8301f |
@@ -356,18 +356,31 @@ const XHR_METHODS = new Set([
|
|||||||
"TRACE",
|
"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
|
* Read `XMLHttpRequest.open(method, url)`. `fs.open(path, "r")` and
|
||||||
* `window.open(url, "_blank")` share the callee name but take no URL second.
|
* `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
|
* XHR normalizes the standard methods' case; extension methods such as
|
||||||
* WebDAV `PROPFIND` are conventionally uppercase tokens.
|
* WebDAV `PROPFIND` are conventionally uppercase tokens.
|
||||||
*/
|
*/
|
||||||
const isXhrMethodArgument = (node: t.Node | null | undefined): boolean => {
|
const xhrOpenUrl = (
|
||||||
const value = stringValue(node);
|
methodNode: t.Node | null | undefined,
|
||||||
return (
|
urlNode: t.Node | null | undefined,
|
||||||
value !== undefined &&
|
): string | undefined => {
|
||||||
(XHR_METHODS.has(value.toUpperCase()) || /^[A-Z][A-Z-]*$/u.test(value))
|
const url = stringValue(urlNode);
|
||||||
);
|
if (url === undefined) return undefined;
|
||||||
|
const method = stringValue(methodNode);
|
||||||
|
if (method === undefined) return isRequestUrlLiteral(url) ? url : undefined;
|
||||||
|
return XHR_METHODS.has(method.toUpperCase()) || /^[A-Z][A-Z-]*$/u.test(method)
|
||||||
|
? url
|
||||||
|
: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Select the literal URL argument for recognized network callees. */
|
/** Select the literal URL argument for recognized network callees. */
|
||||||
@@ -382,9 +395,7 @@ export const endpointArgument = (
|
|||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
|
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
|
||||||
return stringValue(args[0]);
|
return stringValue(args[0]);
|
||||||
// `XMLHttpRequest.open(method, url)`.
|
if (name.endsWith(".open")) return xhrOpenUrl(args[0], args[1]);
|
||||||
if (name.endsWith(".open") && isXhrMethodArgument(args[0]))
|
|
||||||
return stringValue(args[1]);
|
|
||||||
if (
|
if (
|
||||||
["get", "post", "put", "patch", "delete", "request"].some(
|
["get", "post", "put", "patch", "delete", "request"].some(
|
||||||
(method) => name === method || name.endsWith(`.${method}`),
|
(method) => name === method || name.endsWith(`.${method}`),
|
||||||
|
|||||||
@@ -183,11 +183,18 @@ describe("web bundle static-analysis parity", () => {
|
|||||||
fs.open("/tmp/data.txt", "r", done);
|
fs.open("/tmp/data.txt", "r", done);
|
||||||
window.open("https://help.example.test/", "_blank");
|
window.open("https://help.example.test/", "_blank");
|
||||||
xhr.open(method, "/dynamic-method");
|
xhr.open(method, "/dynamic-method");
|
||||||
|
fs.open(path, "r", done);
|
||||||
|
window.open(url, "_blank");
|
||||||
`),
|
`),
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
result.observations.endpoints.map(({ value }) => value).sort(),
|
result.observations.endpoints.map(({ value }) => value).sort(),
|
||||||
).toEqual(["/dav/", "/xhr-get", "https://xhr.example.test/submit"]);
|
).toEqual([
|
||||||
|
"/dav/",
|
||||||
|
"/dynamic-method",
|
||||||
|
"/xhr-get",
|
||||||
|
"https://xhr.example.test/submit",
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("recognizes loadURL endpoints like static analysis", () => {
|
it("recognizes loadURL endpoints like static analysis", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user