diff options
-rw-r--r-- | lib/tls.js | 17 | ||||
-rw-r--r-- | test/parallel/test-tls-basic-validations.js | 18 |
2 files changed, 25 insertions, 10 deletions
diff --git a/lib/tls.js b/lib/tls.js index 849daeb07f3..765e1658ea9 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -49,22 +49,19 @@ function convertProtocols(protocols) { exports.convertNPNProtocols = function(protocols, out) { // If protocols is Array - translate it into buffer if (Array.isArray(protocols)) { - protocols = convertProtocols(protocols); - } - // If it's already a Buffer - store it - if (protocols instanceof Buffer) { - out.NPNProtocols = protocols; + out.NPNProtocols = convertProtocols(protocols); + } else if (protocols instanceof Buffer) { + // Copy new buffer not to be modified by user. + out.NPNProtocols = Buffer.from(protocols); } }; exports.convertALPNProtocols = function(protocols, out) { // If protocols is Array - translate it into buffer if (Array.isArray(protocols)) { - protocols = convertProtocols(protocols); - } - // If it's already a Buffer - store it - if (protocols instanceof Buffer) { - // copy new buffer not to be modified by user + out.ALPNProtocols = convertProtocols(protocols); + } else if (protocols instanceof Buffer) { + // Copy new buffer not to be modified by user. out.ALPNProtocols = Buffer.from(protocols); } }; diff --git a/test/parallel/test-tls-basic-validations.js b/test/parallel/test-tls-basic-validations.js index fa0165b3706..3454da571df 100644 --- a/test/parallel/test-tls-basic-validations.js +++ b/test/parallel/test-tls-basic-validations.js @@ -38,3 +38,21 @@ assert.throws(() => tls.createServer({ticketKeys: new Buffer(0)}), assert.throws(() => tls.createSecurePair({}), /Error: First argument must be a tls module SecureContext/); + +{ + const buffer = Buffer.from('abcd'); + const out = {}; + tls.convertALPNProtocols(buffer, out); + out.ALPNProtocols.write('efgh'); + assert(buffer.equals(Buffer.from('abcd'))); + assert(out.ALPNProtocols.equals(Buffer.from('efgh'))); +} + +{ + const buffer = Buffer.from('abcd'); + const out = {}; + tls.convertNPNProtocols(buffer, out); + out.NPNProtocols.write('efgh'); + assert(buffer.equals(Buffer.from('abcd'))); + assert(out.NPNProtocols.equals(Buffer.from('efgh'))); +} |