-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement getting the source IP address of a request
- Loading branch information
Showing
7 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# coding: utf8 | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from ipaddress import IPv4Network | ||
|
||
from liberapay.testing import Harness | ||
|
||
|
||
class Tests(Harness): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super(Tests, cls).setUpClass() | ||
cls.website._trusted_proxies = getattr(cls.website, 'trusted_proxies', None) | ||
cls.website.trusted_proxies = [ | ||
[IPv4Network('10.0.0.0/8')], | ||
[IPv4Network('141.101.64.0/18')], | ||
] | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.website.trusted_proxies = cls.website._trusted_proxies | ||
super(Tests, cls).tearDownClass() | ||
|
||
def request(self, forwarded_for, source, **kw): | ||
kw['HTTP_X_FORWARDED_FOR'] = forwarded_for | ||
kw['REMOTE_ADDR'] = source | ||
kw.setdefault('return_after', 'attach_environ_to_request') | ||
kw.setdefault('want', 'request') | ||
return self.client.GET('/', **kw).source | ||
|
||
def test_request_source_with_invalid_header_from_trusted_proxy(self): | ||
source = str(self.request(b'f\xc3\xa9e, \t bar', b'10.0.0.1')) | ||
assert source == '10.0.0.1' | ||
|
||
def test_request_source_with_invalid_header_from_untrusted_proxy(self): | ||
source = str(self.request(b'f\xc3\xa9e, \tbar', b'8.8.8.8')) | ||
assert source == '8.8.8.8' | ||
|
||
def test_request_source_with_valid_headers_from_trusted_proxies(self): | ||
source = str(self.request(b'8.8.8.8,141.101.69.139', b'10.0.0.1')) | ||
assert source == '8.8.8.8' | ||
source = str(self.request(b'8.8.8.8', b'10.0.0.2')) | ||
assert source == '8.8.8.8' | ||
|
||
def test_request_source_with_valid_headers_from_untrusted_proxies(self): | ||
# 8.8.8.8 claims that the request came from 0.0.0.0, but we don't trust 8.8.8.8 | ||
source = str(self.request(b'0.0.0.0, 8.8.8.8,141.101.69.140', b'10.0.0.1')) | ||
assert source == '8.8.8.8' | ||
source = str(self.request(b'0.0.0.0, 8.8.8.8', b'10.0.0.1')) | ||
assert source == '8.8.8.8' | ||
|
||
def test_request_source_with_forged_headers_from_untrusted_client(self): | ||
# 8.8.8.8 claims that the request came from a trusted proxy, but we don't trust 8.8.8.8 | ||
source = str(self.request(b'0.0.0.0,141.101.69.141, 8.8.8.8,141.101.69.142', b'10.0.0.1')) | ||
assert source == '8.8.8.8' | ||
source = str(self.request(b'0.0.0.0, 141.101.69.143, 8.8.8.8', b'10.0.0.1')) | ||
assert source == '8.8.8.8' |