2012-02-27 11:09:34 -08:00
|
|
|
# HTTPS
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2017-01-22 19:16:21 -08:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-02 15:14:03 -08:00
|
|
|
|
2020-06-22 13:56:08 -04:00
|
|
|
<!-- source_link=lib/https.js -->
|
|
|
|
|
|
2015-08-13 12:14:34 -04:00
|
|
|
HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a
|
2011-01-21 13:12:35 -08:00
|
|
|
separate module.
|
|
|
|
|
|
2022-03-05 23:43:29 +01:00
|
|
|
## Determining if crypto support is unavailable
|
|
|
|
|
|
|
|
|
|
It is possible for Node.js to be built without including support for the
|
2022-04-20 10:23:41 +02:00
|
|
|
`node:crypto` module. In such cases, attempting to `import` from `https` or
|
|
|
|
|
calling `require('node:https')` will result in an error being thrown.
|
2022-03-05 23:43:29 +01:00
|
|
|
|
|
|
|
|
When using CommonJS, the error thrown can be caught using try/catch:
|
|
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
|
let https;
|
|
|
|
|
try {
|
2022-04-20 10:23:41 +02:00
|
|
|
https = require('node:https');
|
2022-03-05 23:43:29 +01:00
|
|
|
} catch (err) {
|
2022-11-26 23:48:32 +09:00
|
|
|
console.error('https support is disabled!');
|
2022-03-05 23:43:29 +01:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
When using the lexical ESM `import` keyword, the error can only be
|
|
|
|
|
caught if a handler for `process.on('uncaughtException')` is registered
|
|
|
|
|
_before_ any attempt to load the module is made (using, for instance,
|
|
|
|
|
a preload module).
|
|
|
|
|
|
|
|
|
|
When using ESM, if there is a chance that the code may be run on a build
|
|
|
|
|
of Node.js where crypto support is not enabled, consider using the
|
2022-04-09 13:41:30 +02:00
|
|
|
[`import()`][] function instead of the lexical `import` keyword:
|
2022-03-05 23:43:29 +01:00
|
|
|
|
|
|
|
|
```mjs
|
|
|
|
|
let https;
|
|
|
|
|
try {
|
2022-04-20 10:23:41 +02:00
|
|
|
https = await import('node:https');
|
2022-03-05 23:43:29 +01:00
|
|
|
} catch (err) {
|
2022-11-26 23:48:32 +09:00
|
|
|
console.error('https support is disabled!');
|
2022-03-05 23:43:29 +01:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## Class: `https.Agent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.4.5
|
2019-03-04 17:28:29 +01:00
|
|
|
changes:
|
2020-10-01 20:49:03 +02:00
|
|
|
- version: v5.3.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/4252
|
|
|
|
|
description: support `0` `maxCachedSessions` to disable TLS session caching.
|
2019-03-04 17:28:29 +01:00
|
|
|
- version: v2.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2228
|
|
|
|
|
description: parameter `maxCachedSessions` added to `options` for TLS
|
|
|
|
|
sessions reuse.
|
2016-06-23 21:51:12 +02:00
|
|
|
-->
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2018-04-02 08:38:48 +03:00
|
|
|
An [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See
|
2018-02-12 02:31:55 -05:00
|
|
|
[`https.request()`][] for more information.
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2025-11-08 23:34:58 +09:00
|
|
|
Like `http.Agent`, the `createConnection(options[, callback])` method can be overridden
|
|
|
|
|
to customize how TLS connections are established.
|
|
|
|
|
|
2025-11-08 18:09:29 +01:00
|
|
|
> See [`agent.createConnection()`][] for details on overriding this method,
|
2025-11-08 23:34:58 +09:00
|
|
|
> including asynchronous socket creation with a callback.
|
|
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `new Agent([options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-06-02 18:11:48 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
changes:
|
http,https: add built-in proxy support in http/https.request and Agent
This patch implements proxy support for HTTP and HTTPS clients and
agents in the `http` and `https` built-ins`. When NODE_USE_ENV_PROXY
is set to 1, the default global agent would parse the
HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
settings from the environment variables, and proxy the requests
sent through the built-in http/https client accordingly.
To support this, `http.Agent` and `https.Agent` now accept a few new
options:
- `proxyEnv`: when it's an object, the agent would read and parse
the HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
properties from it, and apply them based on the protocol it uses
to send requests. This option allows custom agents to
reuse built-in proxy support by composing options. Global agents
set this to `process.env` when NODE_USE_ENV_PROXY is 1.
- `defaultPort` and `protocol`: these allow setting of the default port
and protocol of the agents. We also need these when configuring
proxy settings and deciding whether a request should be proxied.
Implementation-wise, this adds a `ProxyConfig` internal class to handle
parsing and application of proxy configurations. The configuration
is parsed during agent construction. When requests are made,
the `createConnection()` methods on the agents would check whether
the request should be proxied. If yes, they either connect to the
proxy server (in the case of HTTP reqeusts) or establish a tunnel
(in the case of HTTPS requests) through either a TCP socket (if the
proxy uses HTTP) or a TLS socket (if the proxy uses HTTPS).
When proxying HTTPS requests through a tunnel, the connection listener
is invoked after the tunnel is established. Tunnel establishment uses
the timeout of the request options, if there is one. Otherwise it uses
the timeout of the agent.
If an error is encountered during tunnel establishment, an
ERR_PROXY_TUNNEL would be emitted on the returned socket. If the proxy
server sends a errored status code, the error would contain an
`statusCode` property. If the error is caused by timeout, the error
would contain a `proxyTunnelTimeout` property.
This implementation honors the built-in socket pool and socket limits.
Pooled sockets are still keyed by request endpoints, they are just
connected to the proxy server instead, and the persistence of the
connection can be maintained as long as the proxy server respects
connection/proxy-connection or persist by default (HTTP/1.1)
PR-URL: https://github.com/nodejs/node/pull/58980
Refs: https://github.com/nodejs/node/issues/57872
Refs: https://github.com/nodejs/node/issues/8381
Refs: https://github.com/nodejs/node/issues/15620
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-07-02 01:17:07 +02:00
|
|
|
- version:
|
2025-07-29 09:26:32 +02:00
|
|
|
- v24.5.0
|
2025-10-19 00:30:38 +02:00
|
|
|
- v22.21.0
|
http,https: add built-in proxy support in http/https.request and Agent
This patch implements proxy support for HTTP and HTTPS clients and
agents in the `http` and `https` built-ins`. When NODE_USE_ENV_PROXY
is set to 1, the default global agent would parse the
HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
settings from the environment variables, and proxy the requests
sent through the built-in http/https client accordingly.
To support this, `http.Agent` and `https.Agent` now accept a few new
options:
- `proxyEnv`: when it's an object, the agent would read and parse
the HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
properties from it, and apply them based on the protocol it uses
to send requests. This option allows custom agents to
reuse built-in proxy support by composing options. Global agents
set this to `process.env` when NODE_USE_ENV_PROXY is 1.
- `defaultPort` and `protocol`: these allow setting of the default port
and protocol of the agents. We also need these when configuring
proxy settings and deciding whether a request should be proxied.
Implementation-wise, this adds a `ProxyConfig` internal class to handle
parsing and application of proxy configurations. The configuration
is parsed during agent construction. When requests are made,
the `createConnection()` methods on the agents would check whether
the request should be proxied. If yes, they either connect to the
proxy server (in the case of HTTP reqeusts) or establish a tunnel
(in the case of HTTPS requests) through either a TCP socket (if the
proxy uses HTTP) or a TLS socket (if the proxy uses HTTPS).
When proxying HTTPS requests through a tunnel, the connection listener
is invoked after the tunnel is established. Tunnel establishment uses
the timeout of the request options, if there is one. Otherwise it uses
the timeout of the agent.
If an error is encountered during tunnel establishment, an
ERR_PROXY_TUNNEL would be emitted on the returned socket. If the proxy
server sends a errored status code, the error would contain an
`statusCode` property. If the error is caused by timeout, the error
would contain a `proxyTunnelTimeout` property.
This implementation honors the built-in socket pool and socket limits.
Pooled sockets are still keyed by request endpoints, they are just
connected to the proxy server instead, and the persistence of the
connection can be maintained as long as the proxy server respects
connection/proxy-connection or persist by default (HTTP/1.1)
PR-URL: https://github.com/nodejs/node/pull/58980
Refs: https://github.com/nodejs/node/issues/57872
Refs: https://github.com/nodejs/node/issues/8381
Refs: https://github.com/nodejs/node/issues/15620
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-07-02 01:17:07 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/58980
|
|
|
|
|
description: Add support for `proxyEnv`.
|
|
|
|
|
- version:
|
2025-07-29 09:26:32 +02:00
|
|
|
- v24.5.0
|
2025-10-19 00:30:38 +02:00
|
|
|
- v22.21.0
|
http,https: add built-in proxy support in http/https.request and Agent
This patch implements proxy support for HTTP and HTTPS clients and
agents in the `http` and `https` built-ins`. When NODE_USE_ENV_PROXY
is set to 1, the default global agent would parse the
HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
settings from the environment variables, and proxy the requests
sent through the built-in http/https client accordingly.
To support this, `http.Agent` and `https.Agent` now accept a few new
options:
- `proxyEnv`: when it's an object, the agent would read and parse
the HTTP_PROXY/http_proxy, HTTPS_PROXY/https_proxy, NO_PROXY/no_proxy
properties from it, and apply them based on the protocol it uses
to send requests. This option allows custom agents to
reuse built-in proxy support by composing options. Global agents
set this to `process.env` when NODE_USE_ENV_PROXY is 1.
- `defaultPort` and `protocol`: these allow setting of the default port
and protocol of the agents. We also need these when configuring
proxy settings and deciding whether a request should be proxied.
Implementation-wise, this adds a `ProxyConfig` internal class to handle
parsing and application of proxy configurations. The configuration
is parsed during agent construction. When requests are made,
the `createConnection()` methods on the agents would check whether
the request should be proxied. If yes, they either connect to the
proxy server (in the case of HTTP reqeusts) or establish a tunnel
(in the case of HTTPS requests) through either a TCP socket (if the
proxy uses HTTP) or a TLS socket (if the proxy uses HTTPS).
When proxying HTTPS requests through a tunnel, the connection listener
is invoked after the tunnel is established. Tunnel establishment uses
the timeout of the request options, if there is one. Otherwise it uses
the timeout of the agent.
If an error is encountered during tunnel establishment, an
ERR_PROXY_TUNNEL would be emitted on the returned socket. If the proxy
server sends a errored status code, the error would contain an
`statusCode` property. If the error is caused by timeout, the error
would contain a `proxyTunnelTimeout` property.
This implementation honors the built-in socket pool and socket limits.
Pooled sockets are still keyed by request endpoints, they are just
connected to the proxy server instead, and the persistence of the
connection can be maintained as long as the proxy server respects
connection/proxy-connection or persist by default (HTTP/1.1)
PR-URL: https://github.com/nodejs/node/pull/58980
Refs: https://github.com/nodejs/node/issues/57872
Refs: https://github.com/nodejs/node/issues/8381
Refs: https://github.com/nodejs/node/issues/15620
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-07-02 01:17:07 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/58980
|
|
|
|
|
description: Add support for `defaultPort` and `protocol`.
|
2019-06-17 21:31:37 +02:00
|
|
|
- version: v12.5.0
|
2019-06-02 18:11:48 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/28209
|
|
|
|
|
description: do not automatically set servername if the target host was
|
|
|
|
|
specified using an IP address.
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
* `options` {Object} Set of configurable options to set on the agent.
|
|
|
|
|
Can have the same fields as for [`http.Agent(options)`][], and
|
|
|
|
|
* `maxCachedSessions` {number} maximum number of TLS cached sessions.
|
|
|
|
|
Use `0` to disable TLS session caching. **Default:** `100`.
|
2019-04-19 15:51:24 -04:00
|
|
|
* `servername` {string} the value of
|
|
|
|
|
[Server Name Indication extension][sni wiki] to be sent to the server. Use
|
|
|
|
|
empty string `''` to disable sending the extension.
|
2020-01-12 07:28:26 -08:00
|
|
|
**Default:** host name of the target server, unless the target server
|
2019-06-02 18:11:48 +02:00
|
|
|
is specified using an IP address, in which case the default is `''` (no
|
|
|
|
|
extension).
|
2019-03-04 17:28:29 +01:00
|
|
|
|
2019-08-10 08:48:03 +03:00
|
|
|
See [`Session Resumption`][] for information about TLS session reuse.
|
2019-03-04 17:28:29 +01:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
#### Event: `'keylog'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-10-21 14:27:50 -07:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
|
|
|
|
- v13.2.0
|
|
|
|
|
- v12.16.0
|
2019-10-21 14:27:50 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
* `line` {Buffer} Line of ASCII text, in NSS `SSLKEYLOGFILE` format.
|
|
|
|
|
* `tlsSocket` {tls.TLSSocket} The `tls.TLSSocket` instance on which it was
|
|
|
|
|
generated.
|
|
|
|
|
|
|
|
|
|
The `keylog` event is emitted when key material is generated or received by a
|
|
|
|
|
connection managed by this agent (typically before handshake has completed, but
|
|
|
|
|
not necessarily). This keying material can be stored for debugging, as it
|
|
|
|
|
allows captured TLS traffic to be decrypted. It may be emitted multiple times
|
|
|
|
|
for each socket.
|
|
|
|
|
|
|
|
|
|
A typical use case is to append received lines to a common text file, which is
|
|
|
|
|
later used by software (such as Wireshark) to decrypt the traffic:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// ...
|
|
|
|
|
https.globalAgent.on('keylog', (line, tlsSocket) => {
|
|
|
|
|
fs.appendFileSync('/tmp/ssl-keys.log', line, { mode: 0o600 });
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## Class: `https.Server`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.4
|
|
|
|
|
-->
|
2011-04-28 16:36:04 +09:00
|
|
|
|
2019-08-21 13:32:59 -07:00
|
|
|
* Extends: {tls.Server}
|
|
|
|
|
|
|
|
|
|
See [`http.Server`][] for more information.
|
2011-04-28 16:36:04 +09:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.close([callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2017-10-06 11:50:47 -07:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.1.90
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `callback` {Function}
|
2018-10-31 16:53:38 +08:00
|
|
|
* Returns: {https.Server}
|
2017-10-06 11:50:47 -07:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.close()`][] in the `node:http` module.
|
2022-04-28 12:05:55 +02:00
|
|
|
|
2023-06-25 23:29:58 +03:00
|
|
|
### `server[Symbol.asyncDispose]()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-07-03 10:45:00 -03:00
|
|
|
added: v20.4.0
|
2025-05-26 08:54:25 -07:00
|
|
|
changes:
|
2025-06-08 14:03:03 -04:00
|
|
|
- version: v24.2.0
|
2025-05-26 08:54:25 -07:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/58467
|
|
|
|
|
description: No longer experimental.
|
2023-06-25 23:29:58 +03:00
|
|
|
-->
|
|
|
|
|
|
|
|
|
|
Calls [`server.close()`][httpsServerClose] and returns a promise that
|
|
|
|
|
fulfills when the server has closed.
|
|
|
|
|
|
2022-04-28 12:05:55 +02:00
|
|
|
### `server.closeAllConnections()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-05-10 10:10:45 -03:00
|
|
|
added: v18.2.0
|
2022-04-28 12:05:55 +02:00
|
|
|
-->
|
|
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.closeAllConnections()`][] in the `node:http` module.
|
2022-04-28 12:05:55 +02:00
|
|
|
|
|
|
|
|
### `server.closeIdleConnections()`
|
|
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-05-10 10:10:45 -03:00
|
|
|
added: v18.2.0
|
2022-04-28 12:05:55 +02:00
|
|
|
-->
|
|
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.closeIdleConnections()`][] in the `node:http` module.
|
2017-10-06 11:50:47 -07:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.headersTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2018-11-28 16:48:07 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v11.3.0
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2025-07-15 13:18:42 -04:00
|
|
|
* Type: {number} **Default:** `60000`
|
2018-11-28 16:48:07 +02:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.headersTimeout`][] in the `node:http` module.
|
2018-11-28 16:48:07 +02:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.listen()`
|
2017-10-06 11:50:47 -07:00
|
|
|
|
|
|
|
|
Starts the HTTPS server listening for encrypted connections.
|
|
|
|
|
This method is identical to [`server.listen()`][] from [`net.Server`][].
|
|
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.maxHeadersCount`
|
2018-04-23 03:48:41 +09:00
|
|
|
|
2025-07-15 13:18:42 -04:00
|
|
|
* Type: {number} **Default:** `2000`
|
2018-04-23 03:48:41 +09:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.maxHeadersCount`][] in the `node:http` module.
|
2018-04-23 03:48:41 +09:00
|
|
|
|
2020-05-14 20:21:34 +02:00
|
|
|
### `server.requestTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-14 20:21:34 +02:00
|
|
|
<!-- YAML
|
2020-09-14 16:02:57 -04:00
|
|
|
added: v14.11.0
|
2023-01-13 09:32:19 +01:00
|
|
|
changes:
|
|
|
|
|
- version: v18.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41263
|
|
|
|
|
description: The default request timeout changed
|
|
|
|
|
from no timeout to 300s (5 minutes).
|
2020-05-14 20:21:34 +02:00
|
|
|
-->
|
|
|
|
|
|
2025-07-15 13:18:42 -04:00
|
|
|
* Type: {number} **Default:** `300000`
|
2020-05-14 20:21:34 +02:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.requestTimeout`][] in the `node:http` module.
|
2020-05-14 20:21:34 +02:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.setTimeout([msecs][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.2
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `msecs` {number} **Default:** `120000` (2 minutes)
|
|
|
|
|
* `callback` {Function}
|
2018-10-31 16:53:38 +08:00
|
|
|
* Returns: {https.Server}
|
2013-04-30 12:43:32 +02:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.setTimeout()`][] in the `node:http` module.
|
2013-04-30 12:43:32 +02:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.timeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.2
|
2020-02-08 12:47:37 +03:00
|
|
|
changes:
|
|
|
|
|
- version: v13.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/27558
|
|
|
|
|
description: The default timeout changed from 120s to 0 (no timeout).
|
2016-06-23 21:51:12 +02:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2025-07-15 13:18:42 -04:00
|
|
|
* Type: {number} **Default:** 0 (no timeout)
|
2013-04-30 12:43:32 +02:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.timeout`][] in the `node:http` module.
|
2013-04-30 12:43:32 +02:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
### `server.keepAliveTimeout`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2015-10-29 21:53:43 +02:00
|
|
|
<!-- YAML
|
2017-03-15 20:26:14 -07:00
|
|
|
added: v8.0.0
|
2015-10-29 21:53:43 +02:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2025-07-15 13:18:42 -04:00
|
|
|
* Type: {number} **Default:** `5000` (5 seconds)
|
2015-10-29 21:53:43 +02:00
|
|
|
|
2022-05-11 01:04:13 +02:00
|
|
|
See [`server.keepAliveTimeout`][] in the `node:http` module.
|
2015-10-29 21:53:43 +02:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.createServer([options][, requestListener])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.4
|
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `options` {Object} Accepts `options` from [`tls.createServer()`][],
|
2020-11-09 05:44:32 -08:00
|
|
|
[`tls.createSecureContext()`][] and [`http.createServer()`][].
|
2018-07-12 13:48:11 -04:00
|
|
|
* `requestListener` {Function} A listener to be added to the `'request'` event.
|
2018-10-31 16:53:38 +08:00
|
|
|
* Returns: {https.Server}
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
```mjs
|
|
|
|
|
// curl -k https://localhost:8000/
|
|
|
|
|
import { createServer } from 'node:https';
|
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
key: readFileSync('private-key.pem'),
|
|
|
|
|
cert: readFileSync('certificate.pem'),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createServer(options, (req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2016-01-17 18:39:07 +01:00
|
|
|
// curl -k https://localhost:8000/
|
2022-04-20 10:23:41 +02:00
|
|
|
const https = require('node:https');
|
|
|
|
|
const fs = require('node:fs');
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
|
const options = {
|
2024-08-19 09:52:40 -04:00
|
|
|
key: fs.readFileSync('private-key.pem'),
|
|
|
|
|
cert: fs.readFileSync('certificate.pem'),
|
2016-01-17 18:39:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
https.createServer(options, (req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
```
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2012-05-14 01:08:23 +05:30
|
|
|
Or
|
|
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
```mjs
|
|
|
|
|
import { createServer } from 'node:https';
|
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
pfx: readFileSync('test_cert.pfx'),
|
|
|
|
|
passphrase: 'sample',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createServer(options, (req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const https = require('node:https');
|
|
|
|
|
const fs = require('node:fs');
|
2012-05-14 01:08:23 +05:30
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const options = {
|
2024-08-19 09:52:40 -04:00
|
|
|
pfx: fs.readFileSync('test_cert.pfx'),
|
2022-11-17 08:19:12 -05:00
|
|
|
passphrase: 'sample',
|
2016-01-17 18:39:07 +01:00
|
|
|
};
|
2012-05-14 01:08:23 +05:30
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
https.createServer(options, (req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
```
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
To generate the certificate and key for this example, run:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
|
|
|
|
|
-keyout private-key.pem -out certificate.pem
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then, to generate the `pfx` certificate for this example, run:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
openssl pkcs12 -certpbe AES-256-CBC -export -out test_cert.pfx \
|
|
|
|
|
-inkey private-key.pem -in certificate.pem -passout pass:sample
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.get(options[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.get(url[, options][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
2017-06-02 18:07:06 +03:00
|
|
|
changes:
|
2018-08-13 19:02:06 +10:00
|
|
|
- version: v10.9.0
|
2018-07-01 11:00:24 -04:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21616
|
2018-09-19 17:14:37 +02:00
|
|
|
description: The `url` parameter can now be passed along with a separate
|
|
|
|
|
`options` object.
|
2017-06-02 18:07:06 +03:00
|
|
|
- version: v7.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10638
|
|
|
|
|
description: The `options` parameter can be a WHATWG `URL` object.
|
2016-06-23 21:51:12 +02:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-06 21:46:38 -04:00
|
|
|
* `url` {string | URL}
|
2018-07-12 13:48:11 -04:00
|
|
|
* `options` {Object | string | URL} Accepts the same `options` as
|
2023-07-08 17:25:27 +08:00
|
|
|
[`https.request()`][], with the method set to GET by default.
|
2018-07-12 13:48:11 -04:00
|
|
|
* `callback` {Function}
|
2025-04-30 04:20:21 +08:00
|
|
|
* Returns: {http.ClientRequest}
|
2012-10-08 19:10:29 +02:00
|
|
|
|
2015-11-27 18:30:32 -05:00
|
|
|
Like [`http.get()`][] but for HTTPS.
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2017-06-02 18:07:06 +03:00
|
|
|
`options` can be an object, a string, or a [`URL`][] object. If `options` is a
|
2018-04-24 19:37:43 -05:00
|
|
|
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
|
2017-06-02 18:07:06 +03:00
|
|
|
object, it will be automatically converted to an ordinary `options` object.
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
```mjs
|
|
|
|
|
import { get } from 'node:https';
|
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
|
|
get('https://encrypted.google.com/', (res) => {
|
|
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
console.log('headers:', res.headers);
|
|
|
|
|
|
|
|
|
|
res.on('data', (d) => {
|
|
|
|
|
process.stdout.write(d);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}).on('error', (e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const https = require('node:https');
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
https.get('https://encrypted.google.com/', (res) => {
|
2016-07-26 22:25:08 -04:00
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
console.log('headers:', res.headers);
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
res.on('data', (d) => {
|
|
|
|
|
process.stdout.write(d);
|
|
|
|
|
});
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
}).on('error', (e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.globalAgent`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.5.9
|
2022-06-21 14:50:55 +02:00
|
|
|
changes:
|
|
|
|
|
- version:
|
2022-09-13 12:53:52 -03:00
|
|
|
- v19.0.0
|
2022-06-21 14:50:55 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/43522
|
2024-04-08 22:33:46 +02:00
|
|
|
description: The agent now uses HTTP Keep-Alive and a 5 second timeout by
|
|
|
|
|
default.
|
2016-06-23 21:51:12 +02:00
|
|
|
-->
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2024-04-08 22:33:46 +02:00
|
|
|
Global instance of [`https.Agent`][] for all HTTPS client requests. Diverges
|
|
|
|
|
from a default [`https.Agent`][] configuration by having `keepAlive` enabled and
|
|
|
|
|
a `timeout` of 5 seconds.
|
2015-11-04 17:59:28 -05:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.request(options[, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-12-24 04:07:33 -08:00
|
|
|
## `https.request(url[, options][, callback])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-06-23 21:51:12 +02:00
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.3.6
|
2017-06-02 18:07:06 +03:00
|
|
|
changes:
|
2024-07-19 15:32:30 +02:00
|
|
|
- version:
|
|
|
|
|
- v22.4.0
|
|
|
|
|
- v20.16.0
|
2024-06-07 17:10:47 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/53329
|
|
|
|
|
description: The `clientCertEngine` option depends on custom engine
|
|
|
|
|
support in OpenSSL which is deprecated in OpenSSL 3.
|
2021-09-04 15:29:35 +02:00
|
|
|
- version:
|
|
|
|
|
- v16.7.0
|
|
|
|
|
- v14.18.0
|
2021-07-08 11:43:26 -04:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/39310
|
|
|
|
|
description: When using a `URL` object parsed username
|
|
|
|
|
and password will now be properly URI decoded.
|
2020-04-28 13:54:04 +02:00
|
|
|
- version:
|
2021-09-04 15:29:35 +02:00
|
|
|
- v14.1.0
|
|
|
|
|
- v13.14.0
|
2020-04-12 02:07:35 +08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/32786
|
|
|
|
|
description: The `highWaterMark` option is accepted now.
|
2018-08-13 19:02:06 +10:00
|
|
|
- version: v10.9.0
|
2018-07-01 11:00:24 -04:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/21616
|
2018-09-19 17:14:37 +02:00
|
|
|
description: The `url` parameter can now be passed along with a separate
|
|
|
|
|
`options` object.
|
2017-12-12 03:09:37 -05:00
|
|
|
- version: v9.3.0
|
2017-12-12 04:18:02 -05:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/14903
|
2017-08-17 13:54:05 -07:00
|
|
|
description: The `options` parameter can now include `clientCertEngine`.
|
2017-06-02 18:07:06 +03:00
|
|
|
- version: v7.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10638
|
|
|
|
|
description: The `options` parameter can be a WHATWG `URL` object.
|
2016-06-23 21:51:12 +02:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-06 21:46:38 -04:00
|
|
|
* `url` {string | URL}
|
2018-07-12 13:48:11 -04:00
|
|
|
* `options` {Object | string | URL} Accepts all `options` from
|
2018-02-12 02:31:55 -05:00
|
|
|
[`http.request()`][], with some differences in default values:
|
2019-09-13 00:22:29 -04:00
|
|
|
* `protocol` **Default:** `'https:'`
|
|
|
|
|
* `port` **Default:** `443`
|
|
|
|
|
* `agent` **Default:** `https.globalAgent`
|
2018-07-12 13:48:11 -04:00
|
|
|
* `callback` {Function}
|
2020-12-03 11:40:58 -08:00
|
|
|
* Returns: {http.ClientRequest}
|
2017-03-03 15:23:48 -08:00
|
|
|
|
2012-08-18 14:18:02 +09:00
|
|
|
Makes a request to a secure web server.
|
|
|
|
|
|
2017-10-16 21:23:29 -07:00
|
|
|
The following additional `options` from [`tls.connect()`][] are also accepted:
|
2024-06-07 17:10:47 +01:00
|
|
|
`ca`, `cert`, `ciphers`, `clientCertEngine` (deprecated), `crl`, `dhparam`, `ecdhCurve`,
|
2017-10-16 21:23:29 -07:00
|
|
|
`honorCipherOrder`, `key`, `passphrase`, `pfx`, `rejectUnauthorized`,
|
2020-04-12 02:07:35 +08:00
|
|
|
`secureOptions`, `secureProtocol`, `servername`, `sessionIdContext`,
|
|
|
|
|
`highWaterMark`.
|
2017-03-03 15:23:48 -08:00
|
|
|
|
2017-06-02 18:07:06 +03:00
|
|
|
`options` can be an object, a string, or a [`URL`][] object. If `options` is a
|
2018-04-24 19:37:43 -05:00
|
|
|
string, it is automatically parsed with [`new URL()`][]. If it is a [`URL`][]
|
2017-06-02 18:07:06 +03:00
|
|
|
object, it will be automatically converted to an ordinary `options` object.
|
2012-08-18 14:18:02 +09:00
|
|
|
|
2020-12-03 11:40:58 -08:00
|
|
|
`https.request()` returns an instance of the [`http.ClientRequest`][]
|
|
|
|
|
class. The `ClientRequest` instance is a writable stream. If one needs to
|
|
|
|
|
upload a file with a POST request, then write to the `ClientRequest` object.
|
|
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
```mjs
|
|
|
|
|
import { request } from 'node:https';
|
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
hostname: 'encrypted.google.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
|
|
|
|
method: 'GET',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const req = request(options, (res) => {
|
|
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
console.log('headers:', res.headers);
|
|
|
|
|
|
|
|
|
|
res.on('data', (d) => {
|
|
|
|
|
process.stdout.write(d);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
});
|
|
|
|
|
req.end();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const https = require('node:https');
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2017-04-03 02:55:01 +03:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
hostname: 'encrypted.google.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
2022-11-17 08:19:12 -05:00
|
|
|
method: 'GET',
|
2016-01-17 18:39:07 +01:00
|
|
|
};
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2017-04-03 02:55:01 +03:00
|
|
|
const req = https.request(options, (res) => {
|
2016-07-26 22:25:08 -04:00
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
console.log('headers:', res.headers);
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
res.on('data', (d) => {
|
|
|
|
|
process.stdout.write(d);
|
|
|
|
|
});
|
|
|
|
|
});
|
2011-01-21 13:12:35 -08:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
req.on('error', (e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
});
|
2016-11-15 12:47:30 +01:00
|
|
|
req.end();
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2019-08-29 09:28:03 -04:00
|
|
|
|
2017-03-03 15:23:48 -08:00
|
|
|
Example using options from [`tls.connect()`][]:
|
2011-09-14 20:17:30 +09:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-03 02:55:01 +03:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
hostname: 'encrypted.google.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
|
|
|
|
method: 'GET',
|
2024-08-19 09:52:40 -04:00
|
|
|
key: fs.readFileSync('private-key.pem'),
|
|
|
|
|
cert: fs.readFileSync('certificate.pem'),
|
2016-01-17 18:39:07 +01:00
|
|
|
};
|
|
|
|
|
options.agent = new https.Agent(options);
|
|
|
|
|
|
2017-04-03 02:55:01 +03:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
|
// ...
|
2016-07-14 22:41:29 -07:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2011-09-14 20:17:30 +09:00
|
|
|
|
2017-10-16 21:23:29 -07:00
|
|
|
Alternatively, opt out of connection pooling by not using an [`Agent`][].
|
2011-09-14 20:17:30 +09:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-04-03 02:55:01 +03:00
|
|
|
const options = {
|
2016-01-17 18:39:07 +01:00
|
|
|
hostname: 'encrypted.google.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
|
|
|
|
method: 'GET',
|
2024-08-19 09:52:40 -04:00
|
|
|
key: fs.readFileSync('private-key.pem'),
|
|
|
|
|
cert: fs.readFileSync('certificate.pem'),
|
2022-11-17 08:19:12 -05:00
|
|
|
agent: false,
|
2016-01-17 18:39:07 +01:00
|
|
|
};
|
|
|
|
|
|
2017-04-03 02:55:01 +03:00
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
|
// ...
|
2016-07-14 22:41:29 -07:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-11-13 19:21:49 -08:00
|
|
|
|
2017-06-02 18:07:06 +03:00
|
|
|
Example using a [`URL`][] as `options`:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const options = new URL('https://abc:xyz@example.com');
|
|
|
|
|
|
|
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-12 02:31:55 -05:00
|
|
|
Example pinning on certificate fingerprint, or the public key (similar to
|
|
|
|
|
`pin-sha256`):
|
2018-02-14 09:35:10 -08:00
|
|
|
|
2024-08-19 09:52:40 -04:00
|
|
|
```mjs
|
|
|
|
|
import { checkServerIdentity } from 'node:tls';
|
|
|
|
|
import { Agent, request } from 'node:https';
|
|
|
|
|
import { createHash } from 'node:crypto';
|
|
|
|
|
|
|
|
|
|
function sha256(s) {
|
|
|
|
|
return createHash('sha256').update(s).digest('base64');
|
|
|
|
|
}
|
|
|
|
|
const options = {
|
|
|
|
|
hostname: 'github.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
|
|
|
|
method: 'GET',
|
|
|
|
|
checkServerIdentity: function(host, cert) {
|
|
|
|
|
// Make sure the certificate is issued to the host we are connected to
|
|
|
|
|
const err = checkServerIdentity(host, cert);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pin the public key, similar to HPKP pin-sha256 pinning
|
|
|
|
|
const pubkey256 = 'SIXvRyDmBJSgatgTQRGbInBaAK+hZOQ18UmrSwnDlK8=';
|
|
|
|
|
if (sha256(cert.pubkey) !== pubkey256) {
|
|
|
|
|
const msg = 'Certificate verification error: ' +
|
|
|
|
|
`The public key of '${cert.subject.CN}' ` +
|
|
|
|
|
'does not match our pinned fingerprint';
|
|
|
|
|
return new Error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pin the exact certificate, rather than the pub key
|
|
|
|
|
const cert256 = 'FD:6E:9B:0E:F3:98:BC:D9:04:C3:B2:EC:16:7A:7B:' +
|
|
|
|
|
'0F:DA:72:01:C9:03:C5:3A:6A:6A:E5:D0:41:43:63:EF:65';
|
|
|
|
|
if (cert.fingerprint256 !== cert256) {
|
|
|
|
|
const msg = 'Certificate verification error: ' +
|
|
|
|
|
`The certificate of '${cert.subject.CN}' ` +
|
|
|
|
|
'does not match our pinned fingerprint';
|
|
|
|
|
return new Error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This loop is informational only.
|
|
|
|
|
// Print the certificate and public key fingerprints of all certs in the
|
|
|
|
|
// chain. Its common to pin the public key of the issuer on the public
|
|
|
|
|
// internet, while pinning the public key of the service in sensitive
|
|
|
|
|
// environments.
|
|
|
|
|
let lastprint256;
|
|
|
|
|
do {
|
|
|
|
|
console.log('Subject Common Name:', cert.subject.CN);
|
|
|
|
|
console.log(' Certificate SHA256 fingerprint:', cert.fingerprint256);
|
|
|
|
|
|
|
|
|
|
const hash = createHash('sha256');
|
|
|
|
|
console.log(' Public key ping-sha256:', sha256(cert.pubkey));
|
|
|
|
|
|
|
|
|
|
lastprint256 = cert.fingerprint256;
|
|
|
|
|
cert = cert.issuerCertificate;
|
|
|
|
|
} while (cert.fingerprint256 !== lastprint256);
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.agent = new Agent(options);
|
|
|
|
|
const req = request(options, (res) => {
|
|
|
|
|
console.log('All OK. Server matched our pinned cert or public key');
|
|
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
|
|
|
|
|
res.on('data', (d) => {});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
|
console.error(e.message);
|
|
|
|
|
});
|
|
|
|
|
req.end();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const tls = require('node:tls');
|
|
|
|
|
const https = require('node:https');
|
|
|
|
|
const crypto = require('node:crypto');
|
2018-02-14 09:35:10 -08:00
|
|
|
|
|
|
|
|
function sha256(s) {
|
|
|
|
|
return crypto.createHash('sha256').update(s).digest('base64');
|
|
|
|
|
}
|
|
|
|
|
const options = {
|
|
|
|
|
hostname: 'github.com',
|
|
|
|
|
port: 443,
|
|
|
|
|
path: '/',
|
|
|
|
|
method: 'GET',
|
|
|
|
|
checkServerIdentity: function(host, cert) {
|
|
|
|
|
// Make sure the certificate is issued to the host we are connected to
|
|
|
|
|
const err = tls.checkServerIdentity(host, cert);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 17:27:17 +08:00
|
|
|
// Pin the public key, similar to HPKP pin-sha256 pinning
|
2024-08-19 09:52:40 -04:00
|
|
|
const pubkey256 = 'SIXvRyDmBJSgatgTQRGbInBaAK+hZOQ18UmrSwnDlK8=';
|
2018-02-14 09:35:10 -08:00
|
|
|
if (sha256(cert.pubkey) !== pubkey256) {
|
|
|
|
|
const msg = 'Certificate verification error: ' +
|
|
|
|
|
`The public key of '${cert.subject.CN}' ` +
|
|
|
|
|
'does not match our pinned fingerprint';
|
|
|
|
|
return new Error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 22:36:11 +08:00
|
|
|
// Pin the exact certificate, rather than the pub key
|
2024-08-19 09:52:40 -04:00
|
|
|
const cert256 = 'FD:6E:9B:0E:F3:98:BC:D9:04:C3:B2:EC:16:7A:7B:' +
|
|
|
|
|
'0F:DA:72:01:C9:03:C5:3A:6A:6A:E5:D0:41:43:63:EF:65';
|
2018-02-14 09:35:10 -08:00
|
|
|
if (cert.fingerprint256 !== cert256) {
|
|
|
|
|
const msg = 'Certificate verification error: ' +
|
|
|
|
|
`The certificate of '${cert.subject.CN}' ` +
|
|
|
|
|
'does not match our pinned fingerprint';
|
|
|
|
|
return new Error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This loop is informational only.
|
|
|
|
|
// Print the certificate and public key fingerprints of all certs in the
|
|
|
|
|
// chain. Its common to pin the public key of the issuer on the public
|
|
|
|
|
// internet, while pinning the public key of the service in sensitive
|
|
|
|
|
// environments.
|
|
|
|
|
do {
|
|
|
|
|
console.log('Subject Common Name:', cert.subject.CN);
|
|
|
|
|
console.log(' Certificate SHA256 fingerprint:', cert.fingerprint256);
|
|
|
|
|
|
|
|
|
|
hash = crypto.createHash('sha256');
|
|
|
|
|
console.log(' Public key ping-sha256:', sha256(cert.pubkey));
|
|
|
|
|
|
|
|
|
|
lastprint256 = cert.fingerprint256;
|
|
|
|
|
cert = cert.issuerCertificate;
|
|
|
|
|
} while (cert.fingerprint256 !== lastprint256);
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.agent = new https.Agent(options);
|
|
|
|
|
const req = https.request(options, (res) => {
|
|
|
|
|
console.log('All OK. Server matched our pinned cert or public key');
|
|
|
|
|
console.log('statusCode:', res.statusCode);
|
|
|
|
|
|
|
|
|
|
res.on('data', (d) => {});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('error', (e) => {
|
|
|
|
|
console.error(e.message);
|
|
|
|
|
});
|
|
|
|
|
req.end();
|
|
|
|
|
```
|
2018-04-29 12:49:56 +03:00
|
|
|
|
|
|
|
|
Outputs for example:
|
|
|
|
|
|
2018-02-14 09:35:10 -08:00
|
|
|
```text
|
|
|
|
|
Subject Common Name: github.com
|
2024-08-19 09:52:40 -04:00
|
|
|
Certificate SHA256 fingerprint: FD:6E:9B:0E:F3:98:BC:D9:04:C3:B2:EC:16:7A:7B:0F:DA:72:01:C9:03:C5:3A:6A:6A:E5:D0:41:43:63:EF:65
|
|
|
|
|
Public key ping-sha256: SIXvRyDmBJSgatgTQRGbInBaAK+hZOQ18UmrSwnDlK8=
|
|
|
|
|
Subject Common Name: Sectigo ECC Domain Validation Secure Server CA
|
|
|
|
|
Certificate SHA256 fingerprint: 61:E9:73:75:E9:F6:DA:98:2F:F5:C1:9E:2F:94:E6:6C:4E:35:B6:83:7C:E3:B9:14:D2:24:5C:7F:5F:65:82:5F
|
|
|
|
|
Public key ping-sha256: Eep0p/AsSa9lFUH6KT2UY+9s1Z8v7voAPkQ4fGknZ2g=
|
|
|
|
|
Subject Common Name: USERTrust ECC Certification Authority
|
|
|
|
|
Certificate SHA256 fingerprint: A6:CF:64:DB:B4:C8:D5:FD:19:CE:48:89:60:68:DB:03:B5:33:A8:D1:33:6C:62:56:A8:7D:00:CB:B3:DE:F3:EA
|
|
|
|
|
Public key ping-sha256: UJM2FOhG9aTNY0Pg4hgqjNzZ/lQBiMGRxPD5Y2/e0bw=
|
|
|
|
|
Subject Common Name: AAA Certificate Services
|
|
|
|
|
Certificate SHA256 fingerprint: D7:A7:A0:FB:5D:7E:27:31:D7:71:E9:48:4E:BC:DE:F7:1D:5F:0C:3E:0A:29:48:78:2B:C8:3E:E0:EA:69:9E:F4
|
|
|
|
|
Public key ping-sha256: vRU+17BDT2iGsXvOi76E7TQMcTLXAqj0+jGPdW7L1vM=
|
2018-02-14 09:35:10 -08:00
|
|
|
All OK. Server matched our pinned cert or public key
|
|
|
|
|
statusCode: 200
|
|
|
|
|
```
|
|
|
|
|
|
2021-07-04 20:39:17 -07:00
|
|
|
[`Agent`]: #class-httpsagent
|
|
|
|
|
[`Session Resumption`]: tls.md#session-resumption
|
|
|
|
|
[`URL`]: url.md#the-whatwg-url-api
|
2025-11-08 18:09:29 +01:00
|
|
|
[`agent.createConnection()`]: http.md#agentcreateconnectionoptions-callback
|
2021-07-04 20:39:17 -07:00
|
|
|
[`http.Agent(options)`]: http.md#new-agentoptions
|
|
|
|
|
[`http.Agent`]: http.md#class-httpagent
|
|
|
|
|
[`http.ClientRequest`]: http.md#class-httpclientrequest
|
|
|
|
|
[`http.Server`]: http.md#class-httpserver
|
|
|
|
|
[`http.createServer()`]: http.md#httpcreateserveroptions-requestlistener
|
|
|
|
|
[`http.get()`]: http.md#httpgetoptions-callback
|
|
|
|
|
[`http.request()`]: http.md#httprequestoptions-callback
|
|
|
|
|
[`https.Agent`]: #class-httpsagent
|
|
|
|
|
[`https.request()`]: #httpsrequestoptions-callback
|
2022-07-15 16:44:58 +02:00
|
|
|
[`import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
|
2021-07-04 20:39:17 -07:00
|
|
|
[`net.Server`]: net.md#class-netserver
|
|
|
|
|
[`new URL()`]: url.md#new-urlinput-base
|
2022-05-11 01:04:13 +02:00
|
|
|
[`server.close()`]: http.md#serverclosecallback
|
|
|
|
|
[`server.closeAllConnections()`]: http.md#servercloseallconnections
|
|
|
|
|
[`server.closeIdleConnections()`]: http.md#servercloseidleconnections
|
|
|
|
|
[`server.headersTimeout`]: http.md#serverheaderstimeout
|
|
|
|
|
[`server.keepAliveTimeout`]: http.md#serverkeepalivetimeout
|
2021-07-04 20:39:17 -07:00
|
|
|
[`server.listen()`]: net.md#serverlisten
|
2022-05-11 01:04:13 +02:00
|
|
|
[`server.maxHeadersCount`]: http.md#servermaxheaderscount
|
|
|
|
|
[`server.requestTimeout`]: http.md#serverrequesttimeout
|
|
|
|
|
[`server.setTimeout()`]: http.md#serversettimeoutmsecs-callback
|
|
|
|
|
[`server.timeout`]: http.md#servertimeout
|
2021-07-04 20:39:17 -07:00
|
|
|
[`tls.connect()`]: tls.md#tlsconnectoptions-callback
|
|
|
|
|
[`tls.createSecureContext()`]: tls.md#tlscreatesecurecontextoptions
|
|
|
|
|
[`tls.createServer()`]: tls.md#tlscreateserveroptions-secureconnectionlistener
|
2023-06-25 23:29:58 +03:00
|
|
|
[httpsServerClose]: #serverclosecallback
|
2019-04-19 15:51:24 -04:00
|
|
|
[sni wiki]: https://en.wikipedia.org/wiki/Server_Name_Indication
|