Changeset 116254 in webkit


Ignore:
Timestamp:
May 6, 2012 5:53:33 PM (12 years ago)
Author:
abarth@webkit.org
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.
Location:
trunk
Files:
4 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r116250 r116254  
     12012-05-06  Adam Barth  <abarth@webkit.org>
     2
     3        CSP should let sites both enforce one policy and monitor another
     4        https://bugs.webkit.org/show_bug.cgi?id=85561
     5
     6        Reviewed by Eric Seidel.
     7
     8        Test that we can enforce multiple policies and that we can enforce one
     9        policy while monitoring another.
     10
     11        * http/tests/security/contentSecurityPolicy/combine-multiple-policies-expected.txt: Added.
     12        * http/tests/security/contentSecurityPolicy/combine-multiple-policies.html: Added.
     13        * http/tests/security/contentSecurityPolicy/report-and-enforce-expected.txt: Added.
     14        * http/tests/security/contentSecurityPolicy/report-and-enforce.html: Added.
     15
    1162012-05-06  Mikhail Pozdnyakov  <mikhail.pozdnyakov@intel.com>
    217
  • trunk/Source/WebCore/ChangeLog

    r116253 r116254  
     12012-05-06  Adam Barth  <abarth@webkit.org>
     2
     3        CSP should let sites both enforce one policy and monitor another
     4        https://bugs.webkit.org/show_bug.cgi?id=85561
     5
     6        Reviewed by Eric Seidel.
     7
     8        This patch lets us enforce and/or monitor multiple CSP policies.
     9        Rather than having a single directive list, we now have a vector of
     10        directive lists.
     11
     12        Tests: http/tests/security/contentSecurityPolicy/combine-multiple-policies.html
     13               http/tests/security/contentSecurityPolicy/report-and-enforce.html
     14
     15        * page/ContentSecurityPolicy.cpp:
     16        (WebCore::ContentSecurityPolicy::copyStateFrom):
     17        (WebCore::ContentSecurityPolicy::didReceiveHeader):
     18        (WebCore::ContentSecurityPolicy::deprecatedHeader):
     19        (WebCore::ContentSecurityPolicy::deprecatedHeaderType):
     20            - Even after this patch, workers aren't smart enough to enforce
     21              multiple policies.  They just use the first header, like they did
     22              before.  We'll need to teach workers how to enforce multiple
     23              policies in a future patch.
     24        (WebCore::ContentSecurityPolicy::allowJavaScriptURLs):
     25        (WebCore::ContentSecurityPolicy::allowInlineEventHandlers):
     26        (WebCore::ContentSecurityPolicy::allowInlineScript):
     27        (WebCore::ContentSecurityPolicy::allowInlineStyle):
     28        (WebCore::ContentSecurityPolicy::allowEval):
     29        (WebCore::ContentSecurityPolicy::allowScriptFromSource):
     30        (WebCore::ContentSecurityPolicy::allowObjectFromSource):
     31        (WebCore::ContentSecurityPolicy::allowChildFrameFromSource):
     32        (WebCore::ContentSecurityPolicy::allowImageFromSource):
     33        (WebCore::ContentSecurityPolicy::allowStyleFromSource):
     34        (WebCore::ContentSecurityPolicy::allowFontFromSource):
     35        (WebCore::ContentSecurityPolicy::allowMediaFromSource):
     36        (WebCore::ContentSecurityPolicy::allowConnectFromSource):
     37        * page/ContentSecurityPolicy.h:
     38        * workers/DefaultSharedWorkerRepository.cpp:
     39        (WebCore::SharedWorkerScriptLoader::notifyFinished):
     40        * workers/WorkerMessagingProxy.cpp:
     41        (WebCore::WorkerMessagingProxy::startWorkerContext):
     42
    1432012-05-06  Rob Buis  <rbuis@rim.com>
    244
  • trunk/Source/WebCore/page/ContentSecurityPolicy.cpp

    r116248 r116254  
    888888void ContentSecurityPolicy::copyStateFrom(const ContentSecurityPolicy* other)
    889889{
    890     ASSERT(!m_policy);
    891     if (other->m_policy)
    892         didReceiveHeader(other->header(), other->headerType());
     890    ASSERT(m_policies.isEmpty());
     891    for (CSPDirectiveListVector::const_iterator iter = other->m_policies.begin(); iter != other->m_policies.end(); ++iter)
     892        didReceiveHeader((*iter)->header(), (*iter)->headerType());
    893893}
    894894
    895895void ContentSecurityPolicy::didReceiveHeader(const String& header, HeaderType type)
    896896{
    897     if (m_policy)
    898         return; // The first policy wins.
    899     m_policy = CSPDirectiveList::create(m_scriptExecutionContext, header, type);
     897    m_policies.append(CSPDirectiveList::create(m_scriptExecutionContext, header, type));
    900898}
    901899
     
    905903}
    906904
    907 const String& ContentSecurityPolicy::header() const
    908 {
    909     return m_policy ? m_policy->header() : emptyString();
    910 }
    911 
    912 ContentSecurityPolicy::HeaderType ContentSecurityPolicy::headerType() const
    913 {
    914     return m_policy ? m_policy->headerType() : EnforcePolicy;
     905const String& ContentSecurityPolicy::deprecatedHeader() const
     906{
     907    return m_policies.isEmpty() ? emptyString() : m_policies[0]->header();
     908}
     909
     910ContentSecurityPolicy::HeaderType ContentSecurityPolicy::deprecatedHeaderType() const
     911{
     912    return m_policies.isEmpty() ? EnforcePolicy : m_policies[0]->headerType();
     913}
     914
     915template<bool (CSPDirectiveList::*allowed)() const>
     916bool isAllowedByAll(const CSPDirectiveListVector& policies)
     917{
     918    for (size_t i = 0; i < policies.size(); ++i) {
     919        if (!(policies[i].get()->*allowed)())
     920            return false;
     921    }
     922    return true;
     923}
     924
     925template<bool (CSPDirectiveList::*allowFromURL)(const KURL&) const>
     926bool isAllowedByAll(const CSPDirectiveListVector& policies, const KURL& url)
     927{
     928    for (size_t i = 0; i < policies.size(); ++i) {
     929        if (!(policies[i].get()->*allowFromURL)(url))
     930            return false;
     931    }
     932    return true;
    915933}
    916934
    917935bool ContentSecurityPolicy::allowJavaScriptURLs() const
    918936{
    919     return !m_policy || m_policy->allowJavaScriptURLs();
     937    return isAllowedByAll<&CSPDirectiveList::allowJavaScriptURLs>(m_policies);
    920938}
    921939
    922940bool ContentSecurityPolicy::allowInlineEventHandlers() const
    923941{
    924     return !m_policy || m_policy->allowInlineEventHandlers();
     942    return isAllowedByAll<&CSPDirectiveList::allowInlineEventHandlers>(m_policies);
    925943}
    926944
    927945bool ContentSecurityPolicy::allowInlineScript() const
    928946{
    929     return !m_policy || m_policy->allowInlineScript();
     947    return isAllowedByAll<&CSPDirectiveList::allowInlineScript>(m_policies);
    930948}
    931949
    932950bool ContentSecurityPolicy::allowInlineStyle() const
    933951{
    934     return !m_policy || m_overrideInlineStyleAllowed || m_policy->allowInlineStyle();
     952    if (m_overrideInlineStyleAllowed)
     953        return true;
     954    return isAllowedByAll<&CSPDirectiveList::allowInlineStyle>(m_policies);
    935955}
    936956
    937957bool ContentSecurityPolicy::allowEval() const
    938958{
    939     return !m_policy || m_policy->allowEval();
     959    return isAllowedByAll<&CSPDirectiveList::allowEval>(m_policies);
    940960}
    941961
    942962bool ContentSecurityPolicy::allowScriptFromSource(const KURL& url) const
    943963{
    944     return !m_policy || m_policy->allowScriptFromSource(url);
     964    return isAllowedByAll<&CSPDirectiveList::allowScriptFromSource>(m_policies, url);
    945965}
    946966
    947967bool ContentSecurityPolicy::allowObjectFromSource(const KURL& url) const
    948968{
    949     return !m_policy || m_policy->allowObjectFromSource(url);
     969    return isAllowedByAll<&CSPDirectiveList::allowObjectFromSource>(m_policies, url);
    950970}
    951971
    952972bool ContentSecurityPolicy::allowChildFrameFromSource(const KURL& url) const
    953973{
    954     return !m_policy || m_policy->allowChildFrameFromSource(url);
     974    return isAllowedByAll<&CSPDirectiveList::allowChildFrameFromSource>(m_policies, url);
    955975}
    956976
    957977bool ContentSecurityPolicy::allowImageFromSource(const KURL& url) const
    958978{
    959     return !m_policy || m_policy->allowImageFromSource(url);
     979    return isAllowedByAll<&CSPDirectiveList::allowImageFromSource>(m_policies, url);
    960980}
    961981
    962982bool ContentSecurityPolicy::allowStyleFromSource(const KURL& url) const
    963983{
    964     return !m_policy || m_policy->allowStyleFromSource(url);
     984    return isAllowedByAll<&CSPDirectiveList::allowStyleFromSource>(m_policies, url);
    965985}
    966986
    967987bool ContentSecurityPolicy::allowFontFromSource(const KURL& url) const
    968988{
    969     return !m_policy || m_policy->allowFontFromSource(url);
     989    return isAllowedByAll<&CSPDirectiveList::allowFontFromSource>(m_policies, url);
    970990}
    971991
    972992bool ContentSecurityPolicy::allowMediaFromSource(const KURL& url) const
    973993{
    974     return !m_policy || m_policy->allowMediaFromSource(url);
     994    return isAllowedByAll<&CSPDirectiveList::allowMediaFromSource>(m_policies, url);
    975995}
    976996
    977997bool ContentSecurityPolicy::allowConnectFromSource(const KURL& url) const
    978998{
    979     return !m_policy || m_policy->allowConnectFromSource(url);
    980 }
    981 
    982 }
     999    return isAllowedByAll<&CSPDirectiveList::allowConnectFromSource>(m_policies, url);
     1000}
     1001
     1002}
  • trunk/Source/WebCore/page/ContentSecurityPolicy.h

    r116177 r116254  
    3838class KURL;
    3939
     40typedef Vector<OwnPtr<CSPDirectiveList> > CSPDirectiveListVector;
     41
    4042class ContentSecurityPolicy {
    4143public:
     
    5557    void didReceiveHeader(const String&, HeaderType);
    5658
    57     const String& header() const;
    58     HeaderType headerType() const;
     59    // These functions are wrong becuase they assume that there is only one header.
     60    // FIXME: Replace them with functions that return vectors.
     61    const String& deprecatedHeader() const;
     62    HeaderType deprecatedHeaderType() const;
    5963
    6064    bool allowJavaScriptURLs() const;
     
    8084    ScriptExecutionContext* m_scriptExecutionContext;
    8185    bool m_overrideInlineStyleAllowed;
    82     OwnPtr<CSPDirectiveList> m_policy;
     86    CSPDirectiveListVector m_policies;
    8387};
    8488
  • trunk/Source/WebCore/workers/DefaultSharedWorkerRepository.cpp

    r116177 r116254  
    323323        DefaultSharedWorkerRepository::instance().workerScriptLoaded(*m_proxy, m_worker->scriptExecutionContext()->userAgent(m_scriptLoader->url()),
    324324                                                                     m_scriptLoader->script(), m_port.release(),
    325                                                                      m_worker->scriptExecutionContext()->contentSecurityPolicy()->header(),
    326                                                                      m_worker->scriptExecutionContext()->contentSecurityPolicy()->headerType());
     325                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeader(),
     326                                                                     m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeaderType());
    327327    }
    328328    m_worker->unsetPendingActivity(m_worker.get());
  • trunk/Source/WebCore/workers/WorkerMessagingProxy.cpp

    r116177 r116254  
    273273{
    274274    RefPtr<DedicatedWorkerThread> thread = DedicatedWorkerThread::create(scriptURL, userAgent, sourceCode, *this, *this, startMode,
    275                                                                          m_scriptExecutionContext->contentSecurityPolicy()->header(),
    276                                                                          m_scriptExecutionContext->contentSecurityPolicy()->headerType());
     275                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeader(),
     276                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeaderType());
    277277    workerThreadCreated(thread);
    278278    thread->start();
  • trunk/Source/WebKit/chromium/ChangeLog

    r116243 r116254  
     12012-05-06  Adam Barth  <abarth@webkit.org>
     2
     3        CSP should let sites both enforce one policy and monitor another
     4        https://bugs.webkit.org/show_bug.cgi?id=85561
     5
     6        Reviewed by Eric Seidel.
     7
     8        * src/SharedWorkerRepository.cpp:
     9        (WebCore::SharedWorkerScriptLoader::notifyFinished):
     10        * src/WebWorkerClientImpl.cpp:
     11        (WebKit::WebWorkerClientImpl::startWorkerContext):
     12
    1132012-05-06  Kent Tamura  <tkent@chromium.org>
    214
  • trunk/Source/WebKit/chromium/src/SharedWorkerRepository.cpp

    r116177 r116254  
    172172        // Pass the script off to the worker, then send a connect event.
    173173        m_webWorker->startWorkerContext(m_url, m_name, m_worker->scriptExecutionContext()->userAgent(m_url), m_scriptLoader->script(),
    174                                         m_worker->scriptExecutionContext()->contentSecurityPolicy()->header(),
    175                                         static_cast<WebKit::WebContentSecurityPolicyType>(m_worker->scriptExecutionContext()->contentSecurityPolicy()->headerType()),
     174                                        m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeader(),
     175                                        static_cast<WebKit::WebContentSecurityPolicyType>(m_worker->scriptExecutionContext()->contentSecurityPolicy()->deprecatedHeaderType()),
    176176                                        m_responseAppCacheID);
    177177        sendConnect();
  • trunk/Source/WebKit/chromium/src/WebWorkerClientImpl.cpp

    r116177 r116254  
    8888{
    8989    RefPtr<DedicatedWorkerThread> thread = DedicatedWorkerThread::create(scriptURL, userAgent, sourceCode, *this, *this, startMode,
    90                                                                          m_scriptExecutionContext->contentSecurityPolicy()->header(),
    91                                                                          m_scriptExecutionContext->contentSecurityPolicy()->headerType());
     90                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeader(),
     91                                                                         m_scriptExecutionContext->contentSecurityPolicy()->deprecatedHeaderType());
    9292    m_proxy->workerThreadCreated(thread);
    9393    thread->start();
Note: See TracChangeset for help on using the changeset viewer.