Bug Description
The Double Submit Cookie CSRF protection implemented in backend/csrf.py is vulnerable to Token Injection. The middleware verifies that the csrftoken cookie matches the X-CSRF-Token header using secrets.compare_digest(cookie_token, header_token). However, it does not validate the minimum length or format of the tokens. An attacker can set a short, known token (e.g., "a") in the user's csrftoken cookie (e.g., via a sub-domain XSS or MITM if secure flag is bypassed) and then send a cross-site request with the matching "a" in the header. The server will accept this as valid CSRF validation since "a" == "a".
Steps to Reproduce
- Intercept a legitimate request to a protected endpoint (e.g., POST).
- Modify the
csrftoken cookie value to "a".
- Modify the
X-CSRF-Token request header value to "a".
- Send the request.
- Observe that the server accepts the request with a 200/201 status code instead of throwing a 403 Forbidden.
Expected Behavior
The CSRF middleware should enforce a strict length check on the tokens (e.g., exactly 64 hexadecimal characters) or use HMAC signatures to ensure the token was generated by the server.
Actual Behavior
The middleware accepts any matching token pair, regardless of length, allowing trivial CSRF token injection if an attacker can write a cookie to the victim's browser.
Screenshots / Error Logs
N/A
Environment
- OS: Any
- Python version: Any
- Browser: Any
Additional Context
Advanced level security vulnerability. Occurs in backend/csrf.py inside the CSRFMiddleware class.
Bug Description
The Double Submit Cookie CSRF protection implemented in
backend/csrf.pyis vulnerable to Token Injection. The middleware verifies that thecsrftokencookie matches theX-CSRF-Tokenheader usingsecrets.compare_digest(cookie_token, header_token). However, it does not validate the minimum length or format of the tokens. An attacker can set a short, known token (e.g.,"a") in the user'scsrftokencookie (e.g., via a sub-domain XSS or MITM if secure flag is bypassed) and then send a cross-site request with the matching"a"in the header. The server will accept this as valid CSRF validation since"a" == "a".Steps to Reproduce
csrftokencookie value to"a".X-CSRF-Tokenrequest header value to"a".Expected Behavior
The CSRF middleware should enforce a strict length check on the tokens (e.g., exactly 64 hexadecimal characters) or use HMAC signatures to ensure the token was generated by the server.
Actual Behavior
The middleware accepts any matching token pair, regardless of length, allowing trivial CSRF token injection if an attacker can write a cookie to the victim's browser.
Screenshots / Error Logs
N/A
Environment
Additional Context
Advanced level security vulnerability. Occurs in
backend/csrf.pyinside theCSRFMiddlewareclass.