Skip to content

Commit

Permalink
Add tests regarding navigation inside sandboxed iframes. (#6221)
Browse files Browse the repository at this point in the history
  • Loading branch information
fred-wang authored Jun 21, 2017
1 parent 3961c85 commit 2fbf393
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Check that sandboxed iframe can perform navigation on the top frame
when allow-top-navigation is set</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<iframe sandbox="allow-top-navigation allow-scripts"></iframe>
<script>
if (opener) {
// We're the popup (i.e. a top frame). Load into the iframe the page
// trying to modifying the top frame and transmit the result to our
// opener.
onmessage = function(e) {
opener.postMessage(e.data, "*")
}
document.querySelector("iframe").src = "support/iframe-that-performs-top-navigation-on-popup.html";
} else {
// We are the main test page. Open ourselves as a popup, so that we can
// can experiment navigation of the top frame.
async_test(t => {
window.addEventListener("message", t.step_func_done(e => {
assert_equals(e.data, "can navigate");
e.source.close();
}));
window.open(location.href);
}, "Frames with `allow-top-navigation` should be able to navigate the top frame.");
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Check that sandboxed iframe cannot perform navigation on the top
frame when allow-top-navigation is not set</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<iframe sandbox="allow-scripts"></iframe>
<script>
if (opener) {
// We're the popup (i.e. a top frame). Load into the iframe the page
// trying to modifying the top frame and transmit the result to our
// opener.
onmessage = function(e) {
opener.postMessage(e.data, "*")
}
document.querySelector("iframe").src = "support/iframe-that-performs-top-navigation-on-popup.html";
} else {
// We are the main test page. Open ourselves as a popup, so that we can
// can experiment navigation of the top frame.
async_test(t => {
window.addEventListener("message", t.step_func_done(e => {
assert_equals(e.data, "cannot navigate");
e.source.close();
}));
window.open(location.href);
}, "Frames without `allow-top-navigation` should not be able to navigate the top frame.");
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<meta charset="utf-8">
<title>Check that sandboxed iframe can not navigate their ancestors</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var t = async_test();
onmessage = t.step_func_done(function(e) {
assert_equals(e.data, "can not navigate", "Should have the right message");
});
</script>
<iframe sandbox="allow-scripts" src="support/iframe-tried-to-be-navigated-by-its-child.html"></iframe>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<meta charset="utf-8">
<title>Check that unsandboxed iframe can navigate their ancestors</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var t = async_test();
onmessage = t.step_func_done(function(e) {
assert_equals(e.data, "can navigate", "Should have the right message");
});
</script>
<iframe src="support/iframe-tried-to-be-navigated-by-its-child.html"></iframe>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<meta charset="utf-8">
<title>Check that sandboxed iframe can navigate their descendants</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var t = async_test();
onmessage = t.step_func_done(function(e) {
assert_equals(e.data, "can navigate", "Should have the right message");
});
</script>
<iframe sandbox="allow-scripts" src="support/iframe-trying-to-navigate-its-child.html"></iframe>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<meta charset="utf-8">
<title>Check that sandboxed iframe can navigate itself</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
var t = async_test();
onmessage = t.step_func_done(function(e) {
assert_equals(e.data, "can navigate", "Should have the right message");
});
</script>
<iframe sandbox="allow-scripts" src="support/iframe-trying-to-navigate-itself.html"></iframe>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<body>
<script>
window.onload = function() {
try {
top.location = "data:text/html,\u003c!DOCTYPE html\u003e\u003cscript\u003eopener.postMessage('can navigate', '*');\u003c/script\u003e";
} catch(e) {
top.postMessage("cannot navigate", "*");
}
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<p>This is a frame that tries to navigate its parent.</p>
<script>
window.onload = function() {
try {
parent.location.href = "data:text/html,\u003c!DOCTYPE html\u003e\u003cp\u003eIf this message appears, then this frame has been navigated by its child.\u003c/p\u003e\u003cscript\u003eparent.postMessage('can navigate', '*');\u003c/script\u003e";
} catch(e) {
parent.parent.postMessage("can not navigate", "*");
}
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<p>If this message appears, then this frame has not been navigated by its child.</p>
<iframe src="iframe-that-tries-to-navigate-parent-and-sends-result-to-grandparent.html">
</iframe>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<iframe src="data:text/html,If this message appears, then this frame has not been navigated by its parent."></iframe>
<script>
window.onload = function() {
try {
document.querySelector("iframe").contentWindow.location.href = "data:text/html,\u003c!DOCTYPE html\u003e\u003cp\u003eIf this message appears, then this frame has been navigated by its parent.\u003c/p\u003e\u003cscript\u003eparent.parent.postMessage('can navigate', '*');\u003c/script\u003e";
} catch(e) {
parent.postMessage("can not navigate", "*");
}
}
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<p>If this message appears, then this frame has not been navigated.</p>
<script>
window.onload = function() {
try {
location.href = "data:text/html,\u003c!DOCTYPE html\u003e\u003cp\u003eIf this message appears, then this frame has been navigated.\u003c/p\u003e\u003cscript\u003eparent.postMessage('can navigate', '*');\u003c/script\u003e";
} catch(e) {
parent.postMessage("can not navigate", "*");
}
}
</script>

0 comments on commit 2fbf393

Please sign in to comment.