Skip to content

Commit df39784

Browse files
lucamaraschicjihrig
authored andcommitted
http: verify client method is a string
Prior to this commit, it was possible to pass a truthy non-string value as the HTTP method to the HTTP client, resulting in an exception being thrown. This commit adds validation to the method. PR-URL: #10111 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6967ed4 commit df39784

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/_http_client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ function ClientRequest(options, cb) {
6868
self.socketPath = options.socketPath;
6969
self.timeout = options.timeout;
7070

71-
var method = self.method = (options.method || 'GET').toUpperCase();
71+
var method = options.method;
72+
if (method != null && typeof method !== 'string') {
73+
throw new TypeError('Method must be a string');
74+
}
75+
method = self.method = (method || 'GET').toUpperCase();
7276
if (!common._checkIsHttpToken(method)) {
7377
throw new TypeError('Method must be a valid HTTP token');
7478
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const expectedSuccesses = [undefined, null, 'GET', 'post'];
7+
let requestCount = 0;
8+
9+
const server = http.createServer((req, res) => {
10+
requestCount++;
11+
res.end();
12+
13+
if (expectedSuccesses.length === requestCount) {
14+
server.close();
15+
}
16+
}).listen(0, test);
17+
18+
function test() {
19+
function fail(input) {
20+
assert.throws(() => {
21+
http.request({ method: input, path: '/' }, common.fail);
22+
}, /^TypeError: Method must be a string$/);
23+
}
24+
25+
fail(-1);
26+
fail(1);
27+
fail(0);
28+
fail({});
29+
fail(true);
30+
fail(false);
31+
fail([]);
32+
33+
function ok(method) {
34+
http.request({ method: method, port: server.address().port }).end();
35+
}
36+
37+
expectedSuccesses.forEach((method) => {
38+
ok(method);
39+
});
40+
}

0 commit comments

Comments
 (0)