class SecureServer(Server):
"""textual-serve's server with a token and an Origin check."""
def __init__(
self,
command: str,
*,
host: str = "localhost",
port: int = 8000,
title: str | None = None,
public_url: str | None = None,
token: str | None = None,
) -> None:
super().__init__(command, host=host, port=port, title=title, public_url=public_url)
self.token = token
self.cookie_name = f"acptui_token_{port}" # cookies ignore ports: one per server
@property
def url(self) -> str:
"""The URL to open (with the token)."""
return f"{self.public_url}/?token={self.token}" if self.token else f"{self.public_url}/"
async def _make_app(self) -> web.Application:
app = await super()._make_app()
app.middlewares.append(self._guard)
return app
def origin_allowed(self, origin: str, request: web.Request) -> bool:
"""Whether a WebSocket from ``origin`` may connect (same origin or the public URL)."""
origin = origin.lower()
scheme = "https" if request.secure else "http"
return origin in {origin_of(self.public_url), f"{scheme}://{request.host}".lower()}
def _token_ok(self, request: web.Request) -> bool:
if self.token is None:
return True
given = request.query.get("token") or request.cookies.get(self.cookie_name) or ""
return hmac.compare_digest(given.encode(), self.token.encode())
@web.middleware
async def _guard(
self, request: web.Request, handler: Callable[[web.Request], Awaitable[web.StreamResponse]]
) -> web.StreamResponse:
if request.headers.get("Upgrade", "").lower() == "websocket":
origin = request.headers.get("Origin")
if origin is not None and not self.origin_allowed(origin, request):
raise web.HTTPForbidden(text="WebSocket from another origin")
if not self._token_ok(request):
raise web.HTTPUnauthorized(
text="acptui serve: open the URL with the token it printed (?token=...)."
)
response = await handler(request)
if self.token is not None and "token" in request.query and not response.prepared:
response.set_cookie(
self.cookie_name, self.token, httponly=True, samesite="Strict", path="/"
)
return response
async def on_startup(self, app: web.Application) -> None:
self.console.print(f"acptui serve: {self.command!r}")
self.console.print(f"Open [bold]{self.url}[/bold]", highlight=False)
if self.token is None:
self.console.print("[yellow]No token: anyone on this machine can connect.")
self.console.print("[cyan]Press Ctrl+C to quit")