Re:Why do we have both CSRF protection and CORS?
by bastawhiz on Sunday March 02, 2025 @06:17PM
(#43236333)
The csrf token is ideally tied to your session. If it's anonymous, Eve didn't need Alice to visit a page in the first place. If it's tied to a session, Eve can't create a working token for Alice.
[ Parent
| on HN ]
Re:Why do we have both CSRF protection and CORS?
by smagin on Monday March 03, 2025 @04:08AM
(#43239852)
yup. You didn't imply it but just in case -- this token shouldn't be the same as session token. Session tokens should be `HttpOnly`, so that we don't even expose it to javascript
Re:Why do we have both CSRF protection and CORS?
by blincoln on Monday March 03, 2025 @07:35AM
(#43241155)
The HttpOnly flag isn't really practical in modern web apps where so much logic runs in JS in the browser and makes requests to APIs. It's a leftover from an earlier era of web app architecture.
If it can be enabled without breaking something, sure, its a good idea, but unless your app is 2000s-era ASP.NET code or CGI script, preventing browser-side JS from accessing the session token will probably break something.
Re:Why do we have both CSRF protection and CORS?
by Macha on Monday March 03, 2025 @09:59AM
(#43242345)
Right, but if you're doing a SPA, your SPA makes the login call and stores a copy of the session token in local storage, which unlike a cookie isn't automatically sent on any request, never mind cross-origin ones. Doesn't prevent against XSS of course, but then that's what CSP is for.