You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
authored and
Berik Ashimov
committed
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]>
@@ -771,7 +771,7 @@ Rejects oversized requests at the ASGI scope level before body parsing. Returns
771
771
772
772
### CSRF Protection
773
773
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:
775
775
776
776
```python
777
777
from hawkapi.middleware.csrf import CSRFMiddleware
@@ -786,7 +786,7 @@ app.add_middleware(
786
786
)
787
787
```
788
788
789
-
Returns 403 with `application/problem+json` when the token is missingor 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.
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).
155
155
156
156
```python
157
157
from hawkapi.middleware.csrf import CSRFMiddleware
@@ -162,7 +162,7 @@ app.add_middleware(
162
162
)
163
163
```
164
164
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 missingor 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.
Copy file name to clipboardExpand all lines: docs/guide/security.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,4 +80,4 @@ API keys can also be read from query parameters (`APIKeyQuery`) or cookies (`API
80
80
81
81
## CSRF Protection
82
82
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