Skip to content

Commit d78cf61

Browse files
committed
BACKWARDS-INCOMPATIBLE CHANGE: Removed SetRemoteAddrFromForwardedFor middleware.
In a nutshell, it's been demonstrated that this middleware can never be made reliable enough for general-purpose use, and that (despite documentation to the contrary) its inclusion in Django may lead application developers to assume that the value of ``REMOTE_ADDR`` is "safe" or in some way reliable as a source of authentication. So it's gone. See the Django 1.1 release notes for full details, as well as upgrade instructions. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11363 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 91f1840 commit d78cf61

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

django/middleware/http.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.exceptions import MiddlewareNotUsed
12
from django.utils.http import http_date
23

34
class ConditionalGetMiddleware(object):
@@ -32,24 +33,19 @@ def process_response(self, request, response):
3233

3334
class SetRemoteAddrFromForwardedFor(object):
3435
"""
35-
Middleware that sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, if the
36-
latter is set. This is useful if you're sitting behind a reverse proxy that
37-
causes each request's REMOTE_ADDR to be set to 127.0.0.1.
38-
39-
Note that this does NOT validate HTTP_X_FORWARDED_FOR. If you're not behind
40-
a reverse proxy that sets HTTP_X_FORWARDED_FOR automatically, do not use
41-
this middleware. Anybody can spoof the value of HTTP_X_FORWARDED_FOR, and
42-
because this sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, that means
43-
anybody can "fake" their IP address. Only use this when you can absolutely
44-
trust the value of HTTP_X_FORWARDED_FOR.
36+
This middleware has been removed; see the Django 1.1 release notes for
37+
details.
38+
39+
It previously set REMOTE_ADDR based on HTTP_X_FORWARDED_FOR. However, after
40+
investiagtion, it turns out this is impossible to do in a general manner:
41+
different proxies treat the X-Forwarded-For header differently. Thus, a
42+
built-in middleware can lead to application-level security problems, and so
43+
this was removed in Django 1.1
44+
4545
"""
46-
def process_request(self, request):
47-
try:
48-
real_ip = request.META['HTTP_X_FORWARDED_FOR']
49-
except KeyError:
50-
return None
51-
else:
52-
# HTTP_X_FORWARDED_FOR can be a comma-separated list of IPs. The
53-
# client's IP will be the first one.
54-
real_ip = real_ip.split(",")[0].strip()
55-
request.META['REMOTE_ADDR'] = real_ip
46+
def __init__(self):
47+
import warnings
48+
warnings.warn("SetRemoteAddrFromForwardedFor has been removed. "
49+
"See the Django 1.1 release notes for details.",
50+
category=DeprecationWarning)
51+
raise MiddlewareNotUsed()

docs/ref/middleware.txt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,10 @@ Reverse proxy middleware
122122

123123
.. class:: django.middleware.http.SetRemoteAddrFromForwardedFor
124124

125-
Sets ``request.META['REMOTE_ADDR']`` based on
126-
``request.META['HTTP_X_FORWARDED_FOR']``, if the latter is set. This is useful
127-
if you're sitting behind a reverse proxy that causes each request's
128-
``REMOTE_ADDR`` to be set to ``127.0.0.1``.
129-
130-
**Important note:** This does NOT validate ``HTTP_X_FORWARDED_FOR``. If you're
131-
not behind a reverse proxy that sets ``HTTP_X_FORWARDED_FOR`` automatically, do
132-
not use this middleware. Anybody can spoof the value of
133-
``HTTP_X_FORWARDED_FOR``, and because this sets ``REMOTE_ADDR`` based on
134-
``HTTP_X_FORWARDED_FOR``, that means anybody can "fake" their IP address. Only
135-
use this when you can absolutely trust the value of ``HTTP_X_FORWARDED_FOR``.
125+
.. versionchanged: 1.1
126+
127+
This middleware was removed in Django 1.1. See :ref:`the release notes
128+
<removed-setremoteaddrfromforwardedfor-middleware>` for details.
136129

137130
Locale middleware
138131
-----------------

0 commit comments

Comments
 (0)