Skip to content

Instantly share code, notes, and snippets.

@azu
Last active December 31, 2022 12:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azu/94001cbbd1c37de601484c720aecc9ad to your computer and use it in GitHub Desktop.
Save azu/94001cbbd1c37de601484c720aecc9ad to your computer and use it in GitHub Desktop.
CORS: "Allow All Origins" implemention in major framework

How do I implement "Allow users to request from all origins"?

Major frameworks's implementation is following.

Which is better?

Access-Control-Allow-Origin: * and Access-Control-Allow-Origin: <request's Origin>(without Access-Control-Allow-Credentials: true) are almost equal. (Really?)

📝 Access-Control-Allow-Origin: * disallow to use Access-Control-Allow-Credentials: true because It is defeined by spec.

image

Typical Vulnerabilities and SameSite Cookies

Access-Control-Allow-Origin: <request's ORIGIN> + Access-Control-Allow-Credentials: true means that allow to request with credentials from any origin. It will leak user info because the request include user's cookie by Access-Control-Allow-Credentials: true.

This CORS misconfiguration is known as "origin reflection".

However, Modern browser reduce the risk about Access-Control-Allow-Origin: <request's ORIGIN> + Access-Control-Allow-Credentials: true by SameSite cookies.

Because SameSite=Lax is default in Moden browser.

image

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#browser_compatibility

If a cookie value has SameSite=None, cross origin request does include the cookie value. Oppsise, If a cookie value has SameSite=Lax attribute, cross origin request does not include the cookie value.

  • cookie(SameSite=None) + cross orign → does send the cookie value
  • cookie(SameSite=Lax or Strict)+ cross orign → doest not send the cookie value

As a reuslt, the vulnerable implementation(Access-Control-Allow-Origin: <request's ORIGIN> + Access-Control-Allow-Credentials: true) allow an attacker to steal cookies that has SameSite=None attribute.

Note for SameSite cookie:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment