Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 38149bb

Browse files
bnoordhuisisaacs
authored andcommitted
http: escape unsafe characters in request path
Make http.request() and friends escape unsafe characters in the request path. That is, a request for '/foo bar' is now escaped as '/foo%20bar'. Before this commit, the path was used as-is in the request status line, creating an invalid HTTP request ("GET /foo bar HTTP/1.1"). Fixes #4381.
1 parent 881ef7c commit 38149bb

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/http.js

+5
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,11 @@ ClientRequest.prototype.clearTimeout = function(cb) {
17741774
exports.request = function(options, cb) {
17751775
if (typeof options === 'string') {
17761776
options = url.parse(options);
1777+
} else if (options && options.path) {
1778+
options = util._extend({}, options);
1779+
options.path = encodeURI(options.path);
1780+
// encodeURI() doesn't escape quotes while url.parse() does. Fix up.
1781+
options.path = options.path.replace(/'/g, '%27');
17771782
}
17781783

17791784
if (options.protocol && options.protocol !== 'http:') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var http = require('http');
25+
26+
first();
27+
28+
function first() {
29+
test('/~username/', '/~username/', second);
30+
}
31+
function second() {
32+
test('/\'foo bar\'', '/%27foo%20bar%27', third);
33+
}
34+
function third() {
35+
var expected = '/%3C%3E%22%60%20%0D%0A%09%7B%7D%7C%5C%5E~%60%27';
36+
test('/<>"` \r\n\t{}|\\^~`\'', expected);
37+
}
38+
39+
function test(path, expected, next) {
40+
var server = http.createServer(function(req, res) {
41+
assert.equal(req.url, expected);
42+
res.end('OK');
43+
server.close(function() {
44+
if (next) next();
45+
});
46+
});
47+
server.on('clientError', function(err) {
48+
throw err;
49+
});
50+
var options = {
51+
host: '127.0.0.1',
52+
port: common.PORT,
53+
path: path
54+
};
55+
server.listen(options.port, options.host, function() {
56+
http.get(options);
57+
});
58+
}

0 commit comments

Comments
 (0)