I have a Django app hosted on AWS. I use Elastic Beanstalk and use a .ebextensions/django.config
file to input all my custom server side settings.
I have ALLOWED_HOSTS
set up so that if anybody tries to connect to my site from an invalid host header they get blocked...by Django.
I get all kinds of Django error logging emails saying Invalid HTTP_HOST header: 123.456.789
. -- essentially bots / scanners trying to connect and/or upload malicious content.
I'd like to block these bad requests at a server side as it seems more secure to have that extra blocking layer, and I don't like getting all the emails.
In the Django docs they write that "[they recommend] configuring your web server to ensure it validates incoming HTTP Host headers." I'd like to do that in my .ebextensions/django.config
file.
Here is my current .ebextensions/django.config
file:
container_commands:
01_migrate:
command: "python manage.py migrate --noinput"
02_collectstatic:
command: "python manage.py collectstatic --noinput"
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: myapp/wsgi.py
aws:elasticbeanstalk:container:python:staticfiles:
/static/: static/
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
WSGIApplicationGroup %{GLOBAL}
RewriteEngine On
<If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</If>
Header always set Referrer-Policy no-referrer
Header always set Strict-Transport-Security "max-age=14400; includeSubdomains;"
I believe it is all apache config. I found this SO answer on this subject that writes "To deny requests with no HOST set you can use:"
SetEnvIfNoCase Host .+ VALID_HOST
Order Deny,Allow
Deny from All
Allow from env=VALID_HOST
However I'm unsure of what that code is doing (and if it's what I need), and how to translate it into .ebextensions.
Ultimately I'd like to find out what I can add to my .ebextensions file to make it validate HTTP_HOST headers before they reach Django.