DEV Community

Discussion on: How CORS (Cross-Origin Resource Sharing) Works?

Collapse
 
artis3n profile image
Ari Kalfus

Maybe add a note to the article about how dangerous origin: "*" is outside of an example snippet?

Collapse
 
dipakkr profile image
Deepak Kumar

Hi Ari,

Thanks for your suggestion. Sure, I will add that I know it's a dangerous practice for writing origin:* in production. I just meant to describe how it works.

Really appreciate your reply !

Collapse
 
colombaseppe profile image
ColombaSeppe

Could you please explain how to fix this and why this is dangerous?

Thread Thread
 
sengz profile image
Seng

If you’re allowing origin:* in production, you’re literally allowing any script from any domain to call your server. Which for some malicious user’s intention, they may write some malicious script and hijack your system’s data security.

The way to fix it is simple, just list down the domain that you would like to whitelist instead of using the * wildcard. If you listed down the domains instead of using the wildcard, your server will only allow CORS for the whitelisted domains while reject others that is not in the list.

Hope it helps. :)

Thread Thread
 
artis3n profile image
Ari Kalfus

Seng is right. I'll add that Access-Control-Allow-Origin is only allowed to be a * for unauthenticated requests. The browser will ignore Access-Control-Allow-Credentials: true if your allow-origins header is set to a wildcard. However, this leaves you open to distributed brute force login attacks. The right thing to do would be to check the Referer/Origin header against a known whitelist of sites you allow to access, and set your Access-Control-Allow-Origin header accordingly.