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

email_url uses unquote_plus which breaks values with + in them #388

Open
awbacker opened this issue May 6, 2022 · 1 comment
Open

email_url uses unquote_plus which breaks values with + in them #388

awbacker opened this issue May 6, 2022 · 1 comment

Comments

@awbacker
Copy link

awbacker commented May 6, 2022

When using email_url, if your username or password includes a + then the function will unquote it (change it to a space), breaking the value.

AWS SES passwords can and do have + in them.

I think email_url should be updated to unescape only classic % escaping via unquote(), or to abandon unescaping altogether.

No escaping actually has to be done, since urlparse does not care about the values. If you have a space in your username it will happily let you put it in the url.

>>> u = urlparse("sql://user:one+two%20three four@host.com:23")
>>> u.scheme, u.username, u.password, u.hostname
('sql', 'user', 'one+two%20three four', 'host.com')

This is of course subjective, and when dealing with URIs here I don't think there is always a correct answer. It may depend on where you get your settings from.

An alternative would be to raise an error if one of those conditions was found (space, + or % in the u/p) and instruct the user to pass in an unquote function of their liking or none to just take it as is.

config.update({
            'EMAIL_FILE_PATH': path,
            'EMAIL_HOST_USER': _cast_urlstr(url.username),
            'EMAIL_HOST_PASSWORD': _cast_urlstr(url.password),

...
def _cast_urlstr(v):
    return unquote_plus(v) if isinstance(v, str) else v

The

@awbacker awbacker changed the title email_url uses unquote_plus which breaks paswords/usernames with + in them email_url uses unquote_plus which breaks values with + in them May 6, 2022
@awbacker
Copy link
Author

awbacker commented May 6, 2022

I ended up with this code, just for reference:

_env = environ.Env()
_config: ParseResult = _env.url("EMAIL_URL", "dummymail://")

EMAIL_BACKEND = _env.EMAIL_SCHEMES[_config.scheme]  # just error if misconfigured
EMAIL_HOST_USER = _config.username
EMAIL_HOST_PASSWORD = _config.password
EMAIL_HOST = _config.hostname
EMAIL_PORT = _config.port
EMAIL_USE_TLS = "smtp" in _config.scheme

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

No branches or pull requests

1 participant