From 057685f304632bfe7f1b7a77a8255eb82db6a063 Mon Sep 17 00:00:00 2001 From: moson Date: Sat, 17 Feb 2024 14:12:09 +0100 Subject: [PATCH] fix: Fix package info for 404 errors We try to find packages when a user enters a URL like /somepkg or accidentally opens /somepkg.git in the browser. However, it currently also does this for URL's like /pkgbase/doesnotexist and falsely interprets "pkgbase" part as a package or pkgbase name. This in combination with a pkgbase that is named "pkgbase" generates some misleading 404 message for URL's like /pkgbase/doesnotexist. That being said, we should probably add pkgbase to the blacklist check as well (we do this for pkgname already) and add things like "pkgbase" to the blacklist -> Will be picked up in another commit. Signed-off-by: moson --- aurweb/asgi.py | 2 +- test/test_html.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/aurweb/asgi.py b/aurweb/asgi.py index dfe2d32d..3f60dabf 100644 --- a/aurweb/asgi.py +++ b/aurweb/asgi.py @@ -212,7 +212,7 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> Respon if exc.status_code == http.HTTPStatus.NOT_FOUND: tokens = request.url.path.split("/") matches = re.match("^([a-z0-9][a-z0-9.+_-]*?)(\\.git)?$", tokens[1]) - if matches: + if matches and len(tokens) == 2: try: pkgbase = get_pkg_or_base(matches.group(1)) context = pkgbaseutil.make_context(request, pkgbase) diff --git a/test/test_html.py b/test/test_html.py index ad35154a..9129de00 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -199,7 +199,7 @@ def test_404_with_valid_pkgbase(client: TestClient, pkgbase: PackageBase): assert "To clone the Git repository" in body -def test_404(client: TestClient): +def test_404(client: TestClient, user): """Test HTTPException with status_code == 404 without a valid pkgbase.""" with client as request: response = request.get("/nonexistentroute") @@ -210,6 +210,20 @@ def test_404(client: TestClient): # No `pkgbase` is provided here; we don't see the extra info. assert "To clone the Git repository" not in body + # Create a pkgbase named "pkgbase" + # Should NOT return extra info for "pkgbase" + with db.begin(): + db.create(PackageBase, Name="pkgbase", Maintainer=user) + + with client as request: + response = request.get("/pkgbase/doesnotexist") + assert response.status_code == int(HTTPStatus.NOT_FOUND) + + body = response.text + assert "404 - Page Not Found" in body + # No `pkgbase` is provided here; we don't see the extra info. + assert "To clone the Git repository" not in body + def test_503(client: TestClient): """Test HTTPException with status_code == 503 (Service Unavailable)."""