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‎

Lines changed: 5 additions & 1 deletion
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-
varmethod=self.method=(options.method||'GET').toUpperCase();
71+
varmethod=options.method;
72+
if(method!=null&&typeofmethod!=='string'){
73+
thrownewTypeError('Method must be a string');
74+
}
75+
method=self.method=(method||'GET').toUpperCase();
7276
if(!common._checkIsHttpToken(method)){
7377
thrownewTypeError('Method must be a valid HTTP token');
7478
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
consthttp=require('http');
5+
6+
constexpectedSuccesses=[undefined,null,'GET','post'];
7+
letrequestCount=0;
8+
9+
constserver=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+
functiontest(){
19+
functionfail(input){
20+
assert.throws(()=>{
21+
http.request({method: input,path: '/'},common.fail);
22+
},/^TypeError:Methodmustbeastring$/);
23+
}
24+
25+
fail(-1);
26+
fail(1);
27+
fail(0);
28+
fail({});
29+
fail(true);
30+
fail(false);
31+
fail([]);
32+
33+
functionok(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)