Authorization disclaimer. This walkthrough is limited to the intentionally vulnerable PortSwigger Web Security Academy lab linked below. Test only systems you own or are explicitly authorized to assess.
Overview
The email-change handler accepts a csrf form token only when it matches the non-session csrfKey cookie. The search endpoint reflects its query into Set-Cookie: LastSearchTerm=..., making CRLF cookie injection the bridge that lets an attacker install their own matching csrfKey in the victim’s browser.

Lab objective/setup
This intentionally vulnerable Academy exercise isolates csrf where token is tied to non-session cookie. The supplied accounts and endpoints are used to reproduce only the source note evidence for csrf where token is tied to non-session cookie; all request changes remain inside this Academy instance.
What I observed
The captured email-change request contains both a csrfKey cookie and a separate csrf body parameter. A search for xyz returns Set-Cookie: LastSearchTerm=xyz; inserting CRLF into that search value adds a second Set-Cookie header, allowing the response to write an attacker-chosen csrfKey.


Why it is vulnerable
The validation binds csrf to csrfKey, but csrfKey is not the authentication session and the search response lets an attacker overwrite it. The comparison therefore proves only that two attacker-supplied values correspond, not that the victim initiated the request.
Walkthrough in the authorized PortSwigger lab
- Sign in to the attacker account, capture
POST /my-account/change-email, and retain that account’s validcsrfKeycookie and matchingcsrftoken. - Request
/?search=xyzand confirm that the response setsLastSearchTerm=xyz. Then use%0d%0aSet-Cookie:in the search value to verify that this lab response can set an additional cookie. - On the exploit server, load the search endpoint with a CRLF payload that sets
csrfKeyto the attacker’s captured key in the victim browser. - After that request errors as an image, submit the cross-site email-change form with the attacker’s matching
csrftoken. The victim contributes only their authenticated session cookie; the attacker-controlledcsrfKeyandcsrfpair satisfies the flawed validation.
1 | <html> |



Verification
The final capture shows the Academy marking the csrf where token is tied to non-session cookie exercise as solved after the documented request sequence. No testing was performed outside the isolated lab.

Impact and defensive lesson
The evidence demonstrates the concrete risk of csrf where token is tied to non-session cookie: a server-side trust decision can be bypassed through the exact input or workflow shown in this lab. For csrf where token is tied to non-session cookie, the server must enforce the exact invariant violated in the captured sequence and regression-test that mutation alongside the normal path.