Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d894cb7

Browse filesBrowse files
islandryuruyadorno
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 a8fb1a0 commit d894cb7
Copy full SHA for d894cb7

File tree

Expand file treeCollapse file tree

4 files changed

+32
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+32
-2
lines changed
Open diff view settings
Collapse file

‎src/node_dotenv.cc‎

Copy file name to clipboardExpand all lines: src/node_dotenv.cc
+8-2Lines changed: 8 additions & 2 deletions
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.
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC='basic'
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC="basic"
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-dotenv-edge-cases.js
+22Lines changed: 22 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.