Compare commits

...

1 Commits

Author SHA1 Message Date
Ciro Spaciari
e783515cd0 fix agent port being rewritten 2025-07-24 11:43:09 -07:00
2 changed files with 71 additions and 4 deletions

View File

@@ -614,8 +614,7 @@ function ClientRequest(input, options, cb) {
const urlStr = input;
try {
var urlObject = new URL(urlStr);
} catch (_err) {
void _err;
} catch {
throw $ERR_INVALID_URL(`Invalid URL: ${urlStr}`);
}
input = urlToHttpOptions(urlObject);
@@ -634,7 +633,16 @@ function ClientRequest(input, options, cb) {
} else {
options = ObjectAssign(input || {}, options);
}
{
const url = options.url;
if (typeof url === "string") {
try {
options.url = new URL(url);
} catch {
throw $ERR_INVALID_URL(`Invalid URL: ${url}`);
}
}
}
this[kTls] = null;
this[kAbortController] = null;
@@ -668,7 +676,7 @@ function ClientRequest(input, options, cb) {
}
const defaultPort = options.defaultPort || this[kAgent].defaultPort;
const port = (this[kPort] = options.port || defaultPort || 80);
const port = (this[kPort] = options?.url?.port || options.port || defaultPort || 80);
this[kUseDefaultPort] = this[kPort] === defaultPort;
const host =
(this[kHost] =

View File

@@ -3350,3 +3350,62 @@ describe("HTTP Server Security Tests - Advanced", () => {
});
});
});
it("rejectUnauthorized in agents should work", async () => {
using server = Bun.serve({
tls: COMMON_TLS_CERT,
fetch(req) {
return new Response("Hello World");
},
});
for (const url of [server.url, server.url.href]) {
{
const { promise, resolve, reject } = Promise.withResolvers();
const req = https.request(
{
url,
agent: new https.Agent({
rejectUnauthorized: false,
}),
},
res => {
try {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("text/plain;charset=utf-8");
expect(res.headers["content-length"]).toBe("11");
resolve();
} catch (e) {
reject(e);
}
},
);
req.on("error", reject);
req.end();
await promise;
}
}
{
const rejectUnauthorized = https.globalAgent.options.rejectUnauthorized;
try {
const { promise, resolve, reject } = Promise.withResolvers();
https.globalAgent.options.rejectUnauthorized = false;
const req = https.request(server.url, res => {
try {
expect(res.statusCode).toBe(200);
expect(res.headers["content-type"]).toBe("text/plain;charset=utf-8");
expect(res.headers["content-length"]).toBe("11");
resolve();
} catch (e) {
reject(e);
}
});
req.on("error", reject);
req.end();
await promise;
} finally {
https.globalAgent.options.rejectUnauthorized = rejectUnauthorized;
}
}
});