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

Django improperly configured error if secret key begins with a dollar sign character #284

Closed
DanielOram opened this issue Jan 9, 2021 · 3 comments
Assignees
Labels
duplicate This issue or pull request already exists Edit

Comments

@DanielOram
Copy link

I've come across this error which only seems to occur when using the django-environ package to read a SECRET_KEY from a .env file.

My .env file (with a generated secret key using the get_random_secret_key module from django)

SECRET_KEY=$q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t)

My settings.py file

import environ
env = environ.Env()
environ.Env.read_env()

SECRET_KEY = env('SECRET_KEY')

Running this code will generate the following error

django.core.exceptions.ImproperlyConfigured: Set the q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t) environment variable

Even if I surround the secret key value with single or double quotes the error will still occur.

This error does not occur if I use a different python environment file package such as python-dotenv to read the same .env file.

The django-environ package seems to think the SECRET_KEY value is an environment variable itself.
I know this is a very niche situation as this secret key that I generated just happened to have a leading '$' character but it may catch people out if they're generating their secret key with a script. The error occurs for any secret key value that starts with a '$'.
Full stack trace:

Traceback (most recent call last):
  File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "/opt/anaconda3/envs/django_py/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/danieloram/Desktop/test_env/environ_secret_key.py", line 10, in <module>
    SECRET_KEY = env('SECRET_KEY')
  File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 123, in __call__
    return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
  File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 284, in get_value
    value = self.get_value(value, cast=cast, default=default)
  File "/opt/anaconda3/envs/django_py/lib/python3.9/site-packages/environ/environ.py", line 277, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the q*eo+c&2!)u9^tpd6f=0szxt6+th!j^#z9$1mh!kyen*36($t) environment variable

django version: 3.0.3

django-environ version: 0.4.5

@fdemmer
Copy link
Contributor

fdemmer commented Jan 28, 2021

duplicate of long standing issue #60

@zacwellmer
Copy link

@sergeyklay is there a way to not to escape backslashes when reading environment variables? I'm able to successfully load a private key with python-dotenv but am having issues when loading with django-environ

@sergeyklay
Copy link
Collaborator

@zacwellmer Current issues is duplicate for #60 and is related to using the dollar sign at the beginning of the value. Could you please open a new issue with code to reproduce?

@sergeyklay sergeyklay added the duplicate This issue or pull request already exists Edit label Dec 4, 2021
@sergeyklay sergeyklay self-assigned this Dec 4, 2021
@sergeyklay sergeyklay removed the bug Something isn't working label Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists Edit
Projects
None yet
Development

No branches or pull requests

4 participants