Skip to content

Commit

Permalink
api: support wildcard host and port combinations in origin checks (ge…
Browse files Browse the repository at this point in the history
…tsentry#4582)

This is more commonly needed for CSP where a common path might be
something like: `<ip>:1234` with varying IP addresses.

This allows adding a block rule to address `*:1234` correctly.
  • Loading branch information
mattrobenolt committed Nov 22, 2016
1 parent 26f2193 commit 171b92e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/sentry/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def is_valid_origin(origin, project=None, allowed=None):
- *: allow any domain
- *.domain.com: matches domain.com and all subdomains, on any port
- domain.com: matches domain.com on any port
- *:port: wildcard on hostname, but explicit match on port
"""
if allowed is None:
allowed = get_origins(project)
Expand Down Expand Up @@ -169,9 +170,15 @@ def is_valid_origin(origin, project=None, allowed=None):
parsed_hostname = parsed.hostname

if parsed.port:
parsed_netloc = '%s:%d' % (parsed_hostname, parsed.port)
domain_matches = (
'*', parsed_hostname,
# Explicit hostname + port name
'%s:%d' % (parsed_hostname, parsed.port),
# Wildcard hostname with explicit port
'*:%d' % parsed.port,
)
else:
parsed_netloc = parsed_hostname
domain_matches = ('*', parsed_hostname)

for value in allowed:
try:
Expand All @@ -189,7 +196,7 @@ def is_valid_origin(origin, project=None, allowed=None):
if parsed_hostname.endswith(bits.domain[1:]) or parsed_hostname == bits.domain[2:]:
return True
continue
elif bits.domain not in ('*', parsed_hostname, parsed_netloc):
elif bits.domain not in domain_matches:
continue

# path supports exact, any, and suffix match (with or without *)
Expand Down
4 changes: 4 additions & 0 deletions tests/sentry/utils/http/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def test_unparseable_uri(self):
result = self.isValidOrigin('http://example.com', ['.'])
assert result is False

def test_wildcard_hostname_with_port(self):
result = self.isValidOrigin('http://example.com:1234', ['*:1234'])
assert result is True


class IsValidIPTestCase(TestCase):
def is_valid_ip(self, ip, inputs):
Expand Down

0 comments on commit 171b92e

Please sign in to comment.