mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
fix(javascript): omit requests rejected by the default agent protocol
This commit is contained in:
1 parent
9f74043fa6
commit
f54cc2f3ac
4 files changed
+85
-1
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const requests = (body: string) =>
|
||||
analyzeJavaScriptSemantics(
|
||||
`import http from "node:http"; import https from "node:https"; ${body}`,
|
||||
).requestOperations;
|
||||
|
||||
it.each([
|
||||
'http.request("https://example.test/")',
|
||||
'https.get("http://example.test/")',
|
||||
'http.get("ftp://example.test/")',
|
||||
'https.request("http://example.test/", () => {})',
|
||||
])("omits a request rejected by the default agent protocol: %s", (call) => {
|
||||
expect(requests(call)).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps matching protocols, including normalized scheme spelling", () => {
|
||||
expect(
|
||||
requests(
|
||||
'http.request("HTTP://example.test/"); https.get("https://example.test/");',
|
||||
),
|
||||
).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("keeps options that may override the protocol or agent", () => {
|
||||
expect(
|
||||
requests(
|
||||
'http.request("https://example.test/", {protocol: "http:"}); http.request("https://example.test/", options); https.request("http://example.test/", {agent});',
|
||||
),
|
||||
).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("preserves a mutated default agent", () => {
|
||||
expect(
|
||||
requests(
|
||||
'http.globalAgent.protocol = "https:"; http.request("https://example.test/");',
|
||||
),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps browser requests and shifted arguments", () => {
|
||||
expect(
|
||||
requests(
|
||||
'fetch("/api"); new WebSocket("/socket"); http.request(...args, "https://example.test/");',
|
||||
),
|
||||
).toHaveLength(3);
|
||||
});
|
||||
@@ -74,7 +74,7 @@ it("keeps the fetch candidate when the binding also holds another request", () =
|
||||
import http from "node:http";
|
||||
async function f(){
|
||||
let res = await fetch("https://api.test/x");
|
||||
res = await http.get("https://api.test/y");
|
||||
res = await http.get("http://api.test/y");
|
||||
return res.json();
|
||||
}
|
||||
`);
|
||||
|
||||
@@ -202,6 +202,27 @@ export const builtinDataEffectIdentity = (
|
||||
: { path: candidate, builtinAuthority: "candidate" };
|
||||
};
|
||||
|
||||
/** Recover the default HTTP agent protocol only while its shared slot is intact. */
|
||||
export const semanticDefaultHttpProtocol = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): "http:" | "https:" | undefined => {
|
||||
for (const family of ["http", "https"] as const) {
|
||||
const identity = builtinDataEffectIdentity(callee, state, [
|
||||
family,
|
||||
`node:${family}`,
|
||||
]);
|
||||
if (
|
||||
identity?.builtinAuthority === undefined &&
|
||||
identity?.path.length === 1 &&
|
||||
(identity.path[0] === "request" || identity.path[0] === "get") &&
|
||||
!requiredModuleChanged(family, ["globalAgent", "protocol"], callee, state)
|
||||
)
|
||||
return family === "http" ? "http:" : "https:";
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const builtinDataEffectCallablePathWithAuthority = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js";
|
||||
import {
|
||||
builtinDataEffectIdentity,
|
||||
semanticDefaultHttpProtocol,
|
||||
dataEffectArgumentBindingId as argumentBindingId,
|
||||
dataEffectLiteralString as literalString,
|
||||
dataEffectMemberCallee as memberCallee,
|
||||
@@ -266,6 +267,20 @@ const collectRequests = (
|
||||
if (identity === null) return;
|
||||
const method = identity.method;
|
||||
const endpoint = requestEndpoint(node);
|
||||
// An options object may replace both protocol and agent. A callback cannot.
|
||||
const options = node.arguments[1];
|
||||
if (
|
||||
endpoint !== null &&
|
||||
URL.canParse(endpoint) &&
|
||||
(method === "get" || method === "request") &&
|
||||
(options === undefined ||
|
||||
t.isFunctionExpression(options) ||
|
||||
t.isArrowFunctionExpression(options))
|
||||
) {
|
||||
const protocol = semanticDefaultHttpProtocol(node.callee, context.state);
|
||||
if (protocol !== undefined && new URL(endpoint).protocol !== protocol)
|
||||
return;
|
||||
}
|
||||
// `http.get`/`http.request` parse a string target with `new URL` before the
|
||||
// request exists, so a literal that is not an absolute URL throws
|
||||
// `ERR_INVALID_URL` synchronously — an options object cannot rescue it.
|
||||
|
||||
Reference in new issue
Block a user