36 lines
1.7 KiB
Python
36 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only checks against a public HTTPS relay. Does not create rooms or change settings."""
|
|
import argparse
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("origin", help="HTTPS origin, e.g. https://relay.example.com")
|
|
args = parser.parse_args()
|
|
parsed = urllib.parse.urlparse(args.origin)
|
|
if parsed.scheme != "https" or parsed.path not in ("", "/") or not parsed.hostname or parsed.username or parsed.query or parsed.fragment:
|
|
parser.error("Supply a plain HTTPS origin, without credentials, path, or query.")
|
|
origin = args.origin.rstrip("/")
|
|
|
|
|
|
def request(path, headers=None):
|
|
req = urllib.request.Request(origin + path, headers=headers or {})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=15) as response:
|
|
return response.status, response.headers, response.read()
|
|
except urllib.error.HTTPError as error:
|
|
return error.code, error.headers, error.read()
|
|
|
|
|
|
status, headers, body = request("/health", {"X-Real-IP": "spoofed.invalid", "X-Forwarded-Proto": "http"})
|
|
assert status == 200, ("health/proxy header overwrite", status)
|
|
assert headers.get("Strict-Transport-Security"), "Missing HSTS"
|
|
assert headers.get("X-Content-Type-Options") == "nosniff", "Missing nosniff"
|
|
for path in ("/", "/admin", "/admin/", "/admin/app.js", "/admin/api/settings", "/%61dmin"):
|
|
code, _, _ = request(path)
|
|
assert code in (400, 403, 404), ("public admin route exposed", path, code)
|
|
code, _, _ = request("/health", {"Origin": "https://untrusted.invalid"})
|
|
assert code == 403, ("cross-origin request accepted", code)
|
|
print("PASS: verified HTTPS, HSTS, proxy header replacement, public admin isolation, cross-origin denial")
|