1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Request-scoped CSRF token for admin templates (#1395).
//!
//! `docs/security.md` said the auto-admin "enables CSRF on every
//! mutation by default, and there is no way to opt out". Neither half
//! was true: the only protected route was `POST /login`, and `login.html`
//! was the only one of fourteen templates rendering a token. Every other
//! mutation — create, update, delete, bulk actions, audit cleanup —
//! accepted a cross-site POST carrying the admin's session cookie.
//!
//! Closing it needs two things in the same change: the layer that
//! rejects, and a token in every form. Either alone is useless — the
//! layer without the tokens turns every admin mutation into a 403, and
//! the tokens without the layer protect nothing.
//!
//! This is the second half. It mints (or reuses) the token once per
//! request, parks it in a task-local so `chrome_context` can put it in
//! every template's variables, and attaches the `Set-Cookie` the layer
//! would otherwise add later — later being too late, because the
//! handler has already rendered a token by then and the two would
//! disagree. `CsrfLayer` explicitly stands down when the response
//! already sets the cookie, which is what makes that safe.
use Body;
use Request;
use SET_COOKIE;
use HeaderValue;
use Next;
use Response;
use CURRENT_CSRF_TOKEN;
/// Install the request's CSRF token as a task-local, then attach the
/// cookie that pairs with it.
///
/// Mounted inside the auth gate so it runs for every admin page,
/// including the ones that only read — a GET has to seed the cookie, or
/// the first POST from a fresh browser has nothing to match against.
pub async