From 15a682cc74eb66faf6775218dc3f54187eedd97b Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Thu, 26 May 2022 15:02:28 +0800 Subject: [PATCH] tls: fix convertALPNProtocols accepting ArrayBufferViews PR-URL: https://github.com/nodejs/node/pull/43211 Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- lib/tls.js | 15 +++++++++++++-- test/parallel/test-tls-basic-validations.js | 5 ++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index de20505fde242b..17ad9fb24a0898 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -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); } }; diff --git a/test/parallel/test-tls-basic-validations.js b/test/parallel/test-tls-basic-validations.js index 67058daf158cd0..4a3aab314680ac 100644 --- a/test/parallel/test-tls-basic-validations.js +++ b/test/parallel/test-tls-basic-validations.js @@ -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)); } }