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

Improve quoting of values in set_key #330

Merged
merged 1 commit into from Jun 20, 2021
Merged

Conversation

bbc2
Copy link
Collaborator

@bbc2 bbc2 commented Jun 4, 2021

The value of quote_mode must now be one of auto, never or always, to ensure that users aren't accidentally relying on any other value for their scripts to work.

Surrounding quotes are no longer stripped. This makes it possible for the user to control exactly what goes in the .env file. Note that when doing dotenv set foo 'bar' in Bash, the shell will have already removed the quotes.

Single quotes are used instead of double quotes. This avoids accidentally having values interpreted by the parser or Bash (e.g. if you set a password with dotenv set password 'af$rb0'.

Previously, the auto mode of quoting had the same effect as always. This commit restores the functionality of auto by not quoting alphanumeric values (which don't need quotes). Plenty of other kinds of values also don't need quotes but it's hard to know which ones without actually parsing them, so we just omit quotes for alphanumeric values, at least for now.

The value of `quote_mode` must now be one of `auto`, `never` or
`always`, to ensure that users aren't accidentally relying on any other
value for their scripts to work.

Surrounding quotes are no longer stripped.  This makes it possible for
the user to control exactly what goes in the .env file.  Note that when
doing `dotenv set foo 'bar'` in Bash, the shell will have already
removed the quotes.

Single quotes are used instead of double quotes.  This avoids
accidentally having values interpreted by the parser or Bash (e.g. if
you set a password with `dotenv set password 'af$rb0'`.

Previously, the `auto` mode of quoting had the same effect as `always`.
This commit restores the functionality of `auto` by not quoting
alphanumeric values (which don't need quotes).  Plenty of other kinds of
values also don't need quotes but it's hard to know which ones without
actually parsing them, so we just omit quotes for alphanumeric values,
at least for now.
@bbc2 bbc2 merged commit b3c3195 into theskumar:master Jun 20, 2021
@bbc2 bbc2 deleted the improve-quote-mode branch June 20, 2021 11:30
if quote_mode == "always":
value_out = '"{}"'.format(value_to_set.replace('"', '\\"'))
if quote:
value_out = "'{}'".format(value_to_set.replace("'", "\\'"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just so you know, this is not the correct way to escape strings surrounded by single quotes when it comes to shells like Bash (this is the way).

I don't know if this project has a goal of making .env files compatible with Bash, if it does, than this code will fail in this edge-case. These commands in the Bash command-line will demonstrate my point:

$ printf %s 'one'"'"'two'
one'two
$ printf %s 'one\'two'
> 

(The last command doesn't run after pressing enter, but waits for a terminating single quote.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a very good point. I had actually forgotten about that rule. I'll see if we can do better with regards to matching Bash's behavior but I'm not sure it's worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants