Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotenv: fix env-file parsing #54158

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ void Dotenv::ParseContent(const std::string_view input) {
// Expand new line if \n it's inside double quotes
// Example: EXPAND_NEWLINES = 'expand\nnew\nlines'
if (content.front() == '"') {
auto closing_quote = content.find(content.front(), 1);
std::size_t closing_quote = 0;
do {
closing_quote = content.find(content.front(), closing_quote + 1);
if (closing_quote == std::string_view::npos) {
break;
}
if (closing_quote > 0 && content[closing_quote - 1] != '\\') {
break;
}
} while (closing_quote != std::string_view::npos);

if (closing_quote != std::string_view::npos) {
value = content.substr(1, closing_quote - 1);
std::string multi_line_value = std::string(value);
Expand All @@ -163,6 +173,13 @@ void Dotenv::ParseContent(const std::string_view input) {
pos += 1;
}

pos = 0;
while ((pos = multi_line_value.find(R"(\")", pos)) !=
std::string_view::npos) {
multi_line_value.replace(pos, 2, "\"");
pos += 1;
}

store_.insert_or_assign(std::string(key), multi_line_value);
content.remove_prefix(content.find('\n', closing_quote + 1));
continue;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ [email protected]
SPACED_KEY = parsed
EDGE_CASE_INLINE_COMMENTS="VALUE1" # or "VALUE2" or "VALUE3"

ALL_QUOTES="this 'has' all \"quotes\" in `value` and double quotes around"
ALL_QUOTES_EXPAND_NEWLINES="this\n'has'\nall\n\"quotes\"\nin\n`value`\nand\nmultilines\nto\nexpand"

MULTI_DOUBLE_QUOTED="THIS
IS
A
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ assert.strictEqual(process.env.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines');
assert.strictEqual(process.env.EXPORT_EXAMPLE, 'ignore export');
// Ignore spaces before double quotes to avoid quoted strings as value
assert.strictEqual(process.env.SPACE_BEFORE_DOUBLE_QUOTES, 'space before double quotes');
assert.strictEqual(process.env.ALL_QUOTES, 'this \'has\' all "quotes" in `value` and double quotes around');
assert.strictEqual(process.env.ALL_QUOTES_EXPAND_NEWLINES, 'this\n\'has\'\nall\n"quotes"\nin\n`value`\nand\nmultilines\nto\nexpand');

Loading