This source file includes following definitions.
- schemeRequiresAuthority
- cachedOrigin
- shouldUseInnerURL
- extractInnerURL
- setCache
- shouldTreatAsUniqueOrigin
- m_needsDatabaseIdentifierQuirkForFiles
- m_needsDatabaseIdentifierQuirkForFiles
- m_needsDatabaseIdentifierQuirkForFiles
- create
- createUnique
- isolatedCopy
- setDomainFromDOM
- isSecure
- canAccess
- passesFileCheck
- canRequest
- taintsCanvas
- canReceiveDragData
- isFeedWithNestedProtocolInHTTPFamily
- canDisplay
- canShowNotifications
- grantLoadLocalResources
- grantUniversalAccess
- enforceFilePathSeparation
- isLocal
- toString
- toAtomicString
- toRawString
- toRawAtomicString
- buildRawString
- createFromString
- create
- isSameSchemeHostPort
- urlWithUniqueSecurityOrigin
#include "config.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "platform/weborigin/KURL.h"
#include "platform/weborigin/KnownPorts.h"
#include "platform/weborigin/SchemeRegistry.h"
#include "platform/weborigin/SecurityOriginCache.h"
#include "platform/weborigin/SecurityPolicy.h"
#include "wtf/HexNumber.h"
#include "wtf/MainThread.h"
#include "wtf/StdLibExtras.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
const int InvalidPort = 0;
const int MaxAllowedPort = 65535;
static SecurityOriginCache* s_originCache = 0;
static bool schemeRequiresAuthority(const KURL& url)
{
return url.protocolIsInHTTPFamily() || url.protocolIs("ftp");
}
static SecurityOrigin* cachedOrigin(const KURL& url)
{
if (s_originCache)
return s_originCache->cachedOrigin(url);
return 0;
}
bool SecurityOrigin::shouldUseInnerURL(const KURL& url)
{
if (url.protocolIs("blob"))
return true;
if (url.protocolIs("filesystem"))
return true;
return false;
}
KURL SecurityOrigin::extractInnerURL(const KURL& url)
{
if (url.innerURL())
return *url.innerURL();
return KURL(ParsedURLString, decodeURLEscapeSequences(url.path()));
}
void SecurityOrigin::setCache(SecurityOriginCache* originCache)
{
s_originCache = originCache;
}
static bool shouldTreatAsUniqueOrigin(const KURL& url)
{
if (!url.isValid())
return true;
KURL innerURL = SecurityOrigin::shouldUseInnerURL(url) ? SecurityOrigin::extractInnerURL(url) : url;
if (schemeRequiresAuthority(innerURL) && innerURL.host().isEmpty())
return true;
String protocol = innerURL.protocol().lower();
if (SchemeRegistry::shouldTreatURLSchemeAsNoAccess(protocol))
return true;
return false;
}
SecurityOrigin::SecurityOrigin(const KURL& url)
: m_protocol(url.protocol().isNull() ? "" : url.protocol().lower())
, m_host(url.host().isNull() ? "" : url.host().lower())
, m_port(url.port())
, m_isUnique(false)
, m_universalAccess(false)
, m_domainWasSetInDOM(false)
, m_enforceFilePathSeparation(false)
, m_needsDatabaseIdentifierQuirkForFiles(false)
{
m_domain = m_host;
if (isDefaultPortForProtocol(m_port, m_protocol))
m_port = InvalidPort;
m_canLoadLocalResources = isLocal();
if (m_canLoadLocalResources)
m_filePath = url.path();
}
SecurityOrigin::SecurityOrigin()
: m_protocol("")
, m_host("")
, m_domain("")
, m_port(InvalidPort)
, m_isUnique(true)
, m_universalAccess(false)
, m_domainWasSetInDOM(false)
, m_canLoadLocalResources(false)
, m_enforceFilePathSeparation(false)
, m_needsDatabaseIdentifierQuirkForFiles(false)
{
}
SecurityOrigin::SecurityOrigin(const SecurityOrigin* other)
: m_protocol(other->m_protocol.isolatedCopy())
, m_host(other->m_host.isolatedCopy())
, m_domain(other->m_domain.isolatedCopy())
, m_filePath(other->m_filePath.isolatedCopy())
, m_port(other->m_port)
, m_isUnique(other->m_isUnique)
, m_universalAccess(other->m_universalAccess)
, m_domainWasSetInDOM(other->m_domainWasSetInDOM)
, m_canLoadLocalResources(other->m_canLoadLocalResources)
, m_enforceFilePathSeparation(other->m_enforceFilePathSeparation)
, m_needsDatabaseIdentifierQuirkForFiles(other->m_needsDatabaseIdentifierQuirkForFiles)
{
}
PassRefPtr<SecurityOrigin> SecurityOrigin::create(const KURL& url)
{
if (RefPtr<SecurityOrigin> origin = cachedOrigin(url))
return origin.release();
if (shouldTreatAsUniqueOrigin(url)) {
RefPtr<SecurityOrigin> origin = adoptRef(new SecurityOrigin());
if (url.protocolIs("file")) {
origin->m_needsDatabaseIdentifierQuirkForFiles = true;
}
return origin.release();
}
if (shouldUseInnerURL(url))
return adoptRef(new SecurityOrigin(extractInnerURL(url)));
return adoptRef(new SecurityOrigin(url));
}
PassRefPtr<SecurityOrigin> SecurityOrigin::createUnique()
{
RefPtr<SecurityOrigin> origin = adoptRef(new SecurityOrigin());
ASSERT(origin->isUnique());
return origin.release();
}
PassRefPtr<SecurityOrigin> SecurityOrigin::isolatedCopy() const
{
return adoptRef(new SecurityOrigin(this));
}
void SecurityOrigin::setDomainFromDOM(const String& newDomain)
{
m_domainWasSetInDOM = true;
m_domain = newDomain.lower();
}
bool SecurityOrigin::isSecure(const KURL& url)
{
if (!url.isValid() || SchemeRegistry::shouldTreatURLSchemeAsSecure(url.protocol()))
return true;
if (shouldUseInnerURL(url) && SchemeRegistry::shouldTreatURLSchemeAsSecure(extractInnerURL(url).protocol()))
return true;
return false;
}
bool SecurityOrigin::canAccess(const SecurityOrigin* other) const
{
if (m_universalAccess)
return true;
if (this == other)
return true;
if (isUnique() || other->isUnique())
return false;
bool canAccess = false;
if (m_protocol == other->m_protocol) {
if (!m_domainWasSetInDOM && !other->m_domainWasSetInDOM) {
if (m_host == other->m_host && m_port == other->m_port)
canAccess = true;
} else if (m_domainWasSetInDOM && other->m_domainWasSetInDOM) {
if (m_domain == other->m_domain)
canAccess = true;
}
}
if (canAccess && isLocal())
canAccess = passesFileCheck(other);
return canAccess;
}
bool SecurityOrigin::passesFileCheck(const SecurityOrigin* other) const
{
ASSERT(isLocal() && other->isLocal());
if (!m_enforceFilePathSeparation && !other->m_enforceFilePathSeparation)
return true;
return (m_filePath == other->m_filePath);
}
bool SecurityOrigin::canRequest(const KURL& url) const
{
if (m_universalAccess)
return true;
if (cachedOrigin(url) == this)
return true;
if (isUnique())
return false;
RefPtr<SecurityOrigin> targetOrigin = SecurityOrigin::create(url);
if (targetOrigin->isUnique())
return false;
if (isSameSchemeHostPort(targetOrigin.get()))
return true;
if (SecurityPolicy::isAccessWhiteListed(this, targetOrigin.get()))
return true;
return false;
}
bool SecurityOrigin::taintsCanvas(const KURL& url) const
{
if (canRequest(url))
return false;
if (url.protocolIsData())
return false;
return true;
}
bool SecurityOrigin::canReceiveDragData(const SecurityOrigin* dragInitiator) const
{
if (this == dragInitiator)
return true;
return canAccess(dragInitiator);
}
static bool isFeedWithNestedProtocolInHTTPFamily(const KURL& url)
{
const String& urlString = url.string();
if (!urlString.startsWith("feed", false))
return false;
return urlString.startsWith("feed://", false)
|| urlString.startsWith("feed:http:", false) || urlString.startsWith("feed:https:", false)
|| urlString.startsWith("feeds:http:", false) || urlString.startsWith("feeds:https:", false)
|| urlString.startsWith("feedsearch:http:", false) || urlString.startsWith("feedsearch:https:", false);
}
bool SecurityOrigin::canDisplay(const KURL& url) const
{
if (m_universalAccess)
return true;
String protocol = url.protocol().lower();
if (isFeedWithNestedProtocolInHTTPFamily(url))
return true;
if (SchemeRegistry::canDisplayOnlyIfCanRequest(protocol))
return canRequest(url);
if (SchemeRegistry::shouldTreatURLSchemeAsDisplayIsolated(protocol))
return m_protocol == protocol || SecurityPolicy::isAccessToURLWhiteListed(this, url);
if (SchemeRegistry::shouldTreatURLSchemeAsLocal(protocol))
return canLoadLocalResources() || SecurityPolicy::isAccessToURLWhiteListed(this, url);
return true;
}
SecurityOrigin::Policy SecurityOrigin::canShowNotifications() const
{
if (m_universalAccess)
return AlwaysAllow;
if (isUnique())
return AlwaysDeny;
return Ask;
}
void SecurityOrigin::grantLoadLocalResources()
{
m_canLoadLocalResources = true;
}
void SecurityOrigin::grantUniversalAccess()
{
m_universalAccess = true;
}
void SecurityOrigin::enforceFilePathSeparation()
{
ASSERT(isLocal());
m_enforceFilePathSeparation = true;
}
bool SecurityOrigin::isLocal() const
{
return SchemeRegistry::shouldTreatURLSchemeAsLocal(m_protocol);
}
String SecurityOrigin::toString() const
{
if (isUnique())
return "null";
if (m_protocol == "file" && m_enforceFilePathSeparation)
return "null";
return toRawString();
}
AtomicString SecurityOrigin::toAtomicString() const
{
if (isUnique())
return AtomicString("null", AtomicString::ConstructFromLiteral);
if (m_protocol == "file" && m_enforceFilePathSeparation)
return AtomicString("null", AtomicString::ConstructFromLiteral);
return toRawAtomicString();
}
String SecurityOrigin::toRawString() const
{
if (m_protocol == "file")
return "file://";
StringBuilder result;
buildRawString(result);
return result.toString();
}
AtomicString SecurityOrigin::toRawAtomicString() const
{
if (m_protocol == "file")
return AtomicString("file://", AtomicString::ConstructFromLiteral);
StringBuilder result;
buildRawString(result);
return result.toAtomicString();
}
inline void SecurityOrigin::buildRawString(StringBuilder& builder) const
{
builder.reserveCapacity(m_protocol.length() + m_host.length() + 10);
builder.append(m_protocol);
builder.appendLiteral("://");
builder.append(m_host);
if (m_port) {
builder.append(':');
builder.appendNumber(m_port);
}
}
PassRefPtr<SecurityOrigin> SecurityOrigin::createFromString(const String& originString)
{
return SecurityOrigin::create(KURL(KURL(), originString));
}
PassRefPtr<SecurityOrigin> SecurityOrigin::create(const String& protocol, const String& host, int port)
{
if (port < 0 || port > MaxAllowedPort)
return createUnique();
String decodedHost = decodeURLEscapeSequences(host);
return create(KURL(KURL(), protocol + "://" + host + ":" + String::number(port) + "/"));
}
bool SecurityOrigin::isSameSchemeHostPort(const SecurityOrigin* other) const
{
if (m_host != other->m_host)
return false;
if (m_protocol != other->m_protocol)
return false;
if (m_port != other->m_port)
return false;
if (isLocal() && !passesFileCheck(other))
return false;
return true;
}
const String& SecurityOrigin::urlWithUniqueSecurityOrigin()
{
ASSERT(isMainThread());
DEFINE_STATIC_LOCAL(const String, uniqueSecurityOriginURL, ("data:,"));
return uniqueSecurityOriginURL;
}
}