2
* Copyright (C) 2008, 2009 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
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.
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.
28
#include "CrossOriginPreflightResultCache.h"
30
#include "CrossOriginAccessControl.h"
31
#include "ResourceResponse.h"
32
#include <wtf/CurrentTime.h>
33
#include <wtf/MainThread.h>
34
#include <wtf/StdLibExtras.h>
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.
44
static bool parseAccessControlMaxAge(const String& string, unsigned& expiryDelta)
46
// FIXME: this will not do the correct thing for a number starting with a '+'
48
expiryDelta = string.toUIntStrict(&ok);
52
template<class HashType>
53
static void addToAccessControlAllowList(const String& string, unsigned start, unsigned end, HashSet<String, HashType>& set)
55
StringImpl* stringImpl = string.impl();
59
// Skip white space from start.
60
while (start <= end && isSpaceOrNewline((*stringImpl)[start]))
67
// Skip white space from end.
68
while (end && isSpaceOrNewline((*stringImpl)[end]))
71
set.add(string.substring(start, end - start + 1));
74
template<class HashType>
75
static bool parseAccessControlAllowList(const String& string, HashSet<String, HashType>& set)
79
while ((end = string.find(',', start)) != notFound) {
81
addToAccessControlAllowList(string, start, end - 1, set);
84
if (start != string.length())
85
addToAccessControlAllowList(string, start, string.length() - 1, set);
90
bool CrossOriginPreflightResultCacheItem::parse(const ResourceResponse& response, String& errorDescription)
93
if (!parseAccessControlAllowList(response.httpHeaderField("Access-Control-Allow-Methods"), m_methods)) {
94
errorDescription = "Cannot parse Access-Control-Allow-Methods response header field.";
99
if (!parseAccessControlAllowList(response.httpHeaderField("Access-Control-Allow-Headers"), m_headers)) {
100
errorDescription = "Cannot parse Access-Control-Allow-Headers response header field.";
104
unsigned expiryDelta;
105
if (parseAccessControlMaxAge(response.httpHeaderField("Access-Control-Max-Age"), expiryDelta)) {
106
if (expiryDelta > maxPreflightCacheTimeoutSeconds)
107
expiryDelta = maxPreflightCacheTimeoutSeconds;
109
expiryDelta = defaultPreflightCacheTimeoutSeconds;
111
m_absoluteExpiryTime = currentTime() + expiryDelta;
115
bool CrossOriginPreflightResultCacheItem::allowsCrossOriginMethod(const String& method, String& errorDescription) const
117
if (m_methods.contains(method) || isOnAccessControlSimpleRequestMethodWhitelist(method))
120
errorDescription = "Method " + method + " is not allowed by Access-Control-Allow-Methods.";
124
bool CrossOriginPreflightResultCacheItem::allowsCrossOriginHeaders(const HTTPHeaderMap& requestHeaders, String& errorDescription) const
126
HTTPHeaderMap::const_iterator end = requestHeaders.end();
127
for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
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.";
136
bool CrossOriginPreflightResultCacheItem::allowsRequest(StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders) const
138
String ignoredExplanation;
139
if (m_absoluteExpiryTime < currentTime())
141
if (includeCredentials == AllowStoredCredentials && m_credentials == DoNotAllowStoredCredentials)
143
if (!allowsCrossOriginMethod(method, ignoredExplanation))
145
if (!allowsCrossOriginHeaders(requestHeaders, ignoredExplanation))
150
CrossOriginPreflightResultCache& CrossOriginPreflightResultCache::shared()
152
DEFINE_STATIC_LOCAL(CrossOriginPreflightResultCache, cache, ());
153
ASSERT(isMainThread());
157
void CrossOriginPreflightResultCache::appendEntry(const String& origin, const KURL& url, PassOwnPtr<CrossOriginPreflightResultCacheItem> preflightResult)
159
ASSERT(isMainThread());
160
m_preflightHashMap.set(make_pair(origin, url), preflightResult);
163
bool CrossOriginPreflightResultCache::canSkipPreflight(const String& origin, const KURL& url, StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders)
165
ASSERT(isMainThread());
166
CrossOriginPreflightResultHashMap::iterator cacheIt = m_preflightHashMap.find(make_pair(origin, url));
167
if (cacheIt == m_preflightHashMap.end())
170
if (cacheIt->value->allowsRequest(includeCredentials, method, requestHeaders))
173
m_preflightHashMap.remove(cacheIt);
177
void CrossOriginPreflightResultCache::empty()
179
ASSERT(isMainThread());
180
m_preflightHashMap.clear();
183
} // namespace WebCore