Skip to main content

Module csrf

Module csrf 

Source
Expand description

CSRF protection via double-submit cookie.

Verifies state-changing POSTs with a double-submit __Host- cookie compared in constant time. A form carries the cookie’s token masked with a one-time pad drawn for each render, so no two compressed pages hold the same bytes for an observer to guess from response sizes (BREACH).

Constants§

COOKIE_NAME
Names the __Host--prefixed CSRF cookie.
FIELD_NAME

Functions§

current_token
Reads the current token without setting one, safe inside streamed children.
ensure_token
Ensures a request token before headers send, returning "" without a cookie layer.
field
Renders the hidden field carrying the given token, masked afresh.
mask
The value a form posts under FIELD_NAME for token: the token XORed with a random pad, pad first, in unpadded base64url. Each call draws a new pad, so the same token never renders twice the same; verify accepts any of them. "" when token is not a token ensure_token issued.
verify
Verifies the value a POST submitted under FIELD_NAME is a mask of the cookie’s token with a constant-time compare, failing closed with 403. The cookie’s token itself, unmasked, is refused: no page renders it.