Buckets:

download
raw
7.3 kB
import hashlib
import hmac
import logging
import secrets
from typing import Literal
from urllib.parse import urlparse
from starlette.datastructures import MutableHeaders
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.types import ASGIApp, Message, Receive, Scope, Send
logger = logging.getLogger("fastapi_fullauth.csrf")
SAFE_METHODS = {"GET", "HEAD", "OPTIONS"}
def _sign_token(token: str, secret: str) -> str:
return hmac.new(secret.encode(), token.encode(), hashlib.sha256).hexdigest()
def _make_csrf_value(secret: str) -> str:
token = secrets.token_hex(32)
sig = _sign_token(token, secret)
return f"{token}.{sig}"
def _verify_csrf_value(value: str, secret: str) -> bool:
parts = value.split(".", 1)
if len(parts) != 2:
return False
token, sig = parts
expected = _sign_token(token, secret)
return hmac.compare_digest(sig, expected)
class CSRFMiddleware:
"""Double-submit cookie CSRF protection (pure ASGI middleware).
Sets a signed CSRF cookie on safe requests. State-changing requests
must include an X-CSRF-Token header matching the cookie value.
The signed double-submit token proves the value was server-issued but is not
bound to the user's session, so a party able to *write* a cookie for the
domain (sibling-subdomain takeover, MITM on a plaintext sibling) could plant
a matching cookie+header pair. Set ``trusted_origins`` to the front-end
origins you serve to also require a matching Origin/Referer on state-changing
requests - the recommended defence in depth for cookie-based auth.
"""
def __init__(
self,
app: ASGIApp,
secret: str,
cookie_name: str = "fullauth_csrf",
exempt_paths: list[str] | None = None,
cookie_secure: bool = True,
cookie_samesite: Literal["lax", "strict", "none"] = "lax",
cookie_httponly: bool = False,
cookie_domain: str | None = None,
header_name: str = "X-CSRF-Token",
trusted_origins: list[str] | None = None,
) -> None:
if not secret or len(secret) < 32:
raise ValueError(
"CSRFMiddleware requires a `secret` of at least 32 characters. "
"Pass your FullAuthConfig SECRET_KEY (or a dedicated key)."
)
if cookie_samesite == "none" and not cookie_secure:
raise ValueError(
"CSRFMiddleware with cookie_samesite='none' requires cookie_secure=True; "
"browsers reject a SameSite=None cookie that is not also Secure."
)
self.app = app
self.secret = secret
self.cookie_name = cookie_name
self.exempt_paths: list[str] = exempt_paths or []
self.cookie_secure = cookie_secure
self.cookie_samesite = cookie_samesite
self.cookie_httponly = cookie_httponly
self.cookie_domain = cookie_domain
self.header_name = header_name
# Normalise to scheme://host[:port] with any trailing slash removed.
self.trusted_origins: set[str] = {o.rstrip("/") for o in (trusted_origins or [])}
def _is_exempt(self, path: str) -> bool:
# Anchor on path-segment boundaries so exempting "/api/foo" does not also
# exempt "/api/foobar"; only "/api/foo" itself and "/api/foo/..." match.
return any(path == p or path.startswith(p.rstrip("/") + "/") for p in self.exempt_paths)
def _origin_allowed(self, request: Request) -> bool:
"""When trusted_origins is configured, a present Origin/Referer must match
one of them. Requests with neither header (non-browser clients) defer to
the token check; browsers always send Origin on cross-site unsafe requests."""
if not self.trusted_origins:
return True
origin = request.headers.get("origin")
if origin is None:
referer = request.headers.get("referer")
if referer is not None:
parsed = urlparse(referer)
if not (parsed.scheme and parsed.netloc):
# A present but unparseable Referer must not silently fall back
# to the token-only path; treat it as a failed origin check.
return False
origin = f"{parsed.scheme}://{parsed.netloc}"
if origin is None:
return True
return origin.rstrip("/") in self.trusted_origins
def _cookie_header(self) -> str:
# Reuse Starlette's cookie serialization so attribute formatting matches
# what set_cookie would have produced.
response = Response()
response.set_cookie(
key=self.cookie_name,
value=_make_csrf_value(self.secret),
httponly=self.cookie_httponly,
secure=self.cookie_secure,
samesite=self.cookie_samesite,
domain=self.cookie_domain,
path="/",
)
return response.headers["set-cookie"]
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] != "http":
await self.app(scope, receive, send)
return
request = Request(scope)
method = request.method.upper()
path = request.url.path
if self._is_exempt(path):
await self.app(scope, receive, send)
return
if method in SAFE_METHODS:
if self.cookie_name in request.cookies:
await self.app(scope, receive, send)
return
set_cookie = self._cookie_header()
async def send_with_cookie(message: Message) -> None:
if message["type"] == "http.response.start":
MutableHeaders(scope=message).append("Set-Cookie", set_cookie)
await send(message)
await self.app(scope, receive, send_with_cookie)
return
if not self._origin_allowed(request):
logger.warning("CSRF origin not allowed: %s %s", method, path)
await JSONResponse({"detail": "CSRF origin check failed."}, status_code=403)(
scope, receive, send
)
return
cookie_value = request.cookies.get(self.cookie_name)
header_value = request.headers.get(self.header_name)
if not cookie_value or not header_value:
logger.warning("CSRF token missing: %s %s", method, path)
await JSONResponse({"detail": "CSRF token missing."}, status_code=403)(
scope, receive, send
)
return
if not _verify_csrf_value(cookie_value, self.secret):
logger.warning("CSRF cookie signature invalid: %s %s", method, path)
await JSONResponse({"detail": "CSRF cookie signature invalid."}, status_code=403)(
scope, receive, send
)
return
if not hmac.compare_digest(cookie_value, header_value):
logger.warning("CSRF token mismatch: %s %s", method, path)
await JSONResponse({"detail": "CSRF token mismatch."}, status_code=403)(
scope, receive, send
)
return
await self.app(scope, receive, send)

Xet Storage Details

Size:
7.3 kB
·
Xet hash:
9268f1f2ca8f329a1198250fd48e84f13d95f99bf86cde6495142abe27f3c2fa

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.