Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / ContentSecurityPolicy.h
1 /*
2  * Copyright (C) 2011 Google, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #ifndef ContentSecurityPolicy_h
27 #define ContentSecurityPolicy_h
28
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"
38
39 namespace WTF {
40 class OrdinalNumber;
41 }
42
43 namespace WebCore {
44
45 class ContentSecurityPolicyResponseHeaders;
46 class CSPDirectiveList;
47 class DOMStringList;
48 class JSONObject;
49 class KURL;
50 class ExecutionContextClient;
51 class SecurityOrigin;
52
53 typedef int SandboxFlags;
54 typedef Vector<OwnPtr<CSPDirectiveList> > CSPDirectiveListVector;
55
56 class ContentSecurityPolicy {
57     WTF_MAKE_FAST_ALLOCATED;
58 public:
59     static PassOwnPtr<ContentSecurityPolicy> create(ExecutionContextClient* client)
60     {
61         return adoptPtr(new ContentSecurityPolicy(client));
62     }
63     ~ContentSecurityPolicy();
64
65     void copyStateFrom(const ContentSecurityPolicy*);
66
67     enum HeaderType {
68         Report,
69         Enforce,
70     };
71
72     enum HeaderSource {
73         HeaderSourceHTTP,
74         HeaderSourceMeta
75     };
76
77     enum ReportingStatus {
78         SendReport,
79         SuppressReport
80     };
81
82     enum HashAlgorithms {
83         HashAlgorithmsNone   = 0,
84         HashAlgorithmsSha1   = 1 << 1,
85         HashAlgorithmsSha256 = 1 << 2
86     };
87
88     void didReceiveHeaders(const ContentSecurityPolicyResponseHeaders&);
89     void didReceiveHeader(const String&, HeaderType, HeaderSource);
90
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;
95
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;
102
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;
115
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;
122
123     void usesScriptHashAlgorithms(uint8_t HashAlgorithms);
124     void usesStyleHashAlgorithms(uint8_t HashAlgorithms);
125
126     ReflectedXSSDisposition reflectedXSSDisposition() const;
127
128     ReferrerPolicy referrerPolicy() const;
129     bool didSetReferrerPolicy() const;
130
131     void setOverrideAllowInlineStyle(bool);
132
133     bool isActive() const;
134
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);
149
150     void reportBlockedScriptExecutionToInspector(const String& directiveText) const;
151
152     const KURL url() const;
153     KURL completeURL(const String&) const;
154     SecurityOrigin* securityOrigin() const;
155     void enforceSandboxFlags(SandboxFlags) const;
156     String evalDisabledErrorMessage() const;
157
158     bool experimentalFeaturesEnabled() const;
159
160     static bool shouldBypassMainWorld(ExecutionContext*);
161
162     ExecutionContextClient* client() { return m_client; }
163
164 private:
165     explicit ContentSecurityPolicy(ExecutionContextClient*);
166
167     void logToConsole(const String& message) const;
168     void addPolicyFromHeaderValue(const String&, HeaderType, HeaderSource);
169
170     bool shouldSendViolationReport(const String&) const;
171     void didSendViolationReport(const String&);
172
173     ExecutionContextClient* m_client;
174     bool m_overrideInlineStyleAllowed;
175     CSPDirectiveListVector m_policies;
176
177     HashSet<unsigned, AlreadyHashed> m_violationReportsSent;
178
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
181     // for validation.
182     uint8_t m_scriptHashAlgorithmsUsed;
183     uint8_t m_styleHashAlgorithmsUsed;
184 };
185
186 }
187
188 #endif