Skip to content

Commit cdaab63

Browse files
committed
Add support for serverName HTTPS option
Fixes #2320
1 parent 80ae34b commit cdaab63

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

documentation/5-https.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ If it failed, an `Error` should be returned.
3939
4040
Check [Node.js docs](https://nodejs.org/api/https.html#https_https_request_url_options_callback) for an example.
4141

42+
#### `serverName`
43+
44+
**Type: `string`**\
45+
**Default: `undefined`**
46+
47+
Server name for the [Server Name Indication (SNI)](https://en.wikipedia.org/wiki/Server_Name_Indication) TLS extension.
48+
49+
This is useful when requesting to servers that don't have a proper domain name but use a certificate with a known CN/SAN.
50+
51+
```js
52+
import got from 'got';
53+
54+
// Request to IP address with specific servername for TLS
55+
await got('https://192.168.1.100', {
56+
https: {
57+
serverName: 'example.com'
58+
}
59+
});
60+
```
61+
4262
#### `certificateAuthority`
4363

4464
**Type: `string | Buffer | string[] | Buffer[]`**

source/core/options.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,25 @@ export type HttpsOptions = {
444444
// From `tls.ConnectionOptions`
445445
checkServerIdentity?: CheckServerIdentityFunction;
446446

447+
/**
448+
Server name for the [Server Name Indication (SNI)](https://en.wikipedia.org/wiki/Server_Name_Indication) TLS extension.
449+
450+
This is useful when requesting to servers that don't have a proper domain name but use a certificate with a known CN/SAN.
451+
452+
@example
453+
```
454+
import got from 'got';
455+
456+
// Request to IP address with specific servername for TLS
457+
await got('https://192.168.1.100', {
458+
https: {
459+
serverName: 'example.com'
460+
}
461+
});
462+
```
463+
*/
464+
serverName?: string;
465+
447466
// From `tls.SecureContextOptions`
448467
/**
449468
Override the default Certificate Authorities ([from Mozilla](https://ccadb-public.secure.force.com/mozilla/IncludedCACertificateReport)).
@@ -809,6 +828,7 @@ const defaultInternals: Options['_internals'] = {
809828
alpnProtocols: undefined,
810829
rejectUnauthorized: undefined,
811830
checkServerIdentity: undefined,
831+
serverName: undefined,
812832
certificateAuthority: undefined,
813833
key: undefined,
814834
certificate: undefined,
@@ -2278,6 +2298,7 @@ export default class Options {
22782298

22792299
assert.any([is.boolean, is.undefined], value.rejectUnauthorized);
22802300
assert.any([is.function, is.undefined], value.checkServerIdentity);
2301+
assert.any([is.string, is.undefined], value.serverName);
22812302
assert.any([is.string, is.object, is.array, is.undefined], value.certificateAuthority);
22822303
assert.any([is.string, is.object, is.array, is.undefined], value.key);
22832304
assert.any([is.string, is.object, is.array, is.undefined], value.certificate);
@@ -2514,6 +2535,7 @@ export default class Options {
25142535
pfx: https.pfx,
25152536
rejectUnauthorized: https.rejectUnauthorized,
25162537
checkServerIdentity: https.checkServerIdentity ?? checkServerIdentity,
2538+
servername: https.serverName,
25172539
ciphers: https.ciphers,
25182540
honorCipherOrder: https.honorCipherOrder,
25192541
minVersion: https.minVersion,

test/https.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ test('https request with `checkServerIdentity` NOT OK', withHttpsServer(), async
117117
);
118118
});
119119

120+
test('https request with `serverName` option', withHttpsServer(), async (t, server, got) => {
121+
server.get('/', (request, response) => {
122+
// Get the servername from the TLS connection
123+
const {servername} = request.socket as any;
124+
response.json({servername});
125+
});
126+
127+
const {servername} = await got({
128+
https: {
129+
serverName: 'custom.example.com',
130+
rejectUnauthorized: false,
131+
},
132+
}).json<{servername: string}>();
133+
134+
t.is(servername, 'custom.example.com');
135+
});
136+
120137
// The built-in `openssl` on macOS does not support negative days.
121138
{
122139
const testFunction = process.platform === 'darwin' ? test.skip : test;

0 commit comments

Comments
 (0)