diff --git a/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts new file mode 100644 index 0000000000..7e5f01d5c3 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts @@ -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); +}); diff --git a/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts index 26ef076033..b505c53cb6 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.response_body_readers.test.ts @@ -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(); } `); diff --git a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts index ec824eee9b..4d8f82dc8d 100644 --- a/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts +++ b/src/domain/javascript/javascriptSemanticDataEffectHelpers.ts @@ -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, diff --git a/src/domain/javascript/javascriptSemanticDataEffects.ts b/src/domain/javascript/javascriptSemanticDataEffects.ts index 1d88a37615..2aeb8fb6db 100644 --- a/src/domain/javascript/javascriptSemanticDataEffects.ts +++ b/src/domain/javascript/javascriptSemanticDataEffects.ts @@ -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.