2 * Copyright (C) 2012 Google, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "DOMSecurityPolicy.h"
29 #include "ContentSecurityPolicy.h"
30 #include "ContextDestructionObserver.h"
31 #include "DOMStringList.h"
33 #include "ScriptExecutionContext.h"
34 #include <wtf/text/TextPosition.h>
35 #include <wtf/text/WTFString.h>
41 bool isPolicyActiveInContext(ScriptExecutionContext* context)
43 // If the ScriptExecutionContext has been destroyed, there's no active policy.
47 return context->contentSecurityPolicy()->isActive();
50 template<bool (ContentSecurityPolicy::*allowWithURL)(const KURL&, ContentSecurityPolicy::ReportingStatus) const>
51 bool isAllowedWithURL(ScriptExecutionContext* context, const String& url)
53 if (!isPolicyActiveInContext(context))
56 KURL parsedURL = context->completeURL(url);
57 if (!parsedURL.isValid())
58 return false; // FIXME: Figure out how to throw a JavaScript error.
60 return (context->contentSecurityPolicy()->*allowWithURL)(parsedURL, ContentSecurityPolicy::SuppressReport);
63 template<bool (ContentSecurityPolicy::*allowWithContext)(const String&, const WTF::OrdinalNumber&, ContentSecurityPolicy::ReportingStatus) const>
64 bool isAllowed(ScriptExecutionContext* context)
66 if (!isPolicyActiveInContext(context))
69 return (context->contentSecurityPolicy()->*allowWithContext)(KURL(), WTF::OrdinalNumber::beforeFirst(), ContentSecurityPolicy::SuppressReport);
74 DOMSecurityPolicy::DOMSecurityPolicy(ScriptExecutionContext* context)
75 : ContextDestructionObserver(context)
79 DOMSecurityPolicy::~DOMSecurityPolicy()
83 bool DOMSecurityPolicy::isActive() const
85 return isPolicyActiveInContext(scriptExecutionContext());
88 PassRefPtr<DOMStringList> DOMSecurityPolicy::reportURIs() const
90 RefPtr<DOMStringList> result = DOMStringList::create();
93 scriptExecutionContext()->contentSecurityPolicy()->gatherReportURIs(*result.get());
95 return result.release();
98 bool DOMSecurityPolicy::allowsInlineScript() const
100 return isAllowed<&ContentSecurityPolicy::allowInlineScript>(scriptExecutionContext());
103 bool DOMSecurityPolicy::allowsInlineStyle() const
105 return isAllowed<&ContentSecurityPolicy::allowInlineStyle>(scriptExecutionContext());
108 bool DOMSecurityPolicy::allowsEval() const
113 return scriptExecutionContext()->contentSecurityPolicy()->allowEval(0, ContentSecurityPolicy::SuppressReport);
117 bool DOMSecurityPolicy::allowsConnectionTo(const String& url) const
119 return isAllowedWithURL<&ContentSecurityPolicy::allowConnectToSource>(scriptExecutionContext(), url);
122 bool DOMSecurityPolicy::allowsFontFrom(const String& url) const
124 return isAllowedWithURL<&ContentSecurityPolicy::allowFontFromSource>(scriptExecutionContext(), url);
127 bool DOMSecurityPolicy::allowsFrameFrom(const String& url) const
129 return isAllowedWithURL<&ContentSecurityPolicy::allowChildFrameFromSource>(scriptExecutionContext(), url);
132 bool DOMSecurityPolicy::allowsImageFrom(const String& url) const
134 return isAllowedWithURL<&ContentSecurityPolicy::allowImageFromSource>(scriptExecutionContext(), url);
137 bool DOMSecurityPolicy::allowsMediaFrom(const String& url) const
139 return isAllowedWithURL<&ContentSecurityPolicy::allowMediaFromSource>(scriptExecutionContext(), url);
142 bool DOMSecurityPolicy::allowsObjectFrom(const String& url) const
144 return isAllowedWithURL<&ContentSecurityPolicy::allowObjectFromSource>(scriptExecutionContext(), url);
147 bool DOMSecurityPolicy::allowsScriptFrom(const String& url) const
149 return isAllowedWithURL<&ContentSecurityPolicy::allowScriptFromSource>(scriptExecutionContext(), url);
152 bool DOMSecurityPolicy::allowsStyleFrom(const String& url) const
154 return isAllowedWithURL<&ContentSecurityPolicy::allowStyleFromSource>(scriptExecutionContext(), url);
157 } // namespace WebCore