Skip to content

Commit 7d5ab8b

Browse files
Trotttargos
authored andcommitted
tls: introduce ERR_TLS_INVALID_CONTEXT
It is trivially possible to cause an internal assertion error with tls.createSecurePair(). Throw a friendly error instead. Reserve internal assertions for things that we believe to be impossible. PR-URL: #30718 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 3f1ca18 commit 7d5ab8b

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

doc/api/errors.md

+8
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,14 @@ recommended to use 2048 bits or larger for stronger security.
18011801
A TLS/SSL handshake timed out. In this case, the server must also abort the
18021802
connection.
18031803

1804+
<a id="ERR_TLS_INVALID_CONTEXT">
1805+
### ERR_TLS_INVALID_CONTEXT
1806+
<!-- YAML
1807+
added: REPLACEME
1808+
-->
1809+
1810+
The context must be a `SecureContext`.
1811+
18041812
<a id="ERR_TLS_INVALID_PROTOCOL_METHOD"></a>
18051813
### `ERR_TLS_INVALID_PROTOCOL_METHOD`
18061814

lib/_tls_wrap.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
ERR_SOCKET_CLOSED,
5353
ERR_TLS_DH_PARAM_SIZE,
5454
ERR_TLS_HANDSHAKE_TIMEOUT,
55+
ERR_TLS_INVALID_CONTEXT,
5556
ERR_TLS_RENEGOTIATION_DISABLED,
5657
ERR_TLS_REQUIRED_SERVER_NAME,
5758
ERR_TLS_SESSION_ATTACK,
@@ -513,8 +514,9 @@ TLSSocket.prototype._wrapHandle = function(wrap) {
513514
options.credentials ||
514515
tls.createSecureContext(options);
515516
assert(handle.isStreamBase, 'handle must be a StreamBase');
516-
assert(context.context instanceof NativeSecureContext,
517-
'context.context must be a NativeSecureContext');
517+
if (!(context.context instanceof NativeSecureContext)) {
518+
throw new ERR_TLS_INVALID_CONTEXT('context');
519+
}
518520
const res = tls_wrap.wrap(handle, context.context, !!options.isServer);
519521
res._parent = handle; // C++ "wrap" object: TCPWrap, JSStream, ...
520522
res._parentWrap = wrap; // JS object: net.Socket, JSStreamSocket, ...

lib/internal/errors.js

+1
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ E('ERR_TLS_CERT_ALTNAME_INVALID', function(reason, host, cert) {
11741174
}, Error);
11751175
E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error);
11761176
E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout', Error);
1177+
E('ERR_TLS_INVALID_CONTEXT', '%s must be a SecureContext', TypeError),
11771178
E('ERR_TLS_INVALID_PROTOCOL_VERSION',
11781179
'%j is not a valid %s TLS protocol version', TypeError);
11791180
E('ERR_TLS_PROTOCOL_VERSION_CONFLICT',

test/parallel/test-tls-basic-validations.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ common.expectsError(
7878
assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }),
7979
/TypeError: Ticket keys length must be 48 bytes/);
8080

81-
common.expectsInternalAssertion(
81+
assert.throws(
8282
() => tls.createSecurePair({}),
83-
'context.context must be a NativeSecureContext'
83+
{
84+
message: 'context must be a SecureContext',
85+
code: 'ERR_TLS_INVALID_CONTEXT',
86+
name: 'TypeError',
87+
}
8488
);
8589

8690
{

0 commit comments

Comments
 (0)