diff --git a/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts index 7e5f01d5c3..c1bc16b634 100644 --- a/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts +++ b/src/domain/javascript/javascriptSemanticAnalysis.request_protocols.test.ts @@ -46,3 +46,51 @@ it("keeps browser requests and shifted arguments", () => { ), ).toHaveLength(3); }); + +it.each([ + [ + "named HTTP", + "import { globalAgent, request } from 'node:http'; globalAgent.protocol = 'https:'; request('https://example.test/');", + ], + [ + "named HTTPS", + "import { globalAgent, get } from 'node:https'; globalAgent.protocol = 'http:'; get('http://example.test/');", + ], + [ + "namespace HTTP", + "import * as http from 'node:http'; http.globalAgent.protocol = 'https:'; http.request('https://example.test/');", + ], + [ + "namespace HTTPS", + "import * as https from 'node:https'; https.globalAgent.protocol = 'http:'; https.get('http://example.test/');", + ], +])( + "keeps %s requests after the shared agent protocol changes", + (_import, source) => { + expect(analyzeJavaScriptSemantics(source).requestOperations).toHaveLength( + 1, + ); + }, +); + +it("tracks shared agent aliases and escapes through named exports", () => { + expect( + analyzeJavaScriptSemantics(` + import { globalAgent, request } from "node:http"; + const alias = globalAgent; + configure(alias); + request("https://example.test/"); + `).requestOperations, + ).toHaveLength(1); +}); + +it("ignores later agent mutations and unrelated properties", () => { + expect( + analyzeJavaScriptSemantics(` + import { globalAgent, request } from "node:http"; + globalAgent.label = "metadata"; + request("https://example.test/"); + globalAgent.protocol = "https:"; + `).requestOperations, + ).toEqual([]); +}); diff --git a/src/domain/javascript/javascriptSemanticRequireOrigin.ts b/src/domain/javascript/javascriptSemanticRequireOrigin.ts index 5b066cfd3f..0644873e2f 100644 --- a/src/domain/javascript/javascriptSemanticRequireOrigin.ts +++ b/src/domain/javascript/javascriptSemanticRequireOrigin.ts @@ -1972,6 +1972,12 @@ export const semanticMutableBuiltinOrigin = ( return { specifier, path }; if (specifier === "process" && (path[0] === "env" || path[0] === "argv")) return { specifier, path }; + // Named and namespace exports still expose the same mutable Agent object. + if ( + (specifier === "http" || specifier === "https") && + path[0] === "globalAgent" + ) + return { specifier, path }; return specifier === "fs" && path[0] === "promises" ? { specifier, path } : undefined;