How to protect state-changing operations from CSRF with cookie sessions
If your web application uses cookie sessions, protecting state-changing operations takes more than configuring SameSite. For applications that maintain server-side state, OWASP recommends the synchronizer token pattern: the server compares the request’s CSRF token with the value stored in the user’s session. We’ll cover where to start, the supporting role of cookie attributes, and how to verify the result using an entirely fictional example. The main verification criterion is the actual data state after an action, not just the HTTP response.
Identify the operations that need protection
Start by listing the actions that change product data. For each action, record its handler, how the interface invokes it, and the expected change. State-changing operations should not use GET. This inventory helps you check protection coverage: a CSRF mechanism in one form does not tell you whether other state-changing actions are protected.
In the entirely fictional service ‘Workshop Planner,’ users can change their project name and notification settings. These are our training operations; all accounts and records belong to the testing team. For each operation, we’ll record the initial and target values in advance so we can distinguish an accepted change from a rejected request.
Choose built-in server-side protection
Before developing your own mechanism, check the framework’s or platform’s built-in CSRF protection. Review its configuration and compare its coverage against your list of state-changing operations. The goal is to establish which handlers actually perform the check, including actions submitted through standard forms and AJAX requests.
For an application that maintains server-side state, the recommended approach is the synchronizer token pattern. The server checks that the request contains a valid token and compares it with the value in the user’s session. If the token is missing or does not match, the request must be rejected. The trust boundary here is the server-side check: a state-changing action must pass token validation even when the request uses an active cookie session.
Send the token separately from the session cookie
The CSRF token must be unique to the session, secret, and unpredictable. It is a separate value supplied in the server’s response, not the session cookie itself. A form can submit it in a hidden field, while an AJAX request can send it in a separate header. In both cases, the server must compare the submitted value with the token in the user’s session.
Do not include CSRF tokens in URLs or server logs. During implementation, check logging settings as well as the form or client request. The test report only needs to record the scenario, validation result, and state of the training record; it does not need to contain the secret token value.
Use SameSite as an additional measure
SameSite controls whether cookies are sent with cross-site requests and provides some protection against CSRF. In the application discussed here, use it as an additional measure alongside server-side token validation. Acceptance checks must verify token validation separately: checking cookie settings alone is insufficient to establish the intended result.
SameSite=None allows cookies to be sent with both cross-site and same-site requests; this value requires Secure. The cookie session should use HTTPS throughout its lifetime, not just during login. Include these settings in your configuration review while keeping separate criteria for rejecting requests without a valid CSRF token.
Test the normal action and two rejection cases
Run these checks only in your own test environment, using your own accounts and fictional records. In ‘Workshop Planner,’ we’ll give the project the initial name ‘Training Mockup’ and use ‘Training Mockup — Complete’ as the target. Before each scenario, restore the initial state and keep an active session for the same test user. This keeps the check focused on CSRF within that user’s session.
For each scenario, record the server response, then read the stored project name separately. A successful HTTP response alone does not prove that the change was accepted; a rejection message alone does not confirm that the original data was preserved.
- Normal request with a valid token from the current session: the action is accepted, and the stored name matches the target.
- The same state-changing request without a CSRF token: the server rejects the action, and the stored name remains unchanged.
- The same request with a deliberately incorrect test token value: the server rejects the action, and the stored name remains unchanged.
- Repeat all three scenarios for every operation in the inventory, including changes to notification settings.
Set priorities and define the limits of verification
The first priority is to ensure server-side token validation for every listed state-changing operation and prevent state changes through GET. Then check the token’s properties and transmission, its absence from URLs and logs, and the SameSite and HTTPS settings. A non-public REST API also requires access checks at every endpoint. Assess those results separately: permission checks do not establish that a request without a CSRF token is rejected.
The result should be an inventory of operations with recorded outcomes for all three scenarios and verification of the stored data. This report establishes how the tested handlers behaved in the selected environment, but it does not guarantee the security of the entire product. If you need help reviewing protection coverage and acceptance criteria, you can start with a free audit of your website or the web portion of your product.