Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,22 @@ Agent.prototype.removeSocket = function(s, name, host, port, localAddress) {
var globalAgent = new Agent();
exports.globalAgent = globalAgent;

function check_is_token(val) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check_is_token sounds a bit generic, even though this is in lib/http.js. Maybe check_is_http_token is more explicit?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think it would help to mention that this code is written to conform to the HTTP/1.1 RFC, and from which section comes the set of forbidden characters. Otherwise, it might look like it was chosen arbitrarily.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we want to change the name, and I think general we're more camelCase style -- perhaps checkValidToken

if (!val || typeof val !== 'string')
return false;
for (var n = 0, l = val.length; n < l; n++) {
var c = val.charCodeAt(n);
if (c <= 32 || c == 34 ||
c == 44 || c == 47 ||
c == 123 || c == 125 ||
c == 40 || c == 41 ||
(c >= 58 && c <= 64) ||
(c >= 91 && c <= 93) ||
c >= 128)
return false;
}
return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all callers are just going to take false and throw I would advocate this method should just throw, then all our state is preserved.

Also as far as the error message, I would like to improve it -- ideally the message would be something like: "Invalid character in HTTP method at %d for %s", n, val

Then on the actual error object that's constructed attach the same information.

If we're against throwing in this method, I would advocate returning null or falsey in the happy case, and returning the as described error object in the case where we should throw.

}

function ClientRequest(options, cb) {
var self = this;
Expand All @@ -1365,6 +1381,9 @@ function ClientRequest(options, cb) {
self.socketPath = options.socketPath;

var method = self.method = (options.method || 'GET').toUpperCase();
if (!check_is_token(method)) {
throw new Error('Malformed HTTP request method');
}
self.path = options.path || '/';
if (cb) {
self.once('response', cb);
Expand Down Expand Up @@ -2140,6 +2159,9 @@ Client.prototype.request = function(method, path, headers) {
path = method;
method = 'GET';
}
if (!check_is_token(method)) {
throw new Error('Malformed HTTP request method');
}
options.method = method;
options.path = path;
options.headers = headers;
Expand Down
34 changes: 34 additions & 0 deletions test/simple/test-http-malformed-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var http = require('http');

// this should throw

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I like to mention using comments what the goal of a given test is. We could mention the link to the given issue, and the rationale for this test (not only fixing the "header injection" issue, but also making the API more compliant with the HTTP RFCs). It helps when revisiting tests much later and trying to understand the intent of the author(s).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that a more thorough review of the RFC requirements is going to be necessary overall. I've marked that on my short list to do for v0.14. If folks think it's important enough to do sooner, let me know.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My opinion is that without more thorough tests, we won't be able to be confident that this set of changes does what it intends to do, so I would advocate for including them from the start.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm expanding the test case to check a wider range of valid and invalid characters in the method.

var c = 0;
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could maybe use assert.throws here?

var req = http.request({method:'GET\n'});
} catch (err) {
console.log(err);
c++;
}
assert.equal(c,1);