Skip to content

Commit 513e974

Browse files
bennofsrvagg
authored andcommittedNov 27, 2018
http: disallow two-byte characters in URL path
CVE-2018-12116 Backport of b961d9f to 8.x Original commit: This commit changes node's handling of two-byte characters in the path component of an http URL. Previously, node would just strip the higher byte when generating the request. So this code: ``` http.request({host: "example.com", port: "80", "/N"}) ``` would request `http://example.com/.` (`.` is the character for the byte `0x2e`). This is not useful and can in some cases lead to filter evasion. With this change, the code generates `ERR_UNESCAPED_CHARACTERS`, just like space and control characters already did. PR-URL: #16237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> PR-URL: nodejs-private/node-private#146 Fixes: nodejs-private/security#207 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 53a6e4e commit 513e974

File tree

2 files changed

+14
-34
lines changed

2 files changed

+14
-34
lines changed
 

‎lib/_http_client.js

+2-34
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,7 @@ const { outHeadersKey, ondrain } = require('internal/http');
4141
const { nextTick } = require('internal/process/next_tick');
4242
const is_reused_symbol = require('internal/freelist').symbols.is_reused_symbol;
4343

44-
// The actual list of disallowed characters in regexp form is more like:
45-
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
46-
// with an additional rule for ignoring percentage-escaped characters, but
47-
// that's a) hard to capture in a regular expression that performs well, and
48-
// b) possibly too restrictive for real-world usage. So instead we restrict the
49-
// filter to just control characters and spaces.
50-
//
51-
// This function is used in the case of small paths, where manual character code
52-
// checks can greatly outperform the equivalent regexp (tested in V8 5.4).
53-
function isInvalidPath(s) {
54-
var i = 0;
55-
if (s.charCodeAt(0) <= 32) return true;
56-
if (++i >= s.length) return false;
57-
if (s.charCodeAt(1) <= 32) return true;
58-
if (++i >= s.length) return false;
59-
if (s.charCodeAt(2) <= 32) return true;
60-
if (++i >= s.length) return false;
61-
if (s.charCodeAt(3) <= 32) return true;
62-
if (++i >= s.length) return false;
63-
if (s.charCodeAt(4) <= 32) return true;
64-
if (++i >= s.length) return false;
65-
if (s.charCodeAt(5) <= 32) return true;
66-
++i;
67-
for (; i < s.length; ++i)
68-
if (s.charCodeAt(i) <= 32) return true;
69-
return false;
70-
}
44+
const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/;
7145

7246
function validateHost(host, name) {
7347
if (host != null && typeof host !== 'string') {
@@ -118,13 +92,7 @@ function ClientRequest(options, cb) {
11892
var path;
11993
if (options.path) {
12094
path = String(options.path);
121-
var invalidPath;
122-
if (path.length <= 39) { // Determined experimentally in V8 5.4
123-
invalidPath = isInvalidPath(path);
124-
} else {
125-
invalidPath = /[\u0000-\u0020]/.test(path);
126-
}
127-
if (invalidPath)
95+
if (INVALID_PATH_REGEX.test(path))
12896
throw new TypeError('Request path contains unescaped characters');
12997
}
13098

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
assert.throws(() => {
9+
http.request({
10+
path: '/thisisinvalid\uffe2'
11+
}).end();
12+
}, /Request path contains unescaped characters/);

0 commit comments

Comments
 (0)
Please sign in to comment.