feat: live-sync per websocket im backend
- /api/ws authentifiziert über das Session-Cookie, Origin-Pflicht gegen CSWSH - Hub im Prozess verteilt clip.created/updated/deleted an alle Geräte des Benutzers - Ereignisse erst nach erfolgreichem Commit (BackgroundTasks), keine bei Konflikt - Logout, Session-Widerruf, Passwortänderung und Admin-Aktionen schließen betroffene Verbindungen (4401); Session wird alle 5 Minuten neu geprüft - Heartbeat alle 30 s, höchstens 20 Verbindungen pro Benutzer - CSP erlaubt wss:// zur eigenen Domain, eingehende WS-Nachrichten max. 64 KiB Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a444f28681
commit
0d89399090
12 files changed
+483
-38
No files matched your search
+20
-14
@@ -26,21 +26,26 @@ class OriginCheckMiddleware:
|
||||
await self.app(scope, receive, send)
|
||||
|
||||
|
||||
CONTENT_SECURITY_POLICY = "; ".join(
|
||||
[
|
||||
"default-src 'self'",
|
||||
"script-src 'self'",
|
||||
"style-src 'self'",
|
||||
"img-src 'self' data:",
|
||||
"connect-src 'self'",
|
||||
"frame-ancestors 'none'",
|
||||
"base-uri 'self'",
|
||||
"form-action 'self'",
|
||||
def build_csp(allowed_origins: list[str]) -> str:
|
||||
# Older Safari does not match ws/wss against 'self', so list the socket origins explicitly
|
||||
ws_origins = [
|
||||
o.replace("https://", "wss://", 1) for o in allowed_origins if o.startswith("https://")
|
||||
]
|
||||
)
|
||||
return "; ".join(
|
||||
[
|
||||
"default-src 'self'",
|
||||
"script-src 'self'",
|
||||
"style-src 'self'",
|
||||
"img-src 'self' data:",
|
||||
" ".join(["connect-src 'self'", *ws_origins]),
|
||||
"frame-ancestors 'none'",
|
||||
"base-uri 'self'",
|
||||
"form-action 'self'",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
SECURITY_HEADERS = [
|
||||
(b"content-security-policy", CONTENT_SECURITY_POLICY.encode()),
|
||||
(b"x-content-type-options", b"nosniff"),
|
||||
(b"referrer-policy", b"same-origin"),
|
||||
(b"x-frame-options", b"DENY"),
|
||||
@@ -54,15 +59,16 @@ HSTS_HEADER = (b"strict-transport-security", b"max-age=31536000")
|
||||
class SecurityHeadersMiddleware:
|
||||
"""Add security headers to every HTTP response; HSTS only for HTTPS requests."""
|
||||
|
||||
def __init__(self, app: ASGIApp) -> None:
|
||||
def __init__(self, app: ASGIApp, allowed_origins: list[str]) -> None:
|
||||
self.app = app
|
||||
self.csp_header = (b"content-security-policy", build_csp(allowed_origins).encode())
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
if scope["type"] != "http":
|
||||
await self.app(scope, receive, send)
|
||||
return
|
||||
|
||||
extra = list(SECURITY_HEADERS)
|
||||
extra = [self.csp_header, *SECURITY_HEADERS]
|
||||
if scope.get("scheme") == "https":
|
||||
extra.append(HSTS_HEADER)
|
||||
|
||||
|
||||
Reference in new issue
Block a user