Bug 1927219 - Set consistent opener policy for printing. r=nika

Differential Revision: https://phabricator.services.mozilla.com/D226964
This commit is contained in:
Emilio Cobos Álvarez 2024-10-30 00:25:37 +00:00
parent 079f11645d
commit f0c2b606ee
7 changed files with 78 additions and 39 deletions

View File

@ -420,6 +420,10 @@ already_AddRefed<BrowsingContext> BrowsingContext::CreateDetached(
topPolicy == nsILoadInfo::OPENER_POLICY_UNSAFE_NONE ||
topPolicy == nsILoadInfo::OPENER_POLICY_SAME_ORIGIN_ALLOW_POPUPS ||
aOptions.isForPrinting);
if (aOptions.isForPrinting) {
// Ensure our opener policy is consistent for printing for our top.
fields.Get<IDX_OpenerPolicy>() = topPolicy;
}
} else if (!aParent && group->IsPotentiallyCrossOriginIsolated()) {
// If we're creating a brand-new toplevel BC in a potentially cross-origin
// isolated group, it should start out with a strict opener policy.

View File

@ -69,17 +69,19 @@ async function handleRequest(request, response) {
});
}
response.setHeader("Cache-Control", "no-cache", false);
if (html) {
response.setHeader("Content-Type", "text/html", false);
function setHeaders() {
if (queryString.has("headers")) {
for (const header of queryString.getAll("headers")) {
const [key, value] = header.split(":");
response.setHeader(key, value, false);
}
}
}
response.setHeader("Cache-Control", "no-cache", false);
if (html) {
response.setHeader("Content-Type", "text/html", false);
setHeaders();
response.write(html);
} else {
const path = queryString.get("file");
@ -89,6 +91,7 @@ async function handleRequest(request, response) {
path.endsWith(".xhtml") ? "application/xhtml+xml" : "text/html",
false
);
setHeaders();
// This is a hack to set the correct id for the content document that is to be
// loaded in the iframe.
response.write(doc.replace(`id="body"`, `id="default-iframe-body-id"`));

View File

@ -43,8 +43,7 @@ support-files = ["file_link_modulepreload.html"]
["browser_print_coop.js"]
support-files = [
"file_coop_header.html",
"file_coop_header.html^headers^",
"file_print_coop_helper.html",
]
["browser_print_copies.js"]

View File

@ -13,23 +13,29 @@ const TEST_PATH_SITE = getRootDirectory(gTestPath).replace(
"https://test1.example.com"
);
// Verify if the page with a COOP header can be used for printing preview.
add_task(async function test_print_coop_basic() {
await PrintHelper.withTestPage(async helper => {
await helper.startPrint();
ok(true, "We did not crash.");
await helper.closeDialog();
}, "file_coop_header.html");
});
const COOP_HELPER_FILE =
getRootDirectory(gTestPath).replace("chrome://mochitests/content/", "") +
"file_print_coop_helper.html";
add_task(async function test_window_print_coop() {
for (const base of [TEST_PATH, TEST_PATH_SITE]) {
for (const query of [
async function test_print_flags_with_headers(headers, expectIsolated) {
let params = new URLSearchParams({
file: COOP_HELPER_FILE,
});
for (let header of headers) {
params.append("headers", header);
}
for (const origin of ["https://example.com", "https://test1.example.com"]) {
for (const hash of [
"print",
"print-same-origin-frame",
"print-same-origin-frame-srcdoc",
"print-cross-origin-frame",
]) {
const url = `${base}file_coop_header.html?${query}`;
if (expectIsolated && hash == "print-cross-origin-frame") {
// The iframe wouldn't load.
continue;
}
let url = `${origin}/document-builder.sjs?${params}#${hash}`;
info(`Testing ${url}`);
is(
document.querySelector(".printPreviewBrowser"),
@ -47,7 +53,28 @@ add_task(async function test_window_print_coop() {
ok(true, "Shouldn't crash");
gBrowser.getTabDialogBox(browser).abortAllDialogs();
let isolated = await SpecialPowers.spawn(browser, [], () => {
return content.crossOriginIsolated;
});
is(isolated, expectIsolated, "Expected isolation");
});
}
}
}
add_task(async function test_window_print_coop() {
return test_print_flags_with_headers(
["Cross-Origin-Opener-Policy: same-origin"],
/* expectIsolated = */ false
);
});
add_task(async function test_window_print_coep() {
return test_print_flags_with_headers(
[
"Cross-Origin-Opener-Policy: same-origin",
"Cross-Origin-Embedder-Policy: require-corp",
],
/* expectIsolated = */ true
);
});

View File

@ -1,20 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8">
<p>Hello world</p>
<iframe id="iframe"></iframe>
<script>
onload = function() {
let iframe = document.getElementById("iframe");
switch (location.search) {
case "?print":
window.print();
break;
case "?print-same-origin-frame":
iframe.src = `/document-builder.sjs?html=Hello<script>print()<\/script>`;
break;
case "?print-cross-origin-frame":
iframe.src = `https://example.org/document-builder.sjs?html=Hello<script>print()<\/script>`;
break;
}
}
</script>

View File

@ -1 +0,0 @@
Cross-Origin-Opener-Policy: same-origin

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<meta charset="utf-8">
<p>Hello world</p>
<iframe id="iframe"></iframe>
<script>
onload = function() {
let iframe = document.getElementById("iframe");
switch (location.hash) {
case "#print":
window.print();
break;
case "#print-same-origin-frame": {
let params = new URLSearchParams(location.search);
params.delete("file");
params.set("html", "Hello<script>print()<\/script>");
iframe.src = `/document-builder.sjs?${params}`;
break;
}
case "#print-same-origin-frame-srcdoc":
iframe.srcdoc = `Hello<script>print()<\/script>`;
break;
case "#print-cross-origin-frame":
iframe.src = `https://example.org/document-builder.sjs?html=Hello<script>print()<\/script>`;
break;
}
}
</script>