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
+1
View File
@@ -18,6 +18,7 @@ services:
RANCH_WEB_PASSWORD: ${RANCH_WEB_PASSWORD:?Set RANCH_WEB_PASSWORD in the private .env file} RANCH_WEB_PASSWORD: ${RANCH_WEB_PASSWORD:?Set RANCH_WEB_PASSWORD in the private .env file}
RANCH_WEB_ALLOWED_HOSTS: ${RANCH_WEB_ALLOWED_HOSTS:-localhost,127.0.0.1} RANCH_WEB_ALLOWED_HOSTS: ${RANCH_WEB_ALLOWED_HOSTS:-localhost,127.0.0.1}
RANCH_WEB_ALLOWED_ORIGINS: ${RANCH_WEB_ALLOWED_ORIGINS:-} RANCH_WEB_ALLOWED_ORIGINS: ${RANCH_WEB_ALLOWED_ORIGINS:-}
RANCH_WEB_BASE_PATH: ${RANCH_WEB_BASE_PATH:-}
RANCH_WEB_SECURE_COOKIE: ${RANCH_WEB_SECURE_COOKIE:-false} RANCH_WEB_SECURE_COOKIE: ${RANCH_WEB_SECURE_COOKIE:-false}
ports: ports:
- "${RANCH_WEB_BIND:-127.0.0.1}:${RANCH_WEB_PUBLISH_PORT:-8765}:8765" - "${RANCH_WEB_BIND:-127.0.0.1}:${RANCH_WEB_PUBLISH_PORT:-8765}:8765"
+10 -1
View File
@@ -50,11 +50,14 @@ sh start-web.sh --no-browser
| `RANCH_WEB_ALLOWED_HOSTS` | 明确的额外域名/IP,无通配符 | | `RANCH_WEB_ALLOWED_HOSTS` | 明确的额外域名/IP,无通配符 |
| `RANCH_WEB_ALLOWED_ORIGINS` | 明确的额外 http(s) 来源,逗号分隔 | | `RANCH_WEB_ALLOWED_ORIGINS` | 明确的额外 http(s) 来源,逗号分隔 |
| `RANCH_WEB_SECURE_COOKIE` | HTTPS 反代时设 `true` | | `RANCH_WEB_SECURE_COOKIE` | HTTPS 反代时设 `true` |
| `RANCH_WEB_BASE_PATH` | 默认空(根路径);子路径示例 `/ranch`,不带末尾 `/` |
| `RANCH_WEB_UPDATER_DIR` | 受控宿主机心跳目录;本地默认不设置 | | `RANCH_WEB_UPDATER_DIR` | 受控宿主机心跳目录;本地默认不设置 |
账号密钥、加密凭据、会话、配置和统计保留在服务端数据目录,不写入浏览器 localStorage。管理登录使用 HttpOnly / SameSite cookie,写请求校验 Origin。不要启动多个 Uvicorn worker 或多个服务实例共享同一数据根。 账号密钥、加密凭据、会话、配置和统计保留在服务端数据目录,不写入浏览器 localStorage。管理登录使用 HttpOnly / SameSite cookie,写请求校验 Origin。不要启动多个 Uvicorn worker 或多个服务实例共享同一数据根。
配置管理密码后,未登录访问 `/`、工作台 HTML 或脚本会由服务端转到独立 `/login` 页面;登录前不加载工作台。管理 cookie 使用 `Path=/`,登录和退出均清理旧版 `Path=/api` cookie;退出管理登录或会话失效后,页面隐藏工作台并返回登录页。登录页和工作台 HTML 使用 `Cache-Control: no-store`,本机未配置密码时仍保留 loopback 免密模式。 配置管理密码后,未登录访问 `/`、工作台 HTML 或脚本会由服务端转到独立 `/login` 页面;登录前不加载工作台。管理 cookie 使用配置的子路径(根路径部署为 `Path=/`),登录和退出均清理旧版 cookie;退出管理登录或会话失效后,页面隐藏工作台并返回登录页。登录页和工作台 HTML 使用 `Cache-Control: no-store`,本机未配置密码时仍保留 loopback 免密模式。
子路径反代示例:设置 `RANCH_WEB_BASE_PATH=/ranch`,从 `https://example.com/ranch/` 访问;登录页、静态资源、API 与退出跳转均使用此前缀。代理可保留此前缀或转发时移除一次此前缀。允许的 Host 使用 `example.com`,Origin 使用 `https://example.com`(不含路径);HTTPS 同时设置 `RANCH_WEB_SECURE_COOKIE=true`。容器健康检查继续使用 `/api/health`。省略 `RANCH_WEB_BASE_PATH` 时沿用根路径部署。
## Docker 与在线更新 ## Docker 与在线更新
@@ -78,4 +81,10 @@ python -B -m unittest ranch_web.tests.test_api ranch_web.tests.test_auth ranch_w
python -B -m unittest ranch_web.tests.browser.test_workbench_browser ranch_web.tests.browser.test_admin_login_browser -v python -B -m unittest ranch_web.tests.browser.test_workbench_browser ranch_web.tests.browser.test_admin_login_browser -v
``` ```
子路径浏览器回归(临时离线演示,覆盖保留与移除前缀的代理):
```console
python -B -m unittest ranch_web.tests.browser.test_base_path_browser -v
```
上述离线验证使用临时目录、fake transport / GitHub / Docker 或内置离线演示,不读取私有账户,不访问真实游戏 API。停止取消、配置并发保存、视图索引清理、子页字段保留和并发认证限流的已报问题均已修复并复核。Linux Docker 部署烟测见上文;离线测试及部署烟测均不等同于真实游戏 API 或生产升级/回滚验收。 上述离线验证使用临时目录、fake transport / GitHub / Docker 或内置离线演示,不读取私有账户,不访问真实游戏 API。停止取消、配置并发保存、视图索引清理、子页字段保留和并发认证限流的已报问题均已修复并复核。Linux Docker 部署烟测见上文;离线测试及部署烟测均不等同于真实游戏 API 或生产升级/回滚验收。
+68 -23
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
from collections import OrderedDict from collections import OrderedDict
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from dataclasses import replace from dataclasses import replace
from html import escape
import hashlib import hashlib
import inspect import inspect
import json import json
@@ -20,9 +21,10 @@ from urllib.parse import urlsplit
from fastapi import FastAPI, HTTPException, Path, Query, Request from fastapi import FastAPI, HTTPException, Path, Query, Request
from fastapi.exceptions import RequestValidationError from fastapi.exceptions import RequestValidationError
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr
from starlette._utils import get_route_path
from starlette.concurrency import run_in_threadpool from starlette.concurrency import run_in_threadpool
from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.responses import FileResponse, JSONResponse, RedirectResponse, Response from starlette.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, Response
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
from . import GAME_VERSION, __version__ from . import GAME_VERSION, __version__
@@ -191,6 +193,17 @@ class _Boundary:
if scope["type"] != "http": if scope["type"] != "http":
await self.app(scope, receive, send) await self.app(scope, receive, send)
return return
base_path = self.settings.base_path
path = scope["path"]
# Keep a canonical external path for Starlette's root_path handling. Both
# the boundary and router must strip exactly the same, single prefix.
preserved = base_path and (path == base_path or path.startswith(base_path + "/"))
scope = {**scope, "root_path": base_path, "app_root_path": base_path}
if base_path and (not preserved or path == "/api/health"):
scope["path"] = base_path + path
if "raw_path" in scope:
scope["raw_path"] = base_path.encode("ascii") + scope["raw_path"]
path = get_route_path(scope)
request = Request(scope) request = Request(scope)
async def deny(status, code, message): async def deny(status, code, message):
@@ -205,7 +218,7 @@ class _Boundary:
(b"x-frame-options", b"DENY"), (b"x-frame-options", b"DENY"),
(b"content-security-policy", b"default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"), (b"content-security-policy", b"default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"),
]) ])
if scope["path"].startswith("/api/"): if path.startswith("/api/"):
headers.append((b"cache-control", b"no-store")) headers.append((b"cache-control", b"no-store"))
message = {**message, "headers": headers} message = {**message, "headers": headers}
await send(message) await send(message)
@@ -224,7 +237,14 @@ class _Boundary:
await deny(400, "invalid_host", "请求主机不受信任") await deny(400, "invalid_host", "请求主机不受信任")
return return
is_api = scope["path"] == "/api" or scope["path"].startswith("/api/") if base_path and path == "":
target = base_path + "/"
if scope.get("query_string"):
target += "?" + scope["query_string"].decode("latin-1")
await RedirectResponse(target, status_code=307, headers={"Cache-Control": "no-store"})(scope, receive, secure_send)
return
is_api = path == "/api" or path.startswith("/api/")
if is_api and request.method in _WRITES: if is_api and request.method in _WRITES:
origins = request.headers.getlist("origin") origins = request.headers.getlist("origin")
fetch_site = request.headers.get("sec-fetch-site", "") fetch_site = request.headers.get("sec-fetch-site", "")
@@ -241,7 +261,7 @@ class _Boundary:
except ValueError: except ValueError:
await deny(403, "invalid_origin", "禁止跨站操作,请从当前工作台发起请求") await deny(403, "invalid_origin", "禁止跨站操作,请从当前工作台发起请求")
return return
if is_api and scope["path"] not in _PUBLIC_PATHS and not self.sessions.authenticated(request): if is_api and path not in _PUBLIC_PATHS and not self.sessions.authenticated(request):
status = 401 if self.settings.credential else 403 status = 401 if self.settings.credential else 403
await deny(status, "authentication_required", "请先登录管理工作台" if status == 401 else "本机免密模式只允许本机访问") await deny(status, "authentication_required", "请先登录管理工作台" if status == 401 else "本机免密模式只允许本机访问")
return return
@@ -252,7 +272,7 @@ class _Boundary:
await deny(414, "query_too_large", "查询条件过长") await deny(414, "query_too_large", "查询条件过长")
return return
body_limit = self.settings.max_body_bytes body_limit = self.settings.max_body_bytes
if request.method == "POST" and _YIELDS_IMPORT_PATH.fullmatch(scope["path"]): if request.method == "POST" and _YIELDS_IMPORT_PATH.fullmatch(path):
# An 8 MiB file expands to about 10.7 MiB in base64, plus JSON metadata. # An 8 MiB file expands to about 10.7 MiB in base64, plus JSON metadata.
body_limit = _YIELDS_IMPORT_BODY_BYTES body_limit = _YIELDS_IMPORT_BODY_BYTES
lengths = request.headers.getlist("content-length") lengths = request.headers.getlist("content-length")
@@ -340,6 +360,33 @@ def _no_file_paths(value: Any, depth: int = 0) -> None:
_no_file_paths(item, depth + 1) _no_file_paths(item, depth + 1)
def _page_response(path: FilePath, settings: WebConfig) -> HTMLResponse:
if not path.is_file():
raise HTTPException(503, "前端资源尚未准备完成")
content = path.read_text(encoding="utf-8")
base_path = escape(settings.base_path, quote=True)
metadata = f'<meta name="ranch-web-base-path" content="{base_path}">'
content, replaced = re.subn(
r'''<meta\b(?=[^>]*\bname\s*=\s*["']ranch-web-base-path["'])[^>]*>''',
lambda match: metadata, content, flags=re.IGNORECASE,
)
if not replaced:
content, inserted = re.subn(r"<head\b[^>]*>", lambda match: match[0] + "\n " + metadata,
content, count=1, flags=re.IGNORECASE)
if not inserted:
content = metadata + "\n" + content
if base_path:
def prefix_attribute(match):
value = match[3]
if value != base_path and not value.startswith(base_path + "/"):
value = base_path + value
return match[1] + match[2] + value + match[2]
content = re.sub(r'''(\b(?:src|href|action)\s*=\s*)(["'])(/(?!/)[^"']*)\2''',
prefix_attribute, content, flags=re.IGNORECASE)
return HTMLResponse(content, headers={"Cache-Control": "no-store"})
class _Assets(StaticFiles): class _Assets(StaticFiles):
def __init__(self, *, sessions: _Sessions, **kwargs): def __init__(self, *, sessions: _Sessions, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@@ -354,10 +401,12 @@ class _Assets(StaticFiles):
raise HTTPException(405) raise HTTPException(405)
authenticated = self.sessions.authenticated(Request(scope)) authenticated = self.sessions.authenticated(Request(scope))
if path == "login.html" and authenticated: if path == "login.html" and authenticated:
return RedirectResponse("/", status_code=303, headers={"Cache-Control": "no-store"}) return RedirectResponse(self.sessions.settings.base_path + "/", status_code=303, headers={"Cache-Control": "no-store"})
if path not in _PUBLIC_ASSETS and not authenticated: if path not in _PUBLIC_ASSETS and not authenticated:
return RedirectResponse("/login", status_code=303, headers={"Cache-Control": "no-store"}) return RedirectResponse(self.sessions.settings.base_path + "/login", status_code=303, headers={"Cache-Control": "no-store"})
response = await super().get_response(path, scope) response = await super().get_response(path, scope)
if path in {"index.html", "login.html"} and isinstance(response, FileResponse):
response = await run_in_threadpool(_page_response, FilePath(response.path), self.sessions.settings)
if path not in _PUBLIC_ASSETS or path == "login.html": if path not in _PUBLIC_ASSETS or path == "login.html":
response.headers["Cache-Control"] = "no-store" response.headers["Cache-Control"] = "no-store"
return response return response
@@ -419,7 +468,7 @@ def create_app(data_dir=None, manager=None, config=None, updates=None) -> FastAP
raise RuntimeError("Web 服务关闭未完成,请查看已脱敏日志") from None raise RuntimeError("Web 服务关闭未完成,请查看已脱敏日志") from None
app = FastAPI(title="牧羊人之心 Web 工作台", version=__version__, lifespan=lifespan, app = FastAPI(title="牧羊人之心 Web 工作台", version=__version__, lifespan=lifespan,
docs_url=None, redoc_url=None, openapi_url=None) docs_url=None, redoc_url=None, openapi_url=None, root_path=settings.base_path)
app.state.manager, app.state.updates, app.state.config = manager, updates, settings app.state.manager, app.state.updates, app.state.config = manager, updates, settings
app.add_middleware(_Boundary, settings=settings, sessions=sessions) app.add_middleware(_Boundary, settings=settings, sessions=sessions)
@@ -560,17 +609,19 @@ def create_app(data_dir=None, manager=None, config=None, updates=None) -> FastAP
token = sessions.login(request, body.password) token = sessions.login(request, body.password)
response = JSONResponse({"required": True, "authenticated": True, "access_mode": "password"}) response = JSONResponse({"required": True, "authenticated": True, "access_mode": "password"})
response.set_cookie(COOKIE_NAME, token, max_age=settings.session_ttl, httponly=True, response.set_cookie(COOKIE_NAME, token, max_age=settings.session_ttl, httponly=True,
secure=settings.secure_cookie, samesite="strict", path="/") secure=settings.secure_cookie, samesite="strict", path=settings.base_path or "/")
# Remove the narrower cookie left by older versions before it can shadow the new session. # Clear legacy paths without expiring the newly issued session cookie.
response.delete_cookie(COOKIE_NAME, path="/api", httponly=True, secure=settings.secure_cookie, samesite="strict") for path in dict.fromkeys(("/", "/api", settings.base_path + "/api")):
if path != (settings.base_path or "/"):
response.delete_cookie(COOKIE_NAME, path=path, httponly=True, secure=settings.secure_cookie, samesite="strict")
return response return response
@app.post("/api/auth/logout") @app.post("/api/auth/logout")
def admin_logout(request: Request): def admin_logout(request: Request):
sessions.logout(request) sessions.logout(request)
response = JSONResponse({"authenticated": False}) response = JSONResponse({"authenticated": False})
response.delete_cookie(COOKIE_NAME, path="/", httponly=True, secure=settings.secure_cookie, samesite="strict") for path in dict.fromkeys((settings.base_path or "/", "/", "/api", settings.base_path + "/api")):
response.delete_cookie(COOKIE_NAME, path="/api", httponly=True, secure=settings.secure_cookie, samesite="strict") response.delete_cookie(COOKIE_NAME, path=path, httponly=True, secure=settings.secure_cookie, samesite="strict")
return response return response
@app.get("/api/schema") @app.get("/api/schema")
@@ -680,20 +731,14 @@ def create_app(data_dir=None, manager=None, config=None, updates=None) -> FastAP
@app.get("/", include_in_schema=False) @app.get("/", include_in_schema=False)
def index(request: Request): def index(request: Request):
if not sessions.authenticated(request): if not sessions.authenticated(request):
return RedirectResponse("/login", status_code=303, headers={"Cache-Control": "no-store"}) return RedirectResponse(settings.base_path + "/login", status_code=303, headers={"Cache-Control": "no-store"})
index_path = static_dir / "index.html" return _page_response(static_dir / "index.html", settings)
if not index_path.is_file():
raise HTTPException(503, "前端资源尚未准备完成")
return FileResponse(index_path, media_type="text/html", headers={"Cache-Control": "no-store"})
@app.get("/login", include_in_schema=False) @app.get("/login", include_in_schema=False)
def login_page(request: Request): def login_page(request: Request):
if sessions.authenticated(request): if sessions.authenticated(request):
return RedirectResponse("/", status_code=303, headers={"Cache-Control": "no-store"}) return RedirectResponse(settings.base_path + "/", status_code=303, headers={"Cache-Control": "no-store"})
login_path = static_dir / "login.html" return _page_response(static_dir / "login.html", settings)
if not login_path.is_file():
raise HTTPException(503, "前端资源尚未准备完成")
return FileResponse(login_path, media_type="text/html", headers={"Cache-Control": "no-store"})
app.mount("/static", _Assets(directory=static_dir, sessions=sessions, check_dir=False, follow_symlink=False), name="static") app.mount("/static", _Assets(directory=static_dir, sessions=sessions, check_dir=False, follow_symlink=False), name="static")
return app return app
+10
View File
@@ -122,6 +122,13 @@ def _env_bool(value: str, name: str) -> bool:
raise ValueError(f"{name} 必须是布尔值") raise ValueError(f"{name} 必须是布尔值")
def normalize_base_path(value: str) -> str:
"""Allow only an empty prefix or slash-separated, plain URL segments."""
if not isinstance(value, str) or (value and not re.fullmatch(r"/[A-Za-z0-9_-]+(?:/[A-Za-z0-9_-]+)*", value)):
raise ValueError("RANCH_WEB_BASE_PATH must be empty or /segment[/segment...] using letters, digits, _ and -")
return value
@dataclass(frozen=True) @dataclass(frozen=True)
class WebConfig: class WebConfig:
host: str = DEFAULT_HOST host: str = DEFAULT_HOST
@@ -134,8 +141,10 @@ class WebConfig:
secure_cookie: bool = False secure_cookie: bool = False
session_ttl: int = 43_200 session_ttl: int = 43_200
max_body_bytes: int = 1_048_576 max_body_bytes: int = 1_048_576
base_path: str = ""
def __post_init__(self) -> None: def __post_init__(self) -> None:
object.__setattr__(self, "base_path", normalize_base_path(self.base_path))
object.__setattr__(self, "host", normalize_host(self.host)) object.__setattr__(self, "host", normalize_host(self.host))
object.__setattr__(self, "data_dir", Path(self.data_dir).expanduser().absolute()) object.__setattr__(self, "data_dir", Path(self.data_dir).expanduser().absolute())
object.__setattr__(self, "allowed_hosts", tuple(normalize_host(h) for h in self.allowed_hosts)) object.__setattr__(self, "allowed_hosts", tuple(normalize_host(h) for h in self.allowed_hosts))
@@ -189,6 +198,7 @@ class WebConfig:
"secure_cookie": _env_bool(values.get("RANCH_WEB_SECURE_COOKIE", ""), "RANCH_WEB_SECURE_COOKIE"), "secure_cookie": _env_bool(values.get("RANCH_WEB_SECURE_COOKIE", ""), "RANCH_WEB_SECURE_COOKIE"),
"session_ttl": int(values.get("RANCH_WEB_SESSION_TTL", 43_200)), "session_ttl": int(values.get("RANCH_WEB_SESSION_TTL", 43_200)),
"max_body_bytes": int(values.get("RANCH_WEB_MAX_BODY_BYTES", 1_048_576)), "max_body_bytes": int(values.get("RANCH_WEB_MAX_BODY_BYTES", 1_048_576)),
"base_path": values.get("RANCH_WEB_BASE_PATH", ""),
} }
settings.update(overrides) settings.update(overrides)
return cls(**settings) return cls(**settings)
+5 -2
View File
@@ -1,3 +1,5 @@
import {apiPath, webPath} from './paths.js';
export class ApiError extends Error { export class ApiError extends Error {
constructor(message, status, detail) { constructor(message, status, detail) {
super(message); super(message);
@@ -16,17 +18,18 @@ function errorMessage(data, status) {
} }
export async function request(path, {method = 'GET', body, signal, blob = false} = {}) { export async function request(path, {method = 'GET', body, signal, blob = false} = {}) {
const url = apiPath(path);
const headers = {Accept: blob ? '*/*' : 'application/json'}; const headers = {Accept: blob ? '*/*' : 'application/json'};
if (body !== undefined && !(body instanceof FormData)) headers['Content-Type'] = 'application/json'; if (body !== undefined && !(body instanceof FormData)) headers['Content-Type'] = 'application/json';
let response; let response;
try { try {
response = await fetch(path, {method, credentials: 'same-origin', cache: 'no-store', headers, signal, response = await fetch(url, {method, credentials: 'same-origin', cache: 'no-store', redirect: 'error', headers, signal,
body: body === undefined ? undefined : body instanceof FormData ? body : JSON.stringify(body)}); body: body === undefined ? undefined : body instanceof FormData ? body : JSON.stringify(body)});
} catch (error) { } catch (error) {
if (error.name === 'AbortError') throw error; if (error.name === 'AbortError') throw error;
throw new ApiError('无法连接 Web 服务,请检查服务是否仍在运行。', 0); throw new ApiError('无法连接 Web 服务,请检查服务是否仍在运行。', 0);
} }
if (response.status === 401 && !path.startsWith('/api/auth/')) window.dispatchEvent(new CustomEvent('auth-required')); if (response.status === 401 && !url.startsWith(webPath('/api/auth/'))) window.dispatchEvent(new CustomEvent('auth-required'));
if (!response.ok) { if (!response.ok) {
const data = await response.json().catch(() => null); const data = await response.json().catch(() => null);
throw new ApiError(errorMessage(data, response.status), response.status, data); throw new ApiError(errorMessage(data, response.status), response.status, data);
+9 -3
View File
@@ -1,4 +1,5 @@
import {api, request} from './api.js'; import {api, request} from './api.js';
import {apiPath, webPath} from './paths.js';
import {el, $, $$, clone, getPath, setPath, pathKey, textValue, number, chinaTime, download, csvBlob, optionsList, detailsList, tabKeys} from './dom.js'; import {el, $, $$, clone, getPath, setPath, pathKey, textValue, number, chinaTime, download, csvBlob, optionsList, detailsList, tabKeys} from './dom.js';
import {PageView, PAGES} from './pages.js'; import {PageView, PAGES} from './pages.js';
import {notice, dialog, confirm, filePicker, fileBase64} from './ui.js'; import {notice, dialog, confirm, filePicker, fileBase64} from './ui.js';
@@ -418,7 +419,7 @@ function renderLogs(reset=false) {
const line=el('div',{class:`log-entry ${entry.level || entry.kind || ''}`,'data-entry-id':entry.id}); const line=el('div',{class:`log-entry ${entry.level || entry.kind || ''}`,'data-entry-id':entry.id});
const stamp=entry.time || entry.timestamp || entry.at || ''; const stamp=entry.time || entry.timestamp || entry.at || '';
line.append(el('span',{class:'log-time',text:chinaTime(stamp),title:String(stamp)}),document.createTextNode(entry.message || entry.operation || textValue(entry))); line.append(el('span',{class:'log-time',text:chinaTime(stamp),title:String(stamp)}),document.createTextNode(entry.message || entry.operation || textValue(entry)));
if(entry.details_available || entry.detail_url){line.classList.add('inspectable');line.tabIndex=0;line.setAttribute('role','button');line.title='查看脱敏请求 / 响应 JSON';line.addEventListener('click',()=>showDebugEntry(account,entry));line.addEventListener('keydown',event=>{if(event.key==='Enter'||event.key===' '){event.preventDefault();showDebugEntry(account,entry);}});} if(entry.details_available || entry.detail_url || entry.debug_url){line.classList.add('inspectable');line.tabIndex=0;line.setAttribute('role','button');line.title='查看脱敏请求 / 响应 JSON';line.addEventListener('click',()=>showDebugEntry(account,entry));line.addEventListener('keydown',event=>{if(event.key==='Enter'||event.key===' '){event.preventDefault();showDebugEntry(account,entry);}});}
root.append(line); root.append(line);
} }
log.rendered=log.entries.length; log.rendered=log.entries.length;
@@ -439,7 +440,12 @@ async function fetchLogs(account) {
async function showDebugEntry(account,entry) { async function showDebugEntry(account,entry) {
const pre=el('pre',{class:'debug-json',text:'正在读取所选条目的脱敏详情…'});const open=dialog('请求 / 响应详情(已脱敏)',pre,{info:true}); const pre=el('pre',{class:'debug-json',text:'正在读取所选条目的脱敏详情…'});const open=dialog('请求 / 响应详情(已脱敏)',pre,{info:true});
try{let detail;if(entry.detail_url&&new URL(entry.detail_url,location.href).origin===location.origin)detail=await request(entry.detail_url);else detail=await api.debugEntry(account.id,entry.id);pre.textContent=JSON.stringify(detail.details ?? detail,null,2);}catch(failure){pre.textContent=failure.message;} try{
let path;
try{path=apiPath(entry.detail_url || entry.debug_url);}catch{/* Use the account's own endpoint for untrusted URLs. */}
const detail=await (path?request(path):api.debugEntry(account.id,entry.id));
pre.textContent=JSON.stringify(detail.details ?? detail,null,2);
}catch(failure){pre.textContent=failure.message;}
await open; await open;
} }
@@ -479,7 +485,7 @@ function hideWorkbench() {
function requireAuth() { function requireAuth() {
if(state.redirecting)return; if(state.redirecting)return;
state.redirecting=true;hideWorkbench();location.replace('/login'); state.redirecting=true;hideWorkbench();location.replace(webPath('/login'));
} }
async function bootstrap() { async function bootstrap() {
+1
View File
@@ -3,6 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="ranch-web-base-path" content="">
<meta name="color-scheme" content="light"> <meta name="color-scheme" content="light">
<title>牧羊人之心牧场助手 · Web</title> <title>牧羊人之心牧场助手 · Web</title>
<link rel="icon" href="data:,"> <link rel="icon" href="data:,">
+1
View File
@@ -3,6 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="ranch-web-base-path" content="">
<meta name="color-scheme" content="light"> <meta name="color-scheme" content="light">
<title>管理登录 · 牧羊人之心牧场助手</title> <title>管理登录 · 牧羊人之心牧场助手</title>
<link rel="icon" href="data:,"> <link rel="icon" href="data:,">
+7 -2
View File
@@ -1,4 +1,8 @@
const form = document.querySelector('#auth-form'); const form = document.querySelector('#auth-form');
// Keep login standalone: the server renders the prefix in both HTML documents.
const basePath = (document.querySelector('meta[name="ranch-web-base-path"]')?.content || '').replace(/\/+$/, '');
const webPath = path => `${basePath}${path}`;
form.setAttribute('action', webPath('/api/auth/login'));
const input = document.querySelector('#admin-password'); const input = document.querySelector('#admin-password');
const button = form.querySelector('button[type="submit"]'); const button = form.querySelector('button[type="submit"]');
const status = document.querySelector('#auth-status'); const status = document.querySelector('#auth-status');
@@ -24,16 +28,17 @@ function openWorkbench() {
leaving = true; leaving = true;
input.value = ''; input.value = '';
setBusy(true, '正在进入工作台…'); setBusy(true, '正在进入工作台…');
location.replace('/'); location.replace(webPath('/'));
} }
async function authRequest(path, body) { async function authRequest(path, body) {
let response; let response;
try { try {
response = await fetch(path, { response = await fetch(webPath(path), {
method: body === undefined ? 'GET' : 'POST', method: body === undefined ? 'GET' : 'POST',
credentials: 'same-origin', credentials: 'same-origin',
cache: 'no-store', cache: 'no-store',
redirect: 'error',
headers: body === undefined ? {Accept: 'application/json'} : {Accept: 'application/json', 'Content-Type': 'application/json'}, headers: body === undefined ? {Accept: 'application/json'} : {Accept: 'application/json', 'Content-Type': 'application/json'},
body, body,
}); });
+13
View File
@@ -0,0 +1,13 @@
// The server supplies this value; never infer it from the URL or proxy headers.
const basePath = (document.querySelector('meta[name="ranch-web-base-path"]')?.content || '').replace(/\/+$/, '');
export const webPath = path => `${basePath}${path}`;
export function apiPath(path) {
const url = new URL(path, location.origin);
if (url.origin !== location.origin || url.username || url.password) throw new TypeError('Invalid API URL');
let pathname = url.pathname;
if (basePath && pathname.startsWith(`${basePath}/`)) pathname = pathname.slice(basePath.length);
if (!pathname.startsWith('/api/')) throw new TypeError('Invalid API path');
return webPath(pathname) + url.search;
}
@@ -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()