Skip to content

Commit ac4015d

Browse files
committed
src: add cli option to preserve env vars on dr
1 parent 5ce3d10 commit ac4015d

File tree

11 files changed

+137
-3
lines changed

11 files changed

+137
-3
lines changed

doc/api/cli.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,15 @@ 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`
2062+
2063+
<!-- YAML
2064+
added: REPLACEME
2065+
-->
2066+
2067+
When `--report-preserve-env` is passed the diagnostic report generated will not
2068+
contain the `environmentVariables` data.
2069+
20612070
### `--report-signal=signal`
20622071

20632072
<!-- YAML

doc/api/process.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,6 +3494,16 @@ const { report } = require('node:process');
34943494
console.log(`Report on exception: ${report.reportOnUncaughtException}`);
34953495
```
34963496
3497+
### `process.report.preserveEnv`
3498+
3499+
<!-- YAML
3500+
added: REPLACEME
3501+
-->
3502+
3503+
* {boolean}
3504+
3505+
If `true`, a diagnostic report is generated without the environment variables.
3506+
34973507
### `process.report.signal`
34983508
34993509
<!-- YAML

lib/internal/process/report.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ const report = {
105105

106106
nr.setReportOnUncaughtException(trigger);
107107
},
108+
get preserveEnv() {
109+
return !nr.shouldPreserveEnvironmentVariables();
110+
},
108111
};
109112

110113
function addSignalHandler(sig) {

src/env-inl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,10 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
904904
heap_limit);
905905
}
906906

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

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,8 @@ class Environment final : public MemoryRetainer {
10481048

10491049
inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit);
10501050

1051+
inline bool ShouldPreserveEnvOnReport() const;
1052+
10511053
// Field identifiers for exit_info_
10521054
enum ExitInfoField {
10531055
kExiting = 0,

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ 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);
386390
AddOption("--dns-result-order",
387391
"set default value of verbatim in dns.lookup. Options are "
388392
"'ipv4first' (IPv4 addresses are placed before IPv6 addresses) "

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class EnvironmentOptions : public Options {
177177
#endif // HAVE_INSPECTOR
178178
std::string redirect_warnings;
179179
std::string diagnostic_dir;
180+
bool report_preserve_env = false;
180181
std::string env_file;
181182
std::string optional_env_file;
182183
bool has_env_file_string = false;

src/node_report.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static void PrintJavaScriptErrorProperties(JSONWriter* writer,
7878
static void PrintNativeStack(JSONWriter* writer);
7979
static void PrintResourceUsage(JSONWriter* writer);
8080
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
81+
static void PrintEnvironmentVariables(JSONWriter* writer);
8182
static void PrintSystemInformation(JSONWriter* writer);
8283
static void PrintLoadedLibraries(JSONWriter* writer);
8384
static void PrintComponentVersions(JSONWriter* writer);
@@ -249,6 +250,9 @@ static void WriteNodeReport(Isolate* isolate,
249250
writer.json_arrayend();
250251

251252
// Report operating system information
253+
if (env->ShouldPreserveEnvOnReport()) {
254+
PrintEnvironmentVariables(&writer);
255+
}
252256
PrintSystemInformation(&writer);
253257

254258
writer.json_objectend();
@@ -694,8 +698,7 @@ static void PrintResourceUsage(JSONWriter* writer) {
694698
#endif // RUSAGE_THREAD
695699
}
696700

697-
// Report operating system information.
698-
static void PrintSystemInformation(JSONWriter* writer) {
701+
static void PrintEnvironmentVariables(JSONWriter* writer) {
699702
uv_env_item_t* envitems;
700703
int envcount;
701704
int r;
@@ -715,7 +718,10 @@ static void PrintSystemInformation(JSONWriter* writer) {
715718
}
716719

717720
writer->json_objectend();
721+
}
718722

723+
// Report operating system information.
724+
static void PrintSystemInformation(JSONWriter* writer) {
719725
#ifndef _WIN32
720726
static struct {
721727
const char* description;

src/node_report_module.cc

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

173+
static void ShouldPreserveEnvironmentVariables(
174+
const FunctionCallbackInfo<Value>& info) {
175+
Environment* env = Environment::GetCurrent(info);
176+
info.GetReturnValue().Set(env->ShouldPreserveEnvOnReport());
177+
}
178+
173179
static void SetReportOnUncaughtException(
174180
const FunctionCallbackInfo<Value>& info) {
175181
Environment* env = Environment::GetCurrent(info);
@@ -202,6 +208,10 @@ static void Initialize(Local<Object> exports,
202208
exports,
203209
"shouldReportOnUncaughtException",
204210
ShouldReportOnUncaughtException);
211+
SetMethod(context,
212+
exports,
213+
"shouldPreserveEnvironmentVariables",
214+
ShouldPreserveEnvironmentVariables);
205215
SetMethod(context,
206216
exports,
207217
"setReportOnUncaughtException",
@@ -226,6 +236,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
226236
registry->Register(ShouldReportOnSignal);
227237
registry->Register(SetReportOnSignal);
228238
registry->Register(ShouldReportOnUncaughtException);
239+
registry->Register(ShouldPreserveEnvironmentVariables);
229240
registry->Register(SetReportOnUncaughtException);
230241
}
231242

test/common/report.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ function _validateContent(report, fields = []) {
5959

6060
// Verify that all sections are present as own properties of the report.
6161
const sections = ['header', 'nativeStack', 'javascriptStack', 'libuv',
62-
'environmentVariables', 'sharedObjects', 'resourceUsage', 'workers'];
62+
'sharedObjects', 'resourceUsage', 'workers'];
63+
64+
if (!process.report.preserveEnv)
65+
sections.push('environmentVariables');
66+
6367
if (!isWindows)
6468
sections.push('userLimits');
6569

0 commit comments

Comments
 (0)