Skip to content

Commit f882c9b

Browse files
indutnytargos
authored andcommitted
http: servername === false should disable SNI
There is no way to disable SNI extension when sending a request to HTTPS server. Setting `options.servername` to a falsy value would make Node.js core override it with either hostname or ip address. This change introduces a way to disable SNI completely if this is required for user's application. Setting `options.servername` to `` in `https.request` would disable overrides and thus disable the extension. PR-URL: #27316 Reviewed-By: Steven R Loomis <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 53eefeb commit f882c9b

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

doc/api/https.md

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ An [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See
2929
Can have the same fields as for [`http.Agent(options)`][], and
3030
* `maxCachedSessions` {number} maximum number of TLS cached sessions.
3131
Use `0` to disable TLS session caching. **Default:** `100`.
32+
* `servername` {string} the value of
33+
[Server Name Indication extension][sni wiki] to be sent to the server. Use
34+
empty string `''` to disable sending the extension.
35+
**Default:** hostname or IP address of the target server.
3236

3337
See [`Session Resumption`][] for infomation about TLS session reuse.
3438

@@ -406,3 +410,4 @@ headers: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; p
406410
[`tls.createSecureContext()`]: tls.html#tls_tls_createsecurecontext_options
407411
[`tls.createServer()`]: tls.html#tls_tls_createserver_options_secureconnectionlistener
408412
[`Session Resumption`]: tls.html#tls_session_resumption
413+
[sni wiki]: https://en.wikipedia.org/wiki/Server_Name_Indication

lib/_http_agent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
151151
if (options.socketPath)
152152
options.path = options.socketPath;
153153

154-
if (!options.servername)
154+
if (!options.servername && options.servername !== '')
155155
options.servername = calculateServerName(options, req);
156156

157157
const name = this.getName(options);
@@ -198,7 +198,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
198198
if (options.socketPath)
199199
options.path = options.socketPath;
200200

201-
if (!options.servername)
201+
if (!options.servername && options.servername !== '')
202202
options.servername = calculateServerName(options, req);
203203

204204
const name = this.getName(options);

test/parallel/test-https-agent-sni.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@ let waiting = TOTAL;
1818
const server = https.Server(options, function(req, res) {
1919
if (--waiting === 0) server.close();
2020

21-
res.writeHead(200, {
22-
'x-sni': req.socket.servername
23-
});
21+
const servername = req.socket.servername;
22+
23+
if (servername !== false) {
24+
res.setHeader('x-sni', servername);
25+
}
26+
2427
res.end('hello world');
2528
});
2629

2730
server.listen(0, function() {
2831
function expectResponse(id) {
2932
return common.mustCall(function(res) {
3033
res.resume();
31-
assert.strictEqual(res.headers['x-sni'], `sni.${id}`);
34+
assert.strictEqual(res.headers['x-sni'],
35+
id === false ? undefined : `sni.${id}`);
3236
});
3337
}
3438

@@ -46,4 +50,13 @@ server.listen(0, function() {
4650
rejectUnauthorized: false
4751
}, expectResponse(j));
4852
}
53+
https.get({
54+
agent: agent,
55+
56+
path: '/',
57+
port: this.address().port,
58+
host: '127.0.0.1',
59+
servername: '',
60+
rejectUnauthorized: false
61+
}, expectResponse(false));
4962
});

0 commit comments

Comments
 (0)