From a67ada7d32b18029dc324e37e87065887fbd188d Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Thu, 11 Aug 2016 00:46:06 +0530 Subject: [PATCH] tls: copy the Buffer object before using cherry-pick c26b9af1e277ef94b9bb33d93e1779b0a35f41e2 from v6-staging. `convertNPNProtocols` and `convertALPNProtocols' uses the `protocols` buffer object as it is, and if it is modified outside of core, it might have an impact. This patch makes a copy of the buffer object, before using it. PR-URL: https://github.com/nodejs/node/pull/8055 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Franziska Hinkelmann --- lib/tls.js | 17 +++++++---------- test/parallel/test-tls-basic-validations.js | 9 +++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index 9ac691431d..ab3dca4aab 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -51,22 +51,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 c3ebabc9e6..9a661494b2 100644 --- a/test/parallel/test-tls-basic-validations.js +++ b/test/parallel/test-tls-basic-validations.js @@ -17,3 +17,12 @@ const tls = require('tls'); assert(buffer.equals(Buffer.from('abcd'))); assert(out.NPNProtocols.equals(Buffer.from('efgh'))); } + +{ + 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'))); +}