Skip to content

Commit

Permalink
report: fix network queries in getReport libuv with exclude-network
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Oct 30, 2024
1 parent 26dae59 commit 148fca5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static void WriteNodeReport(Isolate* isolate,

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

writer.json_start();
writer.json_keyvalue("type", "loop");
Expand Down
3 changes: 2 additions & 1 deletion src/node_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
namespace node {
namespace report {
// Function declarations - utility functions in src/node_report_utils.cc
void WalkHandle(uv_handle_t* h, void* arg);
void WalkHandleNetwork(uv_handle_t* h, void* arg);
void WalkHandleNoNetwork(uv_handle_t* h, void* arg);

template <typename T>
std::string ValueToHexString(T value) {
Expand Down
22 changes: 14 additions & 8 deletions src/node_report_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static constexpr auto null = JSONWriter::Null{};
static void ReportEndpoint(uv_handle_t* h,
struct sockaddr* addr,
const char* name,
JSONWriter* writer) {
JSONWriter* writer,
bool exclude_network) {
if (addr == nullptr) {
writer->json_keyvalue(name, null);
return;
Expand All @@ -26,7 +27,7 @@ static void ReportEndpoint(uv_handle_t* h,
reinterpret_cast<sockaddr_in*>(addr)->sin_port :
reinterpret_cast<sockaddr_in6*>(addr)->sin6_port);

if (uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
if (!exclude_network && uv_getnameinfo(h->loop, &endpoint, nullptr, addr, NI_NUMERICSERV) == 0) {
host = endpoint.host;
DCHECK_EQ(port, std::stoi(endpoint.service));
} else {
Expand All @@ -48,7 +49,7 @@ static void ReportEndpoint(uv_handle_t* h,
}

// Utility function to format libuv socket information.
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer, bool exclude_network) {
struct sockaddr_storage addr_storage;
struct sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
Expand All @@ -65,7 +66,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
default:
break;
}
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer);
ReportEndpoint(h, rc == 0 ? addr : nullptr, "localEndpoint", writer, exclude_network);

switch (h->type) {
case UV_UDP:
Expand All @@ -77,7 +78,7 @@ static void ReportEndpoints(uv_handle_t* h, JSONWriter* writer) {
default:
break;
}
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer);
ReportEndpoint(h, rc == 0 ? addr : nullptr, "remoteEndpoint", writer, exclude_network);
}

// Utility function to format libuv pipe information.
Expand Down Expand Up @@ -155,7 +156,7 @@ static void ReportPath(uv_handle_t* h, JSONWriter* writer) {
}

// Utility function to walk libuv handles.
void WalkHandle(uv_handle_t* h, void* arg) {
void WalkHandle(uv_handle_t* h, void* arg, bool exclude_network = false) {
const char* type = uv_handle_type_name(h->type);
JSONWriter* writer = static_cast<JSONWriter*>(arg);
uv_any_handle* handle = reinterpret_cast<uv_any_handle*>(h);
Expand All @@ -177,7 +178,7 @@ void WalkHandle(uv_handle_t* h, void* arg) {
break;
case UV_TCP:
case UV_UDP:
ReportEndpoints(h, writer);
ReportEndpoints(h, writer, exclude_network);
break;
case UV_NAMED_PIPE:
ReportPipeEndpoints(h, writer);
Expand Down Expand Up @@ -267,6 +268,11 @@ void WalkHandle(uv_handle_t* h, void* arg) {
}
writer->json_end();
}

void WalkHandleNetwork(uv_handle_t* h, void* arg) {
WalkHandle(h, arg, false);
}
void WalkHandleNoNetwork(uv_handle_t* h, void* arg) {
WalkHandle(h, arg, true);
}
} // namespace report
} // namespace node
26 changes: 26 additions & 0 deletions test/report/test-report-exclude-network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
require('../common');
const http = require('node:http');
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const tmpdir = require('../common/tmpdir');
Expand Down Expand Up @@ -38,4 +39,29 @@ describe('report exclude network option', () => {
const report = process.report.getReport();
assert.strictEqual(report.header.networkInterfaces, undefined);
});

it('should not do DNS queries in libuv if exclude network', async () => {
const server = http.createServer(function (req, res) {

Check failure on line 44 in test/report/test-report-exclude-network.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected space before function parentheses
res.writeHead(200, {'Content-Type': 'text/plain'});

Check failure on line 45 in test/report/test-report-exclude-network.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

A space is required after '{'

Check failure on line 45 in test/report/test-report-exclude-network.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

A space is required before '}'
res.end();
});
await new Promise((resolve) => server.listen(0, async () => {
await fetch('http://127.0.0.1:' + server.address().port);
server.close();
resolve();
}));
process.report.excludeNetwork = false;
let report = process.report.getReport();
let tcp = report.libuv.find((uv) => uv.type === 'tcp' && uv.writable === true);
assert.notEqual(tcp, null);

Check failure on line 56 in test/report/test-report-exclude-network.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'assert.notEqual' is restricted from being used. Use `assert.notStrictEqual()` rather than `assert.notEqual()`
assert.strictEqual(tcp.localEndpoint.host, 'localhost');
assert.strictEqual(tcp.remoteEndpoint.host, 'localhost');

process.report.excludeNetwork = true;
report = process.report.getReport();
tcp = report.libuv.find((uv) => uv.type === 'tcp' && uv.writable === true);
assert.notEqual(tcp, null);

Check failure on line 63 in test/report/test-report-exclude-network.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'assert.notEqual' is restricted from being used. Use `assert.notStrictEqual()` rather than `assert.notEqual()`
assert.strictEqual(tcp.localEndpoint.host, '127.0.0.1');
assert.strictEqual(tcp.remoteEndpoint.host, '127.0.0.1');
});
});

0 comments on commit 148fca5

Please sign in to comment.