Skip to content

Commit 6308c18

Browse files
Tofandelmarco-ippolito
authored andcommitted
report: fix network queries in getReport libuv with exclude-network
PR-URL: #55602 Reviewed-By: Ethan Arrowood <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 753c3b3 commit 6308c18

File tree

6 files changed

+147
-28
lines changed

6 files changed

+147
-28
lines changed

doc/api/report.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ is provided below for reference.
3030
```json
3131
{
3232
"header": {
33-
"reportVersion": 3,
33+
"reportVersion": 4,
3434
"event": "exception",
3535
"trigger": "Exception",
3636
"filename": "report.20181221.005011.8974.0.001.json",
@@ -320,6 +320,50 @@ is provided below for reference.
320320
"is_active": true,
321321
"address": "0x000055fc7b2cb180",
322322
"loopIdleTimeSeconds": 22644.8
323+
},
324+
{
325+
"type": "tcp",
326+
"is_active": true,
327+
"is_referenced": true,
328+
"address": "0x000055e70fcb85d8",
329+
"localEndpoint": {
330+
"host": "localhost",
331+
"ip4": "127.0.0.1",
332+
"port": 48986
333+
},
334+
"remoteEndpoint": {
335+
"host": "localhost",
336+
"ip4": "127.0.0.1",
337+
"port": 38573
338+
},
339+
"sendBufferSize": 2626560,
340+
"recvBufferSize": 131072,
341+
"fd": 24,
342+
"writeQueueSize": 0,
343+
"readable": true,
344+
"writable": true
345+
},
346+
{
347+
"type": "tcp",
348+
"is_active": true,
349+
"is_referenced": true,
350+
"address": "0x000055e70fcd68c8",
351+
"localEndpoint": {
352+
"host": "ip6-localhost",
353+
"ip6": "::1",
354+
"port": 52266
355+
},
356+
"remoteEndpoint": {
357+
"host": "ip6-localhost",
358+
"ip6": "::1",
359+
"port": 38573
360+
},
361+
"sendBufferSize": 2626560,
362+
"recvBufferSize": 131072,
363+
"fd": 25,
364+
"writeQueueSize": 0,
365+
"readable": false,
366+
"writable": false
323367
}
324368
],
325369
"workers": [],
@@ -459,9 +503,9 @@ meaning of `SIGUSR2` for the said purposes.
459503
* `--report-signal` Sets or resets the signal for report generation
460504
(not supported on Windows). Default signal is `SIGUSR2`.
461505

462-
* `--report-exclude-network` Exclude `header.networkInterfaces` from the
463-
diagnostic report. By default this is not set and the network interfaces
464-
are included.
506+
* `--report-exclude-network` Exclude `header.networkInterfaces` and disable the reverse DNS queries
507+
in `libuv.*.(remote|local)Endpoint.host` from the diagnostic report.
508+
By default this is not set and the network interfaces are included.
465509

466510
A report can also be triggered via an API call from a JavaScript application:
467511

src/node_report.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <cwctype>
2525
#include <fstream>
2626

27-
constexpr int NODE_REPORT_VERSION = 3;
27+
constexpr int NODE_REPORT_VERSION = 4;
2828
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
2929
constexpr double SEC_PER_MICROS = 1e-6;
3030
constexpr int MAX_FRAME_COUNT = node::kMaxFrameCountForLogging;
@@ -203,7 +203,9 @@ static void WriteNodeReport(Isolate* isolate,
203203

204204
writer.json_arraystart("libuv");
205205
if (env != nullptr) {
206-
uv_walk(env->event_loop(), WalkHandle, static_cast<void*>(&writer));
206+
uv_walk(env->event_loop(),
207+
exclude_network ? WalkHandleNoNetwork : WalkHandleNetwork,
208+
static_cast<void*>(&writer));
207209

208210
writer.json_start();
209211
writer.json_keyvalue("type", "loop");

src/node_report.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
namespace node {
2020
namespace report {
2121
// Function declarations - utility functions in src/node_report_utils.cc
22-
void WalkHandle(uv_handle_t* h, void* arg);
22+
void WalkHandleNetwork(uv_handle_t* h, void* arg);
23+
void WalkHandleNoNetwork(uv_handle_t* h, void* arg);
2324

2425
template <typename T>
2526
std::string ValueToHexString(T value) {

src/node_report_utils.cc

+35-20
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,51 @@ static constexpr auto null = JSONWriter::Null{};
1212
static void ReportEndpoint(uv_handle_t* h,
1313
struct sockaddr* addr,
1414
const char* name,
15-
JSONWriter* writer) {
15+
JSONWriter* writer,
16+
bool exclude_network) {
1617
if (addr == nullptr) {
1718
writer->json_keyvalue(name, null);
1819
return;
1920
}
2021

2122
uv_getnameinfo_t endpoint;
2223
char* host = nullptr;
23-
char hostbuf[INET6_ADDRSTRLEN];
2424
const int family = addr->sa_family;
2525
const int port = ntohs(family == AF_INET ?
2626
reinterpret_cast<sockaddr_in*>(addr)->sin_port :
2727
reinterpret_cast<sockaddr_in6*>(addr)->sin6_port);
2828

29-
if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
29+
writer->json_objectstart(name);
30+
if (!exclude_network &&
31+
uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
3032
host = endpoint.host;
3133
DCHECK_EQ(port, std::stoi(endpoint.service));
34+
writer->json_keyvalue("host", host);
35+
}
36+
37+
if (family == AF_INET) {
38+
char ipbuf[INET_ADDRSTRLEN];
39+
if (uv_ip4_name(
40+
reinterpret_cast<sockaddr_in*>(addr), ipbuf, sizeof(ipbuf)) == 0) {
41+
writer->json_keyvalue("ip4", ipbuf);
42+
if (host == nullptr) writer->json_keyvalue("host", ipbuf);
43+
}
3244
} else {
33-
const void* src = family == AF_INET ?
34-
static_cast<void*>(
35-
&(reinterpret_cast<sockaddr_in*>(addr)->sin_addr)) :
36-
static_cast<void*>(
37-
&(reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr));
38-
if (uv_inet_ntop(family, src, hostbuf, sizeof(hostbuf)) == 0) {
39-
host = hostbuf;
45+
char ipbuf[INET6_ADDRSTRLEN];
46+
if (uv_ip6_name(
47+
reinterpret_cast<sockaddr_in6*>(addr), ipbuf, sizeof(ipbuf)) == 0) {
48+
writer->json_keyvalue("ip6", ipbuf);
49+
if (host == nullptr) writer->json_keyvalue("host", ipbuf);
4050
}
4151
}
42-
writer->json_objectstart(name);
43-
if (host != nullptr) {
44-
writer->json_keyvalue("host", host);
45-
}
4652
writer->json_keyvalue("port", port);
4753
writer->json_objectend();
4854
}
4955

5056
// Utility function to format libuv socket information.
51-
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
57+
static void ReportEndpoints(uv_handle_t* h,
58+
JSONWriter* writer,
59+
bool exclude_network) {
5260
struct sockaddr_storage addr_storage;
5361
struct sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
5462
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -65,7 +73,8 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
6573
default:
6674
break;
6775
}
68-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer);
76+
ReportEndpoint(
77+
h, rc == 0 ? addr : nullptr, "localEndpoint", writer, exclude_network);
6978

7079
switch (h->type) {
7180
case UV_UDP:
@@ -77,7 +86,8 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
7786
default:
7887
break;
7988
}
80-
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
89+
ReportEndpoint(
90+
h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer, exclude_network);
8191
}
8292

8393
// Utility function to format libuv pipe information.
@@ -155,7 +165,7 @@ static void ReportPath(uv_handle_t* h, JSONWriter* writer) {
155165
}
156166

157167
// Utility function to walk libuv handles.
158-
void WalkHandle(uv_handle_t* h, void* arg) {
168+
void WalkHandle(uv_handle_t* h, void* arg, bool exclude_network = false) {
159169
const char* type = uv_handle_type_name(h->type);
160170
JSONWriter* writer = static_cast<JSONWriter*>(arg);
161171
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
@@ -177,7 +187,7 @@ void WalkHandle(uv_handle_t* h, void* arg) {
177187
break;
178188
case UV_TCP:
179189
case UV_UDP:
180-
ReportEndpoints(h, writer);
190+
ReportEndpoints(h, writer, exclude_network);
181191
break;
182192
case UV_NAMED_PIPE:
183193
ReportPipeEndpoints(h, writer);
@@ -267,6 +277,11 @@ void WalkHandle(uv_handle_t* h, void* arg) {
267277
}
268278
writer->json_end();
269279
}
270-
280+
void WalkHandleNetwork(uv_handle_t* h, void* arg) {
281+
WalkHandle(h, arg, false);
282+
}
283+
void WalkHandleNoNetwork(uv_handle_t* h, void* arg) {
284+
WalkHandle(h, arg, true);
285+
}
271286
} // namespace report
272287
} // namespace node

test/common/report.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function _validateContent(report, fields = []) {
105105
'glibcVersionRuntime', 'glibcVersionCompiler', 'cwd',
106106
'reportVersion', 'networkInterfaces', 'threadId'];
107107
checkForUnknownFields(header, headerFields);
108-
assert.strictEqual(header.reportVersion, 3); // Increment as needed.
108+
assert.strictEqual(header.reportVersion, 4); // Increment as needed.
109109
assert.strictEqual(typeof header.event, 'string');
110110
assert.strictEqual(typeof header.trigger, 'string');
111111
assert(typeof header.filename === 'string' || header.filename === null);

test/report/test-report-exclude-network.js

+57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
require('../common');
3+
const http = require('node:http');
34
const assert = require('node:assert');
45
const { spawnSync } = require('node:child_process');
56
const tmpdir = require('../common/tmpdir');
@@ -38,4 +39,60 @@ describe('report exclude network option', () => {
3839
const report = process.report.getReport();
3940
assert.strictEqual(report.header.networkInterfaces, undefined);
4041
});
42+
43+
it('should not do DNS queries in libuv if exclude network', async () => {
44+
const server = http.createServer(function(req, res) {
45+
res.writeHead(200, { 'Content-Type': 'text/plain' });
46+
res.end();
47+
});
48+
let ipv6Available = true;
49+
const port = await new Promise((resolve) => server.listen(0, async () => {
50+
await Promise.all([
51+
fetch('http://127.0.0.1:' + server.address().port),
52+
fetch('http://[::1]:' + server.address().port).catch(() => ipv6Available = false),
53+
]);
54+
resolve(server.address().port);
55+
server.close();
56+
}));
57+
process.report.excludeNetwork = false;
58+
let report = process.report.getReport();
59+
let tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
60+
assert.strictEqual(tcp.length, ipv6Available ? 2 : 1);
61+
const findHandle = (local, ip4 = true) => {
62+
return tcp.find(
63+
({ [local ? 'localEndpoint' : 'remoteEndpoint']: ep }) =>
64+
(ep[ip4 ? 'ip4' : 'ip6'] === (ip4 ? '127.0.0.1' : '::1')),
65+
)?.[local ? 'localEndpoint' : 'remoteEndpoint'];
66+
};
67+
try {
68+
// The reverse DNS of 127.0.0.1 can be a lot of things other than localhost
69+
// it could resolve to the server name for instance
70+
assert.notStrictEqual(findHandle(true)?.host, '127.0.0.1');
71+
assert.notStrictEqual(findHandle(false)?.host, '127.0.0.1');
72+
73+
if (ipv6Available) {
74+
assert.notStrictEqual(findHandle(true, false)?.host, '::1');
75+
assert.notStrictEqual(findHandle(false, false)?.host, '::1');
76+
}
77+
} catch (e) {
78+
throw new Error(e?.message + ' in ' + JSON.stringify(tcp, null, 2), { cause: e });
79+
}
80+
81+
process.report.excludeNetwork = true;
82+
report = process.report.getReport();
83+
tcp = report.libuv.filter((uv) => uv.type === 'tcp' && uv.remoteEndpoint?.port === port);
84+
85+
try {
86+
assert.strictEqual(tcp.length, ipv6Available ? 2 : 1);
87+
assert.strictEqual(findHandle(true)?.host, '127.0.0.1');
88+
assert.strictEqual(findHandle(false)?.host, '127.0.0.1');
89+
90+
if (ipv6Available) {
91+
assert.strictEqual(findHandle(true, false)?.host, '::1');
92+
assert.strictEqual(findHandle(false, false)?.host, '::1');
93+
}
94+
} catch (e) {
95+
throw new Error(e?.message + ' in ' + JSON.stringify(tcp, null, 2), { cause: e });
96+
}
97+
});
4198
});

0 commit comments

Comments
 (0)