Skip to content

Commit 08a150f

Browse files
bnoordhuisrvagg
authored andcommittedAug 13, 2018
inspector: don't bind to 0.0.0.0 by default
Change the bind address from 0.0.0.0 to 127.0.0.1 and start respecting the address part of `--inspect=<address>:<port>` so that the bind address can be overridden by the user. Fixes: #21349 PR-URL: #21376 Reviewed-By: James M Snell <[email protected]>
1 parent 58b9497 commit 08a150f

File tree

5 files changed

+88
-25
lines changed

5 files changed

+88
-25
lines changed
 

‎src/inspector_agent.cc

+34-18
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,30 @@ static const uint8_t PROTOCOL_JSON[] = {
3737
#include "v8_inspector_protocol_json.h" // NOLINT(build/include_order)
3838
};
3939

40-
std::string GetWsUrl(int port, const std::string& id) {
40+
std::string GetWsUrl(const sockaddr_in& address, const std::string& id) {
4141
char buf[1024];
42-
snprintf(buf, sizeof(buf), "127.0.0.1:%d/%s", port, id.c_str());
42+
char name[64];
43+
44+
if (uv_ip4_name(&address, name, sizeof(name))) *name = '\0';
45+
const int port = ntohs(address.sin_port);
46+
snprintf(buf, sizeof(buf), "%s:%d/%s", name, port, id.c_str());
47+
4348
return buf;
4449
}
4550

46-
void PrintDebuggerReadyMessage(int port, const std::string& id) {
47-
fprintf(stderr, "Debugger listening on port %d.\n"
51+
void PrintDebuggerReadyMessage(const sockaddr_in& address,
52+
const std::string& id) {
53+
const std::string ws_url = GetWsUrl(address, id);
54+
const size_t slash_pos = ws_url.find('/');
55+
CHECK_NE(slash_pos, std::string::npos);
56+
57+
fprintf(stderr, "Debugger listening on %.*s.\n"
4858
"Warning: This is an experimental feature and could change at any time.\n"
4959
"To start debugging, open the following URL in Chrome:\n"
5060
" chrome-devtools://devtools/remote/serve_file/"
5161
"@" V8_INSPECTOR_REVISION "/inspector.html?"
5262
"experiments=true&v8only=true&ws=%s\n",
53-
port, GetWsUrl(port, id).c_str());
63+
static_cast<int>(slash_pos), ws_url.c_str(), ws_url.c_str());
5464
fflush(stderr);
5565
}
5666

@@ -187,7 +197,8 @@ class AgentImpl {
187197
~AgentImpl();
188198

189199
// Start the inspector agent thread
190-
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
200+
bool Start(v8::Platform* platform, const char* path,
201+
const char* host, int port, bool wait);
191202
// Stop the inspector agent
192203
void Stop();
193204

@@ -234,7 +245,7 @@ class AgentImpl {
234245
uv_thread_t thread_;
235246
uv_loop_t child_loop_;
236247

237-
int port_;
248+
struct sockaddr_in saddr_;
238249
bool wait_;
239250
bool shutting_down_;
240251
State state_;
@@ -380,7 +391,7 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
380391
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
381392
};
382393

383-
AgentImpl::AgentImpl(Environment* env) : port_(0),
394+
AgentImpl::AgentImpl(Environment* env) : saddr_(),
384395
wait_(false),
385396
shutting_down_(false),
386397
state_(State::kNew),
@@ -473,7 +484,10 @@ void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
473484
}
474485

475486
bool AgentImpl::Start(v8::Platform* platform, const char* path,
476-
int port, bool wait) {
487+
const char* address, int port, bool wait) {
488+
if (*address == '\0') address = "127.0.0.1";
489+
if (uv_ip4_addr(address, port, &saddr_)) return false;
490+
477491
auto env = parent_env_;
478492
inspector_ = new V8NodeInspector(this, env, platform);
479493
platform_ = platform;
@@ -485,7 +499,6 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
485499
int err = uv_loop_init(&child_loop_);
486500
CHECK_EQ(err, 0);
487501

488-
port_ = port;
489502
wait_ = wait;
490503

491504
err = uv_thread_create(&thread_, AgentImpl::ThreadCbIO, this);
@@ -661,7 +674,7 @@ void AgentImpl::SendListResponse(InspectorSocket* socket) {
661674
Escape(&response["url"]);
662675

663676
if (!client_socket_) {
664-
std::string address = GetWsUrl(port_, id_);
677+
std::string address = GetWsUrl(saddr_, id_);
665678

666679
std::ostringstream frontend_url;
667680
frontend_url << "https://chrome-devtools-frontend.appspot.com/serve_file/@";
@@ -715,7 +728,6 @@ void AgentImpl::WriteCbIO(uv_async_t* async) {
715728
}
716729

717730
void AgentImpl::WorkerRunIO() {
718-
sockaddr_in addr;
719731
uv_tcp_t server;
720732
int err = uv_loop_init(&child_loop_);
721733
CHECK_EQ(err, 0);
@@ -729,14 +741,18 @@ void AgentImpl::WorkerRunIO() {
729741
uv_fs_req_cleanup(&req);
730742
}
731743
uv_tcp_init(&child_loop_, &server);
732-
uv_ip4_addr("0.0.0.0", port_, &addr);
733744
server.data = this;
734745
err = uv_tcp_bind(&server,
735-
reinterpret_cast<const struct sockaddr*>(&addr), 0);
746+
reinterpret_cast<const struct sockaddr*>(&saddr_), 0);
736747
if (err == 0) {
737748
err = uv_listen(reinterpret_cast<uv_stream_t*>(&server), 1,
738749
OnSocketConnectionIO);
739750
}
751+
if (err == 0) {
752+
int namelen = sizeof(saddr_);
753+
err = uv_tcp_getsockname(&server, reinterpret_cast<sockaddr*>(&saddr_),
754+
&namelen);
755+
}
740756
if (err != 0) {
741757
fprintf(stderr, "Unable to open devtools socket: %s\n", uv_strerror(err));
742758
state_ = State::kError; // Safe, main thread is waiting on semaphore
@@ -746,7 +762,7 @@ void AgentImpl::WorkerRunIO() {
746762
uv_sem_post(&start_sem_);
747763
return;
748764
}
749-
PrintDebuggerReadyMessage(port_, id_);
765+
PrintDebuggerReadyMessage(saddr_, id_);
750766
if (!wait_) {
751767
uv_sem_post(&start_sem_);
752768
}
@@ -830,7 +846,7 @@ void AgentImpl::DispatchMessages() {
830846
if (shutting_down_) {
831847
state_ = State::kDone;
832848
} else {
833-
PrintDebuggerReadyMessage(port_, id_);
849+
PrintDebuggerReadyMessage(saddr_, id_);
834850
state_ = State::kAccepting;
835851
}
836852
inspector_->quitMessageLoopOnPause();
@@ -858,8 +874,8 @@ Agent::~Agent() {
858874
}
859875

860876
bool Agent::Start(v8::Platform* platform, const char* path,
861-
int port, bool wait) {
862-
return impl->Start(platform, path, port, wait);
877+
const char* host, int port, bool wait) {
878+
return impl->Start(platform, path, host, port, wait);
863879
}
864880

865881
void Agent::Stop() {

‎src/inspector_agent.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class Agent {
2929
~Agent();
3030

3131
// Start the inspector agent thread
32-
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
32+
bool Start(v8::Platform* platform, const char* path,
33+
const char* address, int port, bool wait);
3334
// Stop the inspector agent
3435
void Stop();
3536

‎src/node.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@ static struct {
232232
}
233233

234234
bool StartInspector(Environment *env, const char* script_path,
235-
int port, bool wait) {
235+
const char* host, int port, bool wait) {
236236
#if HAVE_INSPECTOR
237-
return env->inspector_agent()->Start(platform_, script_path, port, wait);
237+
return env->inspector_agent()->Start(platform_, script_path,
238+
host, port, wait);
238239
#else
239240
return true;
240241
#endif // HAVE_INSPECTOR
@@ -246,7 +247,7 @@ static struct {
246247
void PumpMessageLoop(Isolate* isolate) {}
247248
void Dispose() {}
248249
bool StartInspector(Environment *env, const char* script_path,
249-
int port, bool wait) {
250+
const char* host, int port, bool wait) {
250251
env->ThrowError("Node compiled with NODE_USE_V8_PLATFORM=0");
251252
return false; // make compiler happy
252253
}
@@ -4092,8 +4093,9 @@ static void DispatchMessagesDebugAgentCallback(Environment* env) {
40924093
static void StartDebug(Environment* env, const char* path, bool wait) {
40934094
CHECK(!debugger_running);
40944095
if (use_inspector) {
4095-
debugger_running = v8_platform.StartInspector(env, path, inspector_port,
4096-
wait);
4096+
debugger_running = v8_platform.StartInspector(env, path,
4097+
inspector_host.c_str(),
4098+
inspector_port, wait);
40974099
} else {
40984100
env->debugger_agent()->set_dispatch_handler(
40994101
DispatchMessagesDebugAgentCallback);

‎test/inspector/inspector-helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ exports.startNodeForInspectorTest = function(callback) {
446446
clearTimeout(timeoutId);
447447
console.log('[err]', text);
448448
if (found) return;
449-
const match = text.match(/Debugger listening on port (\d+)/);
449+
const match = text.match(/Debugger listening on 127\.0\.0\.1:(\d+)\./);
450450
found = true;
451451
child.stderr.removeListener('data', dataCallback);
452452
assert.ok(match, text);
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const { PORT, mustCall, skipIfInspectorDisabled } = require('../common');
3+
4+
skipIfInspectorDisabled();
5+
6+
const assert = require('assert');
7+
const { spawn } = require('child_process');
8+
9+
function test(arg, expected, done) {
10+
const args = [arg, '-p', 'process.debugPort'];
11+
const proc = spawn(process.execPath, args);
12+
proc.stdout.setEncoding('utf8');
13+
proc.stderr.setEncoding('utf8');
14+
let stdout = '';
15+
let stderr = '';
16+
proc.stdout.on('data', (data) => stdout += data);
17+
proc.stderr.on('data', (data) => stderr += data);
18+
proc.stdout.on('close', (hadErr) => assert(!hadErr));
19+
proc.stderr.on('close', (hadErr) => assert(!hadErr));
20+
proc.stderr.on('data', () => {
21+
if (!stderr.includes('\n')) return;
22+
assert(/Debugger listening on (.+)\./.test(stderr));
23+
assert.strictEqual(RegExp.$1, expected);
24+
});
25+
proc.on('exit', (exitCode, signalCode) => {
26+
assert.strictEqual(exitCode, 0);
27+
assert.strictEqual(signalCode, null);
28+
done();
29+
});
30+
}
31+
32+
function one() {
33+
test(`--inspect=${PORT}`, `127.0.0.1:${PORT}`, mustCall(two));
34+
}
35+
36+
function two() {
37+
test(`--inspect=0.0.0.0:${PORT}`, `0.0.0.0:${PORT}`, mustCall(three));
38+
}
39+
40+
function three() {
41+
test(`--inspect=127.0.0.1:${PORT}`, `127.0.0.1:${PORT}`, mustCall());
42+
}
43+
44+
one();

0 commit comments

Comments
 (0)
Please sign in to comment.