Repository: matrix-org/synapse

Issue:
http requests for uris with non-ascii characters cause CRITICAL errors
```
2019-11-22 13:15:59,276 - twisted - 172 - CRITICAL - - 
Capture point (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/opt/synapse/synapse/synapse/app/homeserver.py", line 659, in <module>
    main()
  File "/opt/synapse/synapse/synapse/app/homeserver.py", line 655, in main
    run(hs)
  File "/opt/synapse/synapse/synapse/app/homeserver.py", line 646, in run
    logger=logger,
  File "/opt/synapse/synapse/synapse/app/_base.py", line 139, in start_reactor
    run()
  File "/opt/synapse/synapse/synapse/app/_base.py", line 114, in run
    run_command()
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/base.py", line 1267, in run
    self.mainLoop()
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/base.py", line 1279, in mainLoop
    self.doIteration(t)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/epollreactor.py", line 235, in doPoll
    log.callWithLogger(selectable, _drdw, selectable, fd, event)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/log.py", line 103, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/log.py", line 86, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/context.py", line 122, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/python/context.py", line 85, in callWithContext
    return func(*args,**kw)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
    why = selectable.doRead()
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/tcp.py", line 243, in doRead
    return self._dataReceived(data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/internet/tcp.py", line 249, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 2912, in dataReceived
    return self._channel.dataReceived(data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 2211, in dataReceived
    return basic.LineReceiver.dataReceived(self, data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/protocols/basic.py", line 579, in dataReceived
    why = self.rawDataReceived(data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 2218, in rawDataReceived
    self._transferDecoder.dataReceived(data)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 1699, in dataReceived
    finishCallback(data[contentLength:])
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 2115, in _finishRequestBody
    self.allContentReceived()
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 2190, in allContentReceived
    req.requestReceived(command, path, version)
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/http.py", line 917, in requestReceived
    self.process()
Traceback (most recent call last):
  File "/opt/synapse/env3/lib/python3.5/site-packages/twisted/web/server.py", line 199, in process
    self.render(resrc)
  File "/opt/synapse/synapse/synapse/http/site.py", line 130, in render
    self._started_processing(servlet_name)
  File "/opt/synapse/synapse/synapse/http/site.py", line 233, in _started_processing
    self.get_redacted_uri(),
  File "/opt/synapse/synapse/synapse/http/site.py", line 91, in get_redacted_uri
    uri = self.uri.decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 25: ordinal not in range(128)
```


Discussion:

Resolving patch:
diff --git a/synapse/http/site.py b/synapse/http/site.py
--- a/synapse/http/site.py
+++ b/synapse/http/site.py
@@ -88,7 +88,7 @@ def get_request_id(self):
     def get_redacted_uri(self):
         uri = self.uri
         if isinstance(uri, bytes):
-            uri = self.uri.decode("ascii")
+            uri = self.uri.decode("ascii", errors="replace")
         return redact_uri(uri)
 
     def get_method(self):


Test patch: