GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: fix parseEnv incorrectly splitting multiple ‘=‘ in value · nodejs/node@6add4c5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • benchmark/fixtures
    • valid.env
  • src
    • node_dotenv.cc
  • test
    • fixtures/dotenv
      • valid.env
    • parallel
      • test-dotenv.js
      • test-util-parse-env.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎benchmark/fixtures/valid.env‎

Copy file name to clipboardExpand all lines: benchmark/fixtures/valid.env
+2Lines 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=""
Collapse file

‎src/node_dotenv.cc‎

Copy file name to clipboardExpand all lines: src/node_dotenv.cc
+30-8Lines changed: 30 additions & 8 deletions
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
}
Collapse file

‎test/fixtures/dotenv/valid.env‎

Copy file name to clipboardExpand all lines: test/fixtures/dotenv/valid.env
+2Lines 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=""
Collapse file

‎test/parallel/test-dotenv.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-dotenv.js
+2Lines 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');
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-util-parse-env.js
+2Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.