Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: safely remove the last line from dotenv #55982

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ void Dotenv::ParseContent(const std::string_view input) {
}

store_.insert_or_assign(std::string(key), multi_line_value);
content.remove_prefix(content.find('\n', closing_quote + 1));
auto newline = content.find('\n', closing_quote + 1);
if (newline != std::string_view::npos) {
content.remove_prefix(newline);
}
continue;
}
}
Expand Down Expand Up @@ -210,7 +213,10 @@ void Dotenv::ParseContent(const std::string_view input) {
store_.insert_or_assign(std::string(key), value);
// Select the first newline after the closing quotation mark
// since there could be newline characters inside the value.
content.remove_prefix(content.find('\n', closing_quote + 1));
auto newline = content.find('\n', closing_quote + 1);
if (newline != std::string_view::npos) {
content.remove_prefix(newline);
}
}
} else {
// Regular key value pair.
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/dotenv/no-final-newline-single-quotes.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASIC='basic'
1 change: 1 addition & 0 deletions test/fixtures/dotenv/no-final-newline.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASIC="basic"
22 changes: 22 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const fixtures = require('../common/fixtures');

const validEnvFilePath = '../fixtures/dotenv/valid.env';
const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env';
const noFinalNewlineEnvFilePath = '../fixtures/dotenv/no-final-newline.env';
const noFinalNewlineSingleQuotesEnvFilePath = '../fixtures/dotenv/no-final-newline-single-quotes.env';

describe('.env supports edge cases', () => {
it('supports multiple declarations, including optional ones', async () => {
Expand Down Expand Up @@ -148,4 +150,24 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('should handle file without a final newline', async () => {
const code = `
require('assert').strictEqual(process.env.BASIC, 'basic');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${path.resolve(__dirname, noFinalNewlineEnvFilePath)}`, '--eval', code ],
);

const SingleQuotesChild = await common.spawnPromisified(
process.execPath,
[ `--env-file=${path.resolve(__dirname, noFinalNewlineSingleQuotesEnvFilePath)}`, '--eval', code ],
);

assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
assert.strictEqual(SingleQuotesChild.stderr, '');
assert.strictEqual(SingleQuotesChild.code, 0);
});
});
Loading