This source file includes following definitions.
- originAccessMap
- shouldHideReferrer
- generateReferrerHeader
- isAccessWhiteListed
- isAccessToURLWhiteListed
- addOriginAccessWhitelistEntry
- removeOriginAccessWhitelistEntry
- resetOriginAccessWhitelists
#include "config.h"
#include "platform/weborigin/SecurityPolicy.h"
#include "platform/weborigin/KURL.h"
#include "platform/weborigin/OriginAccessEntry.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "wtf/HashMap.h"
#include "wtf/MainThread.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/text/StringHash.h"
namespace WebCore {
typedef Vector<OriginAccessEntry> OriginAccessWhiteList;
typedef HashMap<String, OwnPtr<OriginAccessWhiteList> > OriginAccessMap;
static OriginAccessMap& originAccessMap()
{
DEFINE_STATIC_LOCAL(OriginAccessMap, originAccessMap, ());
return originAccessMap;
}
bool SecurityPolicy::shouldHideReferrer(const KURL& url, const String& referrer)
{
bool referrerIsSecureURL = protocolIs(referrer, "https");
bool referrerIsWebURL = referrerIsSecureURL || protocolIs(referrer, "http");
if (!referrerIsWebURL)
return true;
if (!referrerIsSecureURL)
return false;
bool URLIsSecureURL = url.protocolIs("https");
return !URLIsSecureURL;
}
String SecurityPolicy::generateReferrerHeader(ReferrerPolicy referrerPolicy, const KURL& url, const String& referrer)
{
if (referrer.isEmpty())
return String();
if (!(protocolIs(referrer, "https") || protocolIs(referrer, "http")))
return String();
switch (referrerPolicy) {
case ReferrerPolicyNever:
return String();
case ReferrerPolicyAlways:
return referrer;
case ReferrerPolicyOrigin: {
String origin = SecurityOrigin::createFromString(referrer)->toString();
if (origin == "null")
return String();
return origin + "/";
}
case ReferrerPolicyDefault:
break;
}
return shouldHideReferrer(url, referrer) ? String() : referrer;
}
bool SecurityPolicy::isAccessWhiteListed(const SecurityOrigin* activeOrigin, const SecurityOrigin* targetOrigin)
{
if (OriginAccessWhiteList* list = originAccessMap().get(activeOrigin->toString())) {
for (size_t i = 0; i < list->size(); ++i) {
if (list->at(i).matchesOrigin(*targetOrigin) != OriginAccessEntry::DoesNotMatchOrigin)
return true;
}
}
return false;
}
bool SecurityPolicy::isAccessToURLWhiteListed(const SecurityOrigin* activeOrigin, const KURL& url)
{
RefPtr<SecurityOrigin> targetOrigin = SecurityOrigin::create(url);
return isAccessWhiteListed(activeOrigin, targetOrigin.get());
}
void SecurityPolicy::addOriginAccessWhitelistEntry(const SecurityOrigin& sourceOrigin, const String& destinationProtocol, const String& destinationDomain, bool allowDestinationSubdomains)
{
ASSERT(isMainThread());
ASSERT(!sourceOrigin.isUnique());
if (sourceOrigin.isUnique())
return;
String sourceString = sourceOrigin.toString();
OriginAccessMap::AddResult result = originAccessMap().add(sourceString, nullptr);
if (result.isNewEntry)
result.storedValue->value = adoptPtr(new OriginAccessWhiteList);
OriginAccessWhiteList* list = result.storedValue->value.get();
list->append(OriginAccessEntry(destinationProtocol, destinationDomain, allowDestinationSubdomains ? OriginAccessEntry::AllowSubdomains : OriginAccessEntry::DisallowSubdomains, OriginAccessEntry::TreatIPAddressAsIPAddress));
}
void SecurityPolicy::removeOriginAccessWhitelistEntry(const SecurityOrigin& sourceOrigin, const String& destinationProtocol, const String& destinationDomain, bool allowDestinationSubdomains)
{
ASSERT(isMainThread());
ASSERT(!sourceOrigin.isUnique());
if (sourceOrigin.isUnique())
return;
String sourceString = sourceOrigin.toString();
OriginAccessMap& map = originAccessMap();
OriginAccessMap::iterator it = map.find(sourceString);
if (it == map.end())
return;
OriginAccessWhiteList* list = it->value.get();
size_t index = list->find(OriginAccessEntry(destinationProtocol, destinationDomain, allowDestinationSubdomains ? OriginAccessEntry::AllowSubdomains : OriginAccessEntry::DisallowSubdomains, OriginAccessEntry::TreatIPAddressAsIPAddress));
if (index == kNotFound)
return;
list->remove(index);
if (list->isEmpty())
map.remove(it);
}
void SecurityPolicy::resetOriginAccessWhitelists()
{
ASSERT(isMainThread());
originAccessMap().clear();
}
}