tilie_server.security#

The origin, body-limit and referrer-policy ASGI middleware.

class tilie_server.security.OriginGuard(app, *, token, hosts, origins)[source]#

Bases: object

Reject requests that do not come from the server’s own origin.

Three checks run on every HTTP request and WebSocket handshake, before any route is reached:

  • the Host header must be one of the server’s hosts, which defeats DNS rebinding (403 otherwise);

  • an Origin header, when present, must be one of the server’s origins, which defeats cross-site requests from other pages (403 otherwise);

  • when the server has a token, every path under /api/ must carry Authorization: Bearer <token> with that token (401 otherwise).

A rejected WebSocket handshake is closed before it is accepted.

Parameters:
tilie_server.security.body_limit_message(limit)[source]#

The refusal of a body longer than limit bytes, the limit named by tilie_server.settings.size_named().

Parameters:

limit (int)

Return type:

str

class tilie_server.security.BodyLimit(app, *, limit)[source]#

Bases: object

Refuse a request body longer than limit bytes with 413.

A Content-Length over the limit is refused at once, without reading the body. A body sent without one (chunked) is read here, chunk by chunk, and refused as soon as the bytes received pass the limit; a body within it is handed to the application as it came, so the route runs only with a whole body inside the limit.

Parameters:
class tilie_server.security.NoReferrer(app)[source]#

Bases: object

Send Referrer-Policy: no-referrer with every HTTP response.

With this policy the browser sends none of the server’s URLs as a Referer, so no other site learns them, whatever a URL may carry. Rejections by OriginGuard and BodyLimit carry the header as well.

Parameters:

app (ASGIApp)

HEADER = (b'referrer-policy', b'no-referrer')#