Skip to content

Commit

Permalink
fixup! src: add cli option to preserve env vars on dr
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Nov 5, 2024
1 parent ac4015d commit d4460ea
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 35 deletions.
5 changes: 3 additions & 2 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -2058,13 +2058,13 @@ Enables report to be generated upon receiving the specified (or predefined)
signal to the running Node.js process. The signal to trigger the report is
specified through `--report-signal`.

### `--report-preserve-env`
### `--report-exclude-env`

<!-- YAML
added: REPLACEME
-->

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

### `--report-signal=signal`
Expand Down Expand Up @@ -3126,6 +3126,7 @@ one is included in the list below.
* `--redirect-warnings`
* `--report-compact`
* `--report-dir`, `--report-directory`
* `--report-exclude-env`
* `--report-exclude-network`
* `--report-filename`
* `--report-on-fatalerror`
Expand Down
2 changes: 1 addition & 1 deletion doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,7 @@ const { report } = require('node:process');
console.log(`Report on exception: ${report.reportOnUncaughtException}`);
```
### `process.report.preserveEnv`
### `process.report.excludeEnv`
<!-- YAML
added: REPLACEME
Expand Down
7 changes: 7 additions & 0 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

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

* `--report-exclude-env` Exclude `environmentVariables` from the
diagnostic report. By default this is not set and the environment
variables are included.

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

```js
Expand Down
9 changes: 7 additions & 2 deletions lib/internal/process/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const {
} = require('internal/validators');
const nr = internalBinding('report');

let excludeEnv;

const report = {
writeReport(file, err) {
if (typeof file === 'object' && file !== null) {
Expand Down Expand Up @@ -105,8 +107,11 @@ const report = {

nr.setReportOnUncaughtException(trigger);
},
get preserveEnv() {
return !nr.shouldPreserveEnvironmentVariables();
get excludeEnv() {
if (excludeEnv === undefined) {
excludeEnv = nr.shouldExcludeEnvironmentVariables();
}
return excludeEnv;
},
};

Expand Down
8 changes: 4 additions & 4 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ inline bool Environment::is_in_heapsnapshot_heap_limit_callback() const {
return is_in_heapsnapshot_heap_limit_callback_;
}

inline bool Environment::report_exclude_env() const {
return options_->report_exclude_env;
}

inline void Environment::AddHeapSnapshotNearHeapLimitCallback() {
DCHECK(!heapsnapshot_near_heap_limit_callback_added_);
heapsnapshot_near_heap_limit_callback_added_ = true;
Expand All @@ -904,10 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
heap_limit);
}

inline bool Environment::ShouldPreserveEnvOnReport() const {
return options_->report_preserve_env;
}

inline void Environment::SetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle,
v8::Global<v8::Value>&& context_frame) {
Expand Down
4 changes: 2 additions & 2 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,12 @@ class Environment final : public MemoryRetainer {
inline void set_heap_snapshot_near_heap_limit(uint32_t limit);
inline bool is_in_heapsnapshot_heap_limit_callback() const;

inline bool report_exclude_env() const;

inline void AddHeapSnapshotNearHeapLimitCallback();

inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit);

inline bool ShouldPreserveEnvOnReport() const;

// Field identifiers for exit_info_
enum ExitInfoField {
kExiting = 0,
Expand Down
9 changes: 5 additions & 4 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,6 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
" (default: current working directory)",
&EnvironmentOptions::diagnostic_dir,
kAllowedInEnvvar);
AddOption("--report-preserve-env",
"Preserve environment variables when generating report",
&EnvironmentOptions::report_preserve_env,
kAllowedInEnvvar);
AddOption("--dns-result-order",
"set default value of verbatim in dns.lookup. Options are "
"'ipv4first' (IPv4 addresses are placed before IPv6 addresses) "
Expand Down Expand Up @@ -881,6 +877,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::tls_max_v1_3,
kAllowedInEnvvar);

AddOption("--report-exclude-env",
"Exclude environment variables when generating report"
" (default: false)",
&EnvironmentOptions::report_exclude_env,
kAllowedInEnvvar);
AddOption("--report-exclude-network",
"exclude network interface diagnostics."
" (default: false)",
Expand Down
2 changes: 1 addition & 1 deletion src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class EnvironmentOptions : public Options {
#endif // HAVE_INSPECTOR
std::string redirect_warnings;
std::string diagnostic_dir;
bool report_preserve_env = false;
std::string env_file;
std::string optional_env_file;
bool has_env_file_string = false;
Expand Down Expand Up @@ -250,6 +249,7 @@ class EnvironmentOptions : public Options {

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

bool report_exclude_env = false;
bool report_exclude_network = false;

inline DebugOptions* get_debug_options() { return &debug_options_; }
Expand Down
47 changes: 39 additions & 8 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ static void WriteNodeReport(Isolate* isolate,
std::ostream& out,
Local<Value> error,
bool compact,
bool exclude_network = false);
bool exclude_network = false,
bool exclude_env = false);
static void PrintVersionInformation(JSONWriter* writer,
bool exclude_network = false);
static void PrintJavaScriptErrorStack(JSONWriter* writer,
Expand Down Expand Up @@ -96,7 +97,8 @@ static void WriteNodeReport(Isolate* isolate,
std::ostream& out,
Local<Value> error,
bool compact,
bool exclude_network) {
bool exclude_network,
bool exclude_env) {
// Obtain the current time and the pid.
TIME_TYPE tm_struct;
DiagnosticFilename::LocalTime(&tm_struct);
Expand Down Expand Up @@ -250,7 +252,7 @@ static void WriteNodeReport(Isolate* isolate,
writer.json_arrayend();

// Report operating system information
if (env->ShouldPreserveEnvOnReport()) {
if (exclude_env == false) {
PrintEnvironmentVariables(&writer);
}
PrintSystemInformation(&writer);
Expand Down Expand Up @@ -921,6 +923,10 @@ std::string TriggerNodeReport(Isolate* isolate,
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
: per_process::cli_options->per_isolate
->per_env->report_exclude_network;
bool exclude_env =
env != nullptr
? env->report_exclude_env()
: per_process::cli_options->per_isolate->per_env->report_exclude_env;

report::WriteNodeReport(isolate,
env,
Expand All @@ -930,7 +936,8 @@ std::string TriggerNodeReport(Isolate* isolate,
*outstream,
error,
compact,
exclude_network);
exclude_network,
exclude_env);

// Do not close stdout/stderr, only close files we opened.
if (outfile.is_open()) {
Expand Down Expand Up @@ -984,8 +991,20 @@ void GetNodeReport(Isolate* isolate,
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
: per_process::cli_options->per_isolate
->per_env->report_exclude_network;
report::WriteNodeReport(
isolate, env, message, trigger, "", out, error, false, exclude_network);
bool exclude_env =
env != nullptr
? env->report_exclude_env()
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
report::WriteNodeReport(isolate,
env,
message,
trigger,
"",
out,
error,
false,
exclude_network,
exclude_env);
}

// External function to trigger a report, writing to a supplied stream.
Expand All @@ -1001,8 +1020,20 @@ void GetNodeReport(Environment* env,
bool exclude_network = env != nullptr ? env->options()->report_exclude_network
: per_process::cli_options->per_isolate
->per_env->report_exclude_network;
report::WriteNodeReport(
isolate, env, message, trigger, "", out, error, false, exclude_network);
bool exclude_env =
env != nullptr
? env->report_exclude_env()
: per_process::cli_options->per_isolate->per_env->report_exclude_env;
report::WriteNodeReport(isolate,
env,
message,
trigger,
"",
out,
error,
false,
exclude_network,
exclude_env);
}

} // namespace node
10 changes: 5 additions & 5 deletions src/node_report_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ static void ShouldReportOnUncaughtException(
env->isolate_data()->options()->report_uncaught_exception);
}

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

static void SetReportOnUncaughtException(
Expand Down Expand Up @@ -210,8 +210,8 @@ static void Initialize(Local<Object> exports,
ShouldReportOnUncaughtException);
SetMethod(context,
exports,
"shouldPreserveEnvironmentVariables",
ShouldPreserveEnvironmentVariables);
"shouldExcludeEnvironmentVariables",
ShouldExcludeEnvironmentVariables);
SetMethod(context,
exports,
"setReportOnUncaughtException",
Expand All @@ -236,7 +236,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ShouldReportOnSignal);
registry->Register(SetReportOnSignal);
registry->Register(ShouldReportOnUncaughtException);
registry->Register(ShouldPreserveEnvironmentVariables);
registry->Register(ShouldExcludeEnvironmentVariables);
registry->Register(SetReportOnUncaughtException);
}

Expand Down
13 changes: 8 additions & 5 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ function _validateContent(report, fields = []) {
const sections = ['header', 'nativeStack', 'javascriptStack', 'libuv',
'sharedObjects', 'resourceUsage', 'workers'];

if (!process.report.preserveEnv)
if (!process.report.excludeEnv) {
sections.push('environmentVariables');
}

if (!isWindows)
sections.push('userLimits');
Expand Down Expand Up @@ -298,10 +299,12 @@ function _validateContent(report, fields = []) {
resource.type === 'loop' ? 'undefined' : 'boolean');
});

// Verify the format of the environmentVariables section.
for (const [key, value] of Object.entries(report.environmentVariables)) {
assert.strictEqual(typeof key, 'string');
assert.strictEqual(typeof value, 'string');
if (!process.report.excludeEnv) {
// Verify the format of the environmentVariables section.
for (const [key, value] of Object.entries(report.environmentVariables)) {
assert.strictEqual(typeof key, 'string');
assert.strictEqual(typeof value, 'string');
}
}

// Verify the format of the userLimits section on non-Windows platforms.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Flags: --report-preserve-env
// Flags: --report-exclude-env
'use strict';

// Test producing a report via API call, using the no-hooks/no-signal interface.
Expand Down

0 comments on commit d4460ea

Please sign in to comment.