fix(util): catch homepage validation exceptions

We were allowing erroneous URLs through, raising exceptions,
from e.g. `http://[localhost:8444/blah`. This patch catches
any ValueErrors raised during the parse process and returns
False, indicating that the validation failed.

This patch also adds testing specifically for `util.valid_homepage`.
We didn't have specific testing for this before; this will allow us
to catch regressions in this area.

Closes #250

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-01-13 19:19:06 -08:00
parent 1ee8d177b4
commit 60ae676075
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 13 additions and 1 deletions

View file

@ -63,7 +63,10 @@ def valid_email(email):
def valid_homepage(homepage):
try:
parts = urlparse(homepage)
except ValueError:
return False
return parts.scheme in ("http", "https") and bool(parts.netloc)

View file

@ -85,3 +85,12 @@ async def test_error_or_result():
response = await util.error_or_result(good_route, Request())
assert response.status_code == HTTPStatus.OK
def test_valid_homepage():
assert util.valid_homepage("http://google.com")
assert util.valid_homepage("https://google.com")
assert not util.valid_homepage("http://[google.com/broken-ipv6")
assert not util.valid_homepage("https://[google.com/broken-ipv6")
assert not util.valid_homepage("gopher://gopher.hprc.utoronto.ca/")