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

Fix bug where dotenv file using quotes greedily consumes values. #3703

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Fixes bug where functions packaged as ES module failed to load on Windows. (#3692)
- Fixes bug parsing dotenv files with multiple quoted values (#3703)
- Tracks use of runtime config and environment variables on function deploys. (#3704)
26 changes: 13 additions & 13 deletions src/functions/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ const RESERVED_KEYS = [
// https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/parser.rb
// prettier-ignore
const LINE_RE = new RegExp(
"^" + // begin line
"\\s*" + // leading whitespaces
"(\\w+)" + // key
"\\s*=\\s*" + // separator (=)
"(" + // begin optional value
"\\s*'(?:\\'|[^'])*'|" + // single quoted or
'\\s*"(?:\\"|[^"])*"|' + // double quoted or
"[^\\#\\r\\n]+" + // unquoted
")?" + // end optional value
"\\s*" + // trailing whitespaces
"(?:#[^\\n]*)?" + // optional comment
"$", // end line
"gms" // flags: global, multiline, dotall
"^" + // begin line
"\\s*" + // leading whitespaces
"(\\w+)" + // key
"\\s*=\\s*" + // separator (=)
"(" + // begin optional value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is no longer optional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it still is? The relevant test case still passes:

https://github.com/firebase/firebase-tools/blob/master/src/test/functions/env.spec.ts#L83

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the backend didn't support it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's what I thought too but just checked deploying empty env var and confirmed that it's a valid value.

"\\s*'(?:\\\\'|[^'])*'|" + // single quoted or
'\\s*"(?:\\\\"|[^"])*"|' + // double quoted or
"[^\\#\\r\\n]+" + // unquoted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we decided that the # character doesn't need escaping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It looks like regex implementation ignores wrongly escaped characters, so I got lucky here:

> "#".match(RegExp("#"))
[ '#', index: 0, input: '#', groups: undefined ]
> "#".match(RegExp("\\#"))
[ '#', index: 0, input: '#', groups: undefined ]

")?" + // end optional value
"\\s*" + // trailing whitespaces
"(?:#[^\\n]*)?" + // optional comment
"$", // end line
"gms" // flags: global, multiline, dotall
);

interface ParseResult {
Expand Down
25 changes: 25 additions & 0 deletions src/test/functions/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,36 @@ BAR=bar
`,
want: { FOO: "foo1\nfoo2", BAR: "bar" },
},
{
description: "should parse many double quoted values",
input: 'FOO="foo"\nBAR="bar"',
want: { FOO: "foo", BAR: "bar" },
},
{
description: "should parse many single quoted values",
input: "FOO='foo'\nBAR='bar'",
want: { FOO: "foo", BAR: "bar" },
},
{
description: "should parse mix of double and single quoted values",
taeold marked this conversation as resolved.
Show resolved Hide resolved
input: `FOO="foo"\nBAR='bar'`,
want: { FOO: "foo", BAR: "bar" },
},
{
description: "should parse double quoted with escaped newlines",
input: 'FOO="foo1\\nfoo2"\nBAR=bar',
want: { FOO: "foo1\nfoo2", BAR: "bar" },
},
{
description: "should leave single quotes when double quoted",
input: `FOO="'foo'"`,
want: { FOO: "'foo'" },
},
{
description: "should leave double quotes when single quoted",
input: `FOO='"foo"'`,
want: { FOO: '"foo"' },
},
{
description: "should unescape escape characters for double quoted values",
input: 'FOO="foo1\\"foo2"',
Expand Down