You are on page 1of 3

Cheat sheet: Django security tips

Lucky you, you user of the web framework for Throttle user authentications Django does allow the use of raw queries, but their use is
perfectionists with deadlines (AKA Django). The not recommended. If you do use them, take extra care to
properly escape any parameters. If you find the Django
Django team has put a lot of thought into their Django provides a lot of security features baked in, but the
authentication system does not inherently protect against brute ORM to be insufficient for your needs, it is possible to use
security practice (find security features in their a different ORM within Django. SQLAlchemy is an
force attacks. A malicious actor could hit your system with
documentation and their security policy is interest- numerous login attempts, and potentially get in. example of an ORM that can be used with Django. If there
ing too). We have summarized some of the best is an ORM that better suits your project, making use of it is
tips to keep your Django project secure. If this kind of attack is of concern for your project, use a project preferable to writing large amounts of raw sql.
like Django Defender to lock out users after too many login
Django Security tips attempts. Use https

What version of Django are you using? Django currently has Protect your source code Regardless of your framework of choice, it is always
three major versions in use (though long term support for 1.11 is preferable to deploy behind HTTPS. Doing so prevents
ending in April 2020). The version of Django you use has a Protecting your source code may seem to be an obvious step, malicious users from intercepting information sent
number of implications on your project. For instance, Django but it is a multi-faceted step and is, therefore, worth exploring. between the client and the server. For https to work
1.11 is the last version to use Python 2 (which was sunsetted at One way to protect your source code is to make sure that it is correctly, it is important to be sure that you have properly
the beginning of 2020). not included in your web server’s root directory. If it is, there is a determined your settings.
possibility that it is served or that, part of it, is executed in a way
Additionally, the choice of version determines what known that you had not planned. The following settings are important to your use of https.
vulnerabilities are present and potentially exploitable in your Be sure you understand them and enable them properly.
application. Learn more about known Django vulnerabilities And although it goes without saying, if your project is sensitive, The documentation does a good job of explaining the
from our vulnerability database or scan your project with Snyk, be sure to use a private repository on GitHub, Bitbucket, or settings, the secure defaults, and why you might need to
and we will let you know if you are using a vulnerable version. Gitlab. Also, make sure to never check your secrets into your change them in a given circumstance.
version control system, regardless of whether you intend to use
Finally, it is important that you have a plan in place to keep your a private repo. It is possible that a private repository does not SECURE_PROXY_SSL_HEADER
Django version up to date. Generally it is a good idea to settle always stay private and someone with access to a private repo SECURE_SSL_REDIRECT
on a long term supported version and have a plan for moving to cannot always be trusted. SESSION_COOKIE_SECURE
the next one up before support ends for your current version.
That way you benefit from maintainer support, but you don’t CSRF_COOKIE_SECURE
have to be changing your version constantly. Use raw queries and custom SQL with caution SECURE_HSTS_SECONDS
SECURE_HSTS_INCLUDE_SUBDOMAINS
Additionally, be sure that you are referencing documentation While it is tempting to write raw sql queries and custom SQL,
doing so may open the door for an attack. Django’s object-rela-
SECURE_HSTS_PRELOAD
from the correct Django version. You would not want to believe
a security policy is in place because you read about it in the tional-mapping (ORM) framework is designed to make querying
documentation, only to discover that there was a mismatch your database easy. Querysets are constructed using query
between the version you are using and the documentation you paramatization. The queries parameters have been abstracted
are reading. away from the queries sql code. A user attempting to perform a
sql injection (execute arbitrary sql on a database) is going to
find it much harder if you always use the ORM.
www.snyk.io
Cheat sheet: Django security tips

Watch your headers Carefully handle user uploads When you include Django (or any open source library for that
matter), typically, you are also including every library that your
First, we want to specifically consider the referer request header. If your web application allows users to upload files, you are library of choice utilizes. Open source is built on open source
This header contains the address of the previous web page from opening yourself to an attack vector and the upload logic should which is built on more open source.
which you arrived at the current page. This information is useful therefore be handled carefully. First, it is important to validate
for analytics, among other things. Sometimes though, It causes all uploaded files to be sure they are what you expect (for Indirect dependencies are as likely to introduce risk as direct
problems because, generally, people do not like to be followed instance, an image file and not a PHP script!). You don’t want it dependencies, but these risks are less likely to be recognized. A
around the web. Due to privacy concerns, it is possible to to be possible for a user to run code. tool like Snyk helps you understand your entire dependency
disable this functionality. Whether a referer request header is tree, and now that Snyk offers fix pull requests for Python, fixing
passed along, can be determined by the referer-policy header. problems (even in indirect dependencies) is easier than ever.
Under certain conditions, partial information is passed along — There are a number of things to help validate uploaded files or
in others, all of the information is included, and sometimes no handle them more carefully. They include the following: Don’t let the perfect get in the way of the good
referer request header information is forwarded.
• Create a whitelist of acceptable file types Every security step you take is a step in the right direction.
When the site is served via https, the referer request header is Limit file size allowed for uploads to prevent denial of Django may be for perfectionists with deadlines, but code
utilized by Django to help prevent cross site request forgery service problems doesn’t have to be perfect to reap security benefits. Implement-
(CRSF) attacks. If you are too strict with your referer-policy • Disable handlers that would execute static files as code ing the concepts discussed above, to the best of your ability, can
header, you disable the functionality of Django’s CRSF protec- (for example, disable Apache’s mod_php) dramatically improve the security of your code and result in a
tion. In the end, you need to weigh the privacy benefits of using healthier, more resilient project. Happy coding, pythonistas!
a strict referer-policy header with the benefits of the CRSF • Make cross site scripting protections work for you. If
protection. It is possible to “split the difference” by only you serve user content from a distinct top level
enabling same-origin referrers in your referer-policy header. domain, the protections against cross site scripting will
kick in to protect you. This can be a domain that you
Be careful with your cookies manage, although it is probably easier to serve the files
from a cloud service or content delivery network
Some cookies are more secure than others — the default cookie provider.
behavior is to connect over http. However, since we already
established that you need to use https, you want to make sure Understand all of your dependencies
your cookies are only being sent over https as well. To prevent
leaking cookies, be sure to set your SESSION_COOKIE_SECURE If you have selected a Django version with little to no known
and CSRF_COOKIE_SECURE settings to True. This instructs the security risks, you probably think that there are no more security
browser to only send these cookies over HTTPS connections. issues to consider. However, it is important to understand the
There are some interesting side effects to setting these open source libraries that Django is importing and whether
parameters to true, but they should be mitigated by redirecting there are vulnerabilities in those libraries.
http traffic to https.

www.snyk.io
Cheat sheet: Django security tips

1. Know your version and use a secure one 7. Be careful with your cookies

What version of Django are you using? The choice of version determines what known Some cookies are more secure than others — the default cookie behavior is to connect
vulnerabilities are present, and potentially exploitable, in your application. Learn more about over http. However, since we already established that you need to use https, you want to
known Django vulnerabilities from our vulnerability database or you scan your project with make sure your cookies are only being sent over https as well. To prevent leaking cookies,
Snyk. be sure to set your SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE settings to
True.
2. Throttle user authentications
8. Carefully handle user uploads
Django provides a lot of security features baked in, but the authentication system does not
inherently protect against brute force attacks. It is important for you to write your own code If your web application allows users to upload files, you are opening yourself to an attack
to prevent this, or use one of many open source solutions (like Django Defender). vector and the upload logic should therefore be handled carefully. It is important to
validate all uploaded files to be sure they are what you expect (for instance, an image file
3. Protect your source code and not a PHP script!).

Make sure your source code is not included in your web server’s root directory. Use a private 9. Understand all of your dependencies
repository if your project is sensitive. Never check your secrets into version control, even if
you are using a private repository. Indirect dependencies are as likely to introduce risk as direct dependencies, but these risks
are less likely to be recognized. A tool like Snyk helps you understand your entire
4. Use raw queries and custom SQL with caution dependency tree, and now that Snyk offers fix pull requests for Python, fixing problems
(even in indirect dependencies) is easier than ever.
While it may be tempting to write raw sql queries and custom SQL, doing so may open the
door for an attack. A user attempting to perform an sql injection (execute arbitrary sql on a 10. Don’t let the perfect get in the way of the good
database) is going to find it much harder, if you always use the ORM.
Every security step you take is a step in the right direction. Django may be for perfection-
5. Use HTTPs ists with deadlines, but code doesn’t have to be perfect to reap security benefits.

Regardless of your framework of choice, it is always preferable to deploy behind HTTPS.


Doing so prevents malicious users from intercepting information sent between the client
and the server.

6. Watch your headers

When the site is served via https, the referer request header is utilized by Django to help
prevent cross site request forgery (CRSF) attacks. If you are too strict with your referer-policy
header, you disable the functionality of Django’s CRSF protection. In the end you need to
weigh the privacy benefits of using a strict referer-policy header with the benefits of the
CRSF protection. www.snyk.io

You might also like