From 447d78567a4efc36cd25329f42aa77105ed59c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 1 Mar 2021 11:15:50 +0100 Subject: [PATCH] contrib: Add a mitmproxy script to block well-known lookups This is useful if a test server sends incorrect homeserver URLs in their well-known data, for example the Synapse instance Complement starts up does so. --- contrib/mitmproxy/well-known-block.py | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 contrib/mitmproxy/well-known-block.py diff --git a/contrib/mitmproxy/well-known-block.py b/contrib/mitmproxy/well-known-block.py new file mode 100644 index 00000000..c41f1652 --- /dev/null +++ b/contrib/mitmproxy/well-known-block.py @@ -0,0 +1,37 @@ +""" +A mitmproxy script that blocks and removes well known Matrix server +information. + +There are two ways a Matrix server can trigger the client to reconfigure the +homeserver URL: + + 1. By responding to a `./well-known/matrix/client` request with a new + homeserver URL. + + 2. By including a new homeserver URL inside the `/login` response. + +To run execute it with mitmproxy: + + >>> mitmproxy -s well-known-block.py` + +""" +import json + +from mitmproxy import http + + +def request(flow): + if flow.request.path == "/.well-known/matrix/client": + flow.response = http.HTTPResponse.make( + 404, # (optional) status code + b"Not found", # (optional) content + {"Content-Type": "text/html"} # (optional) headers + ) + + +def response(flow: http.HTTPFlow): + if flow.request.path == "/_matrix/client/r0/login": + if flow.response.status_code == 200: + body = json.loads(flow.response.content) + body.pop("well_known", None) + flow.response.text = json.dumps(body)