Skip to content

Commit 4ceae2f

Browse files
rayark1marco-ippolito
authored andcommitted
util: fix parseEnv incorrectly splitting multiple ‘=‘ in value
Previously, parseEnv would create multiple environment variables if a single line contained multiple ‘=‘ characters (e.g. A=B=C would become { A: ‘B=C’, B: ‘C’ }). This commit ensures that only the first ‘=‘ is used as the key-value delimiter, and the rest of the line is treated as the value. Fixes: #57411 PR-URL: #57421 Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 30e2504 commit 4ceae2f

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

benchmark/fixtures/valid.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ BASIC=basic
66

77
# previous line intentionally left blank
88
AFTER_LINE=after_line
9+
A="B=C"
10+
B=C=D
911
EMPTY=
1012
EMPTY_SINGLE_QUOTES=''
1113
EMPTY_DOUBLE_QUOTES=""

src/node_dotenv.cc

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,16 @@ void Dotenv::ParseContent(const std::string_view input) {
155155
// If there is no equal character, then ignore everything
156156
auto equal = content.find('=');
157157
if (equal == std::string_view::npos) {
158-
break;
158+
auto newline = content.find('\n');
159+
if (newline != std::string_view::npos) {
160+
// If we used `newline` only,
161+
// the '\n' might remain and cause an empty-line parse
162+
content.remove_prefix(newline + 1);
163+
} else {
164+
content = {};
165+
}
166+
// No valid data here, skip to next line
167+
continue;
159168
}
160169

161170
key = content.substr(0, equal);
@@ -199,7 +208,9 @@ void Dotenv::ParseContent(const std::string_view input) {
199208
store_.insert_or_assign(std::string(key), multi_line_value);
200209
auto newline = content.find('\n', closing_quote + 1);
201210
if (newline != std::string_view::npos) {
202-
content.remove_prefix(newline);
211+
content.remove_prefix(newline + 1);
212+
} else {
213+
content = {};
203214
}
204215
continue;
205216
}
@@ -220,7 +231,7 @@ void Dotenv::ParseContent(const std::string_view input) {
220231
if (newline != std::string_view::npos) {
221232
value = content.substr(0, newline);
222233
store_.insert_or_assign(std::string(key), value);
223-
content.remove_prefix(newline);
234+
content.remove_prefix(newline + 1);
224235
}
225236
} else {
226237
// Example: KEY="value"
@@ -230,8 +241,13 @@ void Dotenv::ParseContent(const std::string_view input) {
230241
// since there could be newline characters inside the value.
231242
auto newline = content.find('\n', closing_quote + 1);
232243
if (newline != std::string_view::npos) {
233-
content.remove_prefix(newline);
244+
// Use +1 to discard the '\n' itself => next line
245+
content.remove_prefix(newline + 1);
246+
} else {
247+
content = {};
234248
}
249+
// No valid data here, skip to next line
250+
continue;
235251
}
236252
} else {
237253
// Regular key value pair.
@@ -247,15 +263,21 @@ void Dotenv::ParseContent(const std::string_view input) {
247263
if (hash_character != std::string_view::npos) {
248264
value = content.substr(0, hash_character);
249265
}
250-
content.remove_prefix(newline);
266+
store_.insert_or_assign(std::string(key), trim_spaces(value));
267+
content.remove_prefix(newline + 1);
251268
} else {
252269
// In case the last line is a single key/value pair
253270
// Example: KEY=VALUE (without a newline at the EOF)
254-
value = content.substr(0);
271+
value = content;
272+
auto hash_char = value.find('#');
273+
if (hash_char != std::string_view::npos) {
274+
value = content.substr(0, hash_char);
275+
}
276+
store_.insert_or_assign(std::string(key), trim_spaces(value));
277+
content = {};
255278
}
256279

257-
value = trim_spaces(value);
258-
store_.insert_or_assign(std::string(key), value);
280+
store_.insert_or_assign(std::string(key), trim_spaces(value));
259281
}
260282
}
261283
}

test/fixtures/dotenv/valid.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ BASIC=basic
66

77
# previous line intentionally left blank
88
AFTER_LINE=after_line
9+
A="B=C"
10+
B=C=D
911
EMPTY=
1012
EMPTY_SINGLE_QUOTES=''
1113
EMPTY_DOUBLE_QUOTES=""

test/parallel/test-dotenv.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ assert.strictEqual(process.env.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines');
8282
assert.strictEqual(process.env.EXPORT_EXAMPLE, 'ignore export');
8383
// Ignore spaces before double quotes to avoid quoted strings as value
8484
assert.strictEqual(process.env.SPACE_BEFORE_DOUBLE_QUOTES, 'space before double quotes');
85+
assert.strictEqual(process.env.A, 'B=C');
86+
assert.strictEqual(process.env.B, 'C=D');

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const fs = require('node:fs');
1111
const validContent = fs.readFileSync(validEnvFilePath, 'utf8');
1212

1313
assert.deepStrictEqual(util.parseEnv(validContent), {
14+
A: 'B=C',
15+
B: 'C=D',
1416
AFTER_LINE: 'after_line',
1517
BACKTICKS: 'backticks',
1618
BACKTICKS_INSIDE_DOUBLE: '`backticks` work inside double quotes',

0 commit comments

Comments
 (0)