Skip to content

Commit

Permalink
Portals: Block focus from advancing into portal
Browse files Browse the repository at this point in the history
Prevents focus from advancing inside a portal frame from the renderer
process, and adds an security check in the browser process.

Bug: 1030838
Change-Id: I1e82d2163142ba357aa4d7adb4f0b66d0fb5b676
  • Loading branch information
a4sriniv authored and chromium-wpt-export-bot committed Jan 16, 2020
1 parent d095073 commit a2da904
Showing 1 changed file with 67 additions and 35 deletions.
102 changes: 67 additions & 35 deletions portals/portals-focus.sub.html
@@ -1,6 +1,8 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/open-blank-host.js"></script>
<body>
<script>
Expand All @@ -14,59 +16,89 @@

promise_test(async t => {
let portal = await createPortal(document, new URL("resources/focus-page-with-button.html", location.href));
portal.onmessage = t.step_func(e => {
assert_unreached("button inside portal should not be focused");
});
portal.postMessage("focus", "*");
return new Promise(r => t.step_timeout(r, 500));
try {
portal.onmessage = t.step_func(e => {
assert_unreached("button inside portal should not be focused");
});
portal.postMessage("focus", "*");
await new Promise(r => t.step_timeout(r, 500));
} finally {
document.body.removeChild(portal);
}
}, "test that an element inside a portal cannot steal focus");

promise_test(async t => {
let portal = await createPortal(document, new URL("resources/focus-page-with-x-origin-iframe.sub.html", location.href));
portal.onmessage = t.step_func(e => {
assert_unreached("button inside portal should not be focused");
});
portal.postMessage("focus", "*");
return new Promise(r => t.step_timeout(r, 500));
try {
portal.onmessage = t.step_func(e => {
assert_unreached("button inside portal should not be focused");
});
portal.postMessage("focus", "*");
await new Promise(r => t.step_timeout(r, 500));
} finally {
document.body.removeChild(portal);
}
}, "test that an element inside a portal's x-origin subframe cannot steal focus");

promise_test(async t => {
let win = await openBlankPortalHost();
let doc = win.document;
try {
let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
let button = doc.createElement("button");
doc.body.appendChild(button);

let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
let button = doc.createElement("button");
doc.body.appendChild(button);
await portal.activate();
doc.body.removeChild(portal);

await portal.activate();
doc.body.removeChild(portal);

button.onfocus = t.step_func(() => {
assert_unreached("button inside adopted portal should not be focused");
});
button.focus();
return new Promise(r => t.step_timeout(r, 500));
button.onfocus = t.step_func(() => {
assert_unreached("button inside adopted portal should not be focused");
});
button.focus();
await new Promise(r => t.step_timeout(r, 500));
} finally {
win.close();
}
}, "test that an element inside an adopted portal cannot steal focus");

promise_test(async t => {
let win = await openBlankPortalHost();
let doc = win.document;
try {
let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
let iframe = doc.createElement("iframe");
iframe.src = new URL("resources/focus-page-with-button.html",
"http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/");
doc.body.appendChild(iframe);
await new Promise(r => iframe.onload = r);

let portal = await createPortal(doc, new URL("resources/simple-portal-adopts-predecessor.html", location.href));
let iframe = doc.createElement("iframe");
iframe.src = new URL("resources/focus-page-with-button.html",
"http://{{hosts[alt][www]}}:{{ports[http][0]}}/portals/");
doc.body.appendChild(iframe);
await new Promise(r => iframe.onload = r);

await portal.activate();
doc.body.removeChild(portal);
await portal.activate();
doc.body.removeChild(portal);

iframe.contentWindow.postMessage("focus", "*");
window.onmessage = t.step_func(() => {
assert_unreached("button inside x-origin iframe inside a portal should not be focused");
});
await new Promise(r => t.step_timeout(r, 500));
iframe.contentWindow.postMessage("focus", "*");
window.onmessage = t.step_func(() => {
assert_unreached("button inside x-origin iframe inside a portal should not be focused");
});
await new Promise(r => t.step_timeout(r, 500));
} finally {
win.close();
}
}, "test that a x-origin iframe inside an adopted portal cannot steal focus");

const TAB = "\ue004"; // https://w3c.github.io/webdriver/#keyboard-actions
promise_test(async t => {
let portal = await createPortal(document, "resources/focus-page-with-button.html");
try {
portal.tabIndex = 0;
await test_driver.send_keys(document.body, TAB);
portal.onmessage = t.step_func(e => {
assert_unreached("button inside portal should not be focused");
});
await new Promise(r => t.step_timeout(r, 500));
} finally {
document.body.removeChild(portal);
}
}, "test that we cannot tab into a portal's contents");

</script>
</body>

0 comments on commit a2da904

Please sign in to comment.