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 <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
Сковорода Никита Андреевич authored and Myles Borins committed 2017-07-10 10:59:41 +01:00
1 parent 681cebb982
commit d6969a717f
2 files changed
+14 -1

No files matched your search

+1 -1
View File
@@ -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' ||
+13
View File
@@ -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$/);