Skip to content

Commit a723280

Browse files
committed
src: remove regex usage for env file parsing
1 parent 756acd0 commit a723280

File tree

7 files changed

+146
-46
lines changed

7 files changed

+146
-46
lines changed

src/node_dotenv.cc

+114-38
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ using v8::NewStringType;
1212
using v8::Object;
1313
using v8::String;
1414

15-
/**
16-
* The inspiration for this implementation comes from the original dotenv code,
17-
* available at https://github.com/motdotla/dotenv
18-
*/
19-
const std::regex LINE(
20-
"\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']"
21-
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?"
22-
":#.*)?"); // NOLINT(whitespace/line_length)
23-
2415
std::vector<std::string> Dotenv::GetPathFromArgs(
2516
const std::vector<std::string>& args) {
2617
const auto find_match = [](const std::string& arg) {
@@ -101,35 +92,129 @@ Local<Object> Dotenv::ToObject(Environment* env) {
10192
return result;
10293
}
10394

104-
void Dotenv::ParseContent(const std::string_view content) {
105-
std::string lines = std::string(content);
106-
lines = std::regex_replace(lines, std::regex("\r\n?"), "\n");
95+
std::string_view trim_spaces(std::string_view input) {
96+
if (input.empty()) return "";
97+
if (input.front() == ' ') {
98+
input.remove_prefix(input.find_first_not_of(' '));
99+
}
100+
if (!input.empty() && input.back() == ' ') {
101+
input = input.substr(0, input.find_last_not_of(' ') + 1);
102+
}
103+
return input;
104+
}
105+
106+
void Dotenv::ParseContent(const std::string_view input) {
107+
std::string_view content = input;
108+
content = trim_spaces(content);
109+
110+
std::string_view key;
111+
std::string_view value;
112+
113+
while (!content.empty()) {
114+
// Skip empty lines and comments
115+
if (content.front() == '\n' || content.front() == '#') {
116+
auto newline = content.find('\n');
117+
if (newline != std::string_view::npos) {
118+
content.remove_prefix(newline + 1);
119+
continue;
120+
}
121+
}
122+
123+
// If there is no equal character, then ignore everything
124+
auto equal = content.find('=');
125+
if (equal == std::string_view::npos) {
126+
break;
127+
}
107128

108-
std::smatch match;
109-
while (std::regex_search(lines, match, LINE)) {
110-
const std::string key = match[1].str();
129+
key = content.substr(0, equal);
130+
content.remove_prefix(equal + 1);
131+
key = trim_spaces(key);
111132

112-
// Default undefined or null to an empty string
113-
std::string value = match[2].str();
133+
if (key.empty()) {
134+
break;
135+
}
114136

115-
// Remove leading whitespaces
116-
value.erase(0, value.find_first_not_of(" \t"));
137+
// Remove export prefix from key
138+
auto have_export = key.compare(0, 7, "export ") == 0;
139+
if (have_export) {
140+
key.remove_prefix(7);
141+
}
117142

118-
// Remove trailing whitespaces
119-
if (!value.empty()) {
120-
value.erase(value.find_last_not_of(" \t") + 1);
143+
// SAFETY: Content is guaranteed to have at least one character
144+
if (content.empty()) {
145+
break;
121146
}
122147

123-
if (!value.empty() && value.front() == '"') {
124-
value = std::regex_replace(value, std::regex("\\\\n"), "\n");
125-
value = std::regex_replace(value, std::regex("\\\\r"), "\r");
148+
// Expand new line if \n it's inside double quotes
149+
// Example: EXPAND_NEWLINES = 'expand\nnew\nlines'
150+
if (content.front() == '"') {
151+
auto closing_quote = content.find(content.front(), 1);
152+
if (closing_quote != std::string_view::npos) {
153+
value = content.substr(1, closing_quote - 1);
154+
std::string multi_line_value = std::string(value);
155+
156+
size_t pos = 0;
157+
while ((pos = multi_line_value.find("\\n", pos)) !=
158+
std::string_view::npos) {
159+
multi_line_value.replace(pos, 2, "\n");
160+
pos += 1;
161+
}
162+
163+
store_.insert_or_assign(std::string(key), multi_line_value);
164+
content.remove_prefix(content.find('\n', closing_quote + 1));
165+
continue;
166+
}
126167
}
127168

128-
// Remove surrounding quotes
129-
value = trim_quotes(value);
169+
// Check if the value is wrapped in quotes, single quotes or backticks
170+
if ((content.front() == '\'' || content.front() == '"' ||
171+
content.front() == '`')) {
172+
auto closing_quote = content.find(content.front(), 1);
173+
174+
// Check if the closing quote is not found
175+
// Example: KEY="value
176+
if (closing_quote == std::string_view::npos) {
177+
// Check if newline exist. If it does, take the entire line as the value
178+
// Example: KEY="value\nKEY2=value2
179+
// The value pair should be `"value`
180+
auto newline = content.find('\n');
181+
if (newline != std::string_view::npos) {
182+
value = content.substr(0, newline);
183+
store_.insert_or_assign(std::string(key), value);
184+
content.remove_prefix(newline);
185+
}
186+
} else {
187+
// Example: KEY="value"
188+
value = content.substr(1, closing_quote - 1);
189+
store_.insert_or_assign(std::string(key), value);
190+
// Select the first newline after the closing quotation mark
191+
// since there could be newline characters inside the value.
192+
content.remove_prefix(content.find('\n', closing_quote + 1));
193+
}
194+
} else {
195+
// Regular key value pair.
196+
// Example: `KEY=this is value`
197+
auto newline = content.find('\n');
198+
199+
if (newline != std::string_view::npos) {
200+
value = content.substr(0, newline);
201+
auto hash_character = value.find('#');
202+
// Check if there is a comment in the line
203+
// Example: KEY=value # comment
204+
// The value pair should be `value`
205+
if (hash_character != std::string_view::npos) {
206+
value = content.substr(0, hash_character);
207+
}
208+
content.remove_prefix(newline);
209+
} else {
210+
// In case the last line is a single key/value pair
211+
// Example: KEY=VALUE (without a newline at the EOF)
212+
value = content.substr(0);
213+
}
130214

131-
store_.insert_or_assign(std::string(key), value);
132-
lines = match.suffix();
215+
value = trim_spaces(value);
216+
store_.insert_or_assign(std::string(key), value);
217+
}
133218
}
134219
}
135220

@@ -179,13 +264,4 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
179264
}
180265
}
181266

182-
std::string_view Dotenv::trim_quotes(std::string_view str) {
183-
static const std::unordered_set<char> quotes = {'"', '\'', '`'};
184-
if (str.size() >= 2 && quotes.count(str.front()) &&
185-
quotes.count(str.back())) {
186-
str = str.substr(1, str.size() - 2);
187-
}
188-
return str;
189-
}
190-
191267
} // namespace node

src/node_dotenv.h

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Dotenv {
3232

3333
private:
3434
std::map<std::string, std::string> store_;
35-
std::string_view trim_quotes(std::string_view str);
3635
};
3736

3837
} // namespace node

test/fixtures/dotenv/multiline.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
JWT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnNl1tL3QjKp3DZWM0T3u
3+
LgGJQwu9WqyzHKZ6WIA5T+7zPjO1L8l3S8k8YzBrfH4mqWOD1GBI8Yjq2L1ac3Y/
4+
bTdfHN8CmQr2iDJC0C6zY8YV93oZB3x0zC/LPbRYpF8f6OqX1lZj5vo2zJZy4fI/
5+
kKcI5jHYc8VJq+KCuRZrvn+3V+KuL9tF9v8ZgjF2PZbU+LsCy5Yqg1M8f5Jp5f6V
6+
u4QuUoobAgMBAAE=
7+
-----END PUBLIC KEY-----"

test/fixtures/dotenv/valid.env

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
BASIC=basic
22

3+
# COMMENTS=work
4+
#BASIC=basic2
5+
#BASIC=basic3
6+
37
# previous line intentionally left blank
48
AFTER_LINE=after_line
59
EMPTY=
@@ -55,7 +59,8 @@ IS
5559
A
5660
"MULTILINE'S"
5761
STRING`
62+
export EXPORT_EXAMPLE = ignore export
63+
5864
MULTI_NOT_VALID_QUOTE="
5965
MULTI_NOT_VALID=THIS
6066
IS NOT MULTILINE
61-
export EXAMPLE = ignore export

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

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const assert = require('node:assert');
55
const path = require('node:path');
66
const { describe, it } = require('node:test');
7+
const fixtures = require('../common/fixtures');
78

89
const validEnvFilePath = '../fixtures/dotenv/valid.env';
910
const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env';
@@ -64,4 +65,20 @@ describe('.env supports edge cases', () => {
6465
assert.strictEqual(child.stderr, '');
6566
assert.strictEqual(child.code, 0);
6667
});
68+
69+
it('should handle multiline quoted values', async () => {
70+
// Ref: https://github.com/nodejs/node/issues/52248
71+
const code = `
72+
process.loadEnvFile('./multiline.env');
73+
require('node:assert').ok(process.env.JWT_PUBLIC_KEY);
74+
`.trim();
75+
const child = await common.spawnPromisified(
76+
process.execPath,
77+
[ '--eval', code ],
78+
{ cwd: fixtures.path('dotenv') },
79+
);
80+
assert.strictEqual(child.stdout, '');
81+
assert.strictEqual(child.stderr, '');
82+
assert.strictEqual(child.code, 0);
83+
});
6784
});

test/parallel/test-dotenv.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ assert.strictEqual(process.env.COMMENTS, undefined);
5858
assert.strictEqual(process.env.EQUAL_SIGNS, 'equals==');
5959
// Retains inner quotes
6060
assert.strictEqual(process.env.RETAIN_INNER_QUOTES, '{"foo": "bar"}');
61-
// Respects equals signs in values
62-
assert.strictEqual(process.env.EQUAL_SIGNS, 'equals==');
63-
// Retains inner quotes
64-
assert.strictEqual(process.env.RETAIN_INNER_QUOTES, '{"foo": "bar"}');
6561
assert.strictEqual(process.env.RETAIN_INNER_QUOTES_AS_STRING, '{"foo": "bar"}');
6662
assert.strictEqual(process.env.RETAIN_INNER_QUOTES_AS_BACKTICKS, '{"foo": "bar\'s"}');
6763
// Retains spaces in string
@@ -83,4 +79,4 @@ assert.strictEqual(process.env.EXPAND_NEWLINES, 'expand\nnew\nlines');
8379
assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines');
8480
assert.strictEqual(process.env.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines');
8581
// Ignore export before key
86-
assert.strictEqual(process.env.EXAMPLE, 'ignore export');
82+
assert.strictEqual(process.env.EXPORT_EXAMPLE, 'ignore export');

test/parallel/util-parse-env.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const fs = require('node:fs');
3232
EMPTY_DOUBLE_QUOTES: '',
3333
EMPTY_SINGLE_QUOTES: '',
3434
EQUAL_SIGNS: 'equals==',
35-
EXAMPLE: 'ignore export',
35+
EXPORT_EXAMPLE: 'ignore export',
3636
EXPAND_NEWLINES: 'expand\nnew\nlines',
3737
INLINE_COMMENTS: 'inline comments',
3838
INLINE_COMMENTS_BACKTICKS: 'inline comments outside of #backticks',

0 commit comments

Comments
 (0)