You already know web security is important to keeping hackers and cyber-thieves from accessing sensitive information. So, in this post we are going to check Django security vulnerabilities and how to fix them. Deployment Checklist First thing first, check your security vulnerabilities by following command: manage.py check --deploy You can see some descriptions which provide information about your Django web application vulnerabilities. Try to google these security issues and fix them before production. The Mozilla Observatory If you already deployed you application then use site to scan the security status of your site. The site also includes third-party scanners which test other security aspects of your site. Observatory by Mozilla Here is the example of scan: Cross site request forgery (CSRF) protection In a web application, basically the webforms take input from the user and send them to server-side components to process them. The server-side components generally expose the service as a , , methods for accepting the data over HTTP. Django has built-in security against most forms of CSRF threats, as long as you have allowed and used it if necessary. POST PUT DELETE As stated in the , be very careful when marking views with the decorator, unless it is absolutely necessary. documentation csrf_exempt If someone has access (through an man-in-the-middle attack or xss) to your csrftoken cookie, then this is a vulnerability. The CSRF protection cannot protect against attacks, so use with (We will discuss it in post later). man-in-the-middle HTTPS HTTP Strict Transport Security Once you’ve set up HTTPS, add these lines in your settings.py: CSRF_COOKIE_SECURE = SESSION_COOKIE_SECURE = True #to avoid transmitting the CSRF cookie over HTTP accidentally. True #to avoid transmitting the session cookie over HTTP accidentally. Cross-site Scripting (XSS) A Cross-site Scripting (XSS) allows an attacker to inject a script into the content of a website or app. When a user visits the infected page the script will execute in the victim’s browser. This allows attackers to steal private information like cookies, account information, etc. enables XSS filtering. Rather than sanitizing the page, the browser will prevent rendering of the page if an attack is detected. X-XSS-Protection: 1; mode=block To enable it in Django, make sure is present in middleware's list and add following lines in your : django.middleware.security.SecurityMiddleware settings.py SECURE_BROWSER_XSS_FILTER = SECURE_CONTENT_TYPE_NOSNIFF = True True Django Admin Security One of the most important thing is to make Django administration secure. Before you deploy your application you must change admin/ path to something only you know. Otherwise, someone can easily type /admin in url and access to adminsitrator login page. #urls.py django.contrib admin django.urls path urlpatterns = [ path( , admin.site.urls) # change admin something different from import from import 'admin/' You can create fake admin login page using and it will notify you if someone try attempt unauthorized access. django-admin-honeypot SSL Redirect Add following line to your to force Django redirect all non-HTTPS requests to HTTPS. settings.py SECURE_SSL_REDIRECT = True Content Security Policy (CSP) If your Django application is large, contains a lot of third-party code, and has a lot of inline scripts and styles scattered all over the project, then you should add to your site. CSP For more information about CSP visit An Introduction to Content Security Policy Django does not have a built-in method for creating a CSP header, so you can install Mozilla’s module and use your browser's console to track the security violations in your code. django-csp Once you installed django-csp, add following lines to your settings.py. CSP_DEFAULT_SRC = ( , ) CSP_STYLE_SRC = ( , ) CSP_SCRIPT_SRC = ( , ) CSP_IMG_SRC = ( , ) CSP_FONT_SRC = ( , ) # Content Security Policy "'none'" "'self'" "'self'" "'self'" "'self'" So, basically, your all inline scripts and styles will not be allowed anymore. All scripts and styles must be loaded from a resource. You can add ‘unsafe-inline’ to your script and style CSP headers, however, it negates the whole policy. Its really important to clean your code from all these inline styles and scripts. However, some external resources such as Google Tag Manager or Google Analytics should be allowed in your CSP policy. To achieve that update your code like this: CSP_DEFAULT_SRC = ( , ) CSP_STYLE_SRC = ( , , ) CSP_SCRIPT_SRC = ( , , , ) CSP_IMG_SRC = ( , , , ) CSP_FONT_SRC = ( , ) CSP_CONNECT_SRC = ( , ) CSP_OBJECT_SRC = ( , ) CSP_BASE_URI = ( , ) CSP_FRAME_ANCESTORS = ( , ) CSP_FORM_ACTION = ( , ) CSP_INCLUDE_NONCE_IN = ( ,) #Content Security Policy "'none'" "'self'" "fonts.googleapis.com" "'sha256-/3kWSXHts8LrwfemLzY9W0tOv5I4eLIhrf0pT8cU0WI='" "'self'" "ajax.googleapis.com" "www.googletagmanager.com" "www.google-analytics.com" "'self'" "data:" "www.googletagmanager.com" "www.google-analytics.com" "'self'" "fonts.gstatic.com" "'self'" "'none'" "'none'" "'none'" "'self'" 'script-src' Fore more information take a look . django-csp documentation Note that this configuration depends on which external resources you are using so please first read the documentation and then apply changes to your site. HTTP Strict Transport Security When this policy is set, browsers will refuse to connect to your site for the given time period if you’re not properly serving HTTPS resources, or if your certificate expires. Add the following lines to your : settings.py SECURE_HSTS_SECONDS = SECURE_HSTS_INCLUDE_SUBDOMAINS = SECURE_HSTS_PRELOAD = 86400 # 1 day True True Extra Security Tools from Nikita Sobolev Use or similar to block brute-force requests django-axes Use header to switch on only things you really need in user's browser Feature-Policy Use header to prevent sensitive information from leaking into other resources Referrer-Policy Use safety to make sure your dependencies are secure and do not contain any known vulnerabilitiesUse to check for your source code to be secure wemake-python-styleguide I recommend to use . It is a new project boilerplate focused on security and code quality. It has everything from the list. And even several more advanced features! wemake-django-template Mission Accomplished! Now your app is almost secure. Additionally you can scan your open ports by using and try to google how to fix these open ports. nmap If you liked the post please visit for more cool stuff like this and share it with your friends! Reverse Python Stay Connected!