Le 15/01/2014 16:42, Les Orchard a �crit :
> We've done a handful of performance tweaks lately to save on HTTP requests that increase the amount of JavaScript & CSS we serve up inline with our HTML.
>
> However, we also have bug 948151 [1] requesting that we use Content Security Policy on MDN. CSP disallows inline CSS & JavaScript by default. You can explicitly enable it, but it's very much discouraged.
As far as background, it is discouraged because if MDN gets XSS'ed, the
attacker could leave an inline JS snippet which is executed no matter
what by the browser.
Note that with a proper CSP policy, an inline script can't leak data
(via classic means like x-origin <img> or XHR). It still can force the
user to leave MDN to a malicious clone or some other form of phishing,
but that's quite some work.
> So, a performance strategy conflicts with a security strategy.
Sad when it comes to this :-(
CSP 1.1 introduces the notion of nonce to work around this problem. I
think it's still being discussed and implemented nowhere (but something
to keep an eye on).
The idea is that an inline <script> has a @nonce attribute and it will
be executed only if the nonce is part of the CSP directive (with some
CSP directive syntax, whatever).
An attacker can only have its inline script executed if it can get to
know the nonce (which is impossible enough if the nonce is randomly
generated).
I believe you can implement this nonce thing yourself following these steps:
1) use unsafe-eval in the CSP directive (I believe disabling eval is a
stupid idea anyway, because there is no risk inherent to eval, only bad
usages)
2) Add a server-generated self-removing <script> encapsulating the
nonces that will be used for inline scripts (self-removing so no one can
access its .textContent... though I'm not sure that it could be harmful)
3) Add @data-nonce to the trusted inline scripts with the nonce values
4) After the HTML is loaded (including the inline scripts which didn't
run), do a document.querySelector to find the trusted inline scripts,
get their textContent and eval that.
This technique does not work if you depend on inline script features
like document.readyState (usually "loading" in inline scripts) or
document.write (in which case, I'll contribute right away to get rid of
these ;-) ), etc.
If I was too abstract, I can do a stand-alone prototype maybe Friday,
maybe next week?
> This is something we need to sort out - ie. will we ever support CSP?
Given how high-profile MDN is, it would be recommended to support CSP no
matter what. The policy can be refined over time.
IMHO, the question shouldn't be "will we support CSP?" (because the
answer should always be yes), but "which policy specifically?"
For instance, you can ship CSP right now with unsafe-inline and create a
second bug to remove it.
To bypass CSP, do data URIs work in script@src?
David