Skip to content

Commit 71a1a00

Browse files
committed
src: add security warning when inspector is running on public/private network
1 parent 2cb2597 commit 71a1a00

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

lib/internal/bootstrap/node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@
155155
});
156156
process.argv[0] = process.execPath;
157157

158+
// Handle inspector security warning
159+
const debugOptions = process.binding('config').debugOptions;
160+
if (debugOptions.host !== '127.0.0.1') {
161+
const {
162+
checkInspectorHost
163+
} = NativeModule.require('internal/inspector_security');
164+
checkInspectorHost(debugOptions.host).then((warning) => {
165+
if (warning !== '') {
166+
process.emitWarning(warning);
167+
}
168+
});
169+
}
170+
158171
// Handle `--debug*` deprecation and invalidation.
159172
if (process._invalidDebug) {
160173
process.emitWarning(

lib/internal/inspector_security.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const dns = require('dns');
4+
const util = require('util');
5+
6+
const lookup = util.promisify(dns.lookup);
7+
8+
const IP_RANGES = {
9+
local: 'LOCAL',
10+
private: 'PRIVATE',
11+
public: 'PUBLIC'
12+
};
13+
14+
function isValidIpV4(parts) {
15+
return parts.length === 4 &&
16+
(parts.every((part) => part >= 0 && part <= 255));
17+
}
18+
19+
function convertPartsToLong(parts) {
20+
return parts[0] * 1e9 + parts[1] * 1e6 + parts[2] * 1e3 + parts[3];
21+
}
22+
23+
// Loopback: 127.0.0.0 - 127.255.255.255.
24+
// Private: 10.0.0.0 - 10.255.255.255, 172.16.0.0 - 172.31.255.255
25+
// 192.168.0.0 - 192.168.255.255
26+
// Public: everything else
27+
function toRange(ip) {
28+
if (ip >= 127000000000 && ip <= 127255255255) {
29+
return IP_RANGES.local;
30+
} else if ((ip >= 10000000000 && ip <= 10255255255) ||
31+
(ip >= 172016000000 && ip <= 172031255255) ||
32+
(ip >= 192168000000 && ip <= 192168255255)) {
33+
return IP_RANGES.private;
34+
}
35+
return IP_RANGES.public;
36+
}
37+
38+
async function checkInspectorHost(host) {
39+
let parts = host.split('.').map((part) => parseInt(part, 10));
40+
41+
if (!isValidIpV4(parts)) {
42+
try {
43+
parts = await lookup(host);
44+
} catch (e) {
45+
return `Inspector: could not determinate the ip of ${host}`;
46+
}
47+
} else {
48+
return '';
49+
}
50+
51+
const ip = convertPartsToLong(parts);
52+
const range = toRange(ip);
53+
54+
if (range === IP_RANGES.local) {
55+
return '';
56+
} else if (range === IP_RANGES.private) {
57+
return 'Inspector: you are running inspector on a private network. ' +
58+
'Make sure you trust all the hosts on this network ' +
59+
'or filter out traffic on your firewall';
60+
} else {
61+
return 'Inspector: you are running inspector on a PUBLIC network. ' +
62+
'This is a high security risk, anyone who has access to your computer ' +
63+
'can run arbitrary code on your machine.';
64+
}
65+
}
66+
67+
module.exports = { checkInspectorHost };

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
'lib/internal/fs/watchers.js',
123123
'lib/internal/http.js',
124124
'lib/internal/inspector_async_hook.js',
125+
'lib/internal/inspector_security.js',
125126
'lib/internal/linkedlist.js',
126127
'lib/internal/modules/cjs/helpers.js',
127128
'lib/internal/modules/cjs/loader.js',

test/sequential/test-inspector-port-zero.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ function test(arg, port = '') {
1919
proc.stdout.on('close', (hadErr) => assert(!hadErr));
2020
proc.stderr.on('close', (hadErr) => assert(!hadErr));
2121
proc.stderr.on('data', () => {
22-
if (!stderr.includes('\n')) return;
22+
if (!stderr.includes('\n') ||
23+
(stderr.includes('Warning: Inspector'))) return;
24+
2325
assert(/Debugger listening on (.+)/.test(stderr));
2426
port = new URL(RegExp.$1).port;
2527
assert(+port > 0);

0 commit comments

Comments
 (0)