Skip to content

Commit 4ca51e8

Browse files
util: fix parseEnv handling of invalid lines
1 parent f1196ee commit 4ca51e8

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

src/node_dotenv.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,24 @@ void Dotenv::ParseContent(const std::string_view input) {
134134
auto newline = content.find('\n');
135135
if (newline != std::string_view::npos) {
136136
content.remove_prefix(newline + 1);
137+
content = trim_spaces(content);
137138
continue;
139+
} else {
140+
break;
138141
}
139142
}
140143

141-
// If there is no equal character, then ignore everything
144+
// Find the next equals sign and newline
142145
auto equal = content.find('=');
143-
if (equal == std::string_view::npos) {
146+
auto newline = content.find('\n');
147+
148+
// If there is no equal character in this line, skip to next line
149+
if (equal == std::string_view::npos || (newline != std::string_view::npos && equal > newline)) {
150+
if (newline != std::string_view::npos) {
151+
content.remove_prefix(newline + 1);
152+
content = trim_spaces(content);
153+
continue;
154+
}
144155
break;
145156
}
146157

@@ -150,12 +161,19 @@ void Dotenv::ParseContent(const std::string_view input) {
150161
content = trim_spaces(content);
151162

152163
if (key.empty()) {
164+
// Skip invalid empty key
165+
if (newline != std::string_view::npos) {
166+
content.remove_prefix(newline + 1);
167+
content = trim_spaces(content);
168+
continue;
169+
}
153170
break;
154171
}
155172

156173
// Remove export prefix from key
157174
if (key.starts_with("export ")) {
158175
key.remove_prefix(7);
176+
key = trim_spaces(key);
159177
}
160178

161179
// SAFETY: Content is guaranteed to have at least one character
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
foo
2+
3+
bar
4+
baz=whatever
5+
VALID_AFTER_INVALID=test
6+
multiple_invalid
7+
lines_without_equals
8+
ANOTHER_VALID=value
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Flags: --env-file test/fixtures/dotenv/invalid-syntax.env
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('node:assert');
6+
const { parseEnv } = require('node:util');
7+
8+
// Test direct parseEnv usage
9+
{
10+
const input = `foo
11+
12+
bar
13+
baz=whatever
14+
VALID_AFTER_INVALID=test
15+
multiple_invalid
16+
lines_without_equals
17+
ANOTHER_VALID=value`;
18+
19+
const result = parseEnv(input);
20+
21+
// Using individual assertions for better error messages
22+
assert.strictEqual(Object.keys(result).length, 3, 'Should only have 3 valid entries');
23+
assert.strictEqual(result.baz, 'whatever', 'baz should have value "whatever"');
24+
assert.strictEqual(result.VALID_AFTER_INVALID, 'test', 'VALID_AFTER_INVALID should have value "test"');
25+
assert.strictEqual(result.ANOTHER_VALID, 'value', 'ANOTHER_VALID should have value "value"');
26+
27+
// Ensure invalid entries are not present
28+
assert.strictEqual(result.foo, undefined, 'foo should not be present');
29+
assert.strictEqual(result.bar, undefined, 'bar should not be present');
30+
assert.strictEqual(result.multiple_invalid, undefined, 'multiple_invalid should not be present');
31+
assert.strictEqual(result.lines_without_equals, undefined, 'lines_without_equals should not be present');
32+
}
33+
34+
// Test edge cases
35+
{
36+
const edgeCases = [
37+
// Empty file
38+
{
39+
input: '\n\n \n ',
40+
expected: {}
41+
},
42+
// Only invalid lines
43+
{
44+
input: 'no_equals_here\nanother_invalid_line\n just_text',
45+
expected: {}
46+
},
47+
// Mixed valid and invalid
48+
{
49+
input: 'VALID1=value1\ninvalid\nVALID2=value2',
50+
expected: {
51+
VALID1: 'value1',
52+
VALID2: 'value2'
53+
}
54+
},
55+
// Lines with spaces but no equals
56+
{
57+
input: ' spaces \nVALID=value\n more spaces ',
58+
expected: {
59+
VALID: 'value'
60+
}
61+
}
62+
];
63+
64+
for (const { input, expected } of edgeCases) {
65+
assert.deepStrictEqual(
66+
parseEnv(input),
67+
expected,
68+
`Failed parsing: ${JSON.stringify(input)}`
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)