Repository navigation
Enforce CSRF for cookie-authenticated API requests. Closes #1705 - #1778
Conversation
|
@regulartim Plz take a look! |
|
Hello @regulartim I know you will be busy, if you have time, plz take a look at it! |
regulartim
left a comment
There was a problem hiding this comment.
Hey @saisiddi , good work! 👍
DashboardConfigView had SessionAuthentication as a second class, but it never ran because the cookie class always succeeds first. I removed it. This only affects a superuser logged in only through a Django session (no token cookie) calling this API directly, which the frontend never does.
Should be fine! :)
The include("certego_saas.apps.auth.urls") in authentication/urls.py is now fully shadowed, since login and logout both have local routes. I left it to keep this PR small. Should I remove it here or in a follow-up?
I think the question here is a different one: do we need the new LogoutView / APIAccessTokenView subclasses and the logout route? Durin's views should pic up DEFAULT_AUTHENTICATION_CLASSES, which you already changed. Then, of course, we still need certego_saas.apps.auth.urls. Can you please check?
|
You're right, durin's views don't set |
regulartim
left a comment
There was a problem hiding this comment.
Thanks @saisiddi ! :)
I found another issue while testing: normal, non-superuser users get a 403 on their first unsafe request after logging in. I created such a user, logged in at the frontend, clicked "log out" and immediately got a toast that it failed (forgot what the toast said). My guess is that that it does not receive an CSRF cookie. Can you check and add a test for non-superusers?
Maybe a @ensure_csrf_cookie decorator on render_reactapp in greedybear/urls.py could resolve the issue?
7992f3e to
45f8ca6
Compare
|
Good catch, thanks! @regulartim The cause: on first load the user is anonymous, so check_authentication returns 401 before get_token() runs. At login, only superusers get a csrftoken cookie, through Django's login() -> rotate_token(). Normal users had no cookie until the 403 interceptor triggered another check_authentication, which is why only the first unsafe request failed. My tests missed it because they only used a superuser. Fix:
Added tests with a non-superuser: the login response sets the cookie, logout with the token returns 204, and without it 403. I also checked it manually through nginx with a normal user: csrftoken is set on the first page load, and logout works. Rebased on develop and resolved the conflicts with QueryParamValidationMixin. |
regulartim
left a comment
There was a problem hiding this comment.
Hey @saisiddi ! Good job, thank you! :)
Description
Cookie-authenticated API requests had no CSRF protection, only
SameSite=Stricton the auth cookie (see #1705). This PR adds a CSRF check for those requests, as discussed in the issue.CSRFProtectedCookieTokenAuthenticationinapi/authentication.py. It runs Django's CSRF check only when the token came from the cookie and the method is not safe (POST/PUT/PATCH/DELETE). Clients usingAuthorization: Token(sensors, scripts) are not affected.CookieTokenAuthenticationin the DRF default and in every view that set it explicitly.LogoutViewandAPIAccessTokenViewcome from durin, so I added small local subclasses for them.check_authenticationnow callsget_token(), so the frontend gets thecsrftokencookie on page load.CSRF_COOKIE_HTTPONLYis nowFalse(option 1 from the issue), so axios can read the cookie. Django's docs sayHttpOnlyon this cookie gives no real protection, since XSS can already make same-origin requests.xsrfCookieName/xsrfHeaderName, so no interceptor was needed.Two things to check:
DashboardConfigViewhadSessionAuthenticationas a second class, but it never ran because the cookie class always succeeds first. I removed it. This only affects a superuser logged in only through a Django session (no token cookie) calling this API directly, which the frontend never does.include("certego_saas.apps.auth.urls")inauthentication/urls.pyis now fully shadowed, since login and logout both have local routes. I left it to keep this PR small. Should I remove it here or in a follow-up?Testing
I tested it through nginx (
http://localhost), not only the dev server:csrftokencookie set/dashboarddirectly and saves the configX-CSRFTokenX-CSRFTokenCSRF Failed: CSRF token missing.Authorization: TokenI also added
tests/authentication/test_csrf_cookie_auth.py(5 tests, usingAPIClient(enforce_csrf_checks=True)and a real auth cookie, since the existing tests useforce_authenticateand never run the authentication class).Related issues
Closes #1705
Follows from Discussion #1693
Type of change
Checklist
Formalities
<feature name>. Closes #999develop.develop.Docs and tests
Ruff) gave 0 errors.GUI changes
No visible UI changes. Only the axios defaults in
index.jsxchanged; frontend tests pass.