Sooner or later every developer whose frontend and API live on different domains meets the red CORS error in the browser console. The quickest fix is to paste a couple of configuration lines from the internet, the error disappears and everyone is happy. The trouble is that this quick fix often opens the API to any website at all. Nothing looks broken, so the hole can stay unnoticed for months while user data is quietly exposed.
What CORS actually controls
Browsers enforce the same-origin policy: JavaScript on https://example.uz may send a request to https://api.example.uz but cannot read the response. CORS relaxes that rule. The server adds Access-Control-Allow-Origin to tell the browser which origins may read the response. So CORS does not protect your server; it tells the browser whom to trust. Non-simple requests, for example with an Authorization header or a JSON body, are preceded by an OPTIONS preflight, and a careless answer there undermines everything too.
The three most dangerous settings
The worst is reflecting the incoming Origin header unchecked while also sending Access-Control-Allow-Credentials: true. Any site, including an attacker's, can then call your API with the user's cookies and read the reply: name, phone number, order history. The second is sloppy matching, such as "allow if the origin ends with example.uz", which also admits https://notexample.uz; a "contains" check lets https://example.uz.misol.uz through. Compare against exact full values only. The third is allowing the null origin for local testing; an attacker can produce it from a sandboxed iframe, so it never belongs in production.
Browsers refuse the combination of * with credentials, and developers often switch to reflecting the origin precisely to get around that, creating the dangerous variant. A plain * is fine for public, unauthenticated data such as exchange rates or a public catalogue.
Example: a learning centre API
A Samarkand learning centre runs its student portal on example.uz and the API on api.example.uz. Tired of CORS errors, the developer reflected the origin and enabled credentials. Now if a logged-in student opens any other site, that site's script can call api.example.uz/me and read the student's personal data, payments and grades, while the server log shows perfectly normal requests.
The fix: an exact allow list
In Nginx a map block works well. For unknown origins the variable stays empty, and Nginx does not send a header with an empty value:
map $http_origin $cors_origin {
default "";
"https://example.uz" $http_origin;
"https://admin.example.uz" $http_origin;
}
server {
server_name api.example.uz;
location /api/ {
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Access-Control-Allow-Credentials "true" always;
add_header Vary Origin always;
}
}
The same logic in PHP, with strict comparison:
<?php
$allowed = ['https://example.uz', 'https://admin.example.uz'];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowed, true)) {
header("Access-Control-Allow-Origin: $origin");
header("Access-Control-Allow-Credentials: true");
}
header("Vary: Origin");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
http_response_code(204);
exit;
}
Vary: Origin tells caches that the response depends on the origin, so a reply prepared for one origin is not served to another.
Testing and the bigger picture
curl -s -I -H "Origin: https://boshqa-sayt.misol.uz" https://api.example.uz/api/me | grep -i access-control
A foreign origin should get no Access-Control-Allow-Origin header, an allowed one should get its own value back, and tricky inputs like null or https://example.uz.misol.uz must not be reflected. Remember that CORS only limits what browser scripts can read; it does not stop requests reaching the server and means nothing to curl or bots. You still need authentication and authorisation on every request, CSRF protection and SameSite cookies. On a sayt.uz VPS the Nginx config is yours to edit, and on shared hosting you control these headers from PHP or .htaccess.