diff --git a/neomi.py b/neomi.py index 8d8fb9a..00c69ca 100644 --- a/neomi.py +++ b/neomi.py @@ -248,6 +248,8 @@ class RequestError(OneArgumentException): class EmptyRequestError(OneArgumentException): text = 'Got an empty request: %s' +class TLSClientHelloError(Exception): pass + class Protocol(enum.Enum): gopher, gopherplus, http = range(3) @@ -280,6 +282,10 @@ def get_request(sockreader, *, config): if len(request) >= config.request_max_size: raise RequestError('Request too long') + # We have enough data to recognise a TLS 1.1+ ClientHello + if len(request) >= 3 and request[:3] == b'\x16\x03\x01': + raise TLSClientHelloError('Got a TLS ClientHello') + # We have enough data to recognise a HTTP request if protocol is None and len(request) >= 5: # Does it look like a HTTP GET request? @@ -354,7 +360,11 @@ def get_request(sockreader, *, config): # Found the end of the path path_end = index - path = request[:path_end].decode('utf-8') + try: + path = request[:path_end].decode('utf-8') + except UnicodeDecodeError as err: + print(request)#debg + raise err # If another field was present, check to see if it marks a Gopher+ request if chr(request[index]) == '\t':