RESOLVED FIXED Bug 53867
Disable script elements when a CSP header is present
https://bugs.webkit.org/show_bug.cgi?id=53867
Summary Disable script elements when a CSP header is present
jochen
Reported 2011-02-06 01:16:57 PST
Disable script elements when a CSP header is present
Attachments
Patch (6.18 KB, patch)
2011-02-06 01:17 PST, jochen
no flags
Patch (11.48 KB, patch)
2011-02-06 08:35 PST, jochen
no flags
Patch (11.47 KB, patch)
2011-02-06 08:46 PST, jochen
no flags
Patch (13.62 KB, patch)
2011-02-09 02:20 PST, jochen
no flags
Patch (13.63 KB, patch)
2011-02-09 02:27 PST, jochen
abarth: review+
Patch (17.85 KB, patch)
2011-02-09 07:06 PST, jochen
no flags
jochen
Comment 1 2011-02-06 01:17:33 PST
jochen
Comment 2 2011-02-06 08:35:05 PST
WebKit Review Bot
Comment 3 2011-02-06 08:37:02 PST
Attachment 81408 [details] did not pass style-queue: Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'LayoutTests/ChangeLog', u'LayoutTests/http..." exit_code: 1 Source/WebCore/page/ContentSecurityPolicy.cpp:46: An else statement can be removed when the prior "if" concludes with a return, break, continue or goto statement. [readability/control_flow] [4] Total errors found: 1 in 16 files If any of these errors are false positives, please file a bug against check-webkit-style.
jochen
Comment 4 2011-02-06 08:46:02 PST
Adam Barth
Comment 5 2011-02-06 10:21:49 PST
Comment on attachment 81409 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=81409&action=review > LayoutTests/http/tests/security/contentSecurityPolicy/no-policy.html:7 > +if (window.layoutTestController) { > + layoutTestController.dumpAsText(); > +} No brackets needed. > LayoutTests/http/tests/security/contentSecurityPolicy/script-src-in-iframe.html:9 > +<meta http-equiv="refresh" content="0; URL=http://localhost:8000/security/contentSecurityPolicy/resources/echo-iframe.pl?q=http://127.0.0.1:8000/security/contentSecurityPolicy/resources/script-src.html&csp=allow%20*;%20script-src%20'none'" /> Why a meta refresh? It would be easier to just open a new window or put the test in a frame. > Source/WebCore/dom/ScriptElement.cpp:161 > + if (!m_element->document()->contentSecurityPolicy()->allowScriptFromUrl(sourceUrl)) > + return; Hum... I'm not sure this will do the right thing if there's a redirect chain involved. This code is just checking the first URL in the redirect chain. We want to check the final URL in the chain... > Source/WebCore/page/ContentSecurityPolicy.cpp:48 > + if (m_document->parentDocument() == m_document) > + return !m_isEnabled; > + return !m_isEnabled && m_document->parentDocument()->contentSecurityPolicy()->allowScriptFromUrl(sourceUrl); What is all this business about the parent document? We shouldn't be talking to the parent document at all. Each resource needs to have its own security policy. > Source/WebCore/page/ContentSecurityPolicy.h:42 > + bool allowScriptFromUrl(const String&) const; allowScriptFromUrl => allowScriptFromURL > Source/WebCore/page/ContentSecurityPolicy.h:49 > String m_header; > + > + bool m_isEnabled; > + > + Document* m_document; A really minor style nit: I'd reverse the order here and remove the blank lines. Usually, I like to put the "parent object" first, which, in this case, is the document. Then isEnabled is the most important piece of state (because it controls the behavior of the whole object).
jochen
Comment 6 2011-02-09 02:20:20 PST
jochen
Comment 7 2011-02-09 02:23:17 PST
This patch doesn't take the redirect chain into account. I guess I need to add another method to HTMLScriptRunnerHost that checks the URL at the end of the redirect chain. will do that in a separate patch. I had to move the header handling from MainResourceLoader to FrameLoader, as the DocumentWriter deletes the document and creates a new one after the headers are received (and therefore the CSP would get lost).
jochen
Comment 8 2011-02-09 02:27:29 PST
jochen
Comment 9 2011-02-09 02:29:30 PST
seems like shouldLoadExternalScriptFromSrc is only used for CSP now, so I can move the call to when the response is there
Adam Barth
Comment 10 2011-02-09 02:31:33 PST
(In reply to comment #9) > seems like shouldLoadExternalScriptFromSrc is only used for CSP now, so I can move the call to when the response is there Yeah, I was going to remove shouldLoadExternalScriptFromSrc, but I left it there in case it would be useful for CSP.
jochen
Comment 11 2011-02-09 02:33:07 PST
(In reply to comment #9) > seems like shouldLoadExternalScriptFromSrc is only used for CSP now, so I can move the call to when the response is there related question: is it possible to write a layout test with a redirect chain that includes other hosts than 127.0.0.1?
Adam Barth
Comment 12 2011-02-09 02:33:48 PST
Comment on attachment 81778 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=81778&action=review This gets redirects wrong, of course, but it's a starting point. > Source/WebCore/page/ContentSecurityPolicy.h:40 > + // Determines whether the external script should be loaded based on the Content > + // Security Policy. This comment doesn't add any value. We should just remove it.
Adam Barth
Comment 13 2011-02-09 02:34:16 PST
It might be nice to add a FIXME about getting redirects right. Ideally, we'd also add a test that demonstrates the bug.
jochen
Comment 14 2011-02-09 02:36:22 PST
(In reply to comment #13) > It might be nice to add a FIXME about getting redirects right. Ideally, we'd also add a test that demonstrates the bug. are there existing similar tests? Since we only got a httpd running on 127.0.0.1:8000, it seems difficult to construct a redirect chain that involves other hosts?
Adam Barth
Comment 15 2011-02-09 02:37:48 PST
> are there existing similar tests? Yes. Look for tests in LayoutTests/http/tests with "redirect" in their name. There's a PHP script to help. > Since we only got a httpd running on 127.0.0.1:8000, it seems difficult to construct a redirect chain that involves other hosts? We usually use localhost as an alternative host name.
WebKit Review Bot
Comment 16 2011-02-09 02:46:26 PST
Build Bot
Comment 17 2011-02-09 03:18:56 PST
Collabora GTK+ EWS bot
Comment 18 2011-02-09 03:21:31 PST
Early Warning System Bot
Comment 19 2011-02-09 03:35:26 PST
WebKit Review Bot
Comment 20 2011-02-09 04:52:23 PST
jochen
Comment 21 2011-02-09 07:06:34 PST
WebKit Review Bot
Comment 22 2011-02-09 07:24:23 PST
WebKit Commit Bot
Comment 23 2011-02-09 09:02:08 PST
The commit-queue encountered the following flaky tests while processing attachment 81807 [details]: http/tests/security/xssAuditor/script-tag-post-null-char.html bug 54110 (author: dbates@webkit.org) The commit-queue is continuing to process your patch.
WebKit Commit Bot
Comment 24 2011-02-09 09:03:52 PST
Comment on attachment 81807 [details] Patch Clearing flags on attachment: 81807 Committed r78058: <http://trac.webkit.org/changeset/78058>
Note You need to log in before you can comment on or make changes to this bug.