Skip to content

Commit d6d08db

Browse files
committed
Fix ident format when using HTTP_X_FORWARDED_FOR
If NUM_PROXIES setting is set to None, HTTP_X_FORWARDED_FOR might be used as is, which might contain spaces and cause errors on cache backends like memcached.
1 parent e9ac1bb commit d6d08db

File tree

1 file changed

+1
-7
lines changed

1 file changed

+1
-7
lines changed

rest_framework/throttling.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_ident(self, request):
3535
client_addr = addrs[-min(num_proxies, len(xff))]
3636
return client_addr.strip()
3737

38-
return xff if xff else remote_addr
38+
return ''.join(xff.split()) if xff else remote_addr
3939

4040
def wait(self):
4141
"""
@@ -173,12 +173,6 @@ def get_cache_key(self, request, view):
173173
if request.user.is_authenticated():
174174
return None # Only throttle unauthenticated requests.
175175

176-
ident = request.META.get('HTTP_X_FORWARDED_FOR')
177-
if ident is None:
178-
ident = request.META.get('REMOTE_ADDR')
179-
else:
180-
ident = ''.join(ident.split())
181-
182176
return self.cache_format % {
183177
'scope': self.scope,
184178
'ident': self.get_ident(request)

0 commit comments

Comments
 (0)