Skip to content

Commit

Permalink
tls: fix convertALPNProtocols accepting ArrayBufferViews
Browse files Browse the repository at this point in the history
PR-URL: #43211
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
LiviaMedeiros committed May 29, 2022
1 parent df56644 commit 15a682c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ const {
} = require('internal/errors').codes;
const internalUtil = require('internal/util');
internalUtil.assertCrypto();
const { isArrayBufferView } = require('internal/util/types');
const {
isArrayBufferView,
isDataView,
isUint8Array,
} = require('internal/util/types');

const net = require('net');
const { getOptionValue } = require('internal/options');
Expand Down Expand Up @@ -143,9 +147,16 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
// If protocols is Array - translate it into buffer
if (ArrayIsArray(protocols)) {
out.ALPNProtocols = convertProtocols(protocols);
} else if (isArrayBufferView(protocols)) {
} else if (Buffer.isBuffer(protocols) || isUint8Array(protocols)) {
// Copy new buffer not to be modified by user.
out.ALPNProtocols = Buffer.from(protocols);
} else if (isDataView(protocols)) {
out.ALPNProtocols = Buffer.from(protocols.buffer.slice(
protocols.byteOffset,
protocols.byteOffset + protocols.byteLength
));
} else if (isArrayBufferView(protocols)) {
out.ALPNProtocols = Buffer.from(protocols.slice().buffer);
}
};

Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-tls-basic-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ assert.throws(
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
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(Buffer.from(expectView)));
assert(out.ALPNProtocols.equals(expected));
}
}

Expand Down

0 comments on commit 15a682c

Please sign in to comment.