CSRF + CORS
Why both, when each matters.
CSRF and CORS get discussed together, confused constantly, and solve different problems in opposite directions. Getting them straight is most of the work.
They point opposite ways
CORS decides whether a browser lets ANOTHER SITE'S JAVASCRIPTread YOUR response. A browser rule, enforced in the browser,protecting the other site's user from your data.CSRF stops another site causing a WRITE to your API using youruser's cookies. A server rule, enforced by you, protectingyour user from the other site.
The sentence that resolves most confusion: CORS does not stop the request being sent. A cross-site form post reaches your handler whatever your CORS policy says. CORS only governs whether the attacking page is allowed to read the answer, and for a write that does damage, the attacker does not care about the answer.
Why cookies make CSRF your problem
Grit puts tokens in HttpOnly cookies, which removes an XSS risk and creates this one. A cookie is attached by the browser to any request to your domain, including one triggered by a page the user merely visited:
<!-- The user is signed in to your app in another tab. --><form action="https://yourapp.com/api/v1/account/email" method="POST"><input name="email" value="attacker@evil.example"></form><script>document.forms[0].submit()</script>
No JavaScript of yours runs, no token is stolen, and the browser sends the session cookie because that is what cookies do. If the endpoint acts on it, the account is gone.
The defence, and why it works
Grit's middleware.CSRF() sets a grit_csrf cookie on safe requests, and requires unsafe methods to echo it back in an X-CSRF-Token header:
cookieRoutes := r.Group("/")cookieRoutes.Use(middleware.CSRF())cookieRoutes.POST("/api/auth/oauth/google/callback", h.OAuthCallback)
The reason this works is worth saying out loud, because it is not that the value is secret. The attacking page can guess, but it cannot read your cookie to copy it into a header, since reading across origins is what the same-origin policy forbids. And a plain HTML form cannot set a custom header at all. So requiring a header that must match a cookie is a requirement only a page on your own origin can satisfy.
SameSite=Strict on the cookie is the second layer: the browser declines to send it on a cross-site request in the first place. Two independent mechanisms, because SameSite handling has varied across browsers and is not something to rely on alone.
The CORS failure nobody can see
This one costs more hours than the attacks do. A request from an origin outside your allow list gets a preflight with no Access-Control-Allow-Origin, which is the correct answer and a completely invisible one:
the browser never sends the real request-> no handler runs-> nothing is logged-> no status code anywhere-> the screen shows a button that does nothing
Moving the admin panel to a different port is enough to produce it. Grit logs a line, once per origin, when it turns one away, which converts a silent dead button into something you can grep for. The fix is CORS_ORIGINS in .env.
Never answer with Access-Control-Allow-Origin: * on an API that uses cookies. The browser refuses to combine the wildcard with credentials, so it will not work, and reaching for it is usually a sign somebody is about to echo back whatever Origin arrived, which is the same as having no policy.
Try it
One. Save an HTML file to your desktop with a form that POSTs to one of your authenticated endpoints. Open it in the browser where you are signed in and submit it.
Two. Note whether the request reached your handler, and what the response was.
Three. Now start your web app on a port that is not in CORS_ORIGINS and try to sign in. Watch the network tab, then your API log, and write down what each one tells you.
Quick check
Spot a typo? Have an idea?
Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.
Suggest an improvement on GitHub