Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: verify method is a string #10111

Closed
wants to merge 5 commits into from
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
6 changes: 5 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ function ClientRequest(options, cb) {
self.socketPath = options.socketPath;
self.timeout = options.timeout;

var method = self.method = (options.method || 'GET').toUpperCase();
var method = options.method;
if (method != null && typeof method !== 'string') {
throw new TypeError('Method must be a string');
}
Copy link
Contributor

@mscdex mscdex Dec 4, 2016

Choose a reason for hiding this comment

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

For the best backwards compatibility (and to emulate the method value selection behavior below the typeof check), I think the undefined/null check should be removed and just test for truthiness:

if (options.method && typeof options.method !== 'string') {
  throw new TypeError('Method must be a string');
}

The reason being that options.method could be one of several non-truthy values, such as 0 and false.

Copy link
Contributor Author

@lucamaraschi lucamaraschi Dec 4, 2016

Choose a reason for hiding this comment

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

In this case if options.method is set to 0 results in a falsy value which is going to default the method to GET in the following check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mscdex that would not fix the non-truthy values, confusing the defaulting of 0 values to GET.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, it's a difference between semver-ness. This change is definitely going to be semver-major as-is, but it could be semver-patch with the check I suggested. It's up to you.

method = self.method = (method || 'GET').toUpperCase();
if (!common._checkIsHttpToken(method)) {
throw new TypeError('Method must be a valid HTTP token');
}
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-http-client-check-http-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const expectedSuccesses = [undefined, null, 'GET', 'post'];
let requestCount = 0;

const server = http.createServer((req, res) => {
requestCount++;
res.end();

if (expectedSuccesses.length === requestCount) {
server.close();
}
}).listen(0, test);

function test() {
function fail(input) {
assert.throws(() => {
http.request({ method: input, path: '/' }, common.fail);
}, /^TypeError: Method must be a string$/);
}

fail(-1);
fail(1);
fail(0);
fail({});
fail(true);
fail(false);
fail([]);

function ok(method) {
http.request({ method: method, port: server.address().port }).end();
}

expectedSuccesses.forEach((method) => {
ok(method);
});
}