Skip to content
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

Localhost filter includes url #5339

Merged
merged 3 commits into from Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sentry/filters/localhost.py
@@ -1,8 +1,10 @@
from __future__ import absolute_import

from .base import Filter
from six.moves.urllib.parse import urlparse

LOCAL_IPS = frozenset(['127.0.0.1', '::1'])
LOCAL_DOMAINS = frozenset(['127.0.0.1', 'localhost'])


class LocalhostFilter(Filter):
Expand All @@ -16,5 +18,14 @@ def get_ip_address(self, data):
except KeyError:
return ''

def get_url(self, data):
try:
return data['sentry.interfaces.Http']['url'] or ''
except KeyError:
return ''

def get_domain(self, data):
return urlparse(self.get_url(data)).netloc

def test(self, data):
return self.get_ip_address(data) in LOCAL_IPS
return self.get_ip_address(data) in LOCAL_IPS or self.get_domain(data) in LOCAL_DOMAINS
22 changes: 21 additions & 1 deletion tests/sentry/filters/test_localhost.py
Expand Up @@ -10,10 +10,13 @@ class LocalhostFilterTest(TestCase):
def apply_filter(self, data):
return self.filter_cls(self.project).test(data)

def get_mock_data(self, client_ip=None):
def get_mock_data(self, client_ip=None, url=None):
return {
'sentry.interfaces.User': {
'ip_address': client_ip,
},
'sentry.interfaces.Http': {
'url': url,
}
}

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

def test_fails_gracefully_without_user(self):
assert not self.apply_filter({})

def test_filters_localhost_domain(self):
data = self.get_mock_data(url='http://localhost/something.html')
assert self.apply_filter(data)

data = self.get_mock_data(url='https://localhost')
assert self.apply_filter(data)

data = self.get_mock_data(url='https://127.0.0.1')
assert self.apply_filter(data)

def test_does_not_filter_non_localhost_domain(self):
data = self.get_mock_data(url='https://getsentry.com/')
assert not self.apply_filter(data)

data = self.get_mock_data(url='http://example.com/index.html?domain=localhost')
assert not self.apply_filter(data)