From d6969a717f7368b7faae97140dccede81f345b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Thu, 1 Jun 2017 22:03:38 +0300 Subject: [PATCH] http: use Buffer.from to avoid Buffer(num) call This fixes a potential Buffer(num) call when the user passes a number as the 'auth' property. This now throws instead of allocating an unitialized memory Buffer and sending that in the Authorization header. Fixes: https://github.com/nodejs/security/issues/111 PR-URL: https://github.com/nodejs/node-private/pull/83 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Sakthipriyan Vairamani --- lib/_http_client.js | 2 +- test/parallel/test-http-auth-number.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-auth-number.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 2002d2bc8ea..b4057a6e507 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -102,7 +102,7 @@ function ClientRequest(options, cb) { if (options.auth && !this.getHeader('Authorization')) { //basic auth this.setHeader('Authorization', 'Basic ' + - new Buffer(options.auth).toString('base64')); + Buffer.from(options.auth).toString('base64')); } if (method === 'GET' || diff --git a/test/parallel/test-http-auth-number.js b/test/parallel/test-http-auth-number.js new file mode 100644 index 00000000000..f2d76482947 --- /dev/null +++ b/test/parallel/test-http-auth-number.js @@ -0,0 +1,13 @@ +'use strict'; + +require('../common'); +const http = require('http'); +const url = require('url'); +const assert = require('assert'); + +const opts = url.parse('http://127.0.0.1:8180'); +opts.auth = 100; + +assert.throws(() => { + http.get(opts); +}, /^TypeError: "value" argument must not be a number$/);