Skip to content

Commit 8faa050

Browse files
committed
src: do not read the next line for an empty value
When an unquoted value was empty but followed by spaces or tabs, as in `KEY= `, the parser trimmed the newline along with the whitespace and took the next line as the value. Strip only spaces and tabs before checking for an empty value. Fixes: #67400 Assisted-by: Claude Code Signed-off-by: TseIan <tseianxwc@gmail.com>
1 parent e8c1160 commit 8faa050

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎src/node_dotenv.cc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ void Dotenv::ParseContent(const std::string_view input) {
194194
content.remove_prefix(equal_or_newline + 1);
195195
key = trim_spaces(key);
196196

197+
// Only strip spaces and tabs here: trim_spaces() also strips '\n', which
198+
// would turn the next line into the value of an empty `KEY= `.
199+
content.remove_prefix(
200+
std::min(content.find_first_not_of(" \t"), content.size()));
201+
197202
// If the value is not present (e.g. KEY=) set it to an empty string
198203
if (content.empty() || content.front() == '\n') {
199204
store_.insert_or_assign(std::string(key), "");

‎test/parallel/test-util-parse-env.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ assert.deepStrictEqual(util.parseEnv(''), { __proto__: null });
6666
assert.deepStrictEqual(util.parseEnv('FOO=bar\nFOO=baz\n'),
6767
{ __proto__: null, FOO: 'baz' });
6868

69+
// Whitespace after an empty value must not pull in the next line.
70+
// Refs: https://github.com/nodejs/node/issues/67400
71+
assert.deepStrictEqual(util.parseEnv('A= \nB=b\nC=\t\nD=d\nE= '),
72+
{ __proto__: null, A: '', B: 'b', C: '', D: 'd', E: '' });
73+
6974
// Test for invalid input.
7075
assert.throws(() => {
7176
for (const value of [null, undefined, {}, []]) {

0 commit comments

Comments
 (0)