Add public ASGI middleware for origin and CSRF checks
Author: jodalCreated Sep 27, 2026Updated Sep 27, 2026
LabelsA-ext-httpready-for-agent
:robot: Summary
Extension authors can protect their ASGI apps with the same origin and CSRF checks as Mopidy's own endpoints. Mopidy ships public ASGI middleware that reads allowed_origins and csrf_protection from the http config section. It works for HTTP and WebSocket scopes. See "Middleware" in the parent spec.
Parent
mopidy/mopidy#2312
Acceptance criteria
- The middleware is part of the public API, is a plain ASGI middleware, and needs no Starlette types from the caller.
- It reads
allowed_originsandcsrf_protectionfrom thehttpconfig. - It blocks and allows the same origins as the current checks, including the special cases for empty,
nullandfile://origins and for same-host origins. - It applies the same CSRF content-type rule and CORS headers as the current JSON-RPC endpoint.
- It rejects blocked WebSocket connections before the handshake.
- Tests register an ASGI test app with the middleware through the test seam, and check allowed and blocked origins and the CSRF rule.
Blocked by
- mopidy/mopidy#2314
Source: mopidy/mopidy