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.
28 #include "SecurityContext.h"
30 #include "ContentSecurityPolicy.h"
31 #include "HTMLParserIdioms.h"
32 #include "SecurityOrigin.h"
33 #include <wtf/text/StringBuilder.h>
37 SecurityContext::SecurityContext()
38 : m_mayDisplaySeamlesslyWithParent(false)
39 , m_haveInitializedSecurityOrigin(false)
40 , m_sandboxFlags(SandboxNone)
44 SecurityContext::~SecurityContext()
48 void SecurityContext::setSecurityOrigin(PassRefPtr<SecurityOrigin> securityOrigin)
50 m_securityOrigin = securityOrigin;
51 m_haveInitializedSecurityOrigin = true;
54 void SecurityContext::setContentSecurityPolicy(PassOwnPtr<ContentSecurityPolicy> contentSecurityPolicy)
56 m_contentSecurityPolicy = contentSecurityPolicy;
59 bool SecurityContext::isSecureTransitionTo(const KURL& url) const
61 // If we haven't initialized our security origin by now, this is probably
62 // a new window created via the API (i.e., that lacks an origin and lacks
63 // a place to inherit the origin from).
64 if (!haveInitializedSecurityOrigin())
67 RefPtr<SecurityOrigin> other = SecurityOrigin::create(url);
68 return securityOrigin()->canAccess(other.get());
71 void SecurityContext::enforceSandboxFlags(SandboxFlags mask)
73 m_sandboxFlags |= mask;
75 // The SandboxOrigin is stored redundantly in the security origin.
76 if (isSandboxed(SandboxOrigin) && securityOrigin() && !securityOrigin()->isUnique())
77 setSecurityOrigin(SecurityOrigin::createUnique());
80 SandboxFlags SecurityContext::parseSandboxPolicy(const String& policy, String& invalidTokensErrorMessage)
82 // http://www.w3.org/TR/html5/the-iframe-element.html#attr-iframe-sandbox
83 // Parse the unordered set of unique space-separated tokens.
84 SandboxFlags flags = SandboxAll;
85 unsigned length = policy.length();
87 unsigned numberOfTokenErrors = 0;
88 StringBuilder tokenErrors;
90 while (start < length && isHTMLSpace(policy[start]))
94 unsigned end = start + 1;
95 while (end < length && !isHTMLSpace(policy[end]))
98 // Turn off the corresponding sandbox flag if it's set as "allowed".
99 String sandboxToken = policy.substring(start, end - start);
100 if (equalIgnoringCase(sandboxToken, "allow-same-origin"))
101 flags &= ~SandboxOrigin;
102 else if (equalIgnoringCase(sandboxToken, "allow-forms"))
103 flags &= ~SandboxForms;
104 else if (equalIgnoringCase(sandboxToken, "allow-scripts")) {
105 flags &= ~SandboxScripts;
106 flags &= ~SandboxAutomaticFeatures;
107 } else if (equalIgnoringCase(sandboxToken, "allow-top-navigation"))
108 flags &= ~SandboxTopNavigation;
109 else if (equalIgnoringCase(sandboxToken, "allow-popups"))
110 flags &= ~SandboxPopups;
111 else if (equalIgnoringCase(sandboxToken, "allow-pointer-lock"))
112 flags &= ~SandboxPointerLock;
114 if (numberOfTokenErrors)
115 tokenErrors.appendLiteral(", '");
117 tokenErrors.append('\'');
118 tokenErrors.append(sandboxToken);
119 tokenErrors.append('\'');
120 numberOfTokenErrors++;
126 if (numberOfTokenErrors) {
127 if (numberOfTokenErrors > 1)
128 tokenErrors.appendLiteral(" are invalid sandbox flags.");
130 tokenErrors.appendLiteral(" is an invalid sandbox flag.");
131 invalidTokensErrorMessage = tokenErrors.toString();