Skip to content

Commit

Permalink
Allow XHR to fetch about: and data: URLs.
Browse files Browse the repository at this point in the history
This was intended to fix #8015 but the tests are all still failing as of
this commit.
  • Loading branch information
emosenkis authored and dagnir committed Apr 2, 2016
1 parent c4208e6 commit ae56187
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 10 additions & 3 deletions components/script/cors.rs
Expand Up @@ -64,15 +64,22 @@ impl CORSRequest {
destination: Url,
mode: RequestMode,
method: Method,
headers: Headers)
headers: Headers,
same_origin_data_url_flag: bool)
-> Result<Option<CORSRequest>, ()> {
if referer.scheme == destination.scheme && referer.host() == destination.host() &&
referer.port() == destination.port() {
return Ok(None); // Not cross-origin, proceed with a normal fetch
}
match &*destination.scheme {
// TODO: If the request's same origin data url flag is set (which isn't the case for XHR)
// we can fetch a data URL normally. about:blank can also be fetched by XHR
// As per (https://fetch.spec.whatwg.org/#main-fetch 5.1.9), about URLs can be fetched
// the same as a basic request.
// TODO: (security-sensitive) restrict the available pages to about:blank and
// about:unicorn (See https://fetch.spec.whatwg.org/#concept-basic-fetch).
"about" => Ok(None),
// As per (https://fetch.spec.whatwg.org/#main-fetch 5.1.9), data URLs can be fetched
// the same as a basic request if the request's same-origin data-URL flag is set.
"data" if same_origin_data_url_flag => Ok(None),
"http" | "https" => {
let mut req = CORSRequest::new(referer, destination, mode, method, headers);
req.preflight_flag = !is_simple_method(&req.method) ||
Expand Down
6 changes: 4 additions & 2 deletions components/script/dom/xmlhttprequest.rs
Expand Up @@ -626,7 +626,8 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
load_data.url.clone(),
mode,
load_data.method.clone(),
combined_headers);
combined_headers,
true);
match cors_request {
Ok(None) => {
let mut buf = String::new();
Expand Down Expand Up @@ -1301,7 +1302,8 @@ impl XMLHttpRequest {
global: GlobalRef) -> ErrorResult {
let cors_request = match cors_request {
Err(_) => {
// Happens in case of cross-origin non-http URIs
// Happens in case of unsupported cross-origin URI schemes.
// Supported schemes are http, https, data, and about.
self.process_partial_response(XHRProgress::Errored(
self.generation_id.get(), Error::Network));
return Err(Error::Network);
Expand Down

0 comments on commit ae56187

Please sign in to comment.