Skip to content

Commit e6a0d77

Browse files
authored
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 74722a5 commit e6a0d77

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

benchmark/fixtures/valid.env

+2
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

+30-8
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,16 @@ void Dotenv::ParseContent(const std::string_view input) {
145145
// If there is no equal character, then ignore everything
146146
auto equal = content.find('=');
147147
if (equal == std::string_view::npos) {
148-
break;
148+
auto newline = content.find('\n');
149+
if (newline != std::string_view::npos) {
150+
// If we used `newline` only,
151+
// the '\n' might remain and cause an empty-line parse
152+
content.remove_prefix(newline + 1);
153+
} else {
154+
content = {};
155+
}
156+
// No valid data here, skip to next line
157+
continue;
149158
}
150159

151160
key = content.substr(0, equal);
@@ -195,7 +204,9 @@ void Dotenv::ParseContent(const std::string_view input) {
195204
store_.insert_or_assign(std::string(key), multi_line_value);
196205
auto newline = content.find('\n', closing_quote + 1);
197206
if (newline != std::string_view::npos) {
198-
content.remove_prefix(newline);
207+
content.remove_prefix(newline + 1);
208+
} else {
209+
content = {};
199210
}
200211
continue;
201212
}
@@ -216,7 +227,7 @@ void Dotenv::ParseContent(const std::string_view input) {
216227
if (newline != std::string_view::npos) {
217228
value = content.substr(0, newline);
218229
store_.insert_or_assign(std::string(key), value);
219-
content.remove_prefix(newline);
230+
content.remove_prefix(newline + 1);
220231
}
221232
} else {
222233
// Example: KEY="value"
@@ -226,8 +237,13 @@ void Dotenv::ParseContent(const std::string_view input) {
226237
// since there could be newline characters inside the value.
227238
auto newline = content.find('\n', closing_quote + 1);
228239
if (newline != std::string_view::npos) {
229-
content.remove_prefix(newline);
240+
// Use +1 to discard the '\n' itself => next line
241+
content.remove_prefix(newline + 1);
242+
} else {
243+
content = {};
230244
}
245+
// No valid data here, skip to next line
246+
continue;
231247
}
232248
} else {
233249
// Regular key value pair.
@@ -243,15 +259,21 @@ void Dotenv::ParseContent(const std::string_view input) {
243259
if (hash_character != std::string_view::npos) {
244260
value = content.substr(0, hash_character);
245261
}
246-
content.remove_prefix(newline);
262+
store_.insert_or_assign(std::string(key), trim_spaces(value));
263+
content.remove_prefix(newline + 1);
247264
} else {
248265
// In case the last line is a single key/value pair
249266
// Example: KEY=VALUE (without a newline at the EOF)
250-
value = content.substr(0);
267+
value = content;
268+
auto hash_char = value.find('#');
269+
if (hash_char != std::string_view::npos) {
270+
value = content.substr(0, hash_char);
271+
}
272+
store_.insert_or_assign(std::string(key), trim_spaces(value));
273+
content = {};
251274
}
252275

253-
value = trim_spaces(value);
254-
store_.insert_or_assign(std::string(key), value);
276+
store_.insert_or_assign(std::string(key), trim_spaces(value));
255277
}
256278
}
257279
}

test/fixtures/dotenv/valid.env

+2
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

+2
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

+2
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)