You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can specify the full list of domains and subdomains allowed in your application:
34
+
You can specify the full list of domains and subdomains allowed in your application.
35
+
As a convenience method, you can use the `*` character as a wildcard.
35
36
36
37
```js
37
38
origins: [
38
39
'http://myapp.com',
39
-
'http://*.myapp.com'
40
+
'http://*.myotherapp.com'
40
41
]
41
42
```
42
43
43
-
For added security, this middleware sets `Access-Control-Allow-Origin` to the origin that matched, not the configured wildcard.
44
-
This means callers won't know about other domains that are supported.
44
+
The `Access-Control-Allow-Origin` header will be set to the actual origin that matched, on a per-request basis. The person making the request will not know about the full configuration, like other allowed domains or any wildcards in use.
45
45
46
-
Setting `origins: ['*']` is also valid, although it comes with obvious security implications. Note that it will still return a customised response (matching Origin), so any caching layer (reverse proxy or CDN) will grow in size accordingly.
46
+
The main side-effect is that every response will include `Vary: Origin`, since the response headers depend on the origin. This is the safest setup, but will decrease your cache hit-rate / increase your cache size with every origin.
47
47
48
-
## Troubleshooting
48
+
## Open CORS setup
49
49
50
-
As per the spec, requests without an `Origin` will not receive any headers. Requests with a matching `Origin` will receive the appropriate response headers. Always be careful that any reverse proxies (e.g. Varnish) very their cache depending on the origin, so you don't serve CORS headers to the wrong request.
50
+
Using `origins: ['*']` is also a valid setup, which comes with obvious security implications. This means **any** domain will be able to query your API. However it does have performance benefits. When using `['*']`, the middleware always responds with `Access-Control-Allow-Origin: *` which means responses can be cached regardless of origins.
51
+
52
+
Each API should weigh the security and performance angles before choosing this approach.
0 commit comments