Django의 SuspiciousOperation 잘못된 HTTP_HOST 헤더
Django 1.5로 업그레이드 한 후 다음과 같은 오류가 발생하기 시작했습니다.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 92, in get_response
response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py", line 57, in process_request
host = request.get_host()
File "/usr/local/lib/python2.7/dist-packages/django/http/request.py", line 72, in get_host
"Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): %s" % host)
SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): www.google.com
<WSGIRequest
path:/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{},
META:{'CONTENT_LENGTH': '',
'CONTENT_TYPE': '',
'DOCUMENT_ROOT': '/etc/nginx/html',
'HTTP_ACCEPT': 'text/html',
'HTTP_HOST': 'www.google.com',
'HTTP_PROXY_CONNECTION': 'close',
'HTTP_USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'PATH_INFO': u'/',
'QUERY_STRING': '',
'REMOTE_ADDR': '210.245.91.104',
'REMOTE_PORT': '49347',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/',
u'SCRIPT_NAME': u'',
'SERVER_NAME': 'www.derekkwok.net',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.0',
'uwsgi.node': 'derekkwok',
'uwsgi.version': '1.4.4',
'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0xb6d99c28>,
'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
'wsgi.input': <uwsgi._Input object at 0x953e698>,
'wsgi.multiprocess': True,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>
내가 설정 한 ALLOWED_HOSTS = ['.derekkwok.net']
내 settings.py 파일에.
여기서 무슨 일이 일어나고 있습니까? 누군가가 Google 인 척하고 내 사이트에 액세스하고 있습니까? 아니면 누군가가 자신의 HTTP_HOST 헤더를 잘못 설정하는 것은 무해한 경우입니까?
귀하의 경우 ALLOWED_HOSTS
IS 올바르게 설정, 그것은 가능 누군가가 헤더를 위장하여 취약점에 대한 귀하의 사이트를 프로빙이다.
현재 Django 개발자가 이것을 500 내부 서버 오류에서 400 응답으로 변경하는 논의가 있습니다. 이 티켓을 참조하십시오 .
If you're using Nginx to forward requests to Django running on Gunicorn/Apache/uWSGI, you can use the following to block bad requests. Thanks to @PaulM for the suggestion and this blog post for an example.
upstream app_server {
server unix:/tmp/gunicorn_mydomain.com.sock fail_timeout=0;
}
server {
...
## Deny illegal Host headers
if ($host !~* ^(mydomain.com|www.mydomain.com)$ ) {
return 444;
}
location / {
proxy_pass http://app_server;
...
}
}
When using Nginx you could set up you servers in a way only requests to the hosts you want get to Django in the first place. That should give you no SuspiciousOperation errors anymore.
server {
# default server
listen 80;
server_name _ default;
return 444;
}
server {
# redirects
listen 80;
server_name example.com old.stuff.example.com;
return 301 http://www.example.com$request_uri;
}
server {
# app
listen 80;
server_name www.example.com; # only hosts in ALLOWED_HOSTS here
location / {
# ...
}
# ... your config/proxy stuff
}
This is fixed in newer versions of Django, but if you're using an affected version (e.g. 1.5) you can add a filter to your logger handler to get rid of these, as outlined in this blog post.
Spoiler:
from django.core.exceptions import SuspiciousOperation
def skip_suspicious_operations(record):
if record.exc_info:
exc_value = record.exc_info[1]
if isinstance(exc_value, SuspiciousOperation):
return False
return True
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
# Define filter
'skip_suspicious_operations': {
'()': 'django.utils.log.CallbackFilter',
'callback': skip_suspicious_operations,
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
# Add filter to list of filters
'filters': ['require_debug_false', 'skip_suspicious_operations'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
참고URL : https://stackoverflow.com/questions/15238506/djangos-suspiciousoperation-invalid-http-host-header
'IT story' 카테고리의 다른 글
LocationManager에 대한 Android 확인 권한 (0) | 2020.09.06 |
---|---|
AWS S3 CLI-엔드 포인트 URL에 연결할 수 없습니다. (0) | 2020.09.06 |
Angular 2 '구성 요소'는 알려진 요소가 아닙니다. (0) | 2020.09.06 |
$ LOAD_PATH (Ruby)에 디렉토리 추가 (0) | 2020.09.06 |
c # : 게터 / 세터 (0) | 2020.09.06 |