Django Web Application Security Checklist 2
Django Web Application Security Checklist 2
1) Debug = False
Please, please, please never deploy your application with DEBUG = TRUE on. Ensure that
DEBUG is set to False. If you don’t do this then Django will expose all your settings and
environment variables when an exception occurs.
# settings.py
DEBUG = FALSE
2) Deployment checklist
The next important step is to run the below command in your terminal. You will then see
information pertaining to your Django web application. This is very useful in giving you a
quick breakdown of the major issues that require your attention before you go through
with deployment. For those of you that like to get ahead early in the game, give those
security messages a quick google.
# settings.py
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
4) SSL redirect
The below line will ensure that your application redirects all non-HTTPS requests to HTTP.
# settings.py
SECURE_SSL_REDIRECT = True
Check the before and after code snippets to understand how to change your admin URL.
Before:
# urls.py
urlpatterns = [
path(‘admin/’, admin.site.urls) # Default admin URL
]
After:
# urls.py
# settings.py
SECURE_HSTS_SECONDS = 86400
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
Be sure that HTTPS is set up, then add the following lines:
# settings.py
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
8) Use python-decouple
Your settings.py file will be full of sensitive information. Be sure to use python-decouple to
keep everything separate and safe.
Step 1:
To install python-decouple in your application, open up your terminal and type in the
following command:
Step 2:
Create a .env file in your repository’s root directory.
Step 3:
As a test, we will store some important data, such as debug and our secret key. So, simply
copy + paste your debug and secret key from settings.py as-is into your .env file.
DEBUG=False
SECRET_KEY=’my_secret_key’
Step 4:
If you happen to be using Git be sure to .gitignore your .env file for security purposes.
Step 5:
# settings.py
Step 6:
Decouple will always return our data as a string. To solve this problem, we need to cast it to
a bool if we are expecting a Boolean or to an int if we are expecting an integer. Go back to
your settings.py and modify your existing debug and secret key values with the following:
# settings.py
Mozilla Observatory
The Mozilla Observatory is a project designed to help developers, system administrators,
and security professionals…
Mozilla Observatory
And that’s that! Your Django web application has the basic components that are required
for it to be secure. Of course, there are more ways to improve the security of your web
application, but you have now implemented the bare minimum that you require.