Skip to content

Fix ident format when using HTTP_X_FORWARDED_FOR #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions rest_framework/throttling.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def get_ident(self, request):
if num_proxies == 0 or xff is None:
return remote_addr
addrs = xff.split(',')
client_addr = addrs[-min(num_proxies, len(xff))]
client_addr = addrs[-min(num_proxies, len(addrs))]
return client_addr.strip()

return xff if xff else remote_addr
return ''.join(xff.split()) if xff else remote_addr

def wait(self):
"""
Expand Down Expand Up @@ -173,12 +173,6 @@ def get_cache_key(self, request, view):
if request.user.is_authenticated():
return None # Only throttle unauthenticated requests.

ident = request.META.get('HTTP_X_FORWARDED_FOR')
if ident is None:
ident = request.META.get('REMOTE_ADDR')
else:
ident = ''.join(ident.split())

return self.cache_format % {
'scope': self.scope,
'ident': self.get_ident(request)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xordoquy note here we weren't using the ident variable

Expand Down