From 783f08818a658b0ce9234265b234a2cee7af1797 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9lestin=20Matte?= <gitlab@cmatte.me>
Date: Tue, 31 Jan 2023 19:39:24 +0100
Subject: [PATCH] auth_receive: Indicate when PGAUTH_KEY is invalid instead of
 crashing

---
 web/pglister/auth.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/web/pglister/auth.py b/web/pglister/auth.py
index d7bd25c..0d12aa8 100644
--- a/web/pglister/auth.py
+++ b/web/pglister/auth.py
@@ -107,7 +107,10 @@ def auth_receive(request):
     decryptor = AES.new(base64.b64decode(settings.PGAUTH_KEY),
                         AES.MODE_CBC,
                         base64.b64decode(str(request.GET['i']), "-_"))
-    s = decryptor.decrypt(base64.b64decode(str(request.GET['d']), "-_")).rstrip(b' ').decode('utf8')
+    try:
+        s = decryptor.decrypt(base64.b64decode(str(request.GET['d']), "-_")).rstrip(b' ').decode('utf8')
+    except UnicodeDecodeError:
+        return HttpResponse("Invalid PGAUTH_KEY.", status=400)
 
     # Now un-urlencode it
     try:
-- 
2.39.0

