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
29 #include "bindings/v8/ScriptState.h"
30 #include "platform/network/HTTPParsers.h"
31 #include "platform/weborigin/ReferrerPolicy.h"
32 #include "wtf/HashSet.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/Vector.h"
35 #include "wtf/text/StringHash.h"
36 #include "wtf/text/TextPosition.h"
37 #include "wtf/text/WTFString.h"
45 class ContentSecurityPolicyResponseHeaders;
46 class CSPDirectiveList;
50 class ExecutionContextClient;
53 typedef int SandboxFlags;
54 typedef Vector<OwnPtr<CSPDirectiveList> > CSPDirectiveListVector;
56 class ContentSecurityPolicy {
57 WTF_MAKE_FAST_ALLOCATED;
59 static PassOwnPtr<ContentSecurityPolicy> create(ExecutionContextClient* client)
61 return adoptPtr(new ContentSecurityPolicy(client));
63 ~ContentSecurityPolicy();
65 void copyStateFrom(const ContentSecurityPolicy*);
77 enum ReportingStatus {
83 HashAlgorithmsNone = 0,
84 HashAlgorithmsSha1 = 1 << 1,
85 HashAlgorithmsSha256 = 1 << 2
88 void didReceiveHeaders(const ContentSecurityPolicyResponseHeaders&);
89 void didReceiveHeader(const String&, HeaderType, HeaderSource);
91 // These functions are wrong because they assume that there is only one header.
92 // FIXME: Replace them with functions that return vectors.
93 const String& deprecatedHeader() const;
94 HeaderType deprecatedHeaderType() const;
96 bool allowJavaScriptURLs(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
97 bool allowInlineEventHandlers(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
98 bool allowInlineScript(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
99 bool allowInlineStyle(const String& contextURL, const WTF::OrdinalNumber& contextLine, ReportingStatus = SendReport) const;
100 bool allowEval(ScriptState* = 0, ReportingStatus = SendReport) const;
101 bool allowPluginType(const String& type, const String& typeAttribute, const KURL&, ReportingStatus = SendReport) const;
103 bool allowScriptFromSource(const KURL&, ReportingStatus = SendReport) const;
104 bool allowObjectFromSource(const KURL&, ReportingStatus = SendReport) const;
105 bool allowChildFrameFromSource(const KURL&, ReportingStatus = SendReport) const;
106 bool allowImageFromSource(const KURL&, ReportingStatus = SendReport) const;
107 bool allowStyleFromSource(const KURL&, ReportingStatus = SendReport) const;
108 bool allowFontFromSource(const KURL&, ReportingStatus = SendReport) const;
109 bool allowMediaFromSource(const KURL&, ReportingStatus = SendReport) const;
110 bool allowConnectToSource(const KURL&, ReportingStatus = SendReport) const;
111 bool allowFormAction(const KURL&, ReportingStatus = SendReport) const;
112 bool allowBaseURI(const KURL&, ReportingStatus = SendReport) const;
113 bool allowAncestors(Frame*, ReportingStatus = SendReport) const;
114 bool allowChildContextFromSource(const KURL&, ReportingStatus = SendReport) const;
116 // The nonce and hash allow functions are guaranteed to not have any side
117 // effects, including reporting.
118 bool allowScriptNonce(const String& nonce) const;
119 bool allowStyleNonce(const String& nonce) const;
120 bool allowScriptHash(const String& source) const;
121 bool allowStyleHash(const String& source) const;
123 void usesScriptHashAlgorithms(uint8_t HashAlgorithms);
124 void usesStyleHashAlgorithms(uint8_t HashAlgorithms);
126 ReflectedXSSDisposition reflectedXSSDisposition() const;
128 ReferrerPolicy referrerPolicy() const;
129 bool didSetReferrerPolicy() const;
131 void setOverrideAllowInlineStyle(bool);
133 bool isActive() const;
135 void reportDirectiveAsSourceExpression(const String& directiveName, const String& sourceExpression) const;
136 void reportDuplicateDirective(const String&) const;
137 void reportInvalidDirectiveValueCharacter(const String& directiveName, const String& value) const;
138 void reportInvalidPathCharacter(const String& directiveName, const String& value, const char) const;
139 void reportInvalidPluginTypes(const String&) const;
140 void reportInvalidSandboxFlags(const String&) const;
141 void reportInvalidSourceExpression(const String& directiveName, const String& source) const;
142 void reportInvalidReflectedXSS(const String&) const;
143 void reportMissingReportURI(const String&) const;
144 void reportUnsupportedDirective(const String&) const;
145 void reportInvalidInReportOnly(const String&) const;
146 void reportInvalidReferrer(const String&) const;
147 void reportReportOnlyInMeta(const String&) const;
148 void reportViolation(const String& directiveText, const String& effectiveDirective, const String& consoleMessage, const KURL& blockedURL, const Vector<KURL>& reportURIs, const String& header);
150 void reportBlockedScriptExecutionToInspector(const String& directiveText) const;
152 const KURL url() const;
153 KURL completeURL(const String&) const;
154 SecurityOrigin* securityOrigin() const;
155 void enforceSandboxFlags(SandboxFlags) const;
156 String evalDisabledErrorMessage() const;
158 bool experimentalFeaturesEnabled() const;
160 static bool shouldBypassMainWorld(ExecutionContext*);
162 ExecutionContextClient* client() { return m_client; }
165 explicit ContentSecurityPolicy(ExecutionContextClient*);
167 void logToConsole(const String& message) const;
168 void addPolicyFromHeaderValue(const String&, HeaderType, HeaderSource);
170 bool shouldSendViolationReport(const String&) const;
171 void didSendViolationReport(const String&);
173 ExecutionContextClient* m_client;
174 bool m_overrideInlineStyleAllowed;
175 CSPDirectiveListVector m_policies;
177 HashSet<unsigned, AlreadyHashed> m_violationReportsSent;
179 // We put the hash functions used on the policy object so that we only need
180 // to calculate a hash once and then distribute it to all of the directives
182 uint8_t m_scriptHashAlgorithmsUsed;
183 uint8_t m_styleHashAlgorithmsUsed;