Commit 0298e4580e8 for nodejs
commit 0298e4580e8d929446760e65c9494b2130d17454
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date: Tue Dec 1 17:57:38 2026 -0300
src: fix dotenv empty values and indented comments
An empty value was stored before the key was normalized, and the
check for it ran before the spaces after `=` were skipped. Only the
path for unquoted values trimmed the start of the next line, so every
branch that ended in `continue` left it untrimmed. As a result:
- `KEY= ` followed by a newline consumed the newline while trimming,
so the next line was stored as the value of `KEY` and its own
variable was lost.
- `export KEY=` was stored as `export KEY` instead of `KEY`.
- `=` followed by a newline was stored under an empty key, while
`=value` is skipped.
- A comment indented with spaces after an empty value, a quoted value
or another comment was stored as a variable named `# ...`.
Skip empty keys and strip the `export` prefix first, skip only spaces
and tabs after `=` before checking for an empty value, and trim the
start of each line at the top of the loop, so it also runs after
`continue`.
Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66685
Fixes: https://github.com/nodejs/node/issues/67400
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc
index 0c6fa30a41d..1b115e3eac3 100644
--- a/src/node_dotenv.cc
+++ b/src/node_dotenv.cc
@@ -157,6 +157,9 @@ void Dotenv::ParseContent(const std::string_view input) {
std::string_view value;
while (!content.empty()) {
+ // Every line starts trimmed, whichever branch consumed the previous one.
+ content = trim_spaces(content);
+
// Skip empty lines and comments
if (content.front() == '\n' || content.front() == '#') {
// Check if the first character of the content is a newline or a hash
@@ -183,7 +186,6 @@ void Dotenv::ParseContent(const std::string_view input) {
content.at(equal_or_newline) == '\n') {
if (equal_or_newline != std::string_view::npos) {
content.remove_prefix(equal_or_newline + 1);
- content = trim_spaces(content);
continue;
}
break;
@@ -194,14 +196,6 @@ void Dotenv::ParseContent(const std::string_view input) {
content.remove_prefix(equal_or_newline + 1);
key = trim_spaces(key);
- // If the value is not present (e.g. KEY=) set it to an empty string
- if (content.empty() || content.front() == '\n') {
- store_.insert_or_assign(std::string(key), "");
- continue;
- }
-
- content = trim_spaces(content);
-
// Skip lines with empty keys after trimming spaces.
// Examples of invalid keys that would be skipped:
// =value
@@ -217,12 +211,15 @@ void Dotenv::ParseContent(const std::string_view input) {
key = trim_spaces(key);
}
- // SAFETY: Content is guaranteed to have at least one character
- if (content.empty()) {
- // In case the last line is a single key without value
- // Example: KEY= (without a newline at the EOF)
+ // Skip spaces and tabs before the value, but not a newline: in
+ // `KEY= \nNEXT=value` the value of KEY is empty.
+ content.remove_prefix(
+ std::min(content.find_first_not_of(" \t"), content.size()));
+
+ // If the value is not present (e.g. KEY=) set it to an empty string
+ if (content.empty() || content.front() == '\n') {
store_.insert_or_assign(std::string(key), "");
- break;
+ continue;
}
// Expand new line if \n it's inside double quotes
@@ -318,8 +315,6 @@ void Dotenv::ParseContent(const std::string_view input) {
content = {};
}
}
-
- content = trim_spaces(content);
}
}
diff --git a/test/parallel/test-util-parse-env.js b/test/parallel/test-util-parse-env.js
index e932a31f683..f68a17358f3 100644
--- a/test/parallel/test-util-parse-env.js
+++ b/test/parallel/test-util-parse-env.js
@@ -66,6 +66,28 @@ assert.deepStrictEqual(util.parseEnv(''), { __proto__: null });
assert.deepStrictEqual(util.parseEnv('FOO=bar\nFOO=baz\n'),
{ __proto__: null, FOO: 'baz' });
+// An empty value must not consume the next line or skip key normalization.
+assert.deepStrictEqual(util.parseEnv('EMPTY= \nNEXT=value\n'),
+ { __proto__: null, EMPTY: '', NEXT: 'value' });
+assert.deepStrictEqual(util.parseEnv('EMPTY=\t\nNEXT=value'),
+ { __proto__: null, EMPTY: '', NEXT: 'value' });
+assert.deepStrictEqual(util.parseEnv('export EMPTY=\nexport SPACED= '),
+ { __proto__: null, EMPTY: '', SPACED: '' });
+assert.deepStrictEqual(util.parseEnv('=\nNEXT=value'),
+ { __proto__: null, NEXT: 'value' });
+
+// Comments indented with spaces are ignored after every kind of line.
+assert.deepStrictEqual(util.parseEnv([
+ 'A="a"', ' # X=1',
+ "B='b'", ' # X=2',
+ 'C=`c`', ' # X=3',
+ 'D=', ' # X=4',
+ 'E= ', ' # X=5',
+ '= # X=6',
+ '# comment', ' # X=7',
+ 'F=f',
+].join('\n')), { __proto__: null, A: 'a', B: 'b', C: 'c', D: '', E: '', F: 'f' });
+
// Test for invalid input.
assert.throws(() => {
for (const value of [null, undefined, {}, []]) {