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

Conversation

taeold
Copy link
Contributor

@taeold taeold commented Aug 24, 2021

Our parser for dotenv file format allows values to be wrapped inside single or double quotes.

The regex we have for parsing these value is too greedy as implemented:

# .env
A="abc"
B="efg"
parse(readFileSync(".env"))
=> { A: 'abc\nB="efg"' }

The change in regex correctly parses the dotenv file:

parse(readFileSync(".env"))
=> { A: "abc", B: "efg"  }

@google-cla google-cla bot added the cla: yes Manual indication that this has passed CLA. label Aug 24, 2021
Copy link
Member

@inlined inlined left a comment

Choose a reason for hiding this comment

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

Sorry, I forgot to hit send again.

"\\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.

"(" + // begin optional 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 ]

@taeold taeold merged commit 0f8dd7d into master Aug 25, 2021
devpeerapong pushed a commit to devpeerapong/firebase-tools that referenced this pull request Dec 14, 2021
…ebase#3703)

Our parser for dotenv file format allows values to be wrapped inside single or double quotes.

The regex we have for parsing these value is too greedy as implemented:

```
# .env
A="abc"
B="efg"
```

```
parse(readFileSync(".env"))
=> { A: 'abc\nB="efg"' }
```

The change in regex correctly parses the dotenv file:

```
parse(readFileSync(".env"))
=> { A: "abc", B: "efg"  }
```
@bkendall bkendall deleted the dl-dotenv-bug branch March 18, 2022 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Manual indication that this has passed CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants