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 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.
30 #include <wtf/Forward.h>
31 #include <wtf/RefPtr.h>
36 class SecurityOriginPolicy;
37 class ContentSecurityPolicy;
41 // See http://www.whatwg.org/specs/web-apps/current-work/#attr-iframe-sandbox for a list of the sandbox flags.
43 SandboxNavigation = 1,
44 SandboxPlugins = 1 << 1,
45 SandboxOrigin = 1 << 2,
46 SandboxForms = 1 << 3,
47 SandboxScripts = 1 << 4,
48 SandboxTopNavigation = 1 << 5,
49 SandboxPopups = 1 << 6, // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12393
50 SandboxAutomaticFeatures = 1 << 7,
51 SandboxPointerLock = 1 << 8,
52 SandboxPropagatesToAuxiliaryBrowsingContexts = 1 << 9,
53 SandboxTopNavigationByUserActivation = 1 << 10,
54 SandboxDocumentDomain = 1 << 11,
55 SandboxAll = -1 // Mask with all bits set to 1.
58 typedef int SandboxFlags;
60 class SecurityContext {
62 SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
63 ContentSecurityPolicy* contentSecurityPolicy() { return m_contentSecurityPolicy.get(); }
65 bool isSecureTransitionTo(const URL&) const;
67 void enforceSandboxFlags(SandboxFlags mask);
68 bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
70 SecurityOriginPolicy* securityOriginPolicy() const { return m_securityOriginPolicy.get(); }
72 // Explicitly override the security origin for this security context.
73 // Note: It is dangerous to change the security origin of a script context
74 // that already contains content.
75 void setSecurityOriginPolicy(RefPtr<SecurityOriginPolicy>&&);
77 WEBCORE_EXPORT SecurityOrigin* securityOrigin() const;
79 static SandboxFlags parseSandboxPolicy(const String& policy, String& invalidTokensErrorMessage);
80 static bool isSupportedSandboxPolicy(StringView);
82 bool foundMixedContent() const { return m_foundMixedContent; }
83 void setFoundMixedContent() { m_foundMixedContent = true; }
84 bool geolocationAccessed() const { return m_geolocationAccessed; }
85 void setGeolocationAccessed() { m_geolocationAccessed = true; }
87 bool isStrictMixedContentMode() const { return m_isStrictMixedContentMode; }
88 void setStrictMixedContentMode(bool strictMixedContentMode) { m_isStrictMixedContentMode = strictMixedContentMode; }
90 // This method implements the "Is the environment settings object settings a secure context?" algorithm from
91 // the Secure Context spec: https://w3c.github.io/webappsec-secure-contexts/#settings-object (Editor's Draft, 17 November 2016)
92 virtual bool isSecureContext() const = 0;
96 virtual ~SecurityContext();
98 void setContentSecurityPolicy(std::unique_ptr<ContentSecurityPolicy>);
100 // It's only appropriate to call this during security context initialization; it's needed for
101 // flags that can't be disabled with allow-* attributes, such as SandboxNavigation.
102 void disableSandboxFlags(SandboxFlags mask) { m_sandboxFlags &= ~mask; }
104 void didFailToInitializeSecurityOrigin() { m_haveInitializedSecurityOrigin = false; }
105 bool haveInitializedSecurityOrigin() const { return m_haveInitializedSecurityOrigin; }
108 RefPtr<SecurityOriginPolicy> m_securityOriginPolicy;
109 std::unique_ptr<ContentSecurityPolicy> m_contentSecurityPolicy;
110 SandboxFlags m_sandboxFlags { SandboxNone };
111 bool m_haveInitializedSecurityOrigin { false };
112 bool m_foundMixedContent { false };
113 bool m_geolocationAccessed { false };
114 bool m_isStrictMixedContentMode { false };
117 } // namespace WebCore