Skip to content

Commit b2dd7ec

Browse files
committed
tls: make Server.getTicketKeys throw instead of assert
assert is for invariants and print a message about the condition being a bug in Node.js if false. throw ERR_INVALID_ARG_VALUE for publicly facing validation.
1 parent ae0aaec commit b2dd7ec

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

lib/internal/tls/wrap.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,8 +1530,9 @@ Server.prototype.getTicketKeys = function getTicketKeys() {
15301530

15311531
Server.prototype.setTicketKeys = function setTicketKeys(keys) {
15321532
validateBuffer(keys);
1533-
assert(keys.byteLength === 48,
1534-
'Session ticket keys must be a 48-byte buffer');
1533+
if (keys.byteLength !== 48) {
1534+
throw new ERR_INVALID_ARG_VALUE('keys', keys.byteLength, 'must be exactly 48 bytes');
1535+
}
15351536
this._sharedCreds.context.setTicketKeys(keys);
15361537
};
15371538

test/parallel/test-tls-ticket-invalid-arg.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@ const server = new tls.Server();
1313
.forEach((arg) =>
1414
assert.throws(
1515
() => server.setTicketKeys(arg),
16-
{ code: 'ERR_INVALID_ARG_TYPE' }
16+
{
17+
name: 'TypeError',
18+
code: 'ERR_INVALID_ARG_TYPE',
19+
message: 'The "buffer" argument must be an instance of Buffer, TypedArray, or DataView.' +
20+
common.invalidArgTypeHelper(arg),
21+
}
1722
));
1823

1924
[new Uint8Array(1), Buffer.from([1]), new DataView(new ArrayBuffer(2))].forEach(
2025
(arg) =>
21-
assert.throws(() => {
22-
server.setTicketKeys(arg);
23-
}, /Session ticket keys must be a 48-byte buffer/)
26+
assert.throws(
27+
() => server.setTicketKeys(arg),
28+
{
29+
name: 'TypeError',
30+
code: 'ERR_INVALID_ARG_VALUE',
31+
message: `The argument 'keys' must be exactly 48 bytes. Received ${arg.byteLength}`,
32+
}
33+
)
2434
);

0 commit comments

Comments
 (0)