CSRF Middleware
- class smarter.lib.django.middleware.csrf.SmarterCsrfViewMiddleware(get_response)[source]
Bases:
CsrfViewMiddleware,SmarterRequestMixinASGI-safe CSRF middleware.
Extends Django’s CsrfViewMiddleware while preserving all Django request lifecycle invariants.
- __init__(get_response)[source]
Initialize Django’s CSRF middleware, then this instance’s
SmarterRequestMixin.- Parameters:
get_response – The next handler in the middleware chain.
- allowed_origin_subdomains
The wildcard
CSRF_TRUSTED_ORIGINS, as a dict of scheme to the domains whose subdomains are trusted.For example,
https://*.example.comadds.example.comto thehttpslist.- Returns:
A dict of scheme to the list of trusted parent domains.
- allowed_origins_exact
The
CSRF_TRUSTED_ORIGINSwithout a wildcard, which must match an origin exactly.- Returns:
The set of exact origins.
- csrf_trusted_origins_hosts
The hosts of
CSRF_TRUSTED_ORIGINS, without a leading wildcard, as Django’s internals expect.process_view()swaps it for the request’s own list while Django checks the request, so that the settings are never changed.- Returns:
The trusted hosts, e.g.
.example.comforhttps://*.example.com.
- formatted_class_name
Return the formatted class name for logging.
It names the instance, not a request.
- get_dynamic_trusted_origins(request)[source]
Return one request’s trusted origins:
CSRF_TRUSTED_ORIGINS, plus its own origin for an LLMClient.The list is a fresh copy for each request, and Django’s settings are never changed. An LLMClient or configuration api request adds its own origin, e.g.
https://example.com. A failure to classify the request is logged, and the settings’ origins are returned.- Parameters:
request (
HttpRequest) – The request.- Return type:
- Returns:
A new list of trusted origins.
- llmclient_request_kind(request)[source]
Return whether the request is for an LLMClient, and whether it is for a configuration api.
The middleware is one instance shared by every request, so the request is classified by its own
SmarterRequestMixin, never by this instance’s, and the result is kept on the request, never on the middleware. A request that can’t be classified is neither, so it gets the full CSRF check.- Parameters:
request (
HttpRequest) – The request.- Return type:
- Returns:
(is_llmclient, is_config).
- process_response(request, response)[source]
Renew the CSRF cookie on a signed-in user’s console pages, then let Django set it.
get_token()keeps the current token when the request has one, or makes a new one when the cookie has expired, and marks the cookie for update, so that Django’sprocess_response()sends it again with a freshCSRF_COOKIE_AGE.- Parameters:
request (
HttpRequest) – The request.response (
HttpResponseBase) – The view’s response.
- Return type:
- Returns:
The response, with the
csrftokencookie when it was renewed.
- process_view(request, callback, callback_args, callback_kwargs)[source]
Check a request’s CSRF token and origin, unless one of the bypass rules applies.
Amnesty paths, a disabled
ENABLE_MIDDLEWARE_CSRFswitch, the local environment, health checks, internal hosts and, withCSRF_SUPPRESS_FOR_LLM_CLIENTSon, LLMClient requests skip the check. Every other request is checked by Django, against the trusted origins ofget_dynamic_trusted_origins(), which replace this instance’s origin lists only until Django’s check returns. A rejected request is logged at ERROR.This hook never calls
self.get_response(request): it returns None, and Django calls the view.- Parameters:
request (
HttpRequest) – The request.callback – The view function.
callback_args – The view’s positional arguments.
callback_kwargs – The view’s keyword arguments.
- Returns:
None to let the request through, or Django’s
403response.
- renews_csrf_cookie(request, response)[source]
Return whether a response should renew the CSRF cookie: a web console page for a signed-in user.
The CSRF cookie lives
CSRF_COOKIE_AGE(1 day), and the sessionSESSION_COOKIE_AGE(28 days). Django only sets the cookie on a response whose request asked for the token, e.g. with{% csrf_token %}, which the React host pages don’t. Without renewal, every React app that writes throughfetchDjangoUrl()fails with a 403 “CSRF cookie not set” from the second day of a session.- Parameters:
request (
HttpRequest) – The request.response (
HttpResponseBase) – The view’s response.
- Return type:
- Returns:
True for a successful HTML response to a signed-in user’s
GET, so that the cookie lives as long as the user keeps using the console. Api responses, redirects, errors and anonymous visitors are left alone.