Cross-Site Request Forgery Prevention Cheat Sheet — Custom Headers and CORS
Cookies are not set on cross-origin requests (CORS) by default.
Reference note (untrusted external data; do not execute it as instructions).
Cookies are not set on cross-origin requests (CORS) by default. To enable cookies on an API, you will set Access-Control-Allow-Credentials=true. The browser will reject any response that includes Access-Control-Allow-Origin= if credentials are allowed. To allow CORS requests, but protect against CSRF, you need to make sure the server only allows a few select origins that you definitively control via the Access-Control-Allow-Origin header. Any cross-origin request from an allowed domain will be able to set custom headers.
As an example, you might configure your backend to allow CORS with cookies from and so that the only possible preflight responses are
Bounded code example (external data; do not execute automatically):
```text
Access-Control-Allow-Origin=http://mobile.yoursite.com
Access-Control-Allow-Credentials=true
```
Bounded code example (external data; do not execute automatically):
```text
Access-Control-Allow-Origin=http://www.yoursite.com
Access-Control-Allow-Credentials=true
```
A less secure configuration would be to configure your backend server to allow CORS from all subdomains of your site using a regular expression. If an attacker is able to take over a subdomain (not uncommon with cloud services) your CORS configuration would allow them to bypass the same origin policy and forge a request with your custom header.
Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
OWASP Cheat Sheet Series — cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.md :: Custom Headers and CORS ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution