Skip to content

Commit 0dfd0c6

Browse files
authored
Localhost filter includes url (#5339)
* spaces * refactor and use urlparse * add test
1 parent 9845ce8 commit 0dfd0c6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/sentry/filters/localhost.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import absolute_import
22

33
from .base import Filter
4+
from six.moves.urllib.parse import urlparse
45

56
LOCAL_IPS = frozenset(['127.0.0.1', '::1'])
7+
LOCAL_DOMAINS = frozenset(['127.0.0.1', 'localhost'])
68

79

810
class LocalhostFilter(Filter):
@@ -16,5 +18,14 @@ def get_ip_address(self, data):
1618
except KeyError:
1719
return ''
1820

21+
def get_url(self, data):
22+
try:
23+
return data['sentry.interfaces.Http']['url'] or ''
24+
except KeyError:
25+
return ''
26+
27+
def get_domain(self, data):
28+
return urlparse(self.get_url(data)).netloc
29+
1930
def test(self, data):
20-
return self.get_ip_address(data) in LOCAL_IPS
31+
return self.get_ip_address(data) in LOCAL_IPS or self.get_domain(data) in LOCAL_DOMAINS

tests/sentry/filters/test_localhost.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ class LocalhostFilterTest(TestCase):
1010
def apply_filter(self, data):
1111
return self.filter_cls(self.project).test(data)
1212

13-
def get_mock_data(self, client_ip=None):
13+
def get_mock_data(self, client_ip=None, url=None):
1414
return {
1515
'sentry.interfaces.User': {
1616
'ip_address': client_ip,
17+
},
18+
'sentry.interfaces.Http': {
19+
'url': url,
1720
}
1821
}
1922

@@ -31,3 +34,20 @@ def test_does_not_filter_external_ip(self):
3134

3235
def test_fails_gracefully_without_user(self):
3336
assert not self.apply_filter({})
37+
38+
def test_filters_localhost_domain(self):
39+
data = self.get_mock_data(url='http://localhost/something.html')
40+
assert self.apply_filter(data)
41+
42+
data = self.get_mock_data(url='https://localhost')
43+
assert self.apply_filter(data)
44+
45+
data = self.get_mock_data(url='https://127.0.0.1')
46+
assert self.apply_filter(data)
47+
48+
def test_does_not_filter_non_localhost_domain(self):
49+
data = self.get_mock_data(url='https://getsentry.com/')
50+
assert not self.apply_filter(data)
51+
52+
data = self.get_mock_data(url='http://example.com/index.html?domain=localhost')
53+
assert not self.apply_filter(data)

0 commit comments

Comments
 (0)