Skip to content

Commit

Permalink
http: throw if 'host' agent header is not a string value
Browse files Browse the repository at this point in the history
If the 'host' agent header is an array or other non-string value, throw.

PR-URL: #29568
Fixes: #29408
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
gntem authored and Trott committed Sep 18, 2019
1 parent a0c6cf8 commit 2daf883
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const net = require('net');
const EventEmitter = require('events');
const debug = require('internal/util/debuglog').debuglog('http');
const { async_id_symbol } = require('internal/async_hooks').symbols;

const {
codes: {
ERR_INVALID_ARG_TYPE,
},
} = require('internal/errors');
// New Agent code.

// The largest departure from the previous implementation is that
Expand Down Expand Up @@ -240,6 +244,11 @@ function calculateServerName(options, req) {
let servername = options.host;
const hostHeader = req.getHeader('host');
if (hostHeader) {
if (typeof hostHeader !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.headers.host',
'String', hostHeader);
}

// abc => abc
// abc:123 => abc
// [::1] => ::1
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-http-client-headers-host-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

require('../common');

const assert = require('assert');
const http = require('http');

{

const options = {
port: '80',
path: '/',
headers: {
host: []
}
};

assert.throws(() => {
http.request(options);
}, {
code: /ERR_INVALID_ARG_TYPE/
}, 'http request should throw when passing array as header host');
}

0 comments on commit 2daf883

Please sign in to comment.