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

Fixing a bug where empty variables in .env files were read as multi-line values (#3934) #3951

Merged
merged 7 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,4 +1,5 @@
- Fixes issue when installing a Firebase Extension where secrets would be created before validation.
- Fixes issue with filtering on a specific storage bucket using functions in the emulator (#3893)
- Fixes check in Cloud Functions for Firebase initialization to check for API enablement before trying to enable them. (#2574)
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled (#3943)
- No longer tries to clean up function build images from Artifact Registry when Artifact Registry is not enabled. (#3943)
- Fixes issue where empty variables in .env files would instead read as multi-line values. (#3934)
3 changes: 2 additions & 1 deletion src/functions/env.ts
Expand Up @@ -49,7 +49,7 @@ const LINE_RE = new RegExp(
"(" + // begin optional value
"\\s*'(?:\\\\'|[^'])*'|" + // single quoted or
'\\s*"(?:\\\\"|[^"])*"|' + // double quoted or
"[^#\\r\\n]+" + // unquoted
"[^#=\\r\\n]*" + // unquoted
Copy link
Contributor

Choose a reason for hiding this comment

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

q: Did we need =?

IIUC, we are making something like

FOO=bar=a

from being a valid entry ({FOO: "bar=a"}) to an invalid one.

Copy link
Contributor Author

@joehan joehan Dec 15, 2021

Choose a reason for hiding this comment

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

Hmmm, its a bit of a weird case - i guess what we really want to do is allow =, but not after a line break?
So something like

[[^#\\r\\n][^#=\\r\\n]]

There may be other regex tricks that could solve this as well - maybe replacing the \\s that. matches whitespace after the equals with something that doesn't match newlines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahhhh, i just read your original code comments more closely, and realized we explicitly don't support multiline unquoted values. I think i know how to fix this, will play around a bit and make sure all the tests work.

")?" + // end optional value
"\\s*" + // trailing whitespaces
"(?:#[^\\n]*)?" + // optional comment
Expand Down Expand Up @@ -109,6 +109,7 @@ export function parse(data: string): ParseResult {
v = v.replace(/\\([\\'"])/g, "$1");
}
}

envs[k] = v;
}

Expand Down
41 changes: 24 additions & 17 deletions src/test/functions/env.spec.ts
Expand Up @@ -135,26 +135,33 @@ BAR=bar
want: { FOO: "foo" },
},
{
description: "should ignore comments",
description: "should handle empty values",
input: `
FOO=foo # comment
# line comment 1
# line comment 2
BAR=bar # another comment
FOO=
BAR= "blah"
`,
want: { FOO: "foo", BAR: "bar" },
},
{
description: "should ignore empty lines",
input: `
FOO=foo


BAR=bar

`,
want: { FOO: "foo", BAR: "bar" },
want: { FOO: "", BAR: "blah" },
},
// {
// description: "should ignore comments",
// input: `
// FOO=foo # comment
// # line comment 1
// # line comment 2
// BAR=bar # another comment
// `,
// want: { FOO: "foo", BAR: "bar" },
// },
// {
// description: "should ignore empty lines",
// input: `
// FOO=foo

// BAR=bar

// `,
// want: { FOO: "foo", BAR: "bar" },
// },
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to uncomment this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 Forgot to save the file before commiting

];

tests.forEach(({ description, input, want }) => {
Expand Down