Skip to content

Commit 9097de0

Browse files
avivkellerxduugu
authored andcommitted
src: don't match after -- in Dotenv::GetPathFromArgs
Co-Authored-By: Cedric Staniewski <[email protected]> PR-URL: #54237 Backport-PR-URL: #56932 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 73b5c16 commit 9097de0

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/node_dotenv.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ using v8::String;
1414
std::vector<std::string> Dotenv::GetPathFromArgs(
1515
const std::vector<std::string>& args) {
1616
const auto find_match = [](const std::string& arg) {
17-
const std::string_view flag = "--env-file";
18-
return strncmp(arg.c_str(), flag.data(), flag.size()) == 0;
17+
auto arg_chars = arg.c_str();
18+
auto arg_len = arg.size();
19+
if (arg_chars[0] != '-' || arg_chars[1] != '-') return false;
20+
if (arg_len == 2) return true; // arg == "--"
21+
const std::string_view flag = "env-file";
22+
const auto len = flag.size();
23+
if (strncmp(arg_chars + 2, flag.data(), len) != 0) return false;
24+
return arg_len == 2 + len || arg_chars[2 + len] == '=';
1925
};
2026
std::vector<std::string> paths;
2127
auto path = std::find_if(args.begin(), args.end(), find_match);
2228

2329
while (path != args.end()) {
30+
if (path->size() == 2 && strncmp(path->c_str(), "--", 2) == 0) {
31+
return paths;
32+
}
2433
auto equal_char = path->find('=');
2534

2635
if (equal_char != std::string::npos) {

test/parallel/test-dotenv-edge-cases.js

+14
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ describe('.env supports edge cases', () => {
9898
assert.strictEqual(child.stderr, '');
9999
assert.strictEqual(child.code, 0);
100100
});
101+
102+
it('should handle when --env-file is passed along with --', async () => {
103+
const child = await common.spawnPromisified(
104+
process.execPath,
105+
[
106+
'--eval', 'assert.strictEqual(process.env.BASIC, undefined);',
107+
'--', '--env-file', validEnvFilePath,
108+
],
109+
{ cwd: fixtures.path('dotenv') },
110+
);
111+
assert.strictEqual(child.stdout, '');
112+
assert.strictEqual(child.stderr, '');
113+
assert.strictEqual(child.code, 0);
114+
});
101115
});

0 commit comments

Comments
 (0)