Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: don't overwrite environment from .env file #49424

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void Dotenv::SetEnvironment(node::Environment* env) {
for (const auto& entry : store_) {
auto key = entry.first;
auto value = entry.second;

auto existing = env->env_vars()->Get(key.data());

if (existing.IsNothing()) {
philnash marked this conversation as resolved.
Show resolved Hide resolved
env->env_vars()->Set(
isolate,
v8::String::NewFromUtf8(
Expand All @@ -58,6 +62,7 @@ void Dotenv::SetEnvironment(node::Environment* env) {
.ToLocalChecked());
}
}
}

void Dotenv::ParsePath(const std::string_view path) {
uv_fs_t req;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
RETAIN_INNER_QUOTES_AS_BACKTICKS=`{"foo": "bar's"}`
TRIM_SPACE_FROM_UNQUOTED= some spaced out string
USERNAME=therealnerdybeast@example.tld
EMAIL=therealnerdybeast@example.tld
philnash marked this conversation as resolved.
Show resolved Hide resolved
SPACED_KEY = parsed
13 changes: 13 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0);
});

it('should not override existing environment variables but introduce new vars', async () => {
const code = `
require('assert').strictEqual(process.env.BASIC, 'existing');
require('assert').strictEqual(process.env.AFTER_LINE, 'after_line');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${validEnvFilePath}`, '--eval', code ],
{ cwd: __dirname, env: { ...process.env, BASIC: 'existing' } },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
});
7 changes: 6 additions & 1 deletion test/parallel/test-dotenv-node-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ describe('.env supports NODE_OPTIONS', () => {
const code = `
require('assert')(new Date().toString().includes('Hawaii'))
`.trim();
// Some CI environments set TZ. Since an env file doesn't override existing
// environment variables, we need to delete it and then pass the env object
// as the environment to spawnPromisified.
const env = { ...process.env };
delete env.TZ;
philnash marked this conversation as resolved.
Show resolved Hide resolved
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${relativePath}`, '--eval', code ],
{ cwd: __dirname },
{ cwd: __dirname, env },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ assert.strictEqual(process.env.RETAIN_INNER_QUOTES_AS_BACKTICKS, '{"foo": "bar\'
// Retains spaces in string
assert.strictEqual(process.env.TRIM_SPACE_FROM_UNQUOTED, 'some spaced out string');
// Parses email addresses completely
assert.strictEqual(process.env.USERNAME, 'therealnerdybeast@example.tld');
assert.strictEqual(process.env.EMAIL, 'therealnerdybeast@example.tld');
// Parses keys and values surrounded by spaces
assert.strictEqual(process.env.SPACED_KEY, 'parsed');