1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 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 Google, Inc. ("Google") 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. 16 * 17 * THIS SOFTWARE IS PROVIDED BY GOOGLE 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. 27 */ 28 29 #include "config.h" 30 #include "platform/weborigin/SecurityPolicy.h" 31 32 #include "platform/weborigin/KURL.h" 33 #include "platform/weborigin/OriginAccessEntry.h" 34 #include "platform/weborigin/SecurityOrigin.h" 35 #include "wtf/HashMap.h" 36 #include "wtf/MainThread.h" 37 #include "wtf/OwnPtr.h" 38 #include "wtf/PassOwnPtr.h" 39 #include "wtf/text/StringHash.h" 40 41 namespace WebCore { 42 43 typedef Vector<OriginAccessEntry> OriginAccessWhiteList; 44 typedef HashMap<String, OwnPtr<OriginAccessWhiteList> > OriginAccessMap; 45 46 static OriginAccessMap& originAccessMap() 47 { 48 DEFINE_STATIC_LOCAL(OriginAccessMap, originAccessMap, ()); 49 return originAccessMap; 50 } 51 52 bool SecurityPolicy::shouldHideReferrer(const KURL& url, const String& referrer) 53 { 54 bool referrerIsSecureURL = protocolIs(referrer, "https"); 55 bool referrerIsWebURL = referrerIsSecureURL || protocolIs(referrer, "http"); 56 57 if (!referrerIsWebURL) 58 return true; 59 60 if (!referrerIsSecureURL) 61 return false; 62 63 bool URLIsSecureURL = url.protocolIs("https"); 64 65 return !URLIsSecureURL; 66 } 67 68 String SecurityPolicy::generateReferrerHeader(ReferrerPolicy referrerPolicy, const KURL& url, const String& referrer) 69 { 70 if (referrer.isEmpty()) 71 return String(); 72 73 switch (referrerPolicy) { 74 case ReferrerPolicyNever: 75 return String(); 76 case ReferrerPolicyAlways: 77 return referrer; 78 case ReferrerPolicyOrigin: { 79 String origin = SecurityOrigin::createFromString(referrer)->toString(); 80 if (origin == "null") 81 return String(); 82 // A security origin is not a canonical URL as it lacks a path. Add / 83 // to turn it into a canonical URL we can use as referrer. 84 return origin + "/"; 85 } 86 case ReferrerPolicyDefault: 87 break; 88 } 89 90 return shouldHideReferrer(url, referrer) ? String() : referrer; 91 } 92 93 bool SecurityPolicy::isAccessWhiteListed(const SecurityOrigin* activeOrigin, const SecurityOrigin* targetOrigin) 94 { 95 if (OriginAccessWhiteList* list = originAccessMap().get(activeOrigin->toString())) { 96 for (size_t i = 0; i < list->size(); ++i) { 97 if (list->at(i).matchesOrigin(*targetOrigin) != OriginAccessEntry::DoesNotMatchOrigin) 98 return true; 99 } 100 } 101 return false; 102 } 103 104 bool SecurityPolicy::isAccessToURLWhiteListed(const SecurityOrigin* activeOrigin, const KURL& url) 105 { 106 RefPtr<SecurityOrigin> targetOrigin = SecurityOrigin::create(url); 107 return isAccessWhiteListed(activeOrigin, targetOrigin.get()); 108 } 109 110 void SecurityPolicy::addOriginAccessWhitelistEntry(const SecurityOrigin& sourceOrigin, const String& destinationProtocol, const String& destinationDomain, bool allowDestinationSubdomains) 111 { 112 ASSERT(isMainThread()); 113 ASSERT(!sourceOrigin.isUnique()); 114 if (sourceOrigin.isUnique()) 115 return; 116 117 String sourceString = sourceOrigin.toString(); 118 OriginAccessMap::AddResult result = originAccessMap().add(sourceString, nullptr); 119 if (result.isNewEntry) 120 result.iterator->value = adoptPtr(new OriginAccessWhiteList); 121 122 OriginAccessWhiteList* list = result.iterator->value.get(); 123 list->append(OriginAccessEntry(destinationProtocol, destinationDomain, allowDestinationSubdomains ? OriginAccessEntry::AllowSubdomains : OriginAccessEntry::DisallowSubdomains, OriginAccessEntry::TreatIPAddressAsIPAddress)); 124 } 125 126 void SecurityPolicy::removeOriginAccessWhitelistEntry(const SecurityOrigin& sourceOrigin, const String& destinationProtocol, const String& destinationDomain, bool allowDestinationSubdomains) 127 { 128 ASSERT(isMainThread()); 129 ASSERT(!sourceOrigin.isUnique()); 130 if (sourceOrigin.isUnique()) 131 return; 132 133 String sourceString = sourceOrigin.toString(); 134 OriginAccessMap& map = originAccessMap(); 135 OriginAccessMap::iterator it = map.find(sourceString); 136 if (it == map.end()) 137 return; 138 139 OriginAccessWhiteList* list = it->value.get(); 140 size_t index = list->find(OriginAccessEntry(destinationProtocol, destinationDomain, allowDestinationSubdomains ? OriginAccessEntry::AllowSubdomains : OriginAccessEntry::DisallowSubdomains, OriginAccessEntry::TreatIPAddressAsIPAddress)); 141 if (index == kNotFound) 142 return; 143 144 list->remove(index); 145 146 if (list->isEmpty()) 147 map.remove(it); 148 } 149 150 void SecurityPolicy::resetOriginAccessWhitelists() 151 { 152 ASSERT(isMainThread()); 153 originAccessMap().clear(); 154 } 155 156 } // namespace WebCore 157