Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request to Support Dynamic Resource Validation #518

Open
gulachek opened this issue Oct 15, 2021 · 11 comments
Open

Request to Support Dynamic Resource Validation #518

gulachek opened this issue Oct 15, 2021 · 11 comments

Comments

@gulachek
Copy link

Transitioning a large dynamic web SPA to using CSP proves difficult. The initial page load needs to anticipate every possible resource that may need to be embedded and needs to list out all of them in the initial request. This is both problematic from a security standpoint (very many false positives that don't actually need to be allowed all the time), and it doesn't scale well, since there's a technical limitation to the maximum size HTTP headers can be, so enumerating every possible domain that's anticipated becomes infeasible.

I'm aware that it's questionable that a web page really needs all of this 3rd party content, but breaking currently supported configuration to load 3rd party links and content is also very undesirable. If there were a way for the server to indicate updates to the user agent's current CSP, or if there were a similar structure to report-uri where the server responded if a violation is okay to serve, this would make supporting these kind of security transitions vastly easier. This issue serves to document this need, and if what I'm currently describing is possible already, I'm happy to learn more.

Potential alternatives include proxying all 3rd party content through the server, but that seems to introduce additional challenges (including more security challenges, which CSP is trying to address in the first place) that could be solved with a seemingly simpler solution to the problem, such as "img-src: 'dynamic(uri)'" or something of that nature.

@annevk
Copy link
Member

annevk commented Oct 18, 2021

Did you attempt to start out with a report-only policy to learn what the policy needs to be and account for? That's typically how these transitions are done as I understand it.

@gulachek
Copy link
Author

gulachek commented Oct 18, 2021 via email

@mozfreddyb
Copy link
Contributor

If you have a high confidence in the scripts you're loading directly (e.g. through <script> elements that the parser sees), then you can use strict-dynamic which will allow subsequent scripts loads transitively. Wouldn't that help?

@gulachek
Copy link
Author

gulachek commented Oct 19, 2021 via email

@mozfreddyb
Copy link
Contributor

I'm surprised that images and iframes are of concern. In that case, I think you'll have to implement this yourself in a Service Worker (SW) (though there are caveats when using a ServiceWorker as a "security"-proxy, e.g., the SW's scope, shift+reload override etc.).

It's unlikely CSP will gain this kind of functionality.

@dveditz
Copy link
Member

dveditz commented Oct 19, 2021

Yes, it's just for scripts--which are what is adding the other content, right? Do you trust them or not?

Unfortunately CSP is not a good fit at limiting content when you don't know what that content is going to be ahead of time.

@gulachek
Copy link
Author

gulachek commented Oct 19, 2021 via email

@mozfreddyb
Copy link
Contributor

I'm curious what you're proposing with a service worker.

I ran a web hacking challenge to enforce Subresource Integrity for all resources using a ServiceWorker-in-the-middle. Unfortunately, my summary is a bit.. chaotic, but I think the included code snippets might still be useful.

@dveditz
Copy link
Member

dveditz commented Oct 20, 2021

I thought strict-dynamic doesn't allow a script to load whatever it wants if it's not another script.

strict-dynamic only controls script loading and doesn't otherwise restrict what scripts can do. Other parts of a CSP might, of course!

we do know ahead of time what content is going to be embedded. It's just not at the time of the initial page load, and I'm suggesting that a dynamic validation mechanism would make CSP more usable for existing web applications that want some security benefit.

How does a browser distinguish between a page legitimately changing its CSP dynamically, and injected content changing the CSP maliciously? A scripting interface was proposed at one point but we never came up with a mechanism that could be used safely. Embedding a security mechanism in the very content that mechanism considers a priori untrustworthy is far from trivial. We've dabbled with it by letting sites dynamically add CSP through <meta> tags, with the restriction that it can only make a CSP more strict, never less strict (and even with that there's potential for mischief).

But let me back up and ask what "security benefit" you're looking for. CSP tries to do many things. We, the creators of CSP, tend to think stopping XSS is the most important since that is one of the top vulnerability classes in web applications. But when you look at how CSP is actually deployed only a minority of CSP-using sites are configured to combat XSS.

Does W3C not support web frameworks that dynamically render [...] Do we not want those frameworks to transition to
the CSP standard to gain the standard security benefits?

Of course we want to. Are you accusing us of acting in bad faith? Please review our Code of Ethics and assume folks here are well-intentioned even when we disagree.

The different frameworks all work differently and will likely have to cooperate with browser controls in creating and enforcing policies that combat security problems as they affect their framework uniquely. Unfortunately we don't see many of their creators here working with us.

CSP currently assumes that every component is either in its own frame or is statically loaded to be included in the initial page load.

Not quite, but you're right that CSP was conceived in an era where most web content worked like that. In fact the two types of XSS CSP is most effective in addressing (reflected and stored XSS) are largely artifacts of that kind of web page. "Fixing" CSP to address the same set of issues in dynamic content may not be structurally possible. So setting CSP aside for a moment

  • what vulnerability scenarios are you trying to stop in dynamic content?
  • how do you expect to communicate the changing policy?

Here's a proposal off the top of my head that might get you thinking: a new <link> relation that declares a Security Policy Websocket. As your page loads dynamic content the socket can push the appropriate policy updates that the browser will enforce on the document. To be clear this is NOT a good proposal: may not be implementable in browsers, timing issues will lead to vulnerabilities, and probably not usable by websites in any event. What would you want to use?

@dveditz
Copy link
Member

dveditz commented Oct 20, 2021

I also invite you to take a look at the "Trusted Types" proposal. It is designed to deal with problems with dynamic content creation that CSP alone can't do much about, although it was not intended to address all the issues CSP tries to address. Trusted Types is usable now in Chrome, Edge, and probably other chromium-based browsers. You could write a flexible and dynamic set of sanitizers, although they would become hugely complex. Even if it doesn't work out in the end, the exercise might help you crystalize what kinds of controls you need a browser to support.

@gulachek
Copy link
Author

gulachek commented Oct 20, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants