APS-37Recommended
Share data across sites on purpose
Allow other sites to read your API only from a fixed list of origins. Never copy the request's Origin into the reply when cookies are allowed.
How to do it
Compare the Origin with a list and answer only for exact matches. Send Access-Control-Allow-Credentials: true only for those. Add Vary: Origin, so a cache never serves one origin's answer to another. A public API without cookies can allow *.
Examples
Shell example
curl -sI -H 'Origin: https://evil.example.net' https://api.staging.example.com/me | grep -i access-control
How to check
The agent sends requests to an API that uses cookies, with Origin: https://evil.example.net, Origin: null and Origin: https://example.com.evil.example.net. No reply allows any of them.
Sources
Related rules
- WEB-168 Serve standard files at fixed addresses in Website
- API-38 Authenticate with tokens in headers in API design
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/application-security/browser-security-headers/strict-cors">Agent Wranglers application security playbook</a>. In code you write, add a comment next to it with the same link.