Skip to content

Commit

Permalink
fix(): do not assign a non-string to process.env
Browse files Browse the repository at this point in the history
Assigning a value to process.env[] leads to stringification of the
value. Example:

```
> process.env.test = {}
> process.env.test
'[object Object]'
```

This problem was hidden because `ConfigService.get` returns the value
of the validated config which would not be stringified.

A special case is when an environment variable has the value
`undefined`. This is because `ConfigService.get` falls back to
process.env which returns the stringified form `'undefined'`.

The stringification is deprecated and might result in a runtime error
in the future. See https://nodejs.org/api/process.html#processenv.

The argument has been changed to Record<string, unknown> to be more
type-safe. If this type was used in the original code, this bug could
have been avoided.
  • Loading branch information
MatthiasKunnen committed Jun 9, 2023
1 parent 5ee253c commit efdb29e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,18 @@ export class ConfigModule {
return config;
}

private static assignVariablesToProcess(config: Record<string, any>) {
private static assignVariablesToProcess(config: Record<string, unknown>) {
if (!isObject(config)) {
return;
}
const keys = Object.keys(config).filter(key => !(key in process.env));
keys.forEach(
key => (process.env[key] = (config as Record<string, any>)[key]),
key => {
const value = config[key];
if (typeof value === 'string') {
process.env[key] = value;
}
},
);
}

Expand Down

0 comments on commit efdb29e

Please sign in to comment.