ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 1 | /* |
ap@webkit.org | b05cfe4 | 2009-03-16 07:12:01 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved. |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 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 | * 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. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "CrossOriginPreflightResultCache.h" |
| 29 | |
| 30 | #include "CrossOriginAccessControl.h" |
| 31 | #include "ResourceResponse.h" |
| 32 | #include <wtf/CurrentTime.h> |
paroga@webkit.org | c309e0e | 2011-07-31 02:23:31 +0000 | [diff] [blame] | 33 | #include <wtf/MainThread.h> |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 34 | #include <wtf/StdLibExtras.h> |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 35 | |
| 36 | namespace WebCore { |
| 37 | |
darin@apple.com | b07ab5a | 2010-07-06 16:14:44 +0000 | [diff] [blame] | 38 | using namespace std; |
| 39 | |
ap@webkit.org | b05cfe4 | 2009-03-16 07:12:01 +0000 | [diff] [blame] | 40 | // These values are at the discretion of the user agent. |
| 41 | static const unsigned defaultPreflightCacheTimeoutSeconds = 5; |
| 42 | static const unsigned maxPreflightCacheTimeoutSeconds = 600; // Should be short enough to minimize the risk of using a poisoned cache after switching to a secure network. |
| 43 | |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 44 | static bool parseAccessControlMaxAge(const String& string, unsigned& expiryDelta) |
| 45 | { |
| 46 | // FIXME: this will not do the correct thing for a number starting with a '+' |
| 47 | bool ok = false; |
| 48 | expiryDelta = string.toUIntStrict(&ok); |
| 49 | return ok; |
| 50 | } |
| 51 | |
| 52 | template<class HashType> |
| 53 | static void addToAccessControlAllowList(const String& string, unsigned start, unsigned end, HashSet<String, HashType>& set) |
| 54 | { |
| 55 | StringImpl* stringImpl = string.impl(); |
| 56 | if (!stringImpl) |
| 57 | return; |
| 58 | |
| 59 | // Skip white space from start. |
| 60 | while (start <= end && isSpaceOrNewline((*stringImpl)[start])) |
| 61 | ++start; |
| 62 | |
| 63 | // only white space |
| 64 | if (start > end) |
| 65 | return; |
| 66 | |
| 67 | // Skip white space from end. |
| 68 | while (end && isSpaceOrNewline((*stringImpl)[end])) |
| 69 | --end; |
| 70 | |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 71 | set.add(string.substring(start, end - start + 1)); |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template<class HashType> |
| 75 | static bool parseAccessControlAllowList(const String& string, HashSet<String, HashType>& set) |
| 76 | { |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 77 | unsigned start = 0; |
| 78 | size_t end; |
| 79 | while ((end = string.find(',', start)) != notFound) { |
commit-queue@webkit.org | 7a4d9ce | 2012-06-10 20:18:36 +0000 | [diff] [blame] | 80 | if (start != end) |
| 81 | addToAccessControlAllowList(string, start, end - 1, set); |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 82 | start = end + 1; |
| 83 | } |
barraclough@apple.com | d643fde | 2010-08-16 23:31:33 +0000 | [diff] [blame] | 84 | if (start != string.length()) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 85 | addToAccessControlAllowList(string, start, string.length() - 1, set); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 90 | bool CrossOriginPreflightResultCacheItem::parse(const ResourceResponse& response, String& errorDescription) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 91 | { |
| 92 | m_methods.clear(); |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 93 | if (!parseAccessControlAllowList(response.httpHeaderField("Access-Control-Allow-Methods"), m_methods)) { |
| 94 | errorDescription = "Cannot parse Access-Control-Allow-Methods response header field."; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 95 | return false; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 96 | } |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 97 | |
| 98 | m_headers.clear(); |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 99 | if (!parseAccessControlAllowList(response.httpHeaderField("Access-Control-Allow-Headers"), m_headers)) { |
| 100 | errorDescription = "Cannot parse Access-Control-Allow-Headers response header field."; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 101 | return false; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 102 | } |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 103 | |
| 104 | unsigned expiryDelta; |
ap@webkit.org | b05cfe4 | 2009-03-16 07:12:01 +0000 | [diff] [blame] | 105 | if (parseAccessControlMaxAge(response.httpHeaderField("Access-Control-Max-Age"), expiryDelta)) { |
| 106 | if (expiryDelta > maxPreflightCacheTimeoutSeconds) |
| 107 | expiryDelta = maxPreflightCacheTimeoutSeconds; |
| 108 | } else |
| 109 | expiryDelta = defaultPreflightCacheTimeoutSeconds; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 110 | |
commit-queue@webkit.org | 7aee36d | 2013-08-27 20:47:27 +0000 | [diff] [blame] | 111 | m_absoluteExpiryTime = monotonicallyIncreasingTime() + expiryDelta; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 115 | bool CrossOriginPreflightResultCacheItem::allowsCrossOriginMethod(const String& method, String& errorDescription) const |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 116 | { |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 117 | if (m_methods.contains(method) || isOnAccessControlSimpleRequestMethodWhitelist(method)) |
| 118 | return true; |
| 119 | |
ap@apple.com | 928494d | 2010-07-08 20:38:07 +0000 | [diff] [blame] | 120 | errorDescription = "Method " + method + " is not allowed by Access-Control-Allow-Methods."; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 121 | return false; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 124 | bool CrossOriginPreflightResultCacheItem::allowsCrossOriginHeaders(const HTTPHeaderMap& requestHeaders, String& errorDescription) const |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 125 | { |
| 126 | HTTPHeaderMap::const_iterator end = requestHeaders.end(); |
| 127 | for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) { |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 128 | if (!m_headers.contains(it->key) && !isOnAccessControlSimpleRequestHeaderWhitelist(it->key, it->value)) { |
| 129 | errorDescription = "Request header field " + it->key.string() + " is not allowed by Access-Control-Allow-Headers."; |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 130 | return false; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 131 | } |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
japhet@chromium.org | 40d9a46 | 2011-08-26 17:41:25 +0000 | [diff] [blame] | 136 | bool CrossOriginPreflightResultCacheItem::allowsRequest(StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders) const |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 137 | { |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 138 | String ignoredExplanation; |
commit-queue@webkit.org | 7aee36d | 2013-08-27 20:47:27 +0000 | [diff] [blame] | 139 | if (m_absoluteExpiryTime < monotonicallyIncreasingTime()) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 140 | return false; |
japhet@chromium.org | 40d9a46 | 2011-08-26 17:41:25 +0000 | [diff] [blame] | 141 | if (includeCredentials == AllowStoredCredentials && m_credentials == DoNotAllowStoredCredentials) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 142 | return false; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 143 | if (!allowsCrossOriginMethod(method, ignoredExplanation)) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 144 | return false; |
ap@apple.com | 67639a2 | 2010-07-06 19:10:34 +0000 | [diff] [blame] | 145 | if (!allowsCrossOriginHeaders(requestHeaders, ignoredExplanation)) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 146 | return false; |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | CrossOriginPreflightResultCache& CrossOriginPreflightResultCache::shared() |
| 151 | { |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 152 | DEFINE_STATIC_LOCAL(CrossOriginPreflightResultCache, cache, ()); |
| 153 | ASSERT(isMainThread()); |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 154 | return cache; |
| 155 | } |
| 156 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 157 | void CrossOriginPreflightResultCache::appendEntry(const String& origin, const URL& url, PassOwnPtr<CrossOriginPreflightResultCacheItem> preflightResult) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 158 | { |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 159 | ASSERT(isMainThread()); |
darin@apple.com | b3b28cc | 2011-12-06 02:55:47 +0000 | [diff] [blame] | 160 | m_preflightHashMap.set(make_pair(origin, url), preflightResult); |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 161 | } |
| 162 | |
darin@apple.com | 5ffbb5c | 2013-09-27 16:39:41 +0000 | [diff] [blame] | 163 | bool CrossOriginPreflightResultCache::canSkipPreflight(const String& origin, const URL& url, StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 164 | { |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 165 | ASSERT(isMainThread()); |
darin@apple.com | b3b28cc | 2011-12-06 02:55:47 +0000 | [diff] [blame] | 166 | CrossOriginPreflightResultHashMap::iterator cacheIt = m_preflightHashMap.find(make_pair(origin, url)); |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 167 | if (cacheIt == m_preflightHashMap.end()) |
| 168 | return false; |
| 169 | |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 170 | if (cacheIt->value->allowsRequest(includeCredentials, method, requestHeaders)) |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 171 | return true; |
| 172 | |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 173 | m_preflightHashMap.remove(cacheIt); |
| 174 | return false; |
| 175 | } |
| 176 | |
weinig@apple.com | 376bdde | 2009-03-24 01:31:50 +0000 | [diff] [blame] | 177 | void CrossOriginPreflightResultCache::empty() |
| 178 | { |
dimich@chromium.org | 582ec8f | 2009-08-22 02:06:45 +0000 | [diff] [blame] | 179 | ASSERT(isMainThread()); |
weinig@apple.com | 376bdde | 2009-03-24 01:31:50 +0000 | [diff] [blame] | 180 | m_preflightHashMap.clear(); |
| 181 | } |
| 182 | |
ap@webkit.org | 213b4b0 | 2009-03-10 08:11:04 +0000 | [diff] [blame] | 183 | } // namespace WebCore |