A browser can attach an application's session cookie to a request the user did not intend to authorize. If the server treats possession of that cookie as sufficient evidence for a mutation, another site may be able to trigger an action under the user's identity. Authentication and request intent need separate checks.
This guide covers browser endpoints authenticated through cookies. It focuses on state-changing actions, cookie scope, authentication transitions, and failure testing. Authorization still determines which resources the authenticated user may change; CSRF defenses do not replace that decision.
Inventory every action that relies on ambient cookies
List routes that create, update, delete, sign in, sign out, or alter account settings. Include older compatibility routes and alternate body formats. A protected JSON endpoint is insufficient if an older form endpoint reaches the same mutation without the protection.
Keep read operations free of intentional side effects. A route called through GET should not remove a resource or change account state merely because the interface labels its link as a button. Changing the visible control does not change how a browser can invoke the route.
In a synthetic account application, updating a display name, adding a payment destination, and terminating a session all require an explicit request-intent policy. The risk differs by action, but none should rely solely on the assumption that requests originate from the application's own page.
Record where the session cookie is sent and which endpoints accept it. Cross-origin APIs, subdomains, embedded pages, and external identity flows can expand that boundary. Document intentional exceptions instead of allowing them to emerge from inconsistent middleware placement.
Enforce request intent at the server
OWASP's CSRF guidance describes synchronizer tokens and other defenses, and emphasizes that XSS can defeat CSRF protections. Use the framework's maintained mechanism where it fits the application rather than inventing a loosely validated token format.
For a stateful session, generate the token server-side, bind it to that session, and validate it on the protected request before applying the mutation. Deliver it through an appropriate response or form mechanism. Avoid putting it in a URL, where it can appear in navigation records and logs.
Reject missing, malformed, expired, or mismatched tokens with a stable public error and no side effect. A frontend check improves usability, but the authoritative decision belongs to the server. Keep token validation and authorization in the same protected request path.
Origin checks or Fetch Metadata can provide additional request context when implemented against an explicit trusted-origin policy. Do not use a broad hostname substring test. Define how the application handles missing metadata and supported non-browser clients rather than silently allowing every exceptional case.
Scope cookies without confusing site and origin
Use HTTPS and set cookie attributes according to the session's purpose. Secure limits ordinary transmission to secure contexts, and HttpOnly prevents ordinary script access to the cookie. Neither means that the browser stops attaching the cookie to requests.
The Set-Cookie reference documents SameSite behavior and cookie prefixes. A __Host- cookie requires Secure, Path=/, and no Domain attribute in supporting browsers. That can help constrain cookie scope when it matches the deployment.
SameSite concerns the site's boundary, which differs from an exact origin. Related subdomains can be same-site while remaining different origins. Review whether all of those subdomains deserve the same trust, especially if some host user-controlled or independently maintained content.
Choose a SameSite policy that supports required navigation and identity flows, then test those flows. Do not assume one setting proves every state-changing route is protected. If cross-site use is intentional, the request-intent mechanism becomes particularly important and must remain active on that path.
Handle authentication transitions and multi-tab behavior
Protect sign-in against unintended session establishment as well as protecting actions after sign-in. In a synthetic failure case, an attacker-selected account is established in the victim's browser, and the victim later adds private data without noticing the identity change.
Bind request-intent checks to the correct pre-authentication or authenticated context. Rotate the session when the authentication state changes according to the framework's session-fixation protections. Reissue associated CSRF state consistently so that an old context cannot authorize a new session's mutation.
Define how tokens behave across multiple tabs and after session expiry. A per-request token may require careful handling of back navigation and concurrent forms. A session-bound token has different replay characteristics. Choose the model deliberately and keep recovery understandable to the user.
If a request fails because the session or token expired, refresh the authenticated context and invite the user to retry. Do not automatically repeat a sensitive action unless its outcome and retry safety are established. Preserve form input without retaining confidential fields in unsafe browser storage.
Test through browser behavior and inspect side effects
Use a concrete protected operation such as changing a synthetic account's display name. Begin with the original name and a known account version. Exercise a valid session with a valid session-bound CSRF token, then repeat the request with the token absent, with a token from another session, and after the originating session has expired. The valid case should produce the intended mutation. Each denied case should preserve the name and version and create no downstream notification.
Next send the same mutation through every supported content type and route alias. Protecting the JSON route is insufficient if a form-compatible route reaches the same update service before request-intent validation. Test that the control executes before the mutation, not merely that middleware eventually returns a rejection response.
For an origin-based supplemental check, include a trusted application origin, an untrusted sibling subdomain, and a missing header under the documented non-browser-client policy. Same-site subdomains can still be different origins; treating every sibling as trusted requires an ownership decision, not a convenient substring match. The credentialed CORS guide explains why denying browser response access cannot substitute for these mutation checks.
Verify that valid requests succeed and missing or mismatched tokens fail before changing state. Use separate synthetic accounts and assert that a token from one session cannot authorize another. Test each supported request format and each route reaching the same action.
Exercise cross-site navigation, form submissions, same-site subdomain requests, session rotation, sign-out, and multiple tabs in supported browsers. Include the deployment's actual redirect and proxy configuration. Calling a handler directly does not reproduce every browser cookie and origin decision.
Inspect database and audit outcomes after denied requests. A rejection status is insufficient if the mutation occurred before validation. Keep logged errors free of cookie values and tokens, following the guide to safe application logs.
Review CSRF, XSS, and authorization as separate boundaries in the API test matrix. The useful evidence is that an authenticated session alone cannot authorize an unintended mutation, while legitimate user actions retain a clear path through token expiry, navigation, and identity changes.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit