Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: Source/core/fetch/CrossOriginAccessControl.cpp

Issue 109773002: Improve precision of error messages from failed CORS checks. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « LayoutTests/platform/mac-snowleopard/http/tests/xmlhttprequest/origin-exact-matching-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 const HTTPHeaderMap& requestHeaderFields = request.httpHeaderFields(); 116 const HTTPHeaderMap& requestHeaderFields = request.httpHeaderFields();
117 117
118 if (requestHeaderFields.size() > 0) { 118 if (requestHeaderFields.size() > 0) {
119 StringBuilder headerBuffer; 119 StringBuilder headerBuffer;
120 HTTPHeaderMap::const_iterator it = requestHeaderFields.begin(); 120 HTTPHeaderMap::const_iterator it = requestHeaderFields.begin();
121 headerBuffer.append(it->key); 121 headerBuffer.append(it->key);
122 ++it; 122 ++it;
123 123
124 HTTPHeaderMap::const_iterator end = requestHeaderFields.end(); 124 HTTPHeaderMap::const_iterator end = requestHeaderFields.end();
125 for (; it != end; ++it) { 125 for (; it != end; ++it) {
126 headerBuffer.append(','); 126 headerBuffer.appendLiteral(", ");
127 headerBuffer.append(' ');
128 headerBuffer.append(it->key); 127 headerBuffer.append(it->key);
129 } 128 }
130 129
131 preflightRequest.setHTTPHeaderField("Access-Control-Request-Headers", he aderBuffer.toString().lower()); 130 preflightRequest.setHTTPHeaderField("Access-Control-Request-Headers", he aderBuffer.toString().lower());
132 } 131 }
133 132
134 return preflightRequest; 133 return preflightRequest;
135 } 134 }
136 135
136 static bool isOriginSeparator(UChar ch)
137 {
138 return isASCIISpace(ch) || ch == ',';
139 }
140
137 bool passesAccessControlCheck(const ResourceResponse& response, StoredCredential s includeCredentials, SecurityOrigin* securityOrigin, String& errorDescription) 141 bool passesAccessControlCheck(const ResourceResponse& response, StoredCredential s includeCredentials, SecurityOrigin* securityOrigin, String& errorDescription)
138 { 142 {
139 AtomicallyInitializedStatic(AtomicString&, accessControlAllowOrigin = *new A tomicString("access-control-allow-origin", AtomicString::ConstructFromLiteral)); 143 AtomicallyInitializedStatic(AtomicString&, accessControlAllowOrigin = *new A tomicString("access-control-allow-origin", AtomicString::ConstructFromLiteral));
140 AtomicallyInitializedStatic(AtomicString&, accessControlAllowCredentials = * new AtomicString("access-control-allow-credentials", AtomicString::ConstructFrom Literal)); 144 AtomicallyInitializedStatic(AtomicString&, accessControlAllowCredentials = * new AtomicString("access-control-allow-credentials", AtomicString::ConstructFrom Literal));
141 145
142 // A wildcard Access-Control-Allow-Origin can not be used if credentials are to be sent, 146 // A wildcard Access-Control-Allow-Origin can not be used if credentials are to be sent,
143 // even with Access-Control-Allow-Credentials set to true. 147 // even with Access-Control-Allow-Credentials set to true.
144 const AtomicString& accessControlOriginString = response.httpHeaderField(acc essControlAllowOrigin); 148 const AtomicString& accessControlOriginString = response.httpHeaderField(acc essControlAllowOrigin);
145 if (accessControlOriginString == starAtom && includeCredentials == DoNotAllo wStoredCredentials) 149 if (accessControlOriginString == starAtom && includeCredentials == DoNotAllo wStoredCredentials)
146 return true; 150 return true;
147 151
148 // FIXME: Access-Control-Allow-Origin can contain a list of origins.
149 if (accessControlOriginString != securityOrigin->toString()) { 152 if (accessControlOriginString != securityOrigin->toString()) {
150 if (accessControlOriginString == starAtom) { 153 if (accessControlOriginString == starAtom) {
151 errorDescription = "Wildcards cannot be used in the 'Access-Control- Allow-Origin' header when the credentials flag is true. Origin '" + securityOrig in->toString() + "' is therefore not allowed access."; 154 errorDescription = "A wildcard '*' cannot be used in the 'Access-Con trol-Allow-Origin' header when the credentials flag is true. Origin '" + securit yOrigin->toString() + "' is therefore not allowed access.";
152 } else if (accessControlOriginString.isEmpty()) { 155 } else if (accessControlOriginString.isEmpty()) {
153 errorDescription = "No 'Access-Control-Allow-Origin' header is prese nt on the requested resource. Origin '" + securityOrigin->toString() + "' is the refore not allowed access."; 156 errorDescription = "No 'Access-Control-Allow-Origin' header is prese nt on the requested resource. Origin '" + securityOrigin->toString() + "' is the refore not allowed access.";
157 } else if (accessControlOriginString.string().find(isOriginSeparator, 0) != kNotFound) {
158 errorDescription = "The 'Access-Control-Allow-Origin' header contain s multiple values '" + accessControlOriginString + "', but only one is allowed. Origin '" + securityOrigin->toString() + "' is therefore not allowed access.";
154 } else { 159 } else {
155 KURL headerOrigin(KURL(), accessControlOriginString); 160 KURL headerOrigin(KURL(), accessControlOriginString);
156 if (!headerOrigin.isValid()) 161 if (!headerOrigin.isValid())
157 errorDescription = "The 'Access-Control-Allow-Origin' header con tains the invalid value '" + accessControlOriginString + "'. Origin '" + securit yOrigin->toString() + "' is therefore not allowed access."; 162 errorDescription = "The 'Access-Control-Allow-Origin' header con tains the invalid value '" + accessControlOriginString + "'. Origin '" + securit yOrigin->toString() + "' is therefore not allowed access.";
158 else 163 else
159 errorDescription = "The 'Access-Control-Allow-Origin' whitelists only '" + accessControlOriginString + "'. Origin '" + securityOrigin->toString( ) + "' is not in the list, and is therefore not allowed access."; 164 errorDescription = "The 'Access-Control-Allow-Origin' header has a value '" + accessControlOriginString + "' that is not equal to the supplied o rigin. Origin '" + securityOrigin->toString() + "' is therefore not allowed acce ss.";
160 } 165 }
161 return false; 166 return false;
162 } 167 }
163 168
164 if (includeCredentials == AllowStoredCredentials) { 169 if (includeCredentials == AllowStoredCredentials) {
165 const AtomicString& accessControlCredentialsString = response.httpHeader Field(accessControlAllowCredentials); 170 const AtomicString& accessControlCredentialsString = response.httpHeader Field(accessControlAllowCredentials);
166 if (accessControlCredentialsString != "true") { 171 if (accessControlCredentialsString != "true") {
167 errorDescription = "Credentials flag is 'true', but the 'Access-Cont rol-Allow-Credentials' header is '" + accessControlCredentialsString + "'. It mu st be 'true' to allow credentials."; 172 errorDescription = "Credentials flag is 'true', but the 'Access-Cont rol-Allow-Credentials' header is '" + accessControlCredentialsString + "'. It mu st be 'true' to allow credentials.";
168 return false; 173 return false;
169 } 174 }
(...skipping 17 matching lines...) Expand all
187 Vector<String> headers; 192 Vector<String> headers;
188 headerValue.split(',', false, headers); 193 headerValue.split(',', false, headers);
189 for (unsigned headerCount = 0; headerCount < headers.size(); headerCount++) { 194 for (unsigned headerCount = 0; headerCount < headers.size(); headerCount++) {
190 String strippedHeader = headers[headerCount].stripWhiteSpace(); 195 String strippedHeader = headers[headerCount].stripWhiteSpace();
191 if (!strippedHeader.isEmpty()) 196 if (!strippedHeader.isEmpty())
192 headerSet.add(strippedHeader); 197 headerSet.add(strippedHeader);
193 } 198 }
194 } 199 }
195 200
196 } // namespace WebCore 201 } // namespace WebCore
OLDNEW
« no previous file with comments | « LayoutTests/platform/mac-snowleopard/http/tests/xmlhttprequest/origin-exact-matching-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698