Skip to content

Commit

Permalink
Bug 1464525 [wpt PR 11171] - Cross-Origin-Resource-Policy tests, a=te…
Browse files Browse the repository at this point in the history
…stonly

Automatic update from web-platform-testsFetch: Cross-Origin-Resource-Policy tests

For whatwg/fetch#733.

WebKit export of https://bugs.webkit.org/show_bug.cgi?id=185840.

--

wpt-commits: 53f7340307c1c0fa4ab96e79d88c69a7870030f4
wpt-pr: 11171
  • Loading branch information
youennf authored and jgraham committed Jul 11, 2018
1 parent 98d913a commit 2fe2cf7
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 1 deletion.
115 changes: 114 additions & 1 deletion testing/web-platform/meta/MANIFEST.json
Original file line number Diff line number Diff line change
Expand Up @@ -274865,6 +274865,41 @@
{}
]
],
"fetch/cross-origin-resource-policy/resources/green.png": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/hello.py": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/iframe.py": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/iframeFetch.html": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/image.py": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/redirect.py": [
[
{}
]
],
"fetch/cross-origin-resource-policy/resources/script.py": [
[
{}
]
],
"fetch/data-urls/README.md": [
[
{}
Expand Down Expand Up @@ -338884,6 +338919,36 @@
{}
]
],
"fetch/cross-origin-resource-policy/fetch-in-iframe.html": [
[
"/fetch/cross-origin-resource-policy/fetch-in-iframe.html",
{}
]
],
"fetch/cross-origin-resource-policy/fetch.html": [
[
"/fetch/cross-origin-resource-policy/fetch.html",
{}
]
],
"fetch/cross-origin-resource-policy/iframe-loads.html": [
[
"/fetch/cross-origin-resource-policy/iframe-loads.html",
{}
]
],
"fetch/cross-origin-resource-policy/image-loads.html": [
[
"/fetch/cross-origin-resource-policy/image-loads.html",
{}
]
],
"fetch/cross-origin-resource-policy/script-loads.html": [
[
"/fetch/cross-origin-resource-policy/script-loads.html",
{}
]
],
"fetch/data-urls/base64.any.js": [
[
"/fetch/data-urls/base64.any.html",
Expand Down Expand Up @@ -418318,7 +418383,7 @@
"support"
],
"common/get-host-info.sub.js": [
"4175d0fff3555e25a646b0673a082fefdc113fe0",
"1eae4a7e29b73d60b9832ef5d8c808f6fd10db5c",
"support"
],
"common/get-host-info.sub.js.headers": [
Expand Down Expand Up @@ -569125,6 +569190,54 @@
"465d933f4e52ef4e5a4bd0de40873410195843cd",
"testharness"
],
"fetch/cross-origin-resource-policy/fetch-in-iframe.html": [
"4d836bed1e90a2d14b1651c0f3229b3f3d0b6b91",
"testharness"
],
"fetch/cross-origin-resource-policy/fetch.html": [
"6a881615d9df0750b640298725be56e60cd5804c",
"testharness"
],
"fetch/cross-origin-resource-policy/iframe-loads.html": [
"8429fdb1695fc73c853dc37bf29544b8139d5396",
"testharness"
],
"fetch/cross-origin-resource-policy/image-loads.html": [
"6e81ede4b474b2516ec735d4d8f99694b4124773",
"testharness"
],
"fetch/cross-origin-resource-policy/resources/green.png": [
"ef91d21307a12b2cfaf33a90dffe16aa1cba42c9",
"support"
],
"fetch/cross-origin-resource-policy/resources/hello.py": [
"0d8e30350c97fd6a040b14348929cf7e87e0e406",
"support"
],
"fetch/cross-origin-resource-policy/resources/iframe.py": [
"d8f4af86d37d2f257b4166a1f7d3001d55eeda69",
"support"
],
"fetch/cross-origin-resource-policy/resources/iframeFetch.html": [
"d66a9c958288a97469e8cfa75eba973e9f35e190",
"support"
],
"fetch/cross-origin-resource-policy/resources/image.py": [
"72f4bbf045fbb61623246d44b763bd06024c0f63",
"support"
],
"fetch/cross-origin-resource-policy/resources/redirect.py": [
"eb237d6f61e042db8454efad97a7ca58ea90eba9",
"support"
],
"fetch/cross-origin-resource-policy/resources/script.py": [
"330a0ae1420b41e63bd639fa24f75e64e4528bcc",
"support"
],
"fetch/cross-origin-resource-policy/script-loads.html": [
"cd28267293f2d20ee78d6b946fe6b8793edf1bae",
"testharness"
],
"fetch/data-urls/README.md": [
"868cb170fa0c5626008fef77e37dee16e76b10d5",
"support"
Expand Down
2 changes: 2 additions & 0 deletions testing/web-platform/tests/common/get-host-info.sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function get_host_info() {
var ORIGINAL_HOST = '{{host}}';
var REMOTE_HOST = (ORIGINAL_HOST === 'localhost') ? '127.0.0.1' : ('www1.' + ORIGINAL_HOST);
var OTHER_HOST = '{{domains[www2]}}';
var NOTSAMESITE_HOST = (ORIGINAL_HOST === 'localhost') ? '127.0.0.1' : ('not-' + ORIGINAL_HOST);

return {
HTTP_PORT: HTTP_PORT,
Expand All @@ -19,6 +20,7 @@ function get_host_info() {
HTTPS_ORIGIN_WITH_CREDS: 'https://foo:bar@' + ORIGINAL_HOST + ':' + HTTPS_PORT,
HTTP_ORIGIN_WITH_DIFFERENT_PORT: 'http://' + ORIGINAL_HOST + ':' + HTTP_PORT2,
HTTP_REMOTE_ORIGIN: 'http://' + REMOTE_HOST + ':' + HTTP_PORT,
HTTP_NOTSAMESITE_ORIGIN: 'http://' + NOTSAMESITE_HOST + ':' + HTTP_PORT,
HTTP_REMOTE_ORIGIN_WITH_DIFFERENT_PORT: 'http://' + REMOTE_HOST + ':' + HTTP_PORT2,
HTTPS_REMOTE_ORIGIN: 'https://' + REMOTE_HOST + ':' + HTTPS_PORT,
HTTPS_REMOTE_ORIGIN_WITH_CREDS: 'https://foo:bar@' + REMOTE_HOST + ':' + HTTPS_PORT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const host = get_host_info();
const remoteBaseURL = host.HTTP_REMOTE_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const notSameSiteBaseURL = host.HTTP_NOTSAMESITE_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const localBaseURL = host.HTTP_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;

function with_iframe(url)
{
return new Promise(function(resolve) {
var frame = document.createElement('iframe');
frame.src = url;
frame.onload = function() { resolve(frame); };
document.body.appendChild(frame);
});
}

function loadIFrameAndFetch(iframeURL, fetchURL, expectedFetchResult, title)
{
promise_test(async () => {
const frame = await with_iframe(iframeURL);
let receiveMessage;
const promise = new Promise((resolve, reject) => {
receiveMessage = (event) => {
if (event.data !== expectedFetchResult) {
reject("Received unexpected message " + event.data);
return;
}
resolve();
}
window.addEventListener("message", receiveMessage, false);
});
frame.contentWindow.postMessage(fetchURL, "*");
return promise.finally(() => {
frame.remove();
window.removeEventListener("message", receiveMessage, false);
});
}, title);
}

// This above data URL should be equivalent to resources/iframeFetch.html
var dataIFrameURL = "data:text/html;base64,PCFET0NUWVBFIGh0bWw+CjxodG1sPgo8aGVhZD4KICAgIDxzY3JpcHQ+CiAgICAgICAgZnVuY3Rpb24gcHJvY2Vzc01lc3NhZ2UoZXZlbnQpCiAgICAgICAgewogICAgICAgICAgICBmZXRjaChldmVudC5kYXRhLCB7IG1vZGU6ICJuby1jb3JzIiB9KS50aGVuKCgpID0+IHsKICAgICAgICAgICAgICAgIHBhcmVudC5wb3N0TWVzc2FnZSgib2siLCAiKiIpOwogICAgICAgICAgICB9LCAoKSA9PiB7CiAgICAgICAgICAgICAgICBwYXJlbnQucG9zdE1lc3NhZ2UoImtvIiwgIioiKTsKICAgICAgICAgICAgfSk7CiAgICAgICAgfQogICAgICAgIHdpbmRvdy5hZGRFdmVudExpc3RlbmVyKCJtZXNzYWdlIiwgcHJvY2Vzc01lc3NhZ2UsIGZhbHNlKTsKICAgIDwvc2NyaXB0Pgo8L2hlYWQ+Cjxib2R5PgogICAgPGgzPlRoZSBpZnJhbWUgbWFraW5nIGEgc2FtZSBvcmlnaW4gZmV0Y2ggY2FsbC48L2gzPgo8L2JvZHk+CjwvaHRtbD4K";

loadIFrameAndFetch(dataIFrameURL, localBaseURL + "resources/hello.py?corp=same-origin", "ko",
"Cross-origin fetch in a data: iframe load fails if the server blocks cross-origin loads with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

loadIFrameAndFetch(dataIFrameURL, localBaseURL + "resources/hello.py?corp=same-site", "ko",
"Cross-origin fetch in a data: iframe load fails if the server blocks cross-origin loads with a 'Cross-Origin-Resource-Policy: same-site' response header.");

loadIFrameAndFetch(remoteBaseURL + "resources/iframeFetch.html", localBaseURL + "resources/hello.py?corp=same-origin", "ko",
"Cross-origin fetch in a cross origin iframe load fails if the server blocks cross-origin loads with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

loadIFrameAndFetch(notSameSiteBaseURL + "resources/iframeFetch.html", localBaseURL + "resources/hello.py?corp=same-site", "ko",
"Cross-origin fetch in a cross origin iframe load fails if the server blocks cross-origin loads with a 'Cross-Origin-Resource-Policy: same-site' response header.");

loadIFrameAndFetch(remoteBaseURL + "resources/iframeFetch.html", remoteBaseURL + "resources/hello.py?corp=same-origin", "ok",
"Same-origin fetch in a cross origin iframe load succeeds if the server blocks cross-origin loads with a 'Cross-Origin-Resource-Policy: same-origin' response header.");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const host = get_host_info();
const localBaseURL = host.HTTP_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const sameSiteBaseURL = "http://" + host.ORIGINAL_HOST + ":" + host.HTTP_PORT2 + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const notSameSiteBaseURL = host.HTTP_NOTSAMESITE_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const httpsBaseURL = host.HTTPS_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;

promise_test(async () => {
const response = await fetch("./resources/hello.py?corp=same-origin");
assert_equals(await response.text(), "hello");
}, "Same-origin fetch with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

promise_test(async () => {
const response = await fetch("./resources/hello.py?corp=same-site");
assert_equals(await response.text(), "hello");
}, "Same-origin fetch with a 'Cross-Origin-Resource-Policy: same-site' response header.");

promise_test(async (test) => {
const response = await fetch(notSameSiteBaseURL + "resources/hello.py?corp=same-origin");
assert_equals(await response.text(), "hello");
}, "Cross-origin cors fetch with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

promise_test(async (test) => {
const response = await fetch(notSameSiteBaseURL + "resources/hello.py?corp=same-site");
assert_equals(await response.text(), "hello");
}, "Cross-origin cors fetch with a 'Cross-Origin-Resource-Policy: same-site' response header.");

promise_test((test) => {
const remoteURL = notSameSiteBaseURL + "resources/hello.py?corp=same-origin";
return promise_rejects(test, new TypeError, fetch(remoteURL, { mode : "no-cors" }));
}, "Cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

promise_test((test) => {
const remoteURL = notSameSiteBaseURL + "resources/hello.py?corp=same-site";
return promise_rejects(test, new TypeError, fetch(remoteURL, { mode: "no-cors" }));
}, "Cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-site' response header.");

promise_test((test) => {
const remoteURL = httpsBaseURL + "resources/hello.py?corp=same-site";
return fetch(remoteURL, { mode: "no-cors" });
}, "Cross-origin no-cors fetch to a same-site URL with a 'Cross-Origin-Resource-Policy: same-site' response header.");

promise_test((test) => {
const remoteURL = httpsBaseURL + "resources/hello.py?corp=same-origin";
return promise_rejects(test, new TypeError, fetch(remoteURL, { mode : "no-cors" }));
}, "Cross-origin no-cors fetch to a same-site URL with a 'Cross-Origin-Resource-Policy: same-origin' response header.");

promise_test(async (test) => {
const remoteSameSiteURL = sameSiteBaseURL + "resources/hello.py?corp=same-site";

await fetch(remoteSameSiteURL, { mode: "no-cors" });

return promise_rejects(test, new TypeError, fetch(sameSiteBaseURL + "resources/hello.py?corp=same-origin", { mode: "no-cors" }));
}, "Valid cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-site' response header.");

promise_test((test) => {
const finalURL = notSameSiteBaseURL + "resources/hello.py?corp=same-origin";
return promise_rejects(test, new TypeError, fetch("resources/redirect.py?redirectTo=" + encodeURIComponent(finalURL), { mode: "no-cors" }));
}, "Cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-origin' response header after a redirection.");

promise_test((test) => {
const finalURL = localBaseURL + "resources/hello.py?corp=same-origin";
return fetch(notSameSiteBaseURL + "resources/redirect.py?redirectTo=" + encodeURIComponent(finalURL), { mode: "no-cors" });
}, "Cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-origin' response header after a cross-origin redirection.");

promise_test(async (test) => {
const finalURL = localBaseURL + "resources/hello.py?corp=same-origin";

await fetch(finalURL, { mode: "no-cors" });

return promise_rejects(test, new TypeError, fetch(notSameSiteBaseURL + "resources/redirect.py?corp=same-origin&redirectTo=" + encodeURIComponent(finalURL), { mode: "no-cors" }));
}, "Cross-origin no-cors fetch with a 'Cross-Origin-Resource-Policy: same-origin' redirect response header.");
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
</head>
<body>
<script>
const host = get_host_info();
const remoteBaseURL = host.HTTP_REMOTE_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;
const localBaseURL = host.HTTP_ORIGIN + window.location.pathname.replace(/\/[^\/]*$/, '/') ;

function with_iframe(url) {
return new Promise(function(resolve) {
var frame = document.createElement('iframe');
frame.src = url;
frame.onload = function() { resolve(frame); };
document.body.appendChild(frame);
});
}

promise_test(async() => {
const url = remoteBaseURL + "resources/iframe.py?corp=same-origin";

await new Promise((resolve, reject) => {
return fetch(url, { mode: "no-cors" }).then(reject, resolve);
});

const iframe = await with_iframe(url);
return new Promise((resolve, reject) => {
window.addEventListener("message", (event) => {
if (event.data !== "pong") {
reject(event.data);
return;
}
resolve();
}, false);
iframe.contentWindow.postMessage("ping", "*");
}).finally(() => {
iframe.remove();
});
}, "Load an iframe that has Cross-Origin-Resource-Policy header");
</script>
</body>
</html>
Loading

0 comments on commit 2fe2cf7

Please sign in to comment.