"""Offline regressions for preserved and stripped reverse-proxy prefixes.""" from html.parser import HTMLParser from http.cookies import SimpleCookie from pathlib import Path import tempfile import unittest from unittest.mock import patch from fastapi.testclient import TestClient from ranch_web.app import create_app from ranch_web.config import COOKIE_NAME, WebConfig from ranch_web.tests.test_api import FakeManager, FakeUpdates, FrontendAssets BASE = "/myrzx_bot" ORIGIN = "https://ranch.example" class _Proxy: """Keep the browser URL/cookies external while changing the upstream path.""" def __init__(self, app, strip): self.app, self.strip = app, strip async def __call__(self, scope, receive, send): if scope["type"] == "http" and self.strip: path = scope["path"] if path == BASE or path.startswith(BASE + "/"): scope = {**scope, "path": path[len(BASE):] or "/", "raw_path": scope["raw_path"][len(BASE):] or b"/"} await self.app(scope, receive, send) class _Document(HTMLParser): def __init__(self, content): super().__init__() self.prefixes, self.urls = [], [] self.feed(content) def handle_starttag(self, tag, attrs): attrs = dict(attrs) if tag == "meta" and attrs.get("name") == "ranch-web-base-path": self.prefixes.append(attrs.get("content")) for name in ("src", "href", "action"): if name in attrs and attrs[name].startswith("/"): self.urls.append((name, attrs[name])) class BasePathTests(unittest.TestCase): def setUp(self): temporary = self.enterContext(tempfile.TemporaryDirectory(prefix="ranch-base-path-")) self.root = Path(temporary) self.manager, self.updates = FakeManager(), FakeUpdates() self.enterContext(patch("requests.sessions.Session.request", side_effect=AssertionError("No network"))) self.enterContext(patch("socket.create_connection", side_effect=AssertionError("No network"))) def client(self, strip=False, base_path=BASE, base_url=ORIGIN): settings = WebConfig.from_env({"RANCH_WEB_BASE_PATH": base_path}, data_dir=self.root, password="offline", secure_cookie=True, allowed_hosts=("ranch.example",)) app = create_app(manager=self.manager, updates=self.updates, config=settings) return self.enterContext(TestClient(_Proxy(app, strip), base_url=base_url, client=("192.0.2.1", 50000))) def login(self, client, prefix=BASE): response = client.post(prefix + "/api/auth/login", json={"password": "offline"}, headers={"Origin": ORIGIN}) self.assertEqual(response.status_code, 200, response.text) return response def cookies(self, response): result = {} for header in response.headers.get_list("set-cookie"): parsed = SimpleCookie() parsed.load(header) cookie = parsed[COOKIE_NAME] result[cookie["path"]] = cookie return result def test_base_path_environment_and_validation(self): self.assertEqual(WebConfig.from_env({"RANCH_WEB_BASE_PATH": BASE}).base_path, BASE) self.assertEqual(WebConfig.from_env({}).base_path, "") for value in ("/", "myrzx_bot", BASE + "/", "//evil", "/a/../b", "/a?b", "/a%2fb"): with self.subTest(value=value), self.assertRaises(ValueError): WebConfig.from_env({"RANCH_WEB_BASE_PATH": value}) def test_exact_mount_redirects_to_slash_preserving_query(self): client = self.client() for suffix in ("", "?next=%2Fapi%2Faccounts"): response = client.get(BASE + suffix, follow_redirects=False) self.assertEqual(response.status_code, 307) self.assertEqual(response.headers["location"], BASE + "/" + suffix) response = client.get(BASE + "/", follow_redirects=False) self.assertEqual(response.status_code, 303) self.assertEqual(response.headers["location"], BASE + "/login") def test_login_html_assets_and_authenticated_workbench(self): entry = Path(__file__).resolve().parents[1] / "static" / "index.html" built_assets = FrontendAssets(entry.read_text(encoding="utf-8")) self.assertTrue(built_assets.scripts) for strip in (False, True): with self.subTest(strip=strip): client = self.client(strip) for path in ("/login", "/static/login.html"): response = client.get(BASE + path) self.assertEqual(response.status_code, 200) document = _Document(response.text) self.assertEqual(document.prefixes, [BASE]) self.assertIn(("action", BASE + "/api/auth/login"), document.urls) self.assertIn(("src", BASE + "/static/login.js"), document.urls) self.assertEqual(FrontendAssets(response.text).scripts, [(BASE + "/static/login.js", "module")]) for name, url in document.urls: self.assertTrue(url.startswith(BASE + "/"), url) self.assertNotIn(BASE + BASE, url) if name != "action": self.assertEqual(client.get(url).status_code, 200, url) for path in ("/", "/static/index.html", *(url for url, _ in built_assets.scripts)): response = client.get(BASE + path, follow_redirects=False) self.assertEqual(response.status_code, 303) self.assertEqual(response.headers["location"], BASE + "/login") self.login(client) for path in ("/login", "/static/login.html"): response = client.get(BASE + path, follow_redirects=False) self.assertEqual(response.status_code, 303) self.assertEqual(response.headers["location"], BASE + "/") for path in ("/", "/static/index.html"): response = client.get(BASE + path) self.assertEqual(response.status_code, 200) document = _Document(response.text) self.assertEqual(document.prefixes, [BASE]) assets = FrontendAssets(response.text) self.assertEqual(assets.scripts, [(BASE + url, kind) for url, kind in built_assets.scripts]) self.assertEqual(assets.urls, [BASE + url for url in built_assets.urls]) self.assertEqual(response.headers["cache-control"], "no-store") for url in assets.urls: self.assertTrue(url.startswith(BASE + "/static/"), url) self.assertNotIn(BASE + BASE, url) asset = client.get(url) self.assertEqual(asset.status_code, 200, url) if url not in assets.stylesheets: self.assertEqual(asset.headers["cache-control"], "no-store") client.cookies.clear() for url, _ in built_assets.scripts: response = client.get(BASE + url, follow_redirects=False) self.assertEqual(response.status_code, 303) self.assertEqual(response.headers["location"], BASE + "/login") self.assertEqual(self.manager.calls, []) def test_secure_cookie_scope_logout_and_revoked_session(self): for strip in (False, True): with self.subTest(strip=strip): client = self.client(strip) cookies = self.cookies(self.login(client)) cookie = cookies[BASE] self.assertTrue(cookie["secure"]) self.assertTrue(cookie["httponly"]) self.assertEqual(cookie["samesite"], "strict") self.assertFalse(cookie["domain"]) for path in ("/", "/api", BASE + "/api"): self.assertEqual(cookies[path]["max-age"], "0") token = client.cookies.get(COOKIE_NAME) self.assertEqual(client.get(BASE + "/api/accounts").status_code, 200) for url in (ORIGIN + "/api/accounts", ORIGIN + BASE + "x/api/accounts", "http://ranch.example" + BASE + "/api/accounts"): self.assertNotIn("cookie", client.build_request("GET", url).headers) response = client.post(BASE + "/api/auth/logout", headers={"Origin": ORIGIN}) self.assertEqual(response.status_code, 200) self.assertEqual(set(self.cookies(response)), {BASE, "/", "/api", BASE + "/api"}) self.assertTrue(all(c["max-age"] == "0" for c in self.cookies(response).values())) self.assertEqual(list(client.cookies.jar), []) response = client.get(BASE + "/api/accounts", headers={"Cookie": f"{COOKIE_NAME}={token}"}) self.assertEqual(response.status_code, 401) def test_auth_and_csrf_apply_in_both_proxy_modes(self): for strip in (False, True): with self.subTest(strip=strip): client = self.client(strip) for path in ("/api/accounts", "/api/accounts/", "/api/system/updates"): response = client.get(BASE + path) self.assertEqual(response.status_code, 401) self.assertEqual(response.json()["error"]["code"], "authentication_required") for path, payload in (("/api/auth/login", {"password": "offline"}), ("/api/auth/logout", {}), ("/api/accounts", {"demo": True})): response = client.post(BASE + path, json=payload, headers={"Origin": "https://attacker.example"}) self.assertEqual(response.status_code, 403) self.assertEqual(response.json()["error"]["code"], "invalid_origin") self.login(client) response = client.post(BASE + "/api/auth/logout", headers={"Sec-Fetch-Site": "cross-site"}) self.assertEqual(response.status_code, 403) self.assertTrue(client.get(BASE + "/api/auth/status").json()["authenticated"]) response = client.post(BASE + "/api/accounts", json={"demo": True}, headers={"Origin": ORIGIN}) self.assertEqual(response.status_code, 201) response = client.get(BASE + "/api/accounts/", follow_redirects=False) self.assertEqual(response.status_code, 307) self.assertEqual(response.headers["location"], ORIGIN + BASE + "/api/accounts") def test_double_prefix_and_forged_forwarded_prefix_cannot_bypass_boundary(self): for strip in (False, True): for forged in (BASE, BASE + BASE, "/api"): with self.subTest(strip=strip, forged=forged): client = self.client(strip) headers = {"X-Forwarded-Prefix": forged} response = client.get(BASE + "/api/accounts", headers=headers) self.assertEqual(response.status_code, 401) response = client.get(BASE + "/", headers=headers, follow_redirects=False) self.assertEqual(response.headers["location"], BASE + "/login") for path in ("/api/accounts", "/static/index.html"): response = client.get(BASE + BASE + path, headers=headers, follow_redirects=False) # After proxy stripping this is indistinguishable from a # preserved path, so it must still hit the normal gate. expected = (401 if path.startswith("/api/") else 303) if strip else 404 self.assertEqual(response.status_code, expected) if expected == 401: self.assertEqual(response.json()["error"]["code"], "authentication_required") elif expected == 303: self.assertEqual(response.headers["location"], BASE + "/login") response = client.post(BASE + BASE + "/api/accounts", json={"demo": True}, headers=headers) self.assertEqual(response.status_code, 401 if strip else 404) response = client.post(BASE + BASE + "/api/accounts", json={}, headers={**headers, "Origin": "https://attacker.example"}) self.assertEqual(response.status_code, 403 if strip else 404) response = client.post(BASE + "/api/accounts", json={}, headers={**headers, "Origin": "https://attacker.example"}) self.assertEqual(response.status_code, 403) self.assertEqual(self.manager.calls, []) self.assertEqual(self.updates.calls, []) def test_localhost_health_remains_public_with_or_without_prefix(self): for strip in (False, True): with self.subTest(strip=strip): client = self.client(strip, base_url="http://localhost") for path in ("/api/health", BASE + "/api/health"): response = client.get(path) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["status"], "ok") self.assertEqual(response.headers["cache-control"], "no-store") self.assertNotIn("set-cookie", response.headers) self.assertEqual(self.manager.calls, []) self.assertEqual(self.updates.calls, []) def test_root_deployment_ignores_forwarded_prefix_and_keeps_root_cookie(self): client = self.client(base_path="") response = client.get("/", headers={"X-Forwarded-Prefix": BASE}, follow_redirects=False) self.assertEqual(response.headers["location"], "/login") response = client.get("/login") self.assertEqual(_Document(response.text).prefixes, [""]) self.assertIn(("src", "/static/login.js"), _Document(response.text).urls) self.assertEqual(client.get("/api/accounts").status_code, 401) cookies = self.cookies(self.login(client, prefix="")) self.assertEqual(set(cookies), {"/", "/api"}) self.assertNotEqual(cookies["/"]["max-age"], "0") self.assertEqual(client.get("/api/accounts").status_code, 200) self.assertEqual(client.get("/api/health").status_code, 200) if __name__ == "__main__": unittest.main()