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 SandboxAll = -1 // Mask with all bits set to 1.
57 typedef int SandboxFlags;
59 class SecurityContext {
61 SandboxFlags sandboxFlags() const { return m_sandboxFlags; }
62 ContentSecurityPolicy* contentSecurityPolicy() { return m_contentSecurityPolicy.get(); }
64 bool isSecureTransitionTo(const URL&) const;
66 void enforceSandboxFlags(SandboxFlags mask);
67 bool isSandboxed(SandboxFlags mask) const { return m_sandboxFlags & mask; }
69 SecurityOriginPolicy* securityOriginPolicy() const { return m_securityOriginPolicy.get(); }
71 // Explicitly override the security origin for this security context.
72 // Note: It is dangerous to change the security origin of a script context
73 // that already contains content.
74 void setSecurityOriginPolicy(RefPtr<SecurityOriginPolicy>&&);
76 WEBCORE_EXPORT SecurityOrigin* securityOrigin() const;
78 static SandboxFlags parseSandboxPolicy(const String& policy, String& invalidTokensErrorMessage);
79 static bool isSupportedSandboxPolicy(StringView);
81 bool foundMixedContent() const { return m_foundMixedContent; }
82 void setFoundMixedContent() { m_foundMixedContent = true; }
83 bool geolocationAccessed() const { return m_geolocationAccessed; }
84 void setGeolocationAccessed() { m_geolocationAccessed = true; }
86 bool isStrictMixedContentMode() const { return m_isStrictMixedContentMode; }
87 void setStrictMixedContentMode(bool strictMixedContentMode) { m_isStrictMixedContentMode = strictMixedContentMode; }
89 // This method implements the "Is the environment settings object settings a secure context?" algorithm from
90 // the Secure Context spec: https://w3c.github.io/webappsec-secure-contexts/#settings-object (Editor's Draft, 17 November 2016)
91 virtual bool isSecureContext() const = 0;
95 virtual ~SecurityContext();
97 void setContentSecurityPolicy(std::unique_ptr<ContentSecurityPolicy>);
99 // It's only appropriate to call this during security context initialization; it's needed for
100 // flags that can't be disabled with allow-* attributes, such as SandboxNavigation.
101 void disableSandboxFlags(SandboxFlags mask) { m_sandboxFlags &= ~mask; }
103 void didFailToInitializeSecurityOrigin() { m_haveInitializedSecurityOrigin = false; }
104 bool haveInitializedSecurityOrigin() const { return m_haveInitializedSecurityOrigin; }
107 RefPtr<SecurityOriginPolicy> m_securityOriginPolicy;
108 std::unique_ptr<ContentSecurityPolicy> m_contentSecurityPolicy;
109 SandboxFlags m_sandboxFlags { SandboxNone };
110 bool m_haveInitializedSecurityOrigin { false };
111 bool m_foundMixedContent { false };
112 bool m_geolocationAccessed { false };
113 bool m_isStrictMixedContentMode { false };
116 } // namespace WebCore