From 073daa7d6f2d80f8eceb037a8c07fb5706c2bc6f Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:21:21 -0400 Subject: [PATCH] src: don't match after` --` in `Dotenv::GetPathFromArgs` --- src/node_dotenv.cc | 10 ++++++---- test/parallel/should-not-write.txt | 1 + test/parallel/test-dotenv-edge-cases.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 test/parallel/should-not-write.txt diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 9e0205b4e6249f..9d00fd3fd8ad98 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -14,21 +14,23 @@ using v8::String; std::vector Dotenv::GetPathFromArgs( const std::vector& args) { const auto find_match = [](const std::string& arg) { - const std::string_view flag = "--env-file"; - return strncmp(arg.c_str(), flag.data(), flag.size()) == 0; + return arg == "--" || arg.starts_with("--env-file"); }; std::vector paths; auto path = std::find_if(args.begin(), args.end(), find_match); while (path != args.end()) { + if (*path == "--") { + return paths; + } + auto equal_char = path->find('='); if (equal_char != std::string::npos) { paths.push_back(path->substr(equal_char + 1)); } else { auto next_path = std::next(path); - - if (next_path == args.end()) { + if (next_path == args.end() || *next_path == "--") { return paths; } diff --git a/test/parallel/should-not-write.txt b/test/parallel/should-not-write.txt new file mode 100644 index 00000000000000..b6fc4c620b67d9 --- /dev/null +++ b/test/parallel/should-not-write.txt @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index f8cd262c8a8092..f652d6467bf338 100644 --- a/test/parallel/test-dotenv-edge-cases.js +++ b/test/parallel/test-dotenv-edge-cases.js @@ -98,4 +98,18 @@ describe('.env supports edge cases', () => { assert.strictEqual(child.stderr, ''); assert.strictEqual(child.code, 0); }); + + it('should handle when --env-file is passed along with --', async () => { + const child = await common.spawnPromisified( + process.execPath, + [ + '--eval', `require('assert').strictEqual(process.env.BASIC, undefined);`, + '--', '--env-file', validEnvFilePath + ], + { cwd: fixtures.path('dotenv') }, + ); + assert.strictEqual(child.stdout, ''); + assert.strictEqual(child.stderr, ''); + assert.strictEqual(child.code, 0); + }); });