Skip to content

Commit fde0e02

Browse files
Berik Ashimovclaude
authored andcommitted
docs: reflect v0.2.0 CSRF signed double-submit behavior
The CSRF middleware now verifies the submitted token's HMAC signature against the secret before the constant-time comparison with the cookie value. Update the README and middleware/security guides so the described behavior matches the shipped implementation. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 8f36136 commit fde0e02

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ class AuthMiddleware(Middleware):
347347
| `CircuitBreakerMiddleware` | Three-state circuit breaker (per-path tracking) |
348348
| `RedisCircuitBreakerMiddleware` | Distributed circuit breaker (Redis-backed) |
349349
| `AdaptiveConcurrencyMiddleware` | Adaptive in-flight limit based on latency |
350-
| `CSRFMiddleware` | Double-submit cookie CSRF protection |
350+
| `CSRFMiddleware` | Signed double-submit cookie CSRF protection |
351351
| `SessionMiddleware` | Signed cookie-based session management |
352352
| `ErrorHandlerMiddleware` | Structured error handling pipeline |
353353
| `PrometheusMiddleware` | Prometheus-compatible `/metrics` endpoint |
@@ -771,7 +771,7 @@ Rejects oversized requests at the ASGI scope level before body parsing. Returns
771771

772772
### CSRF Protection
773773

774-
Double-submit cookie CSRF protection. Safe methods (GET, HEAD, OPTIONS) pass through and receive a signed CSRF token cookie. Unsafe methods require the token in an `X-CSRF-Token` header or `csrf_token` form field:
774+
Signed double-submit cookie CSRF protection. Safe methods (GET, HEAD, OPTIONS) pass through and receive a signed CSRF token cookie. Unsafe methods require the token in an `X-CSRF-Token` header or `csrf_token` form field:
775775

776776
```python
777777
from hawkapi.middleware.csrf import CSRFMiddleware
@@ -786,7 +786,7 @@ app.add_middleware(
786786
)
787787
```
788788

789-
Returns 403 with `application/problem+json` when the token is missing or mismatched. Tokens are HMAC-SHA256 signed and verified with `hmac.compare_digest` for timing safety.
789+
Returns 403 with `application/problem+json` when the token is missing, its signature is invalid, or it does not match the cookie. Tokens are HMAC-SHA256 signed; the submitted token's signature is verified against the secret before a constant-time `hmac.compare_digest` comparison against the cookie value.
790790

791791
### Session Middleware
792792

docs/guide/middleware.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ app.add_middleware(TimingMiddleware)
4848

4949
| Middleware | Description | Extra |
5050
|-----------|-------------|-------|
51-
| `CSRFMiddleware` | CSRF protection (double-submit cookie) ||
51+
| `CSRFMiddleware` | CSRF protection (signed double-submit cookie) ||
5252
| `SessionMiddleware` | Signed cookie-based sessions ||
5353
| `RedisRateLimitMiddleware` | Redis-backed rate limiting | `redis` |
5454
| `CORSMiddleware` | Cross-Origin Resource Sharing ||
@@ -151,7 +151,7 @@ app.add_middleware(PrometheusMiddleware)
151151

152152
## CSRF Middleware
153153

154-
HawkAPI includes CSRF protection using the **double-submit cookie** pattern. A CSRF token is set in a cookie on safe requests (GET, HEAD, OPTIONS) and must be echoed back via a header or form field on unsafe requests (POST, PUT, DELETE, PATCH).
154+
HawkAPI includes CSRF protection using the **signed double-submit cookie** pattern. A CSRF token is set in a cookie on safe requests (GET, HEAD, OPTIONS) and must be echoed back via a header or form field on unsafe requests (POST, PUT, DELETE, PATCH).
155155

156156
```python
157157
from hawkapi.middleware.csrf import CSRFMiddleware
@@ -162,7 +162,7 @@ app.add_middleware(
162162
)
163163
```
164164

165-
On safe requests, the middleware sets a `csrftoken` cookie automatically. On unsafe requests, the client must send the token back via the `X-CSRF-Token` header or a `csrf_token` form field. If the token is missing or does not match, a 403 response is returned.
165+
On safe requests, the middleware sets a `csrftoken` cookie automatically. The token is an HMAC-SHA256-signed value (`<random>.<signature>`). On unsafe requests, the client must send the token back via the `X-CSRF-Token` header or a `csrf_token` form field. The submitted token's HMAC signature is verified against the configured `secret` before a constant-time comparison against the cookie value, so a forged token without a valid signature is rejected. If the token is missing, its signature is invalid, or it does not match the cookie, a 403 response is returned.
166166

167167
| Option | Default | Description |
168168
|--------|---------|-------------|

docs/guide/security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ API keys can also be read from query parameters (`APIKeyQuery`) or cookies (`API
8080

8181
## CSRF Protection
8282

83-
HawkAPI includes built-in CSRF protection via `CSRFMiddleware`, which implements the double-submit cookie pattern. See the [Middleware guide](middleware.md#csrf-middleware) for configuration options and usage examples.
83+
HawkAPI includes built-in CSRF protection via `CSRFMiddleware`, which implements a signed double-submit cookie pattern: the token carries an HMAC-SHA256 signature that is verified against the configured `secret` before the constant-time comparison against the cookie value. See the [Middleware guide](middleware.md#csrf-middleware) for configuration options and usage examples.

0 commit comments

Comments
 (0)