Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15930c1

Browse files
committed
tls: fix convertALPNProtocols accepting ArrayBufferViews
1 parent 331088f commit 15930c1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/tls.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ const {
5454
} = require('internal/errors').codes;
5555
const internalUtil = require('internal/util');
5656
internalUtil.assertCrypto();
57-
const { isArrayBufferView } = require('internal/util/types');
57+
const {
58+
isArrayBufferView,
59+
isUint8Array,
60+
} = require('internal/util/types');
5861

5962
const net = require('net');
6063
const { getOptionValue } = require('internal/options');
@@ -143,9 +146,13 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
143146
// If protocols is Array - translate it into buffer
144147
if (ArrayIsArray(protocols)) {
145148
out.ALPNProtocols = convertProtocols(protocols);
146-
} else if (isArrayBufferView(protocols)) {
149+
} else if (Buffer.isBuffer(protocols) || isUint8Array(protocols)) {
147150
// Copy new buffer not to be modified by user.
148151
out.ALPNProtocols = Buffer.from(protocols);
152+
} else if (isArrayBufferView(protocols)) {
153+
out.ALPNProtocols = Buffer.from(protocols.buffer.slice(),
154+
protocols.byteOffset,
155+
protocols.byteLength);
149156
}
150157
};
151158

test/parallel/test-tls-basic-validations.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,16 @@ assert.throws(
103103
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8');
104104
for (const expectView of common.getArrayBufferViews(inputBuffer)) {
105105
const out = {};
106+
let expected;
107+
if (expectView.constructor === Uint8Array) {
108+
expected = Buffer.from(expectView);
109+
} else {
110+
expected = Buffer.from(expectView.buffer.slice(),
111+
expectView.byteOffset,
112+
expectView.byteLength);
113+
}
106114
tls.convertALPNProtocols(expectView, out);
107-
assert(out.ALPNProtocols.equals(Buffer.from(expectView)));
115+
assert(out.ALPNProtocols.equals(expected));
108116
}
109117
}
110118

0 commit comments

Comments
 (0)