Skip to content

Commit 8e7cbe2

Browse files
committed
src: make debugger listen on 127.0.0.1 by default
Commit 2272052 ("net: bind to `::` TCP address by default") from April 2014 seems to have accidentally changed the default listen address from 127.0.0.1 to 0.0.0.0, a.k.a. the "any" address. From a security viewpoint it's undesirable to accept debug agent connections from anywhere so let's change that back. Users can override the default with the `--debug=<host>:<port>` switch. Fixes: #8081 PR-URL: #8106 Reviewed-By: James M Snell <[email protected]>
1 parent 16f4b8e commit 8e7cbe2

8 files changed

+38
-43
lines changed

src/debug-agent.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Agent::~Agent() {
6969
}
7070

7171

72-
bool Agent::Start(const std::string& host, int port, bool wait) {
72+
bool Agent::Start(const char* host, int port, bool wait) {
7373
int err;
7474

7575
if (state_ == kRunning)

src/debug-agent.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Agent {
7676
typedef void (*DispatchHandler)(node::Environment* env);
7777

7878
// Start the debugger agent thread
79-
bool Start(const std::string& host, int port, bool wait);
79+
bool Start(const char* host, int port, bool wait);
8080
// Listen for debug events
8181
void Enable();
8282
// Stop the debugger agent

src/node.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ static const bool use_inspector = false;
146146
#endif
147147
static bool use_debug_agent = false;
148148
static bool debug_wait_connect = false;
149-
static std::string debug_host; // NOLINT(runtime/string)
149+
static std::string* debug_host; // coverity[leaked_storage]
150150
static int debug_port = 5858;
151-
static std::string inspector_host; // NOLINT(runtime/string)
151+
static std::string* inspector_host; // coverity[leaked_storage]
152152
static int inspector_port = 9229;
153153
static const int v8_default_thread_pool_size = 4;
154154
static int v8_thread_pool_size = v8_default_thread_pool_size;
@@ -3468,15 +3468,15 @@ static bool ParseDebugOpt(const char* arg) {
34683468
return true;
34693469
}
34703470

3471-
std::string* const the_host = use_inspector ? &inspector_host : &debug_host;
3471+
std::string** const the_host = use_inspector ? &inspector_host : &debug_host;
34723472
int* const the_port = use_inspector ? &inspector_port : &debug_port;
34733473

34743474
// FIXME(bnoordhuis) Move IPv6 address parsing logic to lib/net.js.
34753475
// It seems reasonable to support [address]:port notation
34763476
// in net.Server#listen() and net.Socket#connect().
34773477
const size_t port_len = strlen(port);
34783478
if (port[0] == '[' && port[port_len - 1] == ']') {
3479-
the_host->assign(port + 1, port_len - 2);
3479+
*the_host = new std::string(port + 1, port_len - 2);
34803480
return true;
34813481
}
34823482

@@ -3486,13 +3486,13 @@ static bool ParseDebugOpt(const char* arg) {
34863486
// if it's not all decimal digits, it's a host name.
34873487
for (size_t n = 0; port[n] != '\0'; n += 1) {
34883488
if (port[n] < '0' || port[n] > '9') {
3489-
*the_host = port;
3489+
*the_host = new std::string(port);
34903490
return true;
34913491
}
34923492
}
34933493
} else {
34943494
const bool skip = (colon > port && port[0] == '[' && colon[-1] == ']');
3495-
the_host->assign(port + skip, colon - skip);
3495+
*the_host = new std::string(port + skip, colon - skip);
34963496
}
34973497

34983498
char* endptr;
@@ -3769,11 +3769,11 @@ static void StartDebug(Environment* env, bool wait) {
37693769
} else {
37703770
env->debugger_agent()->set_dispatch_handler(
37713771
DispatchMessagesDebugAgentCallback);
3772+
const char* host = debug_host ? debug_host->c_str() : "127.0.0.1";
37723773
debugger_running =
3773-
env->debugger_agent()->Start(debug_host, debug_port, wait);
3774+
env->debugger_agent()->Start(host, debug_port, wait);
37743775
if (debugger_running == false) {
3775-
fprintf(stderr, "Starting debugger on %s:%d failed\n",
3776-
debug_host.c_str(), debug_port);
3776+
fprintf(stderr, "Starting debugger on %s:%d failed\n", host, debug_port);
37773777
fflush(stderr);
37783778
return;
37793779
}

test/parallel/test-debug-port-cluster.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ child.stderr.setEncoding('utf8');
1616

1717
const checkMessages = common.mustCall(() => {
1818
for (let port = PORT_MIN; port <= PORT_MAX; port += 1) {
19-
const re = RegExp(`Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):${port}`);
20-
assert(re.test(stderr));
19+
assert(stderr.includes(`Debugger listening on 127.0.0.1:${port}`));
2120
}
2221
});
2322

test/parallel/test-debug-port-from-cmdline.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function processStderrLine(line) {
3939
function assertOutputLines() {
4040
var expectedLines = [
4141
'Starting debugger agent.',
42-
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):' + debugPort,
42+
'Debugger listening on 127.0.0.1:' + debugPort,
4343
];
4444

4545
assert.equal(outputLines.length, expectedLines.length);
4646
for (var i = 0; i < expectedLines.length; i++)
47-
assert(RegExp(expectedLines[i]).test(outputLines[i]));
47+
assert(expectedLines[i].includes(outputLines[i]));
4848
}

test/parallel/test-debug-port-numbers.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ function kill(child) {
5252

5353
process.on('exit', function() {
5454
for (const child of children) {
55-
const port = child.test.port;
56-
const one = RegExp(`Debugger listening on (\\[::\\]|0\.0\.0\.0):${port}`);
57-
const two = RegExp(`connecting to 127.0.0.1:${port}`);
58-
assert(one.test(child.test.stdout));
59-
assert(two.test(child.test.stdout));
55+
const { port, stdout } = child.test;
56+
assert(stdout.includes(`Debugger listening on 127.0.0.1:${port}`));
57+
assert(stdout.includes(`connecting to 127.0.0.1:${port}`));
6058
}
6159
});

test/parallel/test-debug-signal-cluster.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ process.on('exit', function onExit() {
6363

6464
var expectedLines = [
6565
'Starting debugger agent.',
66-
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):' + (port + 0),
66+
'Debugger listening on 127.0.0.1:' + (port + 0),
6767
'Starting debugger agent.',
68-
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):' + (port + 1),
68+
'Debugger listening on 127.0.0.1:' + (port + 1),
6969
'Starting debugger agent.',
70-
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):' + (port + 2),
70+
'Debugger listening on 127.0.0.1:' + (port + 2),
7171
];
7272

7373
function assertOutputLines() {
@@ -79,5 +79,5 @@ function assertOutputLines() {
7979

8080
assert.equal(outputLines.length, expectedLines.length);
8181
for (var i = 0; i < expectedLines.length; i++)
82-
assert(RegExp(expectedLines[i]).test(outputLines[i]));
82+
assert(expectedLines[i].includes(outputLines[i]));
8383
}

test/sequential/test-debug-host-port.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const spawn = require('child_process').spawn;
66

77
let run = () => {};
8-
function test(args, re) {
8+
function test(args, needle) {
99
const next = run;
1010
run = () => {
1111
const options = {encoding: 'utf8'};
@@ -14,34 +14,32 @@ function test(args, re) {
1414
proc.stderr.setEncoding('utf8');
1515
proc.stderr.on('data', (data) => {
1616
stderr += data;
17-
if (re.test(stderr)) proc.kill();
17+
if (stderr.includes(needle)) proc.kill();
1818
});
1919
proc.on('exit', common.mustCall(() => {
20-
assert(re.test(stderr));
20+
assert(stderr.includes(needle));
2121
next();
2222
}));
2323
};
2424
}
2525

26-
test(['--debug-brk'], /Debugger listening on (\[::\]|0\.0\.0\.0):5858/);
27-
test(['--debug-brk=1234'], /Debugger listening on (\[::\]|0\.0\.0\.0):1234/);
28-
test(['--debug-brk=127.0.0.1'], /Debugger listening on 127\.0\.0\.1:5858/);
29-
test(['--debug-brk=127.0.0.1:1234'], /Debugger listening on 127\.0\.0\.1:1234/);
30-
test(['--debug-brk=localhost'],
31-
/Debugger listening on (\[::\]|127\.0\.0\.1):5858/);
32-
test(['--debug-brk=localhost:1234'],
33-
/Debugger listening on (\[::\]|127\.0\.0\.1):1234/);
26+
test(['--debug-brk'], 'Debugger listening on 127.0.0.1:5858');
27+
test(['--debug-brk=1234'], 'Debugger listening on 127.0.0.1:1234');
28+
test(['--debug-brk=0.0.0.0'], 'Debugger listening on 0.0.0.0:5858');
29+
test(['--debug-brk=0.0.0.0:1234'], 'Debugger listening on 0.0.0.0:1234');
30+
test(['--debug-brk=localhost'], 'Debugger listening on 127.0.0.1:5858');
31+
test(['--debug-brk=localhost:1234'], 'Debugger listening on 127.0.0.1:1234');
3432

3533
if (common.hasIPv6) {
36-
test(['--debug-brk=::'], /Debug port must be in range 1024 to 65535/);
37-
test(['--debug-brk=::0'], /Debug port must be in range 1024 to 65535/);
38-
test(['--debug-brk=::1'], /Debug port must be in range 1024 to 65535/);
39-
test(['--debug-brk=[::]'], /Debugger listening on \[::\]:5858/);
40-
test(['--debug-brk=[::0]'], /Debugger listening on \[::\]:5858/);
41-
test(['--debug-brk=[::]:1234'], /Debugger listening on \[::\]:1234/);
42-
test(['--debug-brk=[::0]:1234'], /Debugger listening on \[::\]:1234/);
34+
test(['--debug-brk=::'], 'Debug port must be in range 1024 to 65535');
35+
test(['--debug-brk=::0'], 'Debug port must be in range 1024 to 65535');
36+
test(['--debug-brk=::1'], 'Debug port must be in range 1024 to 65535');
37+
test(['--debug-brk=[::]'], 'Debugger listening on [::]:5858');
38+
test(['--debug-brk=[::0]'], 'Debugger listening on [::]:5858');
39+
test(['--debug-brk=[::]:1234'], 'Debugger listening on [::]:1234');
40+
test(['--debug-brk=[::0]:1234'], 'Debugger listening on [::]:1234');
4341
test(['--debug-brk=[::ffff:127.0.0.1]:1234'],
44-
/Debugger listening on \[::ffff:127\.0\.0\.1\]:1234/);
42+
'Debugger listening on [::ffff:127.0.0.1]:1234');
4543
}
4644

4745
run(); // Runs tests in reverse order.

0 commit comments

Comments
 (0)