From d7a51916ad347e062609d9f8bb2e2ab6da1b2fab Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Wed, 18 Oct 2023 11:02:50 -0700 Subject: [PATCH] /r/ redirect: handle bad URLs fixes https://console.cloud.google.com/errors/detail/CJ30oPbsoqmI7QE;time=P30D?project=bridgy-federated --- redirect.py | 5 ++++- tests/test_redirect.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/redirect.py b/redirect.py index 01f7069..9d5b9de 100644 --- a/redirect.py +++ b/redirect.py @@ -60,7 +60,10 @@ def redir(to): if not util.is_web(to): error(f'Expected fully qualified URL; got {to}') - to_domain = urllib.parse.urlparse(to).hostname + try: + to_domain = urllib.parse.urlparse(to).hostname + except ValueError as e: + error(f'Invalid URL {to} : {e}') # check conneg accept_as2 = False diff --git a/tests/test_redirect.py b/tests/test_redirect.py index ea8b03e..38c0baa 100644 --- a/tests/test_redirect.py +++ b/tests/test_redirect.py @@ -65,6 +65,10 @@ class RedirectTest(testutil.TestCase): got = self.client.get(r'/r/https://v2.jacky.wtf\"') self.assertEqual(404, got.status_code) + def test_redirect_url_parse_value_error(self): + got = self.client.get(r'/r/https:/[DOMAIN]/') + self.assertEqual(400, got.status_code) + def test_as2(self): self._test_as2(as2.CONTENT_TYPE)