From bb9c5735dbb554a92f24b1fb8b941d87e8e788b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= jaakko.keranen@iki.fi
Date: Sun, 11 Dec 2022 11:01:42 +0200
Subject: [PATCH 1/1] Report Unicode decode error to client
README.md | 1 +
gmcapsule/gemini.py | 36 ++++++++++++++++++++----------------
2 files changed, 21 insertions(+), 16 deletions(-)
diff --git a/README.md b/README.md
index a1af4b9..80b3f0a 100644
--- a/README.md
+++ b/README.md
@@ -48,6 +48,7 @@ The log can be viewed via journalctl (or syslog):
+* Respond with an error code to malformed UTF-8 in the request.
diff --git a/gmcapsule/gemini.py b/gmcapsule/gemini.py
index b086ec6..34ac7f6 100644
--- a/gmcapsule/gemini.py
+++ b/gmcapsule/gemini.py
@@ -376,22 +376,26 @@ class Worker(threading.Thread):
req_mime = None
incoming = safe_recv(stream, MAX_RECV)
while len(data) < MAX_RECV:
data += incoming
crlf_pos = data.find(b'\r\n')
if crlf_pos >= 0:
request = data[:crlf_pos].decode('utf-8')
data = data[crlf_pos + 2:]
break
elif len(data) > MAX_LEN:
# At this point we should have received the line terminator.
self.log(from_addr, 'sent a malformed request')
report_error(stream, 59, "Request exceeds maximum length")
return
incoming = safe_recv(stream, MAX_RECV - len(data))
if len(incoming) <= 0:
break
try:
while len(data) < MAX_RECV:
data += incoming
crlf_pos = data.find(b'\r\n')
if crlf_pos >= 0:
request = data[:crlf_pos].decode('utf-8')
data = data[crlf_pos + 2:]
break
elif len(data) > MAX_LEN:
# At this point we should have received the line terminator.
self.log(from_addr, 'sent a malformed request')
report_error(stream, 59, "Request exceeds maximum length")
return
incoming = safe_recv(stream, MAX_RECV - len(data))
if len(incoming) <= 0:
break
except UnicodeDecodeError:
report_error(stream, 59, "Request contains malformed UTF-8")
return
if not request or len(data) > MAX_RECV:
report_error(stream, 59, "Invalid request")
--
2.25.1
text/plain
This content has been proxied by September (ba2dc).