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
+10
View File
@@ -122,6 +122,13 @@ def _env_bool(value: str, name: str) -> bool:
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)
class WebConfig:
host: str = DEFAULT_HOST
@@ -134,8 +141,10 @@ class WebConfig:
secure_cookie: bool = False
session_ttl: int = 43_200
max_body_bytes: int = 1_048_576
base_path: str = ""
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, "data_dir", Path(self.data_dir).expanduser().absolute())
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"),
"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)),
"base_path": values.get("RANCH_WEB_BASE_PATH", ""),
}
settings.update(overrides)
return cls(**settings)