Skip to content

Commit d4460ea

Browse files
committed
fixup! src: add cli option to preserve env vars on dr
1 parent ac4015d commit d4460ea

File tree

12 files changed

+83
-35
lines changed

12 files changed

+83
-35
lines changed

doc/api/cli.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,13 +2058,13 @@ Enables report to be generated upon receiving the specified (or predefined)
20582058
signal to the running Node.js process. The signal to trigger the report is
20592059
specified through `--report-signal`.
20602060

2061-
### `--report-preserve-env`
2061+
### `--report-exclude-env`
20622062

20632063
<!-- YAML
20642064
added: REPLACEME
20652065
-->
20662066

2067-
When `--report-preserve-env` is passed the diagnostic report generated will not
2067+
When `--report-exclude-env` is passed the diagnostic report generated will not
20682068
contain the `environmentVariables` data.
20692069

20702070
### `--report-signal=signal`
@@ -3126,6 +3126,7 @@ one is included in the list below.
31263126
* `--redirect-warnings`
31273127
* `--report-compact`
31283128
* `--report-dir`, `--report-directory`
3129+
* `--report-exclude-env`
31293130
* `--report-exclude-network`
31303131
* `--report-filename`
31313132
* `--report-on-fatalerror`

doc/api/process.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ const { report } = require('node:process');
34943494
console.log(`Report on exception: ${report.reportOnUncaughtException}`);
34953495
```
34963496
3497-
### `process.report.preserveEnv`
3497+
### `process.report.excludeEnv`
34983498
34993499
<!-- YAML
35003500
added: REPLACEME

doc/api/report.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
<!-- YAML
1212
changes:
13+
- version: REPLACEME
14+
pr-url: https://github.com/nodejs/node/pull/55697
15+
description: Added `--report-exclude-env` option for excluding environment variables from report generation.
1316
- version:
1417
- v22.0.0
1518
- v20.13.0
@@ -465,6 +468,10 @@ meaning of `SIGUSR2` for the said purposes.
465468
diagnostic report. By default this is not set and the network interfaces
466469
are included.
467470

471+
* `--report-exclude-env` Exclude `environmentVariables` from the
472+
diagnostic report. By default this is not set and the environment
473+
variables are included.
474+
468475
A report can also be triggered via an API call from a JavaScript application:
469476

470477
```js

lib/internal/process/report.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const {
1616
} = require('internal/validators');
1717
const nr = internalBinding('report');
1818

19+
let excludeEnv;
20+
1921
const report = {
2022
writeReport(file, err) {
2123
if (typeof file === 'object' && file !== null) {
@@ -105,8 +107,11 @@ const report = {
105107

106108
nr.setReportOnUncaughtException(trigger);
107109
},
108-
get preserveEnv() {
109-
return !nr.shouldPreserveEnvironmentVariables();
110+
get excludeEnv() {
111+
if (excludeEnv === undefined) {
112+
excludeEnv = nr.shouldExcludeEnvironmentVariables();
113+
}
114+
return excludeEnv;
110115
},
111116
};
112117

src/env-inl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@ inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
890890
return is_in_heapsnapshot_heap_limit_callback_;
891891
}
892892

893+
inline bool Environment::report_exclude_env() const {
894+
return options_->report_exclude_env;
895+
}
896+
893897
inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
894898
DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
895899
heapsnapshot_near_heap_limit_callback_added_ = true;
@@ -904,10 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
904908
heap_limit);
905909
}
906910

907-
inline bool Environment::ShouldPreserveEnvOnReport() const {
908-
return options_->report_preserve_env;
909-
}
910-
911911
inline void Environment::SetAsyncResourceContextFrame(
912912
std::uintptr_t async_resource_handle,
913913
v8::Global<v8::Value>&& context_frame) {

src/env.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,12 @@ class Environment final : public MemoryRetainer {
10441044
inline void set_heap_snapshot_near_heap_limit(uint32_t limit);
10451045
inline bool is_in_heapsnapshot_heap_limit_callback() const;
10461046

1047+
inline bool report_exclude_env() const;
1048+
10471049
inline void AddHeapSnapshotNearHeapLimitCallback();
10481050

10491051
inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit);
10501052

1051-
inline bool ShouldPreserveEnvOnReport() const;
1052-
10531053
// Field identifiers for exit_info_
10541054
enum ExitInfoField {
10551055
kExiting = 0,

src/node_options.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
383383
" (default: current working directory)",
384384
&EnvironmentOptions::diagnostic_dir,
385385
kAllowedInEnvvar);
386-
AddOption("--report-preserve-env",
387-
"Preserve environment variables when generating report",
388-
&EnvironmentOptions::report_preserve_env,
389-
kAllowedInEnvvar);
390386
AddOption("--dns-result-order",
391387
"set default value of verbatim in dns.lookup. Options are "
392388
"'ipv4first' (IPv4 addresses are placed before IPv6 addresses) "
@@ -881,6 +877,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
881877
&EnvironmentOptions::tls_max_v1_3,
882878
kAllowedInEnvvar);
883879

880+
AddOption("--report-exclude-env",
881+
"Exclude environment variables when generating report"
882+
" (default: false)",
883+
&EnvironmentOptions::report_exclude_env,
884+
kAllowedInEnvvar);
884885
AddOption("--report-exclude-network",
885886
"exclude network interface diagnostics."
886887
" (default: false)",

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ class EnvironmentOptions : public Options {
177177
#endif // HAVE_INSPECTOR
178178
std::string redirect_warnings;
179179
std::string diagnostic_dir;
180-
bool report_preserve_env = false;
181180
std::string env_file;
182181
std::string optional_env_file;
183182
bool has_env_file_string = false;
@@ -250,6 +249,7 @@ class EnvironmentOptions : public Options {
250249

251250
std::vector<std::string> user_argv;
252251

252+
bool report_exclude_env = false;
253253
bool report_exclude_network = false;
254254

255255
inline DebugOptions* get_debug_options() { return &debug_options_; }

src/node_report.cc

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ static void WriteNodeReport(Isolate* isolate,
6161
std::ostream& out,
6262
Local<Value> error,
6363
bool compact,
64-
bool exclude_network = false);
64+
bool exclude_network = false,
65+
bool exclude_env = false);
6566
static void PrintVersionInformation(JSONWriter* writer,
6667
bool exclude_network = false);
6768
static void PrintJavaScriptErrorStack(JSONWriter* writer,
@@ -96,7 +97,8 @@ static void WriteNodeReport(Isolate* isolate,
9697
std::ostream& out,
9798
Local<Value> error,
9899
bool compact,
99-
bool exclude_network) {
100+
bool exclude_network,
101+
bool exclude_env) {
100102
// Obtain the current time and the pid.
101103
TIME_TYPE tm_struct;
102104
DiagnosticFilename::LocalTime(&tm_struct);
@@ -250,7 +252,7 @@ static void WriteNodeReport(Isolate* isolate,
250252
writer.json_arrayend();
251253

252254
// Report operating system information
253-
if (env->ShouldPreserveEnvOnReport()) {
255+
if (exclude_env == false) {
254256
PrintEnvironmentVariables(&writer);
255257
}
256258
PrintSystemInformation(&writer);
@@ -921,6 +923,10 @@ std::string TriggerNodeReport(Isolate* isolate,
921923
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
922924
: per_process::cli_options->per_isolate
923925
->per_env->report_exclude_network;
926+
bool exclude_env =
927+
env != nullptr
928+
? env->report_exclude_env()
929+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
924930

925931
report::WriteNodeReport(isolate,
926932
env,
@@ -930,7 +936,8 @@ std::string TriggerNodeReport(Isolate* isolate,
930936
*outstream,
931937
error,
932938
compact,
933-
exclude_network);
939+
exclude_network,
940+
exclude_env);
934941

935942
// Do not close stdout/stderr, only close files we opened.
936943
if (outfile.is_open()) {
@@ -984,8 +991,20 @@ void GetNodeReport(Isolate* isolate,
984991
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
985992
: per_process::cli_options->per_isolate
986993
->per_env->report_exclude_network;
987-
report::WriteNodeReport(
988-
isolate, env, message, trigger, "", out, error, false, exclude_network);
994+
bool exclude_env =
995+
env != nullptr
996+
? env->report_exclude_env()
997+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
998+
report::WriteNodeReport(isolate,
999+
env,
1000+
message,
1001+
trigger,
1002+
"",
1003+
out,
1004+
error,
1005+
false,
1006+
exclude_network,
1007+
exclude_env);
9891008
}
9901009

9911010
// External function to trigger a report, writing to a supplied stream.
@@ -1001,8 +1020,20 @@ void GetNodeReport(Environment* env,
10011020
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
10021021
: per_process::cli_options->per_isolate
10031022
->per_env->report_exclude_network;
1004-
report::WriteNodeReport(
1005-
isolate, env, message, trigger, "", out, error, false, exclude_network);
1023+
bool exclude_env =
1024+
env != nullptr
1025+
? env->report_exclude_env()
1026+
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
1027+
report::WriteNodeReport(isolate,
1028+
env,
1029+
message,
1030+
trigger,
1031+
"",
1032+
out,
1033+
error,
1034+
false,
1035+
exclude_network,
1036+
exclude_env);
10061037
}
10071038

10081039
} // namespace node

src/node_report_module.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ static void ShouldReportOnUncaughtException(
170170
env->isolate_data()->options()->report_uncaught_exception);
171171
}
172172

173-
static void ShouldPreserveEnvironmentVariables(
173+
static void ShouldExcludeEnvironmentVariables(
174174
const FunctionCallbackInfo<Value>& info) {
175175
Environment* env = Environment::GetCurrent(info);
176-
info.GetReturnValue().Set(env->ShouldPreserveEnvOnReport());
176+
info.GetReturnValue().Set(env->report_exclude_env());
177177
}
178178

179179
static void SetReportOnUncaughtException(
@@ -210,8 +210,8 @@ static void Initialize(Local<Object> exports,
210210
ShouldReportOnUncaughtException);
211211
SetMethod(context,
212212
exports,
213-
"shouldPreserveEnvironmentVariables",
214-
ShouldPreserveEnvironmentVariables);
213+
"shouldExcludeEnvironmentVariables",
214+
ShouldExcludeEnvironmentVariables);
215215
SetMethod(context,
216216
exports,
217217
"setReportOnUncaughtException",
@@ -236,7 +236,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
236236
registry->Register(ShouldReportOnSignal);
237237
registry->Register(SetReportOnSignal);
238238
registry->Register(ShouldReportOnUncaughtException);
239-
registry->Register(ShouldPreserveEnvironmentVariables);
239+
registry->Register(ShouldExcludeEnvironmentVariables);
240240
registry->Register(SetReportOnUncaughtException);
241241
}
242242

0 commit comments

Comments
 (0)