Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[html] Add tests for parsing COOP values #20873

Merged
merged 9 commits into from
Aug 25, 2020
52 changes: 52 additions & 0 deletions html/cross-origin-opener-policy/header-parsing.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!doctype html>
<meta charset=utf-8>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/common.js"></script>

<div id=log></div>
<script>

let tests = [
// popup Origin, popup COOP, expect opener

// None of the following should be recognized as "same-origin" (hence the
// "expected opener" value of `true`).
[SAME_ORIGIN, { percentEncoded: "same%FForigin" }, true], // non-ASCII byte
[SAME_ORIGIN, "same-origin;", true],
[SAME_ORIGIN, "\u000bsame-origin\u000b", true], // vertical tab
[SAME_ORIGIN, "\u000csame-origin\u000c", true], // form feed
[SAME_ORIGIN, "\u000dsame-origin\u000d", true], // carriage return
[SAME_ORIGIN, "Same-origin", true],
[SAME_ORIGIN, "same-origin;\tfoo=bar", true],
[SAME_ORIGIN, "same-origin ;foo=bar", true],
[SAME_ORIGIN, "same-origin; foo=bar;", true],
[SAME_ORIGIN, "\"same-origin\"", true], // HTTP structured fields "string" item
[SAME_ORIGIN, ":c2FtZS1vcmlnaW4=:", true], // HTTP structured fields "byte sequence" item
[SAME_ORIGIN, "?1", true], // HTTP structured fields "boolean" item
[SAME_ORIGIN, "1", true], // HTTP structured fields "integer or decimal" item
[SAME_ORIGIN, "$same-origin", true], // the item type is unrecognized
[SAME_ORIGIN, "same-origin same-origin", true],
[SAME_ORIGIN, "same-origin,same-origin", true],
[SAME_ORIGIN, "*same-origin ", true],

// All of the following should be recognized as "same-origin" (hence the
// "expected opener" value of `false`).
[SAME_ORIGIN, " same-origin", false],
[SAME_ORIGIN, "same-origin ", false],
[SAME_ORIGIN, "\tsame-origin", false],
[SAME_ORIGIN, "same-origin\t", false],
[SAME_ORIGIN, "same-origin;same-origin", false],
[SAME_ORIGIN, "same-origin; foo=bar", false],
];

run_coop_tests("unspecified", tests);

async_test((t) => {
const channelName = `none_to_${SAME_ORIGIN.name}_duplicated-header`;
const url = `${SAME_ORIGIN.origin}/html/cross-origin-opener-policy/resources/coop-same-origin-repeated.asis?channel=${channelName}`;

url_test(t, url, channelName, true);
}, `unspecified document opening popup to ${SAME_ORIGIN.origin} with repeated COOP header`);
</script>
19 changes: 14 additions & 5 deletions html/cross-origin-opener-policy/resources/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ function url_test(t, url, channelName, hasOpener, openerDOMAccess, callback) {
});
}

function percent_encode(objectOrString) {
if (typeof objectOrString === "object") {
return objectOrString.percentEncoded;
}
return encodeURIComponent(objectOrString);
}

function coop_coep_test(t, host, coop, coep, channelName, hasOpener, openerDOMAccess, callback) {
url_test(t, `${host.origin}/html/cross-origin-opener-policy/resources/coop-coep.py?coop=${encodeURIComponent(coop)}&coep=${coep}&channel=${channelName}`, channelName, hasOpener, openerDOMAccess, callback);
const coopPercentEncoded = percent_encode(coop);
url_test(t, `${host.origin}/html/cross-origin-opener-policy/resources/coop-coep.py?coop=${coopPercentEncoded}&coep=${coep}&channel=${encodeURIComponent(channelName)}`, channelName, hasOpener, openerDOMAccess, callback);
}

function coop_test(t, host, coop, channelName, hasOpener, callback) {
Expand All @@ -59,11 +67,12 @@ function coop_test(t, host, coop, channelName, hasOpener, callback) {

function run_coop_tests(documentCOOPValueTitle, testArray) {
for (const test of testArray) {
let coopName = typeof test[1] === "object" ? test[1].percentEncoded : test[1];
async_test(t => {
coop_test(t, test[0], test[1],
`${documentCOOPValueTitle}_to_${test[0].name}_${test[1].replace(/ /g,"-")}`,
`${documentCOOPValueTitle}_to_${test[0].name}_${coopName.replace(/ /g,"-")}`,
test[2], () => { t.done(); });
}, `${documentCOOPValueTitle} document opening popup to ${test[0].origin} with COOP: "${test[1]}"`);
}, `${documentCOOPValueTitle} document opening popup to ${test[0].origin} with COOP: ${format_value(coopName)}`);
}
}

Expand Down Expand Up @@ -91,5 +100,5 @@ function run_coop_test_iframe (documentTitle, iframe_origin, popup_origin, popup
assert_equals(payload.name, expects_name? name:"", 'name');
});
document.body.append(frame);
}, `${documentTitle} with ${iframe_origin.name} iframe opening popup a ${popup_origin.name} with COOP: ${popup_coop}`);
}
}, `${documentTitle} with ${iframe_origin.name} iframe opening popup a ${popup_origin.name} with COOP: ${format_value(popup_coop)}`);
}
2 changes: 1 addition & 1 deletion html/cross-origin-opener-policy/resources/coop-coep.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main(request, response):
iframe.contentWindow.postMessage(payload, "*");
};
const channelName = new URL(location).searchParams.get("channel");
iframe.src = `${get_host_info().HTTPS_ORIGIN}/html/cross-origin-opener-policy/resources/postback.html?channel=${channelName}`;
iframe.src = `${get_host_info().HTTPS_ORIGIN}/html/cross-origin-opener-policy/resources/postback.html?channel=${encodeURIComponent(channelName)}`;
document.body.appendChild(iframe);
}
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
HTTP/1.1 200 OK
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
Server: BaseHTTP/0.3 Python/2.7.15+
Date: Wed, 18 Dec 2019 00:47:08 GMT

<!doctype html>
<meta charset=utf-8>
<script src="/common/get-host-info.sub.js"></script>
<iframe></iframe>
<script>
const navigate = new URL(location).searchParams.get("navigate");
if (navigate !== null) {
self.location = navigate;
} else {
const iframe = document.querySelector("iframe");
iframe.onload = () => {
const payload = { name: self.name, opener: !!self.opener };
iframe.contentWindow.postMessage(payload, "*");
};
const channelName = new URL(location).searchParams.get("channel");
iframe.src = `${get_host_info().HTTPS_ORIGIN}/html/cross-origin-opener-policy/resources/postback.html?channel=${channelName}`;
}
</script>