Skip to content

Commit 2d5fb4e

Browse files
report: add --report-network-disabled option
Adds a new option `process.report.networkDisabled` and cli option `--report-network-disabled` which will disable any netowkring operations for the `report` generation. Fixes: #46060
1 parent 68885d5 commit 2d5fb4e

File tree

10 files changed

+123
-9
lines changed

10 files changed

+123
-9
lines changed

doc/api/report.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
99
<!-- name=report -->
1010

11+
<!-- YAML
12+
added: REPLACEME
13+
-->
14+
1115
Delivers a JSON-formatted diagnostic summary, written to a file.
1216

1317
The report is intended for development, test, and production use, to capture
@@ -452,6 +456,9 @@ meaning of `SIGUSR2` for the said purposes.
452456
* `--report-signal` Sets or resets the signal for report generation
453457
(not supported on Windows). Default signal is `SIGUSR2`.
454458

459+
* `--report-network-disabled` Disable the `header.networkInterfaces` reporting.
460+
Default is `false`.
461+
455462
A report can also be triggered via an API call from a JavaScript application:
456463

457464
```js
@@ -571,6 +578,8 @@ timestamp, PID, and sequence number.
571578
written. URLs are not supported. Defaults to the current working directory of
572579
the Node.js process.
573580

581+
`networkDisabled` disables the `header.networkInterfaces` reporting.
582+
574583
```js
575584
// Trigger report only on uncaught exceptions.
576585
process.report.reportOnFatalError = false;
@@ -587,6 +596,9 @@ process.report.reportOnFatalError = false;
587596
process.report.reportOnUncaughtException = false;
588597
process.report.reportOnSignal = true;
589598
process.report.signal = 'SIGQUIT';
599+
600+
// Disable network interfaces reporting
601+
process.report.networkDisabled = true;
590602
```
591603

592604
Configuration on module initialization is also available via

lib/internal/process/report.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ const report = {
6060
validateBoolean(b, 'compact');
6161
nr.setCompact(b);
6262
},
63+
get networkDisabled() {
64+
return nr.getNetworkDisabled();
65+
},
66+
set networkDisabled(b) {
67+
validateBoolean(b, 'networkDisabled');
68+
nr.setNetworkDisabled(b);
69+
},
6370
get signal() {
6471
return nr.getSignal();
6572
},

src/node_options.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
785785
"set default TLS maximum to TLSv1.3 (default: TLSv1.3)",
786786
&EnvironmentOptions::tls_max_v1_3,
787787
kAllowedInEnvvar);
788+
789+
AddOption("--report-network-disabled",
790+
"disable network interface diagnostics."
791+
" (default: false)",
792+
&EnvironmentOptions::report_network_disabled,
793+
kAllowedInEnvvar);
788794
}
789795

790796
PerIsolateOptionsParser::PerIsolateOptionsParser(

src/node_options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ class EnvironmentOptions : public Options {
218218

219219
std::vector<std::string> user_argv;
220220

221+
bool report_network_disabled = false;
222+
221223
inline DebugOptions* get_debug_options() { return &debug_options_; }
222224
inline const DebugOptions& debug_options() const { return debug_options_; }
223225

src/node_report.cc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ static void WriteNodeReport(Isolate* isolate,
6161
const std::string& filename,
6262
std::ostream& out,
6363
Local<Value> error,
64-
bool compact);
65-
static void PrintVersionInformation(JSONWriter* writer);
64+
bool compact,
65+
bool network_disabled = false);
66+
static void PrintVersionInformation(JSONWriter* writer,
67+
bool network_disabled = false);
6668
static void PrintJavaScriptErrorStack(JSONWriter* writer,
6769
Isolate* isolate,
6870
Local<Value> error,
@@ -93,7 +95,8 @@ static void WriteNodeReport(Isolate* isolate,
9395
const std::string& filename,
9496
std::ostream& out,
9597
Local<Value> error,
96-
bool compact) {
98+
bool compact,
99+
bool network_disabled) {
97100
// Obtain the current time and the pid.
98101
TIME_TYPE tm_struct;
99102
DiagnosticFilename::LocalTime(&tm_struct);
@@ -174,7 +177,7 @@ static void WriteNodeReport(Isolate* isolate,
174177
}
175178

176179
// Report Node.js and OS version information
177-
PrintVersionInformation(&writer);
180+
PrintVersionInformation(&writer, network_disabled);
178181
writer.json_objectend();
179182

180183
if (isolate != nullptr) {
@@ -256,7 +259,7 @@ static void WriteNodeReport(Isolate* isolate,
256259
}
257260

258261
// Report Node.js version, OS version and machine information.
259-
static void PrintVersionInformation(JSONWriter* writer) {
262+
static void PrintVersionInformation(JSONWriter* writer, bool network_disabled) {
260263
std::ostringstream buf;
261264
// Report Node version
262265
buf << "v" << NODE_VERSION_STRING;
@@ -300,7 +303,8 @@ static void PrintVersionInformation(JSONWriter* writer) {
300303
}
301304

302305
PrintCpuInfo(writer);
303-
PrintNetworkInterfaceInfo(writer);
306+
if (!network_disabled)
307+
PrintNetworkInterfaceInfo(writer);
304308

305309
char host[UV_MAXHOSTNAMESIZE];
306310
size_t host_size = sizeof(host);
@@ -917,8 +921,18 @@ std::string TriggerNodeReport(Isolate* isolate,
917921
compact = per_process::cli_options->report_compact;
918922
}
919923

924+
bool network_disabled = env->options()->report_network_disabled;
925+
920926
report::WriteNodeReport(
921-
isolate, env, message, trigger, filename, *outstream, error, compact);
927+
isolate,
928+
env,
929+
message,
930+
trigger,
931+
filename,
932+
*outstream,
933+
error,
934+
compact,
935+
network_disabled);
922936

923937
// Do not close stdout/stderr, only close files we opened.
924938
if (outfile.is_open()) {
@@ -969,8 +983,9 @@ void GetNodeReport(Isolate* isolate,
969983
if (isolate != nullptr) {
970984
env = Environment::GetCurrent(isolate);
971985
}
986+
bool network_disabled = env->options()->report_network_disabled;
972987
report::WriteNodeReport(
973-
isolate, env, message, trigger, "", out, error, false);
988+
isolate, env, message, trigger, "", out, error, false, network_disabled);
974989
}
975990

976991
// External function to trigger a report, writing to a supplied stream.
@@ -983,8 +998,9 @@ void GetNodeReport(Environment* env,
983998
if (env != nullptr) {
984999
isolate = env->isolate();
9851000
}
1001+
bool network_disabled = env->options()->report_network_disabled;
9861002
report::WriteNodeReport(
987-
isolate, env, message, trigger, "", out, error, false);
1003+
isolate, env, message, trigger, "", out, error, false, network_disabled);
9881004
}
9891005

9901006
} // namespace node

src/node_report_module.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ static void SetCompact(const FunctionCallbackInfo<Value>& info) {
8484
per_process::cli_options->report_compact = compact;
8585
}
8686

87+
static void GetNetworkDisabled(const FunctionCallbackInfo<Value>& info) {
88+
Environment* env = Environment::GetCurrent(info);
89+
info.GetReturnValue().Set(env->options()->report_network_disabled);
90+
}
91+
92+
static void SetNetworkDisabled(const FunctionCallbackInfo<Value>& info) {
93+
Mutex::ScopedLock lock(per_process::cli_options_mutex);
94+
Environment* env = Environment::GetCurrent(info);
95+
Isolate* isolate = env->isolate();
96+
env->options()->report_network_disabled
97+
= info[0]->ToBoolean(isolate)->Value();
98+
}
99+
87100
static void GetDirectory(const FunctionCallbackInfo<Value>& info) {
88101
Mutex::ScopedLock lock(per_process::cli_options_mutex);
89102
Environment* env = Environment::GetCurrent(info);
@@ -174,6 +187,8 @@ static void Initialize(Local<Object> exports,
174187
SetMethod(context, exports, "getReport", GetReport);
175188
SetMethod(context, exports, "getCompact", GetCompact);
176189
SetMethod(context, exports, "setCompact", SetCompact);
190+
SetMethod(context, exports, "getNetworkDisabled", GetNetworkDisabled);
191+
SetMethod(context, exports, "setNetworkDisabled", SetNetworkDisabled);
177192
SetMethod(context, exports, "getDirectory", GetDirectory);
178193
SetMethod(context, exports, "setDirectory", SetDirectory);
179194
SetMethod(context, exports, "getFilename", GetFilename);
@@ -200,6 +215,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
200215
registry->Register(GetReport);
201216
registry->Register(GetCompact);
202217
registry->Register(SetCompact);
218+
registry->Register(GetNetworkDisabled);
219+
registry->Register(SetNetworkDisabled);
203220
registry->Register(GetDirectory);
204221
registry->Register(SetDirectory);
205222
registry->Register(GetFilename);

test/cctest/test_report.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node.h"
2+
#include "node_report.h"
23

34
#include <string>
45
#include "gtest/gtest.h"

test/report/test-report-config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ assert.throws(() => {
6666
}, { code: 'ERR_INVALID_ARG_TYPE' });
6767
assert.strictEqual(process.report.compact, true);
6868

69+
// Verify that process.report.networkDisabled behaves properly.
70+
assert.strictEqual(process.report.networkDisabled, false);
71+
process.report.networkDisabled = true;
72+
assert.strictEqual(process.report.networkDisabled, true);
73+
process.report.networkDisabled = false;
74+
assert.strictEqual(process.report.networkDisabled, false);
75+
assert.throws(() => {
76+
process.report.networkDisabled = {};
77+
}, { code: 'ERR_INVALID_ARG_TYPE' });
78+
assert.strictEqual(process.report.networkDisabled, false);
79+
6980
if (!common.isWindows) {
7081
// Verify that process.report.signal behaves properly.
7182
assert.strictEqual(process.report.signal, 'SIGUSR2');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('node:assert');
4+
const { spawnSync } = require('node:child_process');
5+
const tmpdir = require('../common/tmpdir');
6+
const { describe, it, before } = require('node:test');
7+
const fs = require('node:fs');
8+
const helper = require('../common/report');
9+
10+
function validate(pid) {
11+
const reports = helper.findReports(pid, tmpdir.path);
12+
assert.strictEqual(reports.length, 1);
13+
let report = fs.readFileSync(reports[0], { encoding: 'utf8' });
14+
report = JSON.parse(report);
15+
assert.strictEqual(report.header.networkInterfaces, undefined);
16+
fs.unlinkSync(reports[0]);
17+
}
18+
19+
describe('report network disabled option', () => {
20+
before(() => {
21+
tmpdir.refresh();
22+
process.report.directory = tmpdir.path;
23+
});
24+
25+
it('should be configurable with --report-network-disabled', () => {
26+
const args = ['--report-network-disabled', '-e', 'process.report.writeReport()'];
27+
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
28+
assert.strictEqual(child.status, 0);
29+
assert.strictEqual(child.signal, null);
30+
validate(child.pid);
31+
});
32+
33+
it('should be configurable with report.networkDisabled', () => {
34+
process.report.networkDisabled = true;
35+
process.report.writeReport();
36+
validate(process.pid);
37+
38+
const report = process.report.getReport();
39+
assert.strictEqual(report.header.networkInterfaces, undefined);
40+
});
41+
});

test/report/test-report.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)