Skip to content

Commit accaa55

Browse files
authored
don't append default headers to ip_headers, drop X-Client-Id (#64)
* dont append default headers, drop X-Client-Id * add pr number to changelog
1 parent f8319fe commit accaa55

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## master
22

3+
- [#64](https://github.com/castle/castle-python/pull/64)dropped X-Client-Id from calculation of ip, drop appending default ip headers to the ip_header list config when config is provided (in that case default headers have to explicitly provided)
4+
35
## 3.1.0 (2020-02-27)
46

57
- [#61](https://github.com/castle/castle-python/pull/61) improve headers and ip extractions, improve ip_headers config, add trusted proxies config, added more events to events list

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import and configure the library with your Castle API secret.
4444
configuration.blacklisted = ['HTTP-X-header']
4545
4646
# Castle needs the original IP of the client, not the IP of your proxy or load balancer.
47-
# we try to fetch proper ip based on X-Forwarded-For, X-Client-Id or Remote-Addr headers in that order
47+
# we try to fetch proper ip based on X-Forwarded-For or Remote-Addr headers in that order
4848
# but sometimes proper ip may be stored in different header or order could be different.
4949
# SDK can extract ip automatically for you, but you must configure which ip_headers you would like to use
5050
configuration.ip_headers = []
51-
# Additionally to make X-Forwarded-For or X-Client-Id work better discovering client ip address,
51+
# Additionally to make X-Forwarded-For and other headers work better discovering client ip address,
5252
# and not the address of a reverse proxy server, you can define trusted proxies
5353
# which will help to fetch proper ip from those headers
5454
configuration.trusted_proxies = []

castle/extractors/ip.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33

44

55
# ordered list of ip headers for ip extraction
6-
DEFAULT = ['X-Forwarded-For', 'Client-Ip', 'Remote-Addr']
7-
# default header fallback when ip is not found
8-
FALLBACK = 'Remote-Addr'
6+
DEFAULT = ['X-Forwarded-For', 'Remote-Addr']
97

108

119
class ExtractorsIp(object):
1210
def __init__(self, headers):
1311
self.headers = headers
14-
self.ip_headers = configuration.ip_headers + DEFAULT
12+
if len(configuration.ip_headers) > 0:
13+
self.ip_headers = configuration.ip_headers
14+
else:
15+
self.ip_headers = DEFAULT
1516
self.proxies = configuration.trusted_proxies + TRUSTED_PROXIES
1617

1718
def call(self):
19+
all_ips = []
20+
1821
for ip_header in self.ip_headers:
19-
ip_value = self._calculate_ip(ip_header)
20-
if ip_value:
21-
return ip_value
22+
ips = self._ips_from(ip_header)
23+
filtered_ips = self._remove_proxies(ips)
24+
25+
if len(filtered_ips) > 0:
26+
return filtered_ips[-1]
2227

23-
return self.headers.get(FALLBACK, None)
28+
all_ips = all_ips + ips
2429

25-
def _calculate_ip(self, header):
26-
ips = self._ips_from(header)
27-
filtered_ips = self._remove_proxies(ips)
30+
# fallback to first whatever ip
31+
if len(all_ips) > 0:
32+
return all_ips[0]
2833

29-
if len(filtered_ips) > 0:
30-
return filtered_ips[0]
3134
return None
3235

3336
def _remove_proxies(self, ips):
@@ -51,4 +54,4 @@ def _ips_from(self, header):
5154
if not value:
5255
return []
5356

54-
return re.split(r'[,\s]+', value.strip())[::-1]
57+
return re.split(r'[,\s]+', value.strip())

castle/test/extractors/ip_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def test_extract_ip(self):
1414

1515
def test_extract_ip_when_second_header(self):
1616
headers = {'Cf-Connecting-Ip': '1.2.3.4', 'X-Forwarded-For': '1.1.1.1, 1.2.2.2, 1.2.3.5'}
17-
configuration.ip_headers = ["HTTP_CF_CONNECTING_IP"]
17+
configuration.ip_headers = ["HTTP_CF_CONNECTING_IP", "X-Forwarded-For"]
1818
self.assertEqual(
1919
ExtractorsIp(headers).call(),
2020
'1.2.3.4'
2121
)
2222

2323
def test_extract_ip_when_second_header_with_different_setting(self):
2424
headers = {'Cf-Connecting-Ip': '1.2.3.4', 'X-Forwarded-For': '1.1.1.1, 1.2.2.2, 1.2.3.5'}
25-
configuration.ip_headers = ["CF-CONNECTING-IP"]
25+
configuration.ip_headers = ["CF-CONNECTING-IP", "X-Forwarded-For"]
2626
self.assertEqual(
2727
ExtractorsIp(headers).call(),
2828
'1.2.3.4'

0 commit comments

Comments
 (0)