-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
http: Check for malformed HTTP method for request #9017
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| 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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since all callers are just going to take false and Also as far as the error message, I would like to improve it -- ideally the message would be something like: 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; | ||
|
|
@@ -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); | ||
|
|
@@ -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; | ||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could maybe use |
||
| var req = http.request({method:'GET\n'}); | ||
| } catch (err) { | ||
| console.log(err); | ||
| c++; | ||
| } | ||
| assert.equal(c,1); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check_is_tokensounds a bit generic, even though this is inlib/http.js. Maybecheck_is_http_tokenis more explicit?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
camelCasestyle -- perhapscheckValidToken