Skip to content

Commit 971f5f5

Browse files
islandryuaduh95
authored andcommitted
src: safely remove the last line from dotenv
Refs: #55925 PR-URL: #55982 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 7133c04 commit 971f5f5

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/node_dotenv.cc

+8-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ void Dotenv::ParseContent(const std::string_view input) {
182182
}
183183

184184
store_.insert_or_assign(std::string(key), multi_line_value);
185-
content.remove_prefix(content.find('\n', closing_quote + 1));
185+
auto newline = content.find('\n', closing_quote + 1);
186+
if (newline != std::string_view::npos) {
187+
content.remove_prefix(newline);
188+
}
186189
continue;
187190
}
188191
}
@@ -210,7 +213,10 @@ void Dotenv::ParseContent(const std::string_view input) {
210213
store_.insert_or_assign(std::string(key), value);
211214
// Select the first newline after the closing quotation mark
212215
// since there could be newline characters inside the value.
213-
content.remove_prefix(content.find('\n', closing_quote + 1));
216+
auto newline = content.find('\n', closing_quote + 1);
217+
if (newline != std::string_view::npos) {
218+
content.remove_prefix(newline);
219+
}
214220
}
215221
} else {
216222
// Regular key value pair.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC='basic'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC="basic"

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

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const fixtures = require('../common/fixtures');
88

99
const validEnvFilePath = '../fixtures/dotenv/valid.env';
1010
const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env';
11+
const noFinalNewlineEnvFilePath = '../fixtures/dotenv/no-final-newline.env';
12+
const noFinalNewlineSingleQuotesEnvFilePath = '../fixtures/dotenv/no-final-newline-single-quotes.env';
1113

1214
describe('.env supports edge cases', () => {
1315
it('supports multiple declarations, including optional ones', async () => {
@@ -148,4 +150,24 @@ describe('.env supports edge cases', () => {
148150
assert.strictEqual(child.stderr, '');
149151
assert.strictEqual(child.code, 0);
150152
});
153+
154+
it('should handle file without a final newline', async () => {
155+
const code = `
156+
require('assert').strictEqual(process.env.BASIC, 'basic');
157+
`.trim();
158+
const child = await common.spawnPromisified(
159+
process.execPath,
160+
[ `--env-file=${path.resolve(__dirname, noFinalNewlineEnvFilePath)}`, '--eval', code ],
161+
);
162+
163+
const SingleQuotesChild = await common.spawnPromisified(
164+
process.execPath,
165+
[ `--env-file=${path.resolve(__dirname, noFinalNewlineSingleQuotesEnvFilePath)}`, '--eval', code ],
166+
);
167+
168+
assert.strictEqual(child.stderr, '');
169+
assert.strictEqual(child.code, 0);
170+
assert.strictEqual(SingleQuotesChild.stderr, '');
171+
assert.strictEqual(SingleQuotesChild.code, 0);
172+
});
151173
});

0 commit comments

Comments
 (0)