tls: fix convertALPNProtocols accepting ArrayBufferViews

PR-URL: https://github.com/nodejs/node/pull/43211
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
LiviaMedeiros 2022-05-26 15:02:28 +08:00
parent df5664440e
commit 15a682cc74
No known key found for this signature in database
GPG Key ID: 6E5412F8214FF24A
2 changed files with 17 additions and 3 deletions

View File

@ -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');
@ -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);
}
};

View File

@ -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));
}
}