CORS and Credentialed Cross-Origin Access
Compare two websites requesting a private API response and follow the browser’s decision about which one may read it.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A payroll API correctly returns only Mia's records. Her trusted payroll website may read them. Should a different website open in the same browser also be allowed to read the response?
CORS is the browser's response-sharing mechanism. It is separate from the server's check that the records belong to Mia.
For the examples with credentials, we explicitly assume the browser is allowed to send the API session cookie. CORS permission does not override cookie-delivery rules.
| Requesting website | Intended browser result |
|---|---|
| The approved payroll frontend | May read the authorized response |
| An untrusted website | Must not read the private response |
We'll follow the request, the server's headers, and the browser's decision as three separate steps.
Separate who the server serves from who the browser lets read
Same-origin policy restricts one origin's script from reading many other origins' responses. CORS is an explicit response-sharing mechanism. An endpoint still needs authentication, object authorization, and action-intent defenses. CORS configuration decides whether another web origin may consume a response in a browser context.
// Teaching page on an untrusted local fixture origin.
const response = await fetch('https://payroll.example.test/api/me/payroll', {
credentials: 'include',
});
const data = await response.json();
// Whether this line can read data depends on CORS and credential eligibility.
The credentials option requests inclusion according to browser policy; it does not override SameSite, Secure, third-party cookie restrictions, or unavailable authentication state. A raw HTTP client showing the response is not proof that the page can read it. Observe the actual browser request and script result under stated conditions.
Trace credential eligibility and the final sharing decision.
Send. The browser applies cookie and privacy policy before the authenticated request exists.
fetch(api, { credentials: "include" });
Serve. The server can correctly return only the signed-in employee’s data.
return payroll.forEmployee(actor.userId);
Expose. The final ACAO and ACAC decide whether that origin’s script can read the private result.
Access-Control-Allow-Origin: https://untrusted.example.test
Access-Control-Allow-Credentials: true
Review question: A correct user-scoped query does not authorize every website as a response reader.