Skip to content

Commit

Permalink
tls: fix convertALPNProtocols accepting ArrayBufferViews
Browse files Browse the repository at this point in the history
  • Loading branch information
LiviaMedeiros committed May 26, 2022
1 parent a346572 commit 403e1e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const {
} = require('internal/errors').codes;
const internalUtil = require('internal/util');
internalUtil.assertCrypto();
const { isArrayBufferView } = require('internal/util/types');
const {
isArrayBufferView,
isUint8Array,
} = require('internal/util/types');

const net = require('net');
const { getOptionValue } = require('internal/options');
Expand Down Expand Up @@ -143,9 +146,13 @@ 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 (isArrayBufferView(protocols)) {
out.ALPNProtocols = Buffer.from(protocols.buffer.slice(),
protocols.byteOffset,
protocols.byteLength);
}
};

Expand Down
10 changes: 9 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,16 @@ assert.throws(
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
const out = {};
let expected;
if (expectView.constructor === Uint8Array) {
expected = Buffer.from(expectView);
} else {
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 403e1e3

Please sign in to comment.