mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
tls.connect() with malformed ALPNProtocols hit CHECK_EQ(0, SSL_set_alpn_protos(...)) in the C++ layer and aborted the process with SIGABRT. Validate in JS instead, in convertALPNProtocols, so both client and server fail early with a recoverable ERR_INVALID_ARG_VALUE: - zero-length string protocols now throw from convertProtocols - wire-format buffers are checked for zero-length and truncated entries - an empty buffer or array is still accepted and means skip ALPN, matching the historical behavior for [] The C++ CHECK_EQ is left unchanged: once JS has validated the input, a non-zero SSL_set_alpn_protos return is an internal invariant failure rather than user-facing input. Fixes: https://github.com/nodejs/node/issues/65069 Signed-off-by: Sankalp Thakur <sankalphimself@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65076 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const tls = require('tls');
|
|
|
|
assert.throws(
|
|
() => tls.createSecureContext({ ciphers: 1 }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.ciphers" property must be of type string.' +
|
|
' Received type number (1)'
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ ciphers: 1 }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.ciphers" property must be of type string.' +
|
|
' Received type number (1)'
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createSecureContext({ key: 'dummykey', passphrase: 1 }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /The "options\.passphrase" property must be of type string/
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ key: 'dummykey', passphrase: 1 }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /The "options\.passphrase" property must be of type string/
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ ecdhCurve: 1 }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /The "options\.ecdhCurve" property must be of type string/
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ handshakeTimeout: 'abcd' }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.handshakeTimeout" property must be of type number.' +
|
|
" Received type string ('abcd')"
|
|
}
|
|
);
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ sessionTimeout: 'abcd' }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /The "options\.sessionTimeout" property must be of type number/
|
|
});
|
|
|
|
assert.throws(
|
|
() => tls.createServer({ ticketKeys: 'abcd' }),
|
|
{
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: /The "options\.ticketKeys" property must be an instance of/
|
|
});
|
|
|
|
assert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }), {
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
message: /The property 'options\.ticketKeys' must be exactly 48 bytes/
|
|
});
|
|
|
|
{
|
|
const buffer = Buffer.from([3, 0x61, 0x62, 0x63]);
|
|
const out = {};
|
|
tls.convertALPNProtocols(buffer, out);
|
|
out.ALPNProtocols.write('def', 1);
|
|
assert(buffer.equals(Buffer.from([3, 0x61, 0x62, 0x63])));
|
|
assert(out.ALPNProtocols.equals(Buffer.from([3, 0x64, 0x65, 0x66])));
|
|
}
|
|
|
|
{
|
|
const inputBuffer = Buffer.concat([
|
|
Buffer.from([31]),
|
|
Buffer.alloc(31, 0x61),
|
|
]);
|
|
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
|
|
const out = {};
|
|
const expected = Buffer.from(expectView.buffer.slice(),
|
|
expectView.byteOffset,
|
|
expectView.byteLength);
|
|
tls.convertALPNProtocols(expectView, out);
|
|
assert(out.ALPNProtocols.equals(expected));
|
|
}
|
|
}
|
|
|
|
{
|
|
const protocols = [(new String('a')).repeat(500)];
|
|
const out = {};
|
|
assert.throws(
|
|
() => tls.convertALPNProtocols(protocols, out),
|
|
{
|
|
code: 'ERR_OUT_OF_RANGE',
|
|
message: 'The byte length of the protocol at index 0 exceeds the ' +
|
|
'maximum length. It must be <= 255. Received 500'
|
|
}
|
|
);
|
|
}
|
|
|
|
assert.throws(() => { tls.createSecureContext({ minVersion: 'fhqwhgads' }); },
|
|
{
|
|
code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
|
|
name: 'TypeError'
|
|
});
|
|
|
|
assert.throws(() => { tls.createSecureContext({ maxVersion: 'fhqwhgads' }); },
|
|
{
|
|
code: 'ERR_TLS_INVALID_PROTOCOL_VERSION',
|
|
name: 'TypeError'
|
|
});
|
|
|
|
for (const checkServerIdentity of [undefined, null, 1, true]) {
|
|
assert.throws(() => {
|
|
tls.connect({ checkServerIdentity });
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
});
|
|
}
|