From 23707b6224ad64cb9823c068ee373a629edd355a Mon Sep 17 00:00:00 2001 From: Mohit Malhotra Date: Wed, 17 Jul 2024 14:52:42 +0530 Subject: [PATCH] src: fix env-file flag to ignore spaces before quotes Fix to ignore spaces between '=' and quoted string in env file Fixes: https://github.com/nodejs/node/issues/53461 Signed-off-by: Mohit Malhotra PR-URL: https://github.com/nodejs/node/pull/53786 Reviewed-By: Yagiz Nizipli Reviewed-By: Rafael Gonzaga --- src/node_dotenv.cc | 1 + test/fixtures/dotenv/valid.env | 1 + test/parallel/test-dotenv.js | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index af7433d2a08369..12eb0446de0a7a 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -129,6 +129,7 @@ void Dotenv::ParseContent(const std::string_view input) { key = content.substr(0, equal); content.remove_prefix(equal + 1); key = trim_spaces(key); + content = trim_spaces(content); if (key.empty()) { break; diff --git a/test/fixtures/dotenv/valid.env b/test/fixtures/dotenv/valid.env index 963c4c848a4225..120488d57917e0 100644 --- a/test/fixtures/dotenv/valid.env +++ b/test/fixtures/dotenv/valid.env @@ -38,6 +38,7 @@ RETAIN_INNER_QUOTES={"foo": "bar"} RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}' RETAIN_INNER_QUOTES_AS_BACKTICKS=`{"foo": "bar's"}` TRIM_SPACE_FROM_UNQUOTED= some spaced out string +SPACE_BEFORE_DOUBLE_QUOTES= "space before double quotes" EMAIL=therealnerdybeast@example.tld SPACED_KEY = parsed EDGE_CASE_INLINE_COMMENTS="VALUE1" # or "VALUE2" or "VALUE3" diff --git a/test/parallel/test-dotenv.js b/test/parallel/test-dotenv.js index 88afd58b5d766e..3c81bf98782a97 100644 --- a/test/parallel/test-dotenv.js +++ b/test/parallel/test-dotenv.js @@ -80,3 +80,5 @@ assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines'); assert.strictEqual(process.env.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines'); // Ignore export before key 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');