5

CORS is such a mess. What are current best practices?

 1 year ago
source link: https://lobste.rs/s/2qr2nf/cors_is_such_mess_what_are_current_best
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

CORS is such a mess. What are current best practices?

I’m looking into implementing CORS (again, it seems like this is something that comes up every few years, and every few years I have to re-orient myself about how it all works), and as always it’s so confusing. (Here I’m talking about Access-Control-Allow-Origin type stuff, primarily, as CORS was initially a structured way to relax the same-origin policy on requests. I’m not as familiar or concerned with some of the newer headers for mitigating Spectre-type attacks. Should I be?)

Any CORS experts out there with “best practice” recommendations? The security and threat model is so counterintuitive.

Is the whole point of the CORS model basically to handle the browser’s decision to send cookies on every request? If the browser just refused to send cookies by default on non-same-origin requests and prompted the user to “Allow Once” or “Allow Always” like it does for saving passwords, wouldn’t that also solve the problem (and not to mention CSRF as well, which CORS doesn’t address).

The server needs to handle arbitrary traffic from arbitrary clients, so resources should be protected appropriately. The only thing particularly unique about the browser is that it chooses to send cookie credentials, possibly against the user’s intentions.

With all that in mind, it seems like these are maybe best practices (somewhat counterintuitively):

  • When possible always set Access-Control-Allow-Origin: *. Everywhere online seems to recommend not including the header, if it’s not necessary, or being as specific as possible with the origins you allow and validating against a regex or an allowlist. But, since ACAO * does not allow credentials, then that’s actually safer, right? And if your backend has to expect traffic from, say, curl, or whatever, then you might as well acknowledge that fact fundamentally and say arbitrary JS scripts out there can also hit the endpoint (as long as, similarly to curl, they don’t include a cookie). Is there a downside to this approach?

  • Access-Control-Allow-Credentials: true - this is the truly dangerous one, since the whole threat model of CORS is about a malicious website sending an authenticated request to your server without the user’s consent. So in this case, you do need to carefully set ACAO to specifically the origin that your own real site is at.

  • What should you do about CORP, COEP, etc - all the new headers?


Recommend

  • 127
    • 微信 mp.weixin.qq.com 6 years ago
    • Cache

    [译] 理解 CORS

  • 195

    Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to let a user agent gain permission to access selected resources from a server on a different origin (domain)…

  • 114
    • beckjin.com 6 years ago
    • Cache

    .NET Core API CORS

    最近参与一个前后端分离的项目,后端基于 .NET Core 2.1 开发,在进行前后端对接的过程中,被跨域问题折腾得有点脾气了,这里把经验和大家分享一下。 GET/POST 请求 在服务端不做任何调整的情况下,前端发起 AJAX 请...

  • 82
    • www.tuicool.com 5 years ago
    • Cache

    Do You Really Know CORS?

    Cross-Origin Resource Sharing No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin http://www.sesam...

  • 81
    • 掘金 juejin.im 5 years ago
    • Cache

    跨域资源共享——CORS

    跨域资源共享(Cross-Origin Resource Sharing)是一种机制,它使用额外的 HTTP 头部告诉浏览器可以让一个web应用进行跨域资源请求。 请求类型 简单请求 若一个请求同时满足下述所有条件,则该请求可视为“简单请求”(注:灰色字体内容

  • 133
    • www.tuicool.com 5 years ago
    • Cache

    Enable CORS using Fiddler

    An easier way to point localhost to testing or production environments.

  • 12

    Table of ContentsThe error in your browser’s console#No ‘Access-Control-...

  • 5

    There's No Such Thing as a Best Price in BitcoinJanuary 12th 2022 new story6When you're investing, th...

  • 6

    Comments Login to leave a comment Matt B...

  • 3

    A Tasty Treats Podcast for...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK