2 * Copyright (C) 2007, 2008 Apple 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef SecurityOrigin_h
30 #define SecurityOrigin_h
32 #include "PlatformString.h"
33 #include <wtf/ThreadSafeRefCounted.h>
40 class SecurityOrigin : public ThreadSafeRefCounted<SecurityOrigin> {
48 static PassRefPtr<SecurityOrigin> create(const KURL&);
49 static PassRefPtr<SecurityOrigin> createUnique();
51 static PassRefPtr<SecurityOrigin> createFromDatabaseIdentifier(const String&);
52 static PassRefPtr<SecurityOrigin> createFromString(const String&);
53 static PassRefPtr<SecurityOrigin> create(const String& protocol, const String& host, int port);
55 // Create a deep copy of this SecurityOrigin. This method is useful
56 // when marshalling a SecurityOrigin to another thread.
57 PassRefPtr<SecurityOrigin> isolatedCopy();
59 // Set the domain property of this security origin to newDomain. This
60 // function does not check whether newDomain is a suffix of the current
61 // domain. The caller is responsible for validating newDomain.
62 void setDomainFromDOM(const String& newDomain);
63 bool domainWasSetInDOM() const { return m_domainWasSetInDOM; }
65 String protocol() const { return m_protocol; }
66 String host() const { return m_host; }
67 String domain() const { return m_domain; }
68 unsigned short port() const { return m_port; }
70 // Returns true if a given URL is secure, based either directly on its
71 // own protocol, or, when relevant, on the protocol of its "inner URL"
72 // Protocols like blob: and filesystem: fall into this latter category.
73 static bool isSecure(const KURL&);
75 // Returns true if this SecurityOrigin can script objects in the given
76 // SecurityOrigin. For example, call this function before allowing
77 // script from one security origin to read or write objects from
78 // another SecurityOrigin.
79 bool canAccess(const SecurityOrigin*) const;
81 // Returns true if this SecurityOrigin can read content retrieved from
82 // the given URL. For example, call this function before issuing
84 bool canRequest(const KURL&) const;
86 // Returns true if drawing an image from this URL taints a canvas from
87 // this security origin. For example, call this function before
88 // drawing an image onto an HTML canvas element with the drawImage API.
89 bool taintsCanvas(const KURL&) const;
91 // Returns true if this SecurityOrigin can receive drag content from the
92 // initiator. For example, call this function before allowing content to be
93 // dropped onto a target.
94 bool canReceiveDragData(const SecurityOrigin* dragInitiator) const;
96 // Returns true if |document| can display content from the given URL (e.g.,
97 // in an iframe or as an image). For example, web sites generally cannot
98 // display content from the user's files system.
99 bool canDisplay(const KURL&) const;
101 // Returns true if this SecurityOrigin can load local resources, such
102 // as images, iframes, and style sheets, and can link to local URLs.
103 // For example, call this function before creating an iframe to a
106 // Note: A SecurityOrigin might be allowed to load local resources
107 // without being able to issue an XMLHttpRequest for a local URL.
108 // To determine whether the SecurityOrigin can issue an
109 // XMLHttpRequest for a URL, call canRequest(url).
110 bool canLoadLocalResources() const { return m_canLoadLocalResources; }
112 // Explicitly grant the ability to load local resources to this
115 // Note: This method exists only to support backwards compatibility
116 // with older versions of WebKit.
117 void grantLoadLocalResources();
119 // Explicitly grant the ability to access very other SecurityOrigin.
121 // WARNING: This is an extremely powerful ability. Use with caution!
122 void grantUniversalAccess();
124 bool canAccessDatabase() const { return !isUnique(); }
125 bool canAccessLocalStorage() const { return !isUnique(); }
126 bool canAccessCookies() const { return !isUnique(); }
127 bool canAccessPasswordManager() const { return !isUnique(); }
128 bool canAccessFileSystem() const { return !isUnique(); }
129 Policy canShowNotifications() const;
131 // Technically, we should always allow access to sessionStorage, but we
132 // currently don't handle creating a sessionStorage area for unique
134 bool canAccessSessionStorage() const { return !isUnique(); }
136 // The local SecurityOrigin is the most privileged SecurityOrigin.
137 // The local SecurityOrigin can script any document, navigate to local
138 // resources, and can set arbitrary headers on XMLHttpRequests.
139 bool isLocal() const;
141 // The origin is a globally unique identifier assigned when the Document is
142 // created. http://www.whatwg.org/specs/web-apps/current-work/#sandboxOrigin
144 // There's a subtle difference between a unique origin and an origin that
145 // has the SandboxOrigin flag set. The latter implies the former, and, in
146 // addition, the SandboxOrigin flag is inherited by iframes.
147 bool isUnique() const { return m_isUnique; }
149 // Marks a file:// origin as being in a domain defined by its path.
150 // FIXME 81578: The naming of this is confusing. Files with restricted access to other local files
151 // still can have other privileges that can be remembered, thereby not making them unique.
152 void enforceFilePathSeparation();
154 // Convert this SecurityOrigin into a string. The string
155 // representation of a SecurityOrigin is similar to a URL, except it
156 // lacks a path component. The string representation does not encode
157 // the value of the SecurityOrigin's domain property.
159 // When using the string value, it's important to remember that it might be
160 // "null". This happens when this SecurityOrigin is unique. For example,
161 // this SecurityOrigin might have come from a sandboxed iframe, the
162 // SecurityOrigin might be empty, or we might have explicitly decided that
163 // we shouldTreatURLSchemeAsNoAccess.
164 String toString() const;
166 // Similar to toString(), but does not take into account any factors that
167 // could make the string return "null".
168 String toRawString() const;
170 // Serialize the security origin to a string that could be used as part of
171 // file names. This format should be used in storage APIs only.
172 String databaseIdentifier() const;
174 // This method checks for equality between SecurityOrigins, not whether
175 // one origin can access another. It is used for hash table keys.
176 // For access checks, use canAccess().
177 // FIXME: If this method is really only useful for hash table keys, it
178 // should be refactored into SecurityOriginHash.
179 bool equal(const SecurityOrigin*) const;
181 // This method checks for equality, ignoring the value of document.domain
182 // (and whether it was set) but considering the host. It is used for postMessage.
183 bool isSameSchemeHostPort(const SecurityOrigin*) const;
187 explicit SecurityOrigin(const KURL&);
188 explicit SecurityOrigin(const SecurityOrigin*);
190 // FIXME: Rename this function to something more semantic.
191 bool passesFileCheck(const SecurityOrigin*) const;
195 mutable String m_encodedHost;
198 unsigned short m_port;
200 bool m_universalAccess;
201 bool m_domainWasSetInDOM;
202 bool m_canLoadLocalResources;
203 bool m_enforceFilePathSeparation;
204 bool m_needsDatabaseIdentifierQuirkForFiles;
207 } // namespace WebCore
209 #endif // SecurityOrigin_h