|
| 1 | +"""Security tests for the OAuth local callback server. |
| 2 | +
|
| 3 | +Covers GHSA-32xc-7x5c-8vmf: the `/set_token` and `/log` endpoints must reject |
| 4 | +unauthenticated POSTs (no / wrong OAuth `state`), and the server must bind to |
| 5 | +loopback only rather than all interfaces. |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import os |
| 10 | +import threading |
| 11 | +import urllib.error |
| 12 | +import urllib.request |
| 13 | + |
| 14 | +from uipath._cli._auth._auth_server import HTTPServer |
| 15 | + |
| 16 | +STATE = "LEGITIMATE_OAUTH_STATE_ABCDE12345" |
| 17 | +CODE_VERIFIER = "LEGITIMATE_PKCE_CODE_VERIFIER" |
| 18 | +DOMAIN = "cloud.uipath.com" |
| 19 | + |
| 20 | +ATTACKER_PAYLOAD = { |
| 21 | + "access_token": "attacker-token", |
| 22 | + "refresh_token": "attacker-refresh", |
| 23 | + "expires_in": 3600, |
| 24 | + "token_type": "Bearer", |
| 25 | + "scope": "offline_access", |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +def _request(port, path, data, headers=None, method="POST"): |
| 30 | + req = urllib.request.Request( |
| 31 | + f"http://127.0.0.1:{port}{path}", |
| 32 | + data=data, |
| 33 | + headers=headers or {}, |
| 34 | + method=method, |
| 35 | + ) |
| 36 | + try: |
| 37 | + with urllib.request.urlopen(req, timeout=5) as resp: |
| 38 | + return resp.status, resp.read().decode("utf-8") |
| 39 | + except urllib.error.HTTPError as exc: |
| 40 | + return exc.code, exc.read().decode("utf-8") |
| 41 | + |
| 42 | + |
| 43 | +def _post(port, path, body, headers=None): |
| 44 | + return _request( |
| 45 | + port, |
| 46 | + path, |
| 47 | + json.dumps(body).encode("utf-8"), |
| 48 | + {"Content-Type": "application/json", **(headers or {})}, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +async def test_endpoints_reject_unauthenticated_posts(tmp_path, monkeypatch): |
| 53 | + """Only requests carrying the matching OAuth state are accepted. |
| 54 | +
|
| 55 | + Exercises both /set_token and /log with missing, wrong, and valid state. |
| 56 | + """ |
| 57 | + monkeypatch.chdir(tmp_path) |
| 58 | + |
| 59 | + # Binding happens in create_server; the listen socket is up before the |
| 60 | + # handler thread starts, so connections queue and no readiness sleep is |
| 61 | + # needed. redirect_uri/client_id are required by the GET (index.html) path. |
| 62 | + server = HTTPServer( |
| 63 | + port=0, redirect_uri="http://localhost/callback", client_id="test-client" |
| 64 | + ) |
| 65 | + httpd = server.create_server(STATE, CODE_VERIFIER, DOMAIN) |
| 66 | + port = httpd.server_address[1] |
| 67 | + |
| 68 | + results = {} |
| 69 | + |
| 70 | + def client(): |
| 71 | + # DNS rebinding |
| 72 | + results["rebind_get"] = _request( |
| 73 | + port, "/", None, {"Host": "not-localhost.com"}, method="GET" |
| 74 | + ) |
| 75 | + results["rebind_post"] = _post( |
| 76 | + port, |
| 77 | + "/set_token", |
| 78 | + ATTACKER_PAYLOAD, |
| 79 | + {"X-Auth-State": STATE, "Host": "evil.com"}, |
| 80 | + ) |
| 81 | + # GET serves index.html with the OAuth params substituted in. |
| 82 | + results["get"] = _request(port, "/anything", None, method="GET") |
| 83 | + # /set_token: missing and wrong state are rejected. |
| 84 | + results["set_missing"] = _post(port, "/set_token", ATTACKER_PAYLOAD) |
| 85 | + results["set_wrong"] = _post( |
| 86 | + port, "/set_token", ATTACKER_PAYLOAD, {"X-Auth-State": "not-the-state"} |
| 87 | + ) |
| 88 | + # Valid state but a non-JSON body -> graceful 400, not 500. |
| 89 | + results["set_malformed"] = _request( |
| 90 | + port, "/set_token", b"not json", {"X-Auth-State": STATE} |
| 91 | + ) |
| 92 | + # Unknown path -> 404. |
| 93 | + results["unknown"] = _post(port, "/nope", {"x": 1}, {"X-Auth-State": STATE}) |
| 94 | + # /log: missing and valid state. |
| 95 | + results["log_missing"] = _post(port, "/log", {"msg": "x"}) |
| 96 | + results["log_valid"] = _post( |
| 97 | + port, "/log", {"msg": "x"}, {"X-Auth-State": STATE} |
| 98 | + ) |
| 99 | + # Valid /set_token last, to capture the token and unblock start(). |
| 100 | + results["set_valid"] = _post( |
| 101 | + port, "/set_token", {"access_token": "real"}, {"X-Auth-State": STATE} |
| 102 | + ) |
| 103 | + |
| 104 | + t = threading.Thread(target=client, daemon=True) |
| 105 | + t.start() |
| 106 | + token_data = await server.start(STATE, CODE_VERIFIER, DOMAIN) |
| 107 | + t.join(timeout=5) |
| 108 | + |
| 109 | + # DNS rebinding: forged Host is rejected on both GET and POST. |
| 110 | + assert results["rebind_get"][0] == 403 |
| 111 | + assert results["rebind_post"][0] == 403 |
| 112 | + |
| 113 | + # GET returns the page with the real state injected, placeholder gone. |
| 114 | + assert results["get"][0] == 200 |
| 115 | + assert STATE in results["get"][1] |
| 116 | + assert "__PY_REPLACE_EXPECTED_STATE__" not in results["get"][1] |
| 117 | + |
| 118 | + assert results["set_missing"][0] == 403 |
| 119 | + assert results["set_wrong"][0] == 403 |
| 120 | + assert results["set_malformed"][0] == 400 |
| 121 | + assert results["unknown"][0] == 404 |
| 122 | + assert results["log_missing"][0] == 403 |
| 123 | + assert results["log_valid"][0] == 200 |
| 124 | + assert results["set_valid"][0] == 200 |
| 125 | + |
| 126 | + # Only the valid, state-bearing request was accepted. |
| 127 | + assert token_data == {"access_token": "real"} |
| 128 | + # The state-protected /log write happened for the valid request only. |
| 129 | + assert os.path.exists(tmp_path / ".uipath" / ".error_log") |
| 130 | + |
| 131 | + |
| 132 | +def test_server_binds_to_loopback_only(): |
| 133 | + server = HTTPServer(port=0) |
| 134 | + httpd = server.create_server(STATE, CODE_VERIFIER, DOMAIN) |
| 135 | + try: |
| 136 | + assert httpd.server_address[0] == "127.0.0.1" |
| 137 | + finally: |
| 138 | + httpd.server_close() |
0 commit comments