Access-Control-Allow-Origin: *lets JavaScript on any site read the response. Keep it for public resources such as web fonts or an open API, never on your pages.- WebSec0 takes 10 points off the headers score when the response to your home page carries
*. It sends noOriginheader, so it cannot see the other CORS mistakes. - For an API called with cookies, compare the
Originagainst a list of exact origins, send it back only when it matches, and addVary: Origin. - A global
app.use(cors())in Express sends*on every route. Scope it to the API and give it a list of origins.
What does Access-Control-Allow-Origin: * mean?
Access-Control-Allow-Origin tells the browser which other sites may read
a response with JavaScript. The value * allows every site. Without the
header, a script on another origin can still send the request, but the
browser hides the response from it.
Access-Control-Allow-Origin: *The wildcard is right for resources anyone may read: web fonts, an open API, public data. It is wrong on your pages and on anything that depends on who or where the visitor is. The fix is to send it only where it is needed, and to use a list of trusted origins for everything else.
When is * safe, and when is it not?
Browsers never send cookies to a response marked *: MDN states that using
the wildcard with credentials results in an error. So * does not expose
what a logged-in user sees. It exposes what the visitor’s browser can reach
without cookies, which is more than the public internet:
| Resource | * is |
|---|---|
| Web fonts, public images and scripts on a CDN | Fine: any site may use them |
| An open API without authentication | Fine, if every answer is public |
| Your HTML pages | Unnecessary: no other site needs to read them |
| Anything on an intranet, or behind an IP or VPN allowlist | A leak: any site the visitor opens can read it through their browser |
| An API that uses cookies or tokens | Broken: the browser blocks credentialed reads of * |
The intranet case is the one PortSwigger’s Web Security Academy describes: the attacker’s page uses the visitor’s browser as a proxy to internal addresses it cannot reach itself.
How WebSec0 grades Access-Control-Allow-Origin
Access-Control-Allow-Origin is not one of the six graded headers. WebSec0
reads it on the response to your home page, after following redirects on the
same host, and applies a penalty to the headers score:
| Value on the home page | Effect |
|---|---|
* |
−10, shown as “Permissive cross-origin policy” |
| A specific origin | Shown, no penalty |
| No header | Nothing |
With all six security headers passing, * on the home page takes the score
from 100 to 90: an A instead of an A+, which starts at 95. WebSec0 sends its
request without an Origin header. A server that copies any Origin back
into the response therefore shows nothing to the scanner, although that is
a worse mistake than * (see
CORS mistakes WebSec0 cannot see).
Where does the wildcard come from?
Check what your home page sends:
curl -sI https://example.com/ | grep -i "^access-control-allow-origin"A * on a page usually comes from a setting that applies to the whole site:
app.use(cors()) in an Express application, an add_header or
Header set line copied into the main server block, or a CORS option in the
hosting panel or CDN. Fix it where it is set. If you cannot change the
application, the server in front of it can remove the header (see below).
Fix CORS in the application
The application knows which routes other sites call, so that is the best
place for CORS. In Express, the cors
middleware with no options allows * on every route:
app.use(cors());In our test with Express 5.3 and cors 2.8, the home page, the API and the
answers to preflight requests all carried Access-Control-Allow-Origin: *.
Mount the middleware on the API only, with the list of origins that call it:
app.use('/api', cors({
origin: ['https://app.example.com', 'https://admin.example.com'],
credentials: true,
}));The home page then had no CORS header. On /api/, the middleware sent back
https://app.example.com and https://admin.example.com, and nothing to
https://app.example.com.evil.net or null, always with Vary: Origin.
It also answered the preflight OPTIONS requests. Other frameworks have the
same two settings: the routes CORS applies to, and a list of allowed origins.
Fix CORS at the server
These configurations keep * on public web fonts, remove the application’s
* from every other response, and answer the API with an allowlist. Each was
run behind an application that sent Access-Control-Allow-Origin: *
everywhere, and checked with curl on the home page, a 404, a font, and the
API with allowed, unknown, look-alike and null origins.
The application still answers preflight OPTIONS requests, which a browser
sends before a PUT, a JSON body or a custom header. If it does not, it must
start doing so, or the browser blocks those calls. Traefik’s middleware is the
exception: it answers preflights itself.
Nginx
A map compares the Origin with exact strings. add_header skips a header
whose value is empty, so origins that do not match get no CORS header at all:
map $http_origin $cors_origin {
default "";
"https://app.example.com" $http_origin;
"https://admin.example.com" $http_origin;
}
map $cors_origin $cors_credentials {
"" "";
default "true";
}
server {
# ...
include snippets/security-headers.conf;
proxy_hide_header Access-Control-Allow-Origin;
location / {
proxy_pass http://127.0.0.1:8080;
}
# Public web fonts: any site may read them
location /fonts/ {
root /var/www/example.com;
include snippets/security-headers.conf;
add_header Access-Control-Allow-Origin "*" always;
}
# API called with cookies from your own front ends
location /api/ {
proxy_pass http://127.0.0.1:8080;
include snippets/security-headers.conf;
add_header Access-Control-Allow-Origin $cors_origin always;
add_header Access-Control-Allow-Credentials $cors_credentials always;
add_header Vary Origin always;
}
}proxy_hide_header removes the application’s header. A location with its
own add_header drops the ones inherited from server, so each one includes
the security headers again; the
security headers hub for Nginx explains
why. On nginx 1.28, 1.30 and 1.31, the home page had no CORS header, fonts had
*, and the API answered only the two listed origins.
Apache
SetEnvIf tests the Origin with an anchored regular expression and stores
it. The headers that use it are only sent when it is set:
<VirtualHost *:443>
# ...
ProxyPass "/fonts/" "!"
ProxyPass "/" "http://127.0.0.1:8080/"
Header unset Access-Control-Allow-Origin
# Public web fonts: any site may read them
<Location "/fonts/">
Header always set Access-Control-Allow-Origin "*"
</Location>
# API called with cookies from your own front ends
SetEnvIf Origin "^(https://(app|admin)\.example\.com)$" CORS_ORIGIN=$1
<Location "/api/">
Header always set Access-Control-Allow-Origin "%{CORS_ORIGIN}e" env=CORS_ORIGIN
Header always set Access-Control-Allow-Credentials "true" env=CORS_ORIGIN
Header always merge Vary "Origin"
</Location>
</VirtualHost>ProxyPass "/fonts/" "!" serves the fonts from the DocumentRoot instead of
the application. On httpd 2.4.58, 2.4.62 and 2.4.69, the results matched
Nginx’s, including on the application’s 404 pages. Keep both ^ and $: the
regular expression is the allowlist.
Caddy
A named matcher with several header Origin lines matches any of the listed
values:
example.com {
# Public web fonts: any site may read them
handle /fonts/* {
root * /var/www/example.com
header Access-Control-Allow-Origin "*"
file_server
}
handle {
# API called with cookies from your own front ends
@cors_allowed {
path /api/*
header Origin https://app.example.com
header Origin https://admin.example.com
}
header @cors_allowed {
Access-Control-Allow-Origin {header.Origin}
Access-Control-Allow-Credentials true
}
header /api/* Vary Origin
reverse_proxy 127.0.0.1:8080 {
header_down -Access-Control-Allow-Origin
}
}
}header_down -Access-Control-Allow-Origin removes the application’s header;
without it, Caddy’s header lines and the application’s would both be sent.
On Caddy 2.9, 2.10 and 2.11, the results matched Nginx’s.
Traefik
The headers middleware has CORS options. In our test it did not remove the
application’s header: an unknown origin still received the application’s *.
Delete it in the same middleware with customResponseHeaders:
http:
middlewares:
no-cors:
headers:
customResponseHeaders:
Access-Control-Allow-Origin: ""
api-cors:
headers:
customResponseHeaders:
Access-Control-Allow-Origin: ""
accessControlAllowOriginList:
- "https://app.example.com"
- "https://admin.example.com"
accessControlAllowCredentials: true
accessControlAllowMethods: [GET, POST, PUT, DELETE]
accessControlAllowHeaders: [Content-Type]
accessControlMaxAge: 600
addVaryHeader: true
routers:
site:
rule: "Host(`example.com`)"
entryPoints: [websecure]
tls: {}
service: backend
middlewares: [no-cors]
fonts:
rule: "Host(`example.com`) && PathPrefix(`/fonts/`)"
entryPoints: [websecure]
tls: {}
service: backend
api:
rule: "Host(`example.com`) && PathPrefix(`/api/`)"
entryPoints: [websecure]
tls: {}
service: backend
middlewares: [api-cors]The longer rules of fonts and api take priority over site. fonts has
no middleware, so the application’s * passes through. List your security
headers middleware on all three routers. On Traefik 2.11, 3.6 and 3.7, the
API answered only the two listed origins, and Traefik answered the preflight
requests itself with the listed methods and headers. It sent
Access-Control-Allow-Credentials: true to every origin; without a matching
Access-Control-Allow-Origin, browsers still block the read.
CORS mistakes WebSec0 cannot see
WebSec0 checks only for * on the home page. These mistakes need an Origin
header to show up, and most of them are worse than *, because they combine
with cookies:
| Mistake | What happens | Fix |
|---|---|---|
Sending back any Origin, with credentials allowed |
Any site can read the logged-in user’s data, as PortSwigger’s examples show | Compare with a list of exact origins. |
An unanchored pattern such as ~example\.com |
In our test, Nginx accepted https://app.example.com.evil.net and https://evilexample.com; Traefik’s https://.*\.example\.com accepted the first |
Use exact strings, or anchor with ^ and $, as in ^https://[a-z0-9-]+\.example\.com$. |
null in the allowlist |
MDN notes that sandboxed documents and data: URLs have the null origin, and any site can create one |
Never allow null. |
No Vary: Origin with a per-origin value |
A cache can serve one origin’s header to another | Add Vary: Origin wherever the value depends on the request. |
| Several origins in one header | The header holds a single origin; the browser rejects a list | Choose the one that matches the request. |
* with credentials |
The browser blocks the response | Send the exact origin instead. |
| Treating CORS as access control | The request still reaches the server and runs; only the read is blocked | Keep authentication and CSRF protection. |
Verify it
Check the home page, then the API with an allowed origin and with one that is not. Use your own URLs:
curl -sI https://example.com/ | grep -i "^access-control"
curl -sI -H "Origin: https://app.example.com" https://example.com/api/me | grep -iE "^(access-control|vary)"
curl -sI -H "Origin: https://attacker.example" https://example.com/api/me | grep -iE "^(access-control|vary)"The home page should print nothing. The allowed origin should come back in
access-control-allow-origin, with vary: Origin. The last request should
print no access-control-allow-origin line: if it shows * or
https://attacker.example, any site can read the API. Then scan the site with
WebSec0; the headers report lists Access-Control-Allow-Origin under the
additional headers.
Questions
Frequently asked questions
Is Access-Control-Allow-Origin: * a security risk?
Not for public data that anyone could download anyway. It is a risk on pages and APIs whose content depends on where the request comes from, such as an intranet or an IP allowlist: any site can read them through the visitor's browser.
Can I use Access-Control-Allow-Origin: * with cookies?
No. MDN states that using the wildcard with credentials results in an error: the browser blocks the response. To send cookies cross-origin, return the exact origin and Access-Control-Allow-Credentials: true.
How do I allow several origins?
The header holds a single origin. Compare the request's Origin against your list on the server, send back the matching origin, and add Vary: Origin so caches keep one copy per origin.
Why does WebSec0 flag Access-Control-Allow-Origin on my home page?
It takes 10 points off when the response to your home page carries Access-Control-Allow-Origin: *. A page rarely needs to be read by other sites, so the wildcard there usually comes from a global setting that covers the whole site.
Does WebSec0 detect a server that reflects any origin?
No. WebSec0 sends no Origin header, so a server that echoes it back shows no Access-Control-Allow-Origin to the scanner. Test it yourself with curl -H "Origin: https://attacker.example".
Does CORS block requests from other sites?
No. CORS decides whether the browser lets the calling page read the response. The request still reaches your server, so CORS does not replace authentication or CSRF protection.
References
Sources
- Access-Control-Allow-Origin headerMDN Web Docs
- Cross-Origin Resource Sharing (CORS)MDN Web Docs
- Access-Control-Allow-Credentials headerMDN Web Docs
- What is CORS (cross-origin resource sharing)?PortSwigger Web Security Academy
- Module ngx_http_map_modulenginx.org
- Apache Module mod_setenvifApache HTTP Server Project
- Traefik Proxy: Headers middlewareTraefik Labs
