Quick revision
The short answer
allowedOriginPatterns("*") with allowCredentials(true) makes the server echo back any origin and allow cookies, so any website a logged-in user visits can send authenticated requests with their cookies and read the responses. That is worse than CSRF, because the attacker gets the data, not just the action. The correct setup allows only known origins per environment from configuration, enables credentials only where cookies are really used, and is checked in security reviews with tests that unknown origins are rejected.
What to remember
In this answer, we saw that allowedOriginPatterns("*") with allowCredentials(true) echoes any origin and lets every website make cookie-authenticated requests and read the responses, which is worse than CSRF because it exposes data. The correct setup lists known origins per environment from configuration, uses credentialed CORS only where cookies are needed, and is enforced through reviews, a starter check and tests that unknown origins are rejected.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
allowedOriginPatterns("*") with allowCredentials(true) makes the server echo back any origin and allow cookies, so any website a logged-in user visits can send authenticated requests with their cookies and read the responses. That is worse than CSRF, because the attacker gets the data, not just the action. The correct setup allows only known origins per environment from configuration, enables credentials only where cookies are really used, and is checked in security reviews with tests that unknown origins are rejected.
Why this question is asked
CORS errors are annoying, they block development, and a wildcard makes them disappear at once. That is why this configuration turns up so often, sometimes copied from an answer online and left in place for production. Interviewers ask the question because explaining the risk precisely, and replacing it with something workable, shows that we understand what CORS is protecting in the first place. In this answer, we will recap the role of credentials in CORS, see why this combination is the dangerous one, walk through an attack, and then build a correct, environment-aware configuration and the checks that keep it correct.
1A Quick Recap: What CORS Protects
The same-origin policy lets a page send requests to other origins but stops its JavaScript from reading the responses. CORS lets a server relax that rule for specific origins. When the request is credentialed, meaning the browser includes the user's cookies because the page used credentials: "include", the server must answer with Access-Control-Allow-Origin set to the exact requesting origin and Access-Control-Allow-Credentials: true. Browsers refuse the wildcard * for credentialed requests, precisely because allowing every site to read responses made with a user's cookies is dangerous.
allowedOriginPatterns("*") is the way around that browser rule. Instead of sending *, Spring takes whatever Origin the request carries and echoes it back as the allowed origin. The browser sees an exact match and lets the page read the response. So the combination does exactly what the browser rule was designed to prevent.
2The Attack, Step by Step
A user is logged in to https://app.example.com, which uses a session cookie for https://api.example.com. They visit a page on https://evil.example, perhaps from a link in an email. That page runs a script.
5 messages in order. Play them, or step through them one at a time.
Notice how this differs from CSRF. In a CSRF attack the attacker can make the browser perform an action but cannot read the result. Here they can do both: read the user's statements, profile and anything else the API returns, and also read a CSRF token from a response and then perform state-changing requests that CSRF protection would otherwise stop. One permissive CORS setting quietly undoes both the same-origin policy and the CSRF defence for every endpoint it covers.
3Why It Matters Even Behind Login
Teams sometimes argue that the endpoints require authentication, so the risk is small. But authentication is exactly what the attack borrows: the request carries the victim's own cookie and passes every authorization check as the victim. The only thing standing between evil.example and the data was the browser's refusal to let it read the response, and the configuration removed that. The more sensitive the API, the worse the setting is.
4A Correct Setup
The fix starts with an explicit list of the origins that really need access, per environment, read from configuration rather than hard-coded. Production allows the production front end; staging allows the staging front end; local development allows localhost on the port the developers use. Wildcard patterns, if used at all, are narrow, such as https://*.app.example.com for preview deployments, never a bare *.
| Setting | Before | After |
|---|---|---|
| Allowed origins | Any (echoed back) | Listed per environment |
| Credentials | All endpoints | Only cookie-based /bff paths |
| Bearer-token APIs | Credentialed CORS | No credentials, or no CORS |
| Unknown origin | Allowed | Rejected (403) |
What changed is that the server only trusts origins it knows, and only the paths that use cookies get credentialed CORS. A bearer-token API usually does not need allowCredentials at all: the front end adds the Authorization header itself, and without cookies an attacker's page has nothing of the user's to attach. Remember that cookie-authenticated paths also keep CSRF protection; CORS is not a replacement for it.
5Keeping It Correct
Because the bad configuration is so tempting during development, it needs a guard. Security reviews should include the CORS configuration explicitly, and the platform starter can refuse to start if allowCredentials is true together with a bare * pattern. An automated test in each service sends a preflight and a real request with Origin: https://evil.example and expects no Access-Control-Allow-Origin header, and a second test checks that the real front-end origin is allowed. A periodic scan of deployed services with an unknown origin catches anything that slipped through.
6Follow-up questions
Is allowedOrigins("*") without credentials safe?
Much safer, because the browser will not attach cookies for a wildcard response, so only public, unauthenticated data can be read. It is fine for truly public APIs, but still pointless for private ones that should only be used by our own front end.
Does this risk exist for mobile apps?
No. CORS only matters in browsers. A mobile app is not restricted by it, and it is also not a victim of it, because no other site's script runs with its credentials.
Why not reflect the origin only if it ends with example.com?
Hand-written suffix checks are easy to get wrong: evil-example.com or example.com.evil.net can slip past a naive check. A list of exact origins, or Spring's well-defined origin patterns, avoids that.
7Conclusion
In this answer, we saw that allowedOriginPatterns("*") with allowCredentials(true) echoes any origin and lets every website make cookie-authenticated requests and read the responses, which is worse than CSRF because it exposes data. The correct setup lists known origins per environment from configuration, uses credentialed CORS only where cookies are needed, and is enforced through reviews, a starter check and tests that unknown origins are rejected.
Open with
The risk is that this combination turns off the protection CORS exists to provide. Browsers do not allow a wildcard origin with credentials, but allowedOriginPatterns star works around that by echoing back whatever Origin the request has.
Then, if they want more
So if a logged-in user visits any website, that site's JavaScript can call our API with credentials include, the browser attaches the user's cookie, our server answers with Allow-Origin set to the attacker's origin and Allow-Credentials true, and the script reads the response.
That is worse than CSRF: with CSRF the attacker can trigger an action but cannot read the result, and here they can read the user's data, and even read a CSRF token and then perform actions. The fact that the endpoints require login does not help, because the attack uses the victim's own session.
To fix it, I would list the allowed origins per environment in configuration, the production front end in prod and localhost in dev, and never use a bare wildcard pattern. I would enable credentials only on the paths that actually use cookies, like a BFF, and for bearer-token APIs I would not use credentialed CORS at all.
Then I would keep it from coming back: CORS is a line item in security reviews, the starter refuses star with credentials, and each service has a test that sends Origin evil.example and expects no CORS headers.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- OWASP Top Ten
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A team set allowedOriginPatterns("*") with allowCredentials(true) on all endpoints to...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/a6b19cec9d/