Skip to content

Commit 2a580b9

Browse files
DemianParkhomenkoaduh95
authored andcommitted
lib: add warning when binding inspector to public IP
Add `isLoopback` function to `internal/net` module to check if a given host is a loopback address. Add a warning when binding the inspector to a public IP with an open port, as it allows external hosts to connect to the inspector. Fixes: #23444 Refs: https://nodejs.org/api/cli.html#--inspecthostport PR-URL: #55736 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent c45894f commit 2a580b9

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lib/inspector.js

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const {
1818
ERR_INSPECTOR_NOT_WORKER,
1919
} = require('internal/errors').codes;
2020

21+
const { isLoopback } = require('internal/net');
22+
2123
const { hasInspector } = internalBinding('config');
2224
if (!hasInspector)
2325
throw new ERR_INSPECTOR_NOT_AVAILABLE();
@@ -172,6 +174,17 @@ function inspectorOpen(port, host, wait) {
172174
if (isUint32(port)) {
173175
validateInt32(port, 'port', 0, 65535);
174176
}
177+
if (host && !isLoopback(host)) {
178+
process.emitWarning(
179+
'Binding the inspector to a public IP with an open port is insecure, ' +
180+
'as it allows external hosts to connect to the inspector ' +
181+
'and perform a remote code execution attack. ' +
182+
'Documentation can be found at ' +
183+
'https://nodejs.org/api/cli.html#--inspecthostport',
184+
'SecurityWarning',
185+
);
186+
}
187+
175188
open(port, host);
176189
if (wait)
177190
waitForDebugger();

lib/internal/net.js

+17
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,28 @@ function makeSyncWrite(fd) {
6868
};
6969
}
7070

71+
/**
72+
* https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
73+
* https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
74+
* https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml
75+
*/
76+
function isLoopback(host) {
77+
const hostLower = host.toLowerCase();
78+
79+
return (
80+
hostLower === 'localhost' ||
81+
hostLower.startsWith('127.') ||
82+
hostLower.startsWith('[::1]') ||
83+
hostLower.startsWith('[0:0:0:0:0:0:0:1]')
84+
);
85+
}
86+
7187
module.exports = {
7288
kReinitializeHandle: Symbol('kReinitializeHandle'),
7389
isIP,
7490
isIPv4,
7591
isIPv6,
7692
makeSyncWrite,
7793
normalizedArgsSymbol: Symbol('normalizedArgs'),
94+
isLoopback,
7895
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
common.skipIfInspectorDisabled();
5+
6+
const inspector = require('inspector');
7+
inspector.open(0, '0.0.0.0', false);
8+
common.expectWarning(
9+
'SecurityWarning',
10+
'Binding the inspector to a public IP with an open port is insecure, ' +
11+
'as it allows external hosts to connect to the inspector ' +
12+
'and perform a remote code execution attack. ' +
13+
'Documentation can be found at ' +
14+
'https://nodejs.org/api/cli.html#--inspecthostport'
15+
);
16+
inspector.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
const net = require('internal/net');
6+
7+
const loopback = [
8+
'localhost',
9+
'127.0.0.1',
10+
'127.0.0.255',
11+
'127.1.2.3',
12+
'[::1]',
13+
'[0:0:0:0:0:0:0:1]',
14+
];
15+
16+
const loopbackNot = [
17+
'example.com',
18+
'192.168.1.1',
19+
'10.0.0.1',
20+
'255.255.255.255',
21+
'[2001:db8::1]',
22+
'[fe80::1]',
23+
'8.8.8.8',
24+
];
25+
26+
for (const address of loopback) {
27+
assert.strictEqual(net.isLoopback(address), true);
28+
}
29+
30+
for (const address of loopbackNot) {
31+
assert.strictEqual(net.isLoopback(address), false);
32+
}

0 commit comments

Comments
 (0)