Skip to content

Commit

Permalink
http: always use regex for invalid path check
Browse files Browse the repository at this point in the history
Using the "optimized" version was not significantly faster and even
slower for larger n.
  • Loading branch information
bennofs committed Nov 22, 2017
1 parent 58ac26f commit 60b3290
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,7 @@ const { outHeadersKey } = require('internal/http');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');

// The actual list of disallowed characters in regexp form is more like:
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
// with an additional rule for ignoring percentage-escaped characters, but
// that's a) hard to capture in a regular expression that performs well, and
// b) possibly too restrictive for real-world usage. So instead we restrict the
// filter to just control characters, spaces and two-byte characters.
//
// This function is used in the case of small paths, where manual character code
// checks can greatly outperform the equivalent regexp (tested in V8 5.4).
function isInvalidPath(s) {
var i = 0;
if (s.charCodeAt(0) <= 32 || s.charCodeAt(0) > 0xFF) return true;
if (++i >= s.length) return false;
if (s.charCodeAt(1) <= 32 || s.charCodeAt(1) > 0xFF) return true;
if (++i >= s.length) return false;
if (s.charCodeAt(2) <= 32 || s.charCodeAt(2) > 0xFF) return true;
if (++i >= s.length) return false;
if (s.charCodeAt(3) <= 32 || s.charCodeAt(3) > 0xFF) return true;
if (++i >= s.length) return false;
if (s.charCodeAt(4) <= 32 || s.charCodeAt(4) > 0xFF) return true;
if (++i >= s.length) return false;
if (s.charCodeAt(5) <= 32 || s.charCodeAt(5) > 0xFF) return true;
++i;
for (; i < s.length; ++i)
if (s.charCodeAt(i) <= 32 || s.charCodeAt(i) > 0xFF) return true;
return false;
}
const INVALID_PATH_REGEX = /[\u0000-\u0020\u0100-\uffff]/;

function validateHost(host, name) {
if (host != null && typeof host !== 'string') {
Expand Down Expand Up @@ -117,13 +91,7 @@ function ClientRequest(options, cb) {
var path;
if (options.path) {
path = String(options.path);
var invalidPath;
if (path.length <= 39) { // Determined experimentally in V8 5.4
invalidPath = isInvalidPath(path);
} else {
invalidPath = /[\u0000-\u0020\u0100-\uffff]/.test(path);
}
if (invalidPath)
if (INVALID_PATH_REGEX.test(path))
throw new errors.TypeError('ERR_UNESCAPED_CHARACTERS', 'Request path');
}

Expand Down

0 comments on commit 60b3290

Please sign in to comment.