Title: [116254] trunk
Revision
116254
Author
[email protected]
Date
2012-05-06 17:53:33 -0700 (Sun, 06 May 2012)

Log Message

CSP should let sites both enforce one policy and monitor another
https://bugs.webkit.org/show_bug.cgi?id=85561

Reviewed by Eric Seidel.

Source/WebCore:

This patch lets us enforce and/or monitor multiple CSP policies.
Rather than having a single directive list, we now have a vector of
directive lists.

Tests: http/tests/security/contentSecurityPolicy/combine-multiple-policies.html
       http/tests/security/contentSecurityPolicy/report-and-enforce.html

* page/ContentSecurityPolicy.cpp:
(WebCore::ContentSecurityPolicy::copyStateFrom):
(WebCore::ContentSecurityPolicy::didReceiveHeader):
(WebCore::ContentSecurityPolicy::deprecatedHeader):
(WebCore::ContentSecurityPolicy::deprecatedHeaderType):
    - Even after this patch, workers aren't smart enough to enforce
      multiple policies.  They just use the first header, like they did
      before.  We'll need to teach workers how to enforce multiple
      policies in a future patch.
(WebCore::ContentSecurityPolicy::allowJavaScriptURLs):
(WebCore::ContentSecurityPolicy::allowInlineEventHandlers):
(WebCore::ContentSecurityPolicy::allowInlineScript):
(WebCore::ContentSecurityPolicy::allowInlineStyle):
(WebCore::ContentSecurityPolicy::allowEval):
(WebCore::ContentSecurityPolicy::allowScriptFromSource):
(WebCore::ContentSecurityPolicy::allowObjectFromSource):
(WebCore::ContentSecurityPolicy::allowChildFrameFromSource):
(WebCore::ContentSecurityPolicy::allowImageFromSource):
(WebCore::ContentSecurityPolicy::allowStyleFromSource):
(WebCore::ContentSecurityPolicy::allowFontFromSource):
(WebCore::ContentSecurityPolicy::allowMediaFromSource):
(WebCore::ContentSecurityPolicy::allowConnectFromSource):
* page/ContentSecurityPolicy.h:
* workers/DefaultSharedWorkerRepository.cpp:
(WebCore::SharedWorkerScriptLoader::notifyFinished):
* workers/WorkerMessagingProxy.cpp:
(WebCore::WorkerMessagingProxy::startWorkerContext):

Source/WebKit/chromium:

* src/SharedWorkerRepository.cpp:
(WebCore::SharedWorkerScriptLoader::notifyFinished):
* src/WebWorkerClientImpl.cpp:
(WebKit::WebWorkerClientImpl::startWorkerContext):

LayoutTests:

Test that we can enforce multiple policies and that we can enforce one
policy while monitoring another.

* http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt: Added.
* http/tests/security/contentSecurityPolicy/combine-multiple-policies.html: Added.
* http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt: Added.
* http/tests/security/contentSecurityPolicy/report-and-enforce.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (116253 => 116254)


--- trunk/LayoutTests/ChangeLog	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/LayoutTests/ChangeLog	2012-05-07 00:53:33 UTC (rev 116254)
@@ -1,3 +1,18 @@
+2012-05-06  Adam Barth  <[email protected]>
+
+        CSP should let sites both enforce one policy and monitor another
+        https://bugs.webkit.org/show_bug.cgi?id=85561
+
+        Reviewed by Eric Seidel.
+
+        Test that we can enforce multiple policies and that we can enforce one
+        policy while monitoring another.
+
+        * http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt: Added.
+        * http/tests/security/contentSecurityPolicy/combine-multiple-policies.html: Added.
+        * http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt: Added.
+        * http/tests/security/contentSecurityPolicy/report-and-enforce.html: Added.
+
 2012-05-06  Mikhail Pozdnyakov  <[email protected]>
 
         [EFL] EFL's LayoutTestController setJavaScriptCanAccessClipboard implementation

Added: trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt (0 => 116254)


--- trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt	2012-05-07 00:53:33 UTC (rev 116254)
@@ -0,0 +1,5 @@
+CONSOLE MESSAGE: Refused to execute inline script because of Content-Security-Policy.
+
+CONSOLE MESSAGE: Refused to apply inline style because of Content-Security-Policy.
+
+This test checks that we enforce all the supplied policies. This test passes if it doesn't alert fail and if the style doesn't apply.

Added: trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies.html (0 => 116254)


--- trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/security/contentSecurityPolicy/combine-multiple-policies.html	2012-05-07 00:53:33 UTC (rev 116254)
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="X-WebKit-CSP" content="script-src 'self'">
+<meta http-equiv="X-WebKit-CSP" content="style-src 'none'">
+<script src=""
+</head>
+<body>
+This test checks that we enforce all the supplied policies.  This test passes
+if it doesn't alert fail and if the style doesn't apply.
+<script>
+alert('FAIL');
+</script>
+<style>
+body {
+    background-color: red;
+}
+</style>
+</body>
+</html>

Added: trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt (0 => 116254)


--- trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt	2012-05-07 00:53:33 UTC (rev 116254)
@@ -0,0 +1,12 @@
+CONSOLE MESSAGE: [Report Only] Refused to execute inline script because of Content-Security-Policy.
+
+ALERT: PASS
+CONSOLE MESSAGE: Refused to load image from 'http://127.0.0.1:8000/security/resources/abe.png' because of Content-Security-Policy.
+
+CSP report received:
+CONTENT_TYPE: application/x-www-form-urlencoded
+HTTP_REFERER: http://127.0.0.1:8000/security/contentSecurityPolicy/report-and-enforce.html
+REQUEST_METHOD: POST
+=== POST DATA =""
+document-url: http://127.0.0.1:8000/security/contentSecurityPolicy/report-and-enforce.html
+violated-directive: script-src 'self'

Added: trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce.html (0 => 116254)


--- trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/security/contentSecurityPolicy/report-and-enforce.html	2012-05-07 00:53:33 UTC (rev 116254)
@@ -0,0 +1,9 @@
+<meta http-equiv="X-WebKit-CSP" content="img-src 'none'">
+<meta http-equiv="X-WebKit-CSP-Report-Only" content="script-src 'self'; report-uri resources/save-report.php">
+<script>
+// This script block will trigger a violation report but shouldn't be blocked.
+alert('PASS');
+</script>
+This image should be blocked, but should not show up in the violation report.
+<img src=""
+<script src=""

Modified: trunk/Source/WebCore/ChangeLog (116253 => 116254)


--- trunk/Source/WebCore/ChangeLog	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebCore/ChangeLog	2012-05-07 00:53:33 UTC (rev 116254)
@@ -1,3 +1,45 @@
+2012-05-06  Adam Barth  <[email protected]>
+
+        CSP should let sites both enforce one policy and monitor another
+        https://bugs.webkit.org/show_bug.cgi?id=85561
+
+        Reviewed by Eric Seidel.
+
+        This patch lets us enforce and/or monitor multiple CSP policies.
+        Rather than having a single directive list, we now have a vector of
+        directive lists.
+
+        Tests: http/tests/security/contentSecurityPolicy/combine-multiple-policies.html
+               http/tests/security/contentSecurityPolicy/report-and-enforce.html
+
+        * page/ContentSecurityPolicy.cpp:
+        (WebCore::ContentSecurityPolicy::copyStateFrom):
+        (WebCore::ContentSecurityPolicy::didReceiveHeader):
+        (WebCore::ContentSecurityPolicy::deprecatedHeader):
+        (WebCore::ContentSecurityPolicy::deprecatedHeaderType):
+            - Even after this patch, workers aren't smart enough to enforce
+              multiple policies.  They just use the first header, like they did
+              before.  We'll need to teach workers how to enforce multiple
+              policies in a future patch.
+        (WebCore::ContentSecurityPolicy::allowJavaScriptURLs):
+        (WebCore::ContentSecurityPolicy::allowInlineEventHandlers):
+        (WebCore::ContentSecurityPolicy::allowInlineScript):
+        (WebCore::ContentSecurityPolicy::allowInlineStyle):
+        (WebCore::ContentSecurityPolicy::allowEval):
+        (WebCore::ContentSecurityPolicy::allowScriptFromSource):
+        (WebCore::ContentSecurityPolicy::allowObjectFromSource):
+        (WebCore::ContentSecurityPolicy::allowChildFrameFromSource):
+        (WebCore::ContentSecurityPolicy::allowImageFromSource):
+        (WebCore::ContentSecurityPolicy::allowStyleFromSource):
+        (WebCore::ContentSecurityPolicy::allowFontFromSource):
+        (WebCore::ContentSecurityPolicy::allowMediaFromSource):
+        (WebCore::ContentSecurityPolicy::allowConnectFromSource):
+        * page/ContentSecurityPolicy.h:
+        * workers/DefaultSharedWorkerRepository.cpp:
+        (WebCore::SharedWorkerScriptLoader::notifyFinished):
+        * workers/WorkerMessagingProxy.cpp:
+        (WebCore::WorkerMessagingProxy::startWorkerContext):
+
 2012-05-06  Rob Buis  <[email protected]>
 
         InlineBox::setHasBadParent should be debug only

Modified: trunk/Source/WebCore/page/ContentSecurityPolicy.cpp (116253 => 116254)


--- trunk/Source/WebCore/page/ContentSecurityPolicy.cpp	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebCore/page/ContentSecurityPolicy.cpp	2012-05-07 00:53:33 UTC (rev 116254)
@@ -887,16 +887,14 @@
 
 void ContentSecurityPolicy::copyStateFrom(const ContentSecurityPolicy* other) 
 {
-    ASSERT(!m_policy);
-    if (other->m_policy)
-        didReceiveHeader(other->header(), other->headerType());
+    ASSERT(m_policies.isEmpty());
+    for (CSPDirectiveListVector::const_iterator iter = other->m_policies.begin(); iter != other->m_policies.end(); ++iter)
+        didReceiveHeader((*iter)->header(), (*iter)->headerType());
 }
 
 void ContentSecurityPolicy::didReceiveHeader(const String& header, HeaderType type)
 {
-    if (m_policy)
-        return; // The first policy wins.
-    m_policy = CSPDirectiveList::create(m_scriptExecutionContext, header, type);
+    m_policies.append(CSPDirectiveList::create(m_scriptExecutionContext, header, type));
 }
 
 void ContentSecurityPolicy::setOverrideAllowInlineStyle(bool value)
@@ -904,79 +902,101 @@
     m_overrideInlineStyleAllowed = value;
 }
 
-const String& ContentSecurityPolicy::header() const
+const String& ContentSecurityPolicy::deprecatedHeader() const
 {
-    return m_policy ? m_policy->header() : emptyString();
+    return m_policies.isEmpty() ? emptyString() : m_policies[0]->header();
 }
 
-ContentSecurityPolicy::HeaderType ContentSecurityPolicy::headerType() const
+ContentSecurityPolicy::HeaderType ContentSecurityPolicy::deprecatedHeaderType() const
 {
-    return m_policy ? m_policy->headerType() : EnforcePolicy;
+    return m_policies.isEmpty() ? EnforcePolicy : m_policies[0]->headerType();
 }
 
+template<bool (CSPDirectiveList::*allowed)() const>
+bool isAllowedByAll(const CSPDirectiveListVector& policies)
+{
+    for (size_t i = 0; i < policies.size(); ++i) {
+        if (!(policies[i].get()->*allowed)())
+            return false;
+    }
+    return true;
+}
+
+template<bool (CSPDirectiveList::*allowFromURL)(const KURL&) const>
+bool isAllowedByAll(const CSPDirectiveListVector& policies, const KURL& url)
+{
+    for (size_t i = 0; i < policies.size(); ++i) {
+        if (!(policies[i].get()->*allowFromURL)(url))
+            return false;
+    }
+    return true;
+}
+
 bool ContentSecurityPolicy::allowJavaScriptURLs() const
 {
-    return !m_policy || m_policy->allowJavaScriptURLs();
+    return isAllowedByAll<&CSPDirectiveList::allowJavaScriptURLs>(m_policies);
 }
 
 bool ContentSecurityPolicy::allowInlineEventHandlers() const
 {
-    return !m_policy || m_policy->allowInlineEventHandlers();
+    return isAllowedByAll<&CSPDirectiveList::allowInlineEventHandlers>(m_policies);
 }
 
 bool ContentSecurityPolicy::allowInlineScript() const
 {
-    return !m_policy || m_policy->allowInlineScript();
+    return isAllowedByAll<&CSPDirectiveList::allowInlineScript>(m_policies);
 }
 
 bool ContentSecurityPolicy::allowInlineStyle() const
 {
-    return !m_policy || m_overrideInlineStyleAllowed || m_policy->allowInlineStyle();
+    if (m_overrideInlineStyleAllowed)
+        return true;
+    return isAllowedByAll<&CSPDirectiveList::allowInlineStyle>(m_policies);
 }
 
 bool ContentSecurityPolicy::allowEval() const
 {
-    return !m_policy || m_policy->allowEval();
+    return isAllowedByAll<&CSPDirectiveList::allowEval>(m_policies);
 }
 
 bool ContentSecurityPolicy::allowScriptFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowScriptFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowScriptFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowObjectFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowObjectFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowObjectFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowChildFrameFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowChildFrameFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowChildFrameFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowImageFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowImageFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowImageFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowStyleFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowStyleFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowStyleFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowFontFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowFontFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowFontFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowMediaFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowMediaFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowMediaFromSource>(m_policies, url);
 }
 
 bool ContentSecurityPolicy::allowConnectFromSource(const KURL& url) const
 {
-    return !m_policy || m_policy->allowConnectFromSource(url);
+    return isAllowedByAll<&CSPDirectiveList::allowConnectFromSource>(m_policies, url);
 }
 
 }

Modified: trunk/Source/WebCore/page/ContentSecurityPolicy.h (116253 => 116254)


--- trunk/Source/WebCore/page/ContentSecurityPolicy.h	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebCore/page/ContentSecurityPolicy.h	2012-05-07 00:53:33 UTC (rev 116254)
@@ -37,6 +37,8 @@
 class ScriptExecutionContext;
 class KURL;
 
+typedef Vector<OwnPtr<CSPDirectiveList> > CSPDirectiveListVector;
+
 class ContentSecurityPolicy {
 public:
     static PassOwnPtr<ContentSecurityPolicy> create(ScriptExecutionContext* scriptExecutionContext)
@@ -54,8 +56,10 @@
 
     void didReceiveHeader(const String&, HeaderType);
 
-    const String& header() const;
-    HeaderType headerType() const;
+    // These functions are wrong becuase they assume that there is only one header.
+    // FIXME: Replace them with functions that return vectors.
+    const String& deprecatedHeader() const;
+    HeaderType deprecatedHeaderType() const;
 
     bool allowJavaScriptURLs() const;
     bool allowInlineEventHandlers() const;
@@ -79,7 +83,7 @@
 
     ScriptExecutionContext* m_scriptExecutionContext;
     bool m_overrideInlineStyleAllowed;
-    OwnPtr<CSPDirectiveList> m_policy;
+    CSPDirectiveListVector m_policies;
 };
 
 }

Modified: trunk/Source/WebCore/workers/DefaultSharedWorkerRepository.cpp (116253 => 116254)


--- trunk/Source/WebCore/workers/DefaultSharedWorkerRepository.cpp	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebCore/workers/DefaultSharedWorkerRepository.cpp	2012-05-07 00:53:33 UTC (rev 116254)
@@ -322,8 +322,8 @@
         InspectorInstrumentation::scriptImported(m_worker->scriptExecutionContext(), m_scriptLoader->identifier(), m_scriptLoader->script());
         DefaultSharedWorkerRepository::instance().workerScriptLoaded(*m_proxy, m_worker->scriptExecutionContext()->userAgent(m_scriptLoader->url()),
                                                                      m_scriptLoader->script(), m_port.release(),
-                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->header(),
-                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->headerType());
+                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeader(),
+                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeaderType());
     }
     m_worker->unsetPendingActivity(m_worker.get());
     this->deref(); // This frees this object - must be the last action in this function.

Modified: trunk/Source/WebCore/workers/WorkerMessagingProxy.cpp (116253 => 116254)


--- trunk/Source/WebCore/workers/WorkerMessagingProxy.cpp	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebCore/workers/WorkerMessagingProxy.cpp	2012-05-07 00:53:33 UTC (rev 116254)
@@ -272,8 +272,8 @@
 void WorkerMessagingProxy::startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode startMode)
 {
     RefPtr<DedicatedWorkerThread> thread = DedicatedWorkerThread::create(scriptURL, userAgent, sourceCode, *this, *this, startMode,
-                                                                         m_scriptExecutionContext->contentSecurityPolicy()->header(),
-                                                                         m_scriptExecutionContext->contentSecurityPolicy()->headerType());
+                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeader(),
+                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeaderType());
     workerThreadCreated(thread);
     thread->start();
     InspectorInstrumentation::didStartWorkerContext(m_scriptExecutionContext.get(), this, scriptURL);

Modified: trunk/Source/WebKit/chromium/ChangeLog (116253 => 116254)


--- trunk/Source/WebKit/chromium/ChangeLog	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebKit/chromium/ChangeLog	2012-05-07 00:53:33 UTC (rev 116254)
@@ -1,3 +1,15 @@
+2012-05-06  Adam Barth  <[email protected]>
+
+        CSP should let sites both enforce one policy and monitor another
+        https://bugs.webkit.org/show_bug.cgi?id=85561
+
+        Reviewed by Eric Seidel.
+
+        * src/SharedWorkerRepository.cpp:
+        (WebCore::SharedWorkerScriptLoader::notifyFinished):
+        * src/WebWorkerClientImpl.cpp:
+        (WebKit::WebWorkerClientImpl::startWorkerContext):
+
 2012-05-06  Kent Tamura  <[email protected]>
 
         Rename ICULocale to LocaleICU, part 2

Modified: trunk/Source/WebKit/chromium/src/SharedWorkerRepository.cpp (116253 => 116254)


--- trunk/Source/WebKit/chromium/src/SharedWorkerRepository.cpp	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebKit/chromium/src/SharedWorkerRepository.cpp	2012-05-07 00:53:33 UTC (rev 116254)
@@ -171,8 +171,8 @@
         InspectorInstrumentation::scriptImported(m_worker->scriptExecutionContext(), m_scriptLoader->identifier(), m_scriptLoader->script());
         // Pass the script off to the worker, then send a connect event.
         m_webWorker->startWorkerContext(m_url, m_name, m_worker->scriptExecutionContext()->userAgent(m_url), m_scriptLoader->script(),
-                                        m_worker->scriptExecutionContext()->contentSecurityPolicy()->header(),
-                                        static_cast<WebKit::WebContentSecurityPolicyType>(m_worker->scriptExecutionContext()->contentSecurityPolicy()->headerType()),
+                                        m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeader(),
+                                        static_cast<WebKit::WebContentSecurityPolicyType>(m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeaderType()),
                                         m_responseAppCacheID);
         sendConnect();
     }

Modified: trunk/Source/WebKit/chromium/src/WebWorkerClientImpl.cpp (116253 => 116254)


--- trunk/Source/WebKit/chromium/src/WebWorkerClientImpl.cpp	2012-05-07 00:45:13 UTC (rev 116253)
+++ trunk/Source/WebKit/chromium/src/WebWorkerClientImpl.cpp	2012-05-07 00:53:33 UTC (rev 116254)
@@ -87,8 +87,8 @@
 void WebWorkerClientImpl::startWorkerContext(const KURL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode startMode)
 {
     RefPtr<DedicatedWorkerThread> thread = DedicatedWorkerThread::create(scriptURL, userAgent, sourceCode, *this, *this, startMode,
-                                                                         m_scriptExecutionContext->contentSecurityPolicy()->header(),
-                                                                         m_scriptExecutionContext->contentSecurityPolicy()->headerType());
+                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeader(),
+                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeaderType());
     m_proxy->workerThreadCreated(thread);
     thread->start();
     InspectorInstrumentation::didStartWorkerContext(m_scriptExecutionContext.get(), m_proxy, scriptURL);
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to