From 178b5ccde372feae2171cdaa6d480cef0e63fb38 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 8 Jan 2024 07:53:09 -0800 Subject: [PATCH] /convert: handle missing protocols fixes https://console.cloud.google.com/errors/detail/CLHg5u_DuYGWaw;time=P30D?project=bridgy-federated --- convert.py | 10 ++++++++-- tests/test_convert.py | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/convert.py b/convert.py index 05bb52e..4fed879 100644 --- a/convert.py +++ b/convert.py @@ -35,8 +35,10 @@ def convert(dest, _, src=None): :func:`convert_source_path_redirect` """ if src: + src_cls = PROTOCOLS.get(src) + if not src_cls: + error(f'No protocol found for {src}', status=404) logger.info(f'Overriding any domain protocol with {src}') - src_cls = PROTOCOLS[src] else: src_cls = Protocol.for_request(fed=Protocol) if not src_cls: @@ -108,4 +110,8 @@ def convert_source_path_redirect(src, dest, _): request.url = request.url.replace(f'/{src}/', '/') return convert(dest, None, src) - return redirect(subdomain_wrap(PROTOCOLS[src], new_path), code=301) + proto = PROTOCOLS.get(src) + if not proto: + error(f'No protocol found for {src}', status=404) + + return redirect(subdomain_wrap(proto, new_path), code=301) diff --git a/tests/test_convert.py b/tests/test_convert.py index be5e347..dc2bc48 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -330,3 +330,10 @@ A ☕ reply self.assert_multiline_equals(HTML, resp.get_data(as_text=True), ignore_blanks=True) + def test_missing_protocols(self): + resp = self.client.get('/convert/https:/user.com/post') + self.assertEqual(404, resp.status_code) + + resp = self.client.get('/convert/https:/user.com/post', + base_url='https://fed.brid.gy/') + self.assertEqual(404, resp.status_code)