2 * Copyright (C) 2011 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.
26 #ifndef ContentSecurityPolicy_h
27 #define ContentSecurityPolicy_h
30 #include <wtf/PassOwnPtr.h>
31 #include <wtf/RefCounted.h>
32 #include <wtf/Vector.h>
33 #include <wtf/text/TextPosition.h>
34 #include <wtf/text/WTFString.h>
42 class CSPDirectiveList;
43 class ScriptCallStack;
45 class ScriptExecutionContext;
48 typedef int SandboxFlags;
49 typedef Vector<OwnPtr<CSPDirectiveList> > CSPDirectiveListVector;
51 class ContentSecurityPolicy {
53 static PassOwnPtr<ContentSecurityPolicy> create(ScriptExecutionContext* scriptExecutionContext)
55 return adoptPtr(new ContentSecurityPolicy(scriptExecutionContext));
57 ~ContentSecurityPolicy();
59 void copyStateFrom(const ContentSecurityPolicy*);
66 enum ReportingStatus {
71 void didReceiveHeader(const String&, HeaderType);
73 // These functions are wrong because they assume that there is only one header.
74 // FIXME: Replace them with functions that return vectors.
75 const String& deprecatedHeader() const;
76 HeaderType deprecatedHeaderType() const;
78 bool allowJavaScriptURLs(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
79 bool allowInlineEventHandlers(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
80 bool allowInlineScript(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
81 bool allowInlineStyle(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
82 bool allowEval(PassRefPtr<ScriptCallStack>, ReportingStatus = SendReport) const;
83 bool allowScriptNonce(const String& nonce, const String& contextURL, const WTF::OrdinalNumber& contextLine, const KURL& = KURL()) const;
85 bool allowScriptFromSource(const KURL&, ReportingStatus = SendReport) const;
86 bool allowObjectFromSource(const KURL&, ReportingStatus = SendReport) const;
87 bool allowChildFrameFromSource(const KURL&, ReportingStatus = SendReport) const;
88 bool allowImageFromSource(const KURL&, ReportingStatus = SendReport) const;
89 bool allowStyleFromSource(const KURL&, ReportingStatus = SendReport) const;
90 bool allowFontFromSource(const KURL&, ReportingStatus = SendReport) const;
91 bool allowMediaFromSource(const KURL&, ReportingStatus = SendReport) const;
92 bool allowConnectToSource(const KURL&, ReportingStatus = SendReport) const;
94 void setOverrideAllowInlineStyle(bool);
96 bool isActive() const;
97 void gatherReportURIs(DOMStringList&) const;
99 void reportDuplicateDirective(const String&) const;
100 void reportInvalidNonce(const String&) const;
101 void reportIgnoredPathComponent(const String& directiveName, const String& completeSource, const String& path) const;
102 void reportUnrecognizedDirective(const String&) const;
103 void reportViolation(const String& directiveText, const String& consoleMessage, const KURL& blockedURL, const Vector<KURL>& reportURIs, const String& header, const String& contextURL = String(), const WTF::OrdinalNumber& contextLine = WTF::OrdinalNumber::beforeFirst(), PassRefPtr<ScriptCallStack> = 0) const;
105 const KURL& url() const;
106 KURL completeURL(const String&) const;
107 SecurityOrigin* securityOrigin() const;
108 void enforceSandboxFlags(SandboxFlags) const;
111 explicit ContentSecurityPolicy(ScriptExecutionContext*);
113 void logToConsole(const String& message, const String& contextURL = String(), const WTF::OrdinalNumber& contextLine = WTF::OrdinalNumber::beforeFirst(), PassRefPtr<ScriptCallStack> = 0) const;
115 ScriptExecutionContext* m_scriptExecutionContext;
116 bool m_overrideInlineStyleAllowed;
117 CSPDirectiveListVector m_policies;