fix: support authenticated web deployments under a reverse proxy path

This commit is contained in:
AttilaNet
2026-11-10 20:01:44 +08:00
parent 0230a933b0
commit 99aff423d8
12 changed files with 529 additions and 31 deletions
@@ -0,0 +1,161 @@
"""Offline Edge regressions for externally prefixed deployments.
Run: python -B -m unittest ranch_web.tests.browser.test_base_path_browser -v
Only disposable demo accounts and a synthetic administrator password are used.
"""
from __future__ import annotations
from pathlib import Path
import socket
import tempfile
import threading
import time
import unittest
from unittest.mock import patch
from urllib.parse import urlsplit
from .test_workbench_browser import AVAILABLE, EDGE, OfflineUpdates
if AVAILABLE:
from playwright.sync_api import expect
@unittest.skipUnless(AVAILABLE, "Install Python Playwright and set RANCH_WEB_BROWSER to a system Chromium browser")
class BasePathBrowserTests(unittest.TestCase):
password = "offline-prefix-password-not-a-real-credential"
@classmethod
def setUpClass(cls):
from playwright.sync_api import sync_playwright
cls.playwright = sync_playwright().start()
cls.addClassCleanup(cls.playwright.stop)
cls.browser = cls.playwright.chromium.launch(executable_path=str(EDGE), headless=True)
cls.addClassCleanup(cls.browser.close)
def check_deployment(self, prefix, *, strip_prefix=False):
import uvicorn
from ranch_web.app import create_app
from ranch_web.config import WebConfig
from ranch_web.service import WebManager
network = patch("requests.sessions.Session.request", side_effect=AssertionError("Live HTTP is forbidden"))
network.start()
self.addCleanup(network.stop)
temp = tempfile.TemporaryDirectory(prefix="ranch-web-prefix-browser-")
self.addCleanup(temp.cleanup)
manager = WebManager(temp.name, start_scheduler=False)
self.addCleanup(manager.close)
account = manager.create_account("Offline prefix demo", demo=True)["id"]
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
origin = f"http://127.0.0.1:{port}"
base = origin + prefix
app = create_app(manager=manager, config=WebConfig(
port=port, data_dir=Path(temp.name), password=self.password, base_path=prefix,
), updates=OfflineUpdates())
async def proxy(scope, receive, send):
# Model a proxy that removes the external prefix before forwarding.
if strip_prefix and scope["type"] == "http" and scope["path"].startswith(prefix + "/"):
scope = {**scope, "path": scope["path"][len(prefix):],
"raw_path": scope["raw_path"][len(prefix):]}
await app(scope, receive, send)
server = uvicorn.Server(uvicorn.Config(proxy, host="127.0.0.1", port=port, log_level="error"))
thread = threading.Thread(target=server.run, daemon=True)
thread.start()
def stop_server():
server.should_exit = True
thread.join(timeout=10)
self.addCleanup(stop_server)
deadline = time.monotonic() + 15
while not server.started and time.monotonic() < deadline:
time.sleep(.02)
self.assertTrue(server.started, "Temporary prefixed browser server failed to start")
context = self.browser.new_context(viewport={"width": 1280, "height": 800})
self.addCleanup(context.close)
requests, escapes, errors = [], [], []
def offline_only(route):
if route.request.url.startswith(base + "/"):
route.continue_()
else:
escapes.append(route.request.url)
route.abort()
context.route("**/*", offline_only)
context.on("request", lambda request: requests.append(request))
page = context.new_page()
page.on("pageerror", lambda failure: errors.append(str(failure)))
page.on("console", lambda message: errors.append(message.text) if message.type == "error"
and ("security policy" in message.text.lower() or "csp" in message.text.lower()) else None)
def assert_login():
expect(page).to_have_url(base + "/login")
expect(page.locator("#auth-form")).to_be_visible()
expect(page.locator("#admin-password")).to_be_enabled()
expect(page.locator("#auth-form")).to_have_attribute("action", prefix + "/api/auth/login")
self.assertEqual(page.locator("#app, nav, #account-tabs, #page-tabs, #page-host").count(), 0)
self.assertEqual(page.locator("script").count(), 1)
expect(page.locator("script")).to_have_attribute("src", prefix + "/static/login.js")
expect(page.locator('link[rel="stylesheet"]')).to_have_attribute("href", prefix + "/static/login.css")
expect(page.locator('meta[name="ranch-web-base-path"]')).to_have_attribute("content", prefix)
response = page.goto(base + "/", wait_until="networkidle")
assert_login()
self.assertEqual(response.headers["cache-control"], "no-store")
self.assertIn("script-src 'self'", response.headers["content-security-policy"])
self.assertEqual([urlsplit(request.url).path for request in requests if request.resource_type == "script"],
[prefix + "/static/login.js"])
self.assertIn(base + "/api/auth/status", [request.url for request in requests])
page.locator("#admin-password").fill(self.password)
with page.expect_response(base + "/api/auth/login") as login:
page.locator('#auth-form button[type="submit"]').click()
self.assertEqual(login.value.status, 200)
self.assertEqual(login.value.request.post_data_json, {"password": self.password})
expect(page).to_have_url(base + "/")
expect(page.locator("#app")).to_be_visible()
expect(page.locator("#status-text")).to_have_text("工作台已连接")
expect(page.locator("#save-status")).to_have_text("策略已自动保存")
expect(page.locator('link[rel="stylesheet"]')).to_have_attribute("href", prefix + "/static/styles.css")
self.assertEqual(context.cookies()[0]["path"], prefix or "/")
self.assertEqual(page.evaluate("localStorage.length + sessionStorage.length"), 0)
for key in "login incubator bag ranch daily hatching mainline battles gifts commissions exploration maps statistics plans yields".split():
page.locator(f'.page-tab[data-page="{key}"]').click()
panel = page.locator(f'.page-panel[data-page="{key}"]:not([hidden])')
expect(panel.locator('.page-loading[data-loading="false"]')).to_have_count(1)
self.assertEqual(panel.locator(".page-error:not([hidden])").count(), 0)
# The server redirects an authenticated login URL back to this workbench.
page.goto(base + "/login", wait_until="networkidle")
expect(page).to_have_url(base + "/")
expect(page.locator("#status-text")).to_have_text("工作台已连接")
cookies = context.cookies()
page.locator(".menu > summary").first.click()
with page.expect_response(base + "/api/auth/logout") as logout:
page.locator('[data-command="admin-logout"]').click()
self.assertEqual(logout.value.status, 200)
assert_login()
self.assertEqual(context.cookies(), [])
context.add_cookies(cookies)
page.goto(base + "/", wait_until="networkidle")
assert_login()
self.assertEqual(escapes, [], "Browser requests escaped the configured prefix or local server")
self.assertEqual(errors, [], "JavaScript or CSP errors")
self.assertTrue(any(request.url == base + "/static/paths.js" for request in requests))
def test_preserved_prefix(self):
self.check_deployment("/ranch")
def test_proxy_strips_nested_prefix(self):
self.check_deployment("/tools/ranch", strip_prefix=True)
if __name__ == "__main__":
unittest.main()
+243
View File
@@ -0,0 +1,243 @@
"""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
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):
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)
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", "/static/app.js"):
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])
self.assertIn(("src", BASE + "/static/app.js"), document.urls)
self.assertEqual(response.headers["cache-control"], "no-store")
self.assertEqual(client.get(BASE + "/static/app.js").status_code, 200)
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()