Skip to content

Commit 913a49a

Browse files
authored
Only overwrite servername in tls connect when host is not an IP address (#354)
1 parent 1699a09 commit 913a49a

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

.changeset/lovely-boxes-heal.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"https-proxy-agent": patch
3+
"pac-proxy-agent": patch
4+
"socks-proxy-agent": patch
5+
---
6+
7+
Only overwrite servername in tls connect when host is not an IP address

packages/https-proxy-agent/src/index.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import type { OutgoingHttpHeaders } from 'http';
1010

1111
const debug = createDebug('https-proxy-agent');
1212

13+
const setServernameFromNonIpHost = <
14+
T extends { host?: string; servername?: string }
15+
>(
16+
options: T
17+
) => {
18+
if (
19+
options.servername === undefined &&
20+
options.host &&
21+
!net.isIP(options.host)
22+
) {
23+
return {
24+
...options,
25+
servername: options.host,
26+
};
27+
}
28+
return options;
29+
};
30+
1331
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1432
type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;
1533

@@ -92,12 +110,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
92110
let socket: net.Socket;
93111
if (proxy.protocol === 'https:') {
94112
debug('Creating `tls.Socket`: %o', this.connectOpts);
95-
const servername =
96-
this.connectOpts.servername || this.connectOpts.host;
97-
socket = tls.connect({
98-
...this.connectOpts,
99-
servername,
100-
});
113+
socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));
101114
} else {
102115
debug('Creating `net.Socket`: %o', this.connectOpts);
103116
socket = net.connect(this.connectOpts);
@@ -146,11 +159,14 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
146159
// The proxy is connecting to a TLS server, so upgrade
147160
// this socket connection to a TLS connection.
148161
debug('Upgrading socket connection to TLS');
149-
const servername = opts.servername || opts.host;
150162
return tls.connect({
151-
...omit(opts, 'host', 'path', 'port'),
163+
...omit(
164+
setServernameFromNonIpHost(opts),
165+
'host',
166+
'path',
167+
'port'
168+
),
152169
socket,
153-
servername,
154170
});
155171
}
156172

packages/pac-proxy-agent/src/index.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ import { getQuickJS } from '@tootallnate/quickjs-emscripten';
2424

2525
const debug = createDebug('pac-proxy-agent');
2626

27+
const setServernameFromNonIpHost = <
28+
T extends { host?: string; servername?: string }
29+
>(
30+
options: T
31+
) => {
32+
if (
33+
options.servername === undefined &&
34+
options.host &&
35+
!net.isIP(options.host)
36+
) {
37+
return {
38+
...options,
39+
servername: options.host,
40+
};
41+
}
42+
return options;
43+
};
2744
type Protocols = keyof typeof gProtocols;
2845

2946
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -238,11 +255,7 @@ export class PacProxyAgent<Uri extends string> extends Agent {
238255
if (type === 'DIRECT') {
239256
// Direct connection to the destination endpoint
240257
if (secureEndpoint) {
241-
const servername = opts.servername || opts.host;
242-
socket = tls.connect({
243-
...opts,
244-
servername,
245-
});
258+
socket = tls.connect(setServernameFromNonIpHost(opts));
246259
} else {
247260
socket = net.connect(opts);
248261
}

packages/socks-proxy-agent/src/index.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ import { URL } from 'url';
99

1010
const debug = createDebug('socks-proxy-agent');
1111

12+
const setServernameFromNonIpHost = <
13+
T extends { host?: string; servername?: string }
14+
>(
15+
options: T
16+
) => {
17+
if (
18+
options.servername === undefined &&
19+
options.host &&
20+
!net.isIP(options.host)
21+
) {
22+
return {
23+
...options,
24+
servername: options.host,
25+
};
26+
}
27+
return options;
28+
};
29+
1230
function parseSocksURL(url: URL): { lookup: boolean; proxy: SocksProxy } {
1331
let lookup = false;
1432
let type: SocksProxy['type'] = 5;
@@ -79,8 +97,7 @@ export type SocksProxyAgentOptions = Omit<
7997
'ipaddress' | 'host' | 'port' | 'type' | 'userId' | 'password'
8098
> & {
8199
socketOptions?: SocksSocketOptions;
82-
} &
83-
http.AgentOptions;
100+
} & http.AgentOptions;
84101

85102
export class SocksProxyAgent extends Agent {
86103
static protocols = [
@@ -171,11 +188,14 @@ export class SocksProxyAgent extends Agent {
171188
// The proxy is connecting to a TLS server, so upgrade
172189
// this socket connection to a TLS connection.
173190
debug('Upgrading socket connection to TLS');
174-
const servername = opts.servername || opts.host;
175191
const tlsSocket = tls.connect({
176-
...omit(opts, 'host', 'path', 'port'),
192+
...omit(
193+
setServernameFromNonIpHost(opts),
194+
'host',
195+
'path',
196+
'port'
197+
),
177198
socket,
178-
servername,
179199
});
180200

181201
tlsSocket.once('error', (error) => {

0 commit comments

Comments
 (0)