1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
namespace content {
class FramebustingBrowserTest : public ContentBrowserTest {
public:
FramebustingBrowserTest() = default;
protected:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
WebContentsImpl* web_contents() const {
return static_cast<WebContentsImpl*>(shell()->web_contents());
}
};
// Verifies that cross-origin iframes cannot navigate the top frame to a
// different origin (sometimes called "framebusting") without user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest, FailsWithoutUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents(), "child",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true);
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(
ExecJs(child, "top.location = 'foo'", EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that cross-origin iframes can navigate the top frame to a different
// origin (sometimes called "framebusting") with user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest, SucceedsWithUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
GURL other_url =
embedded_test_server()->GetURL("other.test", "/defaultresponse");
RenderFrameHost* child = CreateSubframe(web_contents(), "child", other_url,
/*wait_for_navigation=*/true);
TestNavigationObserver observer(web_contents());
// By default `ExecJs()` executes the provided script with user activation.
EXPECT_TRUE(ExecJs(child, "top.location = '/defaultresponse'"));
// The top frame is indeed navigated successfully.
observer.Wait();
EXPECT_EQ(web_contents()->GetLastCommittedURL(), other_url);
}
// Verifies that cross-origin iframes can navigate the top frame to a different
// origin (sometimes called "framebusting") with user activation, even after
// a couple `setTimeout()` calls.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
SucceedsWithAsyncUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
GURL other_url =
embedded_test_server()->GetURL("other.test", "/defaultresponse");
RenderFrameHost* child = CreateSubframe(web_contents(), "child", other_url,
/*wait_for_navigation=*/true);
TestNavigationObserver observer(web_contents());
// By default `ExecJs()` executes the provided script with a user activation.
//
// With user activation, the navigation should succeed even through nested
// `setTimeout()` calls.
EXPECT_TRUE(ExecJs(child, R"(
setTimeout(() => {
setTimeout(() => {
top.location = '/defaultresponse';
}, 0);
}, 0);
)"));
// The top frame is indeed navigated successfully.
observer.Wait();
EXPECT_EQ(web_contents()->GetLastCommittedURL(), other_url);
}
// Verifies that cross-origin unsandboxed iframes cannot escalate the
// allow-top-navigation sandbox privilege in a child iframe, which would allow
// it to navigate the top frame to a different origin (sometimes called
// "framebusting") without user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
FailsFromGrandchildPrivilegeEscalationInSandboxFlags) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true);
RenderFrameHost* grandchild = CreateSubframe(
child, "grandchild",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true,
{.sandbox_flags = "allow-scripts allow-top-navigation"});
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(ExecJs(grandchild, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that a grandchild cross-origin unsandboxed iframe cannot give itself
// allow-top-navigation sandbox privileges via its delivered sandbox flags in
// the HTTP response header, which would allow it to navigate the top frame to a
// different origin (sometimes called "framebusting") without user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
FailsFromGrandchildPrivilegeEscalationInDeliveredFlags) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true);
RenderFrameHost* grandchild =
CreateSubframe(child, "grandchild",
embedded_test_server()->GetURL(
"other.test",
"/set-header?Content-Security-Policy: sandbox "
"allow-scripts allow-top-navigation"),
/*wait_for_navigation=*/true);
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(ExecJs(grandchild, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that a child cross-origin unsandboxed iframe document cannot give
// itself allow-top-navigation sandbox privileges via its delivered sandbox
// flags in the HTTP response header, which would allow it to navigate the top
// frame to a different origin (sometimes called "framebusting") without user
// activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
FailsFromChildPrivilegeEscalationInDeliveredFlags) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child =
CreateSubframe(web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL(
"other.test",
"/set-header?Content-Security-Policy: sandbox "
"allow-scripts allow-top-navigation"),
/*wait_for_navigation=*/true);
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that a navigation to a cross-site document consumes sticky user
// activation, preventing the new document from navigating the top frame to a
// different origin (sometimes called "framebusting") without user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest, FailsAfterCrossSiteNavigation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("foo.com", "/defaultresponse"),
/*wait_for_navigation=*/true);
// Give the child iframe user activation.
EXPECT_TRUE(ExecJs(child, ""));
// Perform a cross-site navigation. This should clear the sticky user
// activation state.
GURL navigate_url =
embedded_test_server()->GetURL("other.test", "/defaultresponse");
EXPECT_TRUE(ExecJs(child, JsReplace("location.href = $1", navigate_url),
EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
child =
web_contents()->GetPrimaryMainFrame()->child_at(0)->current_frame_host();
EXPECT_EQ(child->GetLastCommittedURL(), navigate_url);
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that a navigation to a same-site document maintains sticky user
// activation, allow the new document to navigate the top frame to a
// different origin (sometimes called "framebusting") without transient user
// activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
SucceedsAfterSameSiteNavigation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("foo.com", "/defaultresponse"),
/*wait_for_navigation=*/true);
// Give the child iframe user activation.
EXPECT_TRUE(ExecJs(child, ""));
// Perform a same-site but cross-origin navigation. This should keep the
// sticky user activation state.
GURL navigate_url =
embedded_test_server()->GetURL("subdomain.foo.com", "/defaultresponse");
EXPECT_TRUE(ExecJs(child, JsReplace("location.href = $1", navigate_url),
EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
child =
web_contents()->GetPrimaryMainFrame()->child_at(0)->current_frame_host();
EXPECT_EQ(child->GetLastCommittedURL(), navigate_url);
EXPECT_TRUE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
}
// Verifies that a navigation to a same-site document without sticky user
// activation keeps the unset activation state, preventing the new document from
// navigating the top frame to a different origin (sometimes called
// "framebusting") without transient user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
FailsAfterSameSiteNavigationWithoutUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("foo.com", "/defaultresponse"),
/*wait_for_navigation=*/true);
// Perform a same-site but cross-origin navigation. There is no sticky user
// activation state, so the newly navigated page should not have sticky user
// activation either.
GURL navigate_url =
embedded_test_server()->GetURL("subdomain.foo.com", "/defaultresponse");
EXPECT_TRUE(ExecJs(child, JsReplace("location.href = $1", navigate_url),
EXECUTE_SCRIPT_NO_USER_GESTURE));
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
child =
web_contents()->GetPrimaryMainFrame()->child_at(0)->current_frame_host();
EXPECT_EQ(child->GetLastCommittedURL(), navigate_url);
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
EXPECT_FALSE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
}
// Verifies that cross-origin iframes sandboxed with
// "allow-top-navigation-by-user-activation" can only navigate the top frame to
// a different origin (sometimes called "framebusting") when they have user
// activation.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
AllowTopNavigationByUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents()->GetPrimaryMainFrame(), "child",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true,
{.sandbox_flags =
"allow-scripts allow-top-navigation-by-user-activation"});
WebContentsConsoleObserver console_observer(web_contents());
console_observer.SetPattern("*permission to navigate the target frame*");
// The initial top-level navigation should fail without user activation.
EXPECT_FALSE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
ASSERT_TRUE(console_observer.Wait());
// Once the frame has user activation, the top-level navigation should
// succeed.
EXPECT_TRUE(ExecJs(child, ""));
EXPECT_TRUE(ExecJs(child, "window.top.location = 'foo'",
EXECUTE_SCRIPT_NO_USER_GESTURE));
}
// Verifies that cross-origin iframes can navigate the top frame to another URL
// belonging to the top frame's origin without user activation.
//
// This is non-standard, unspecified behavior.
// See also https://www.chromestatus.com/features/5851021045661696.
IN_PROC_BROWSER_TEST_F(FramebustingBrowserTest,
SucceedsInSameOriginWithoutUserActivation) {
ASSERT_TRUE(NavigateToURL(
shell(), embedded_test_server()->GetURL("/defaultresponse")));
RenderFrameHost* child = CreateSubframe(
web_contents(), "child",
embedded_test_server()->GetURL("other.test", "/defaultresponse"),
/*wait_for_navigation=*/true);
TestNavigationObserver observer(web_contents());
GURL destination = embedded_test_server()->GetURL("/echo");
EXPECT_TRUE(ExecJs(child, JsReplace("top.location = $1", destination),
EXECUTE_SCRIPT_NO_USER_GESTURE));
// The top frame is indeed navigated successfully.
observer.Wait();
EXPECT_EQ(web_contents()->GetLastCommittedURL(), destination);
}
} // namespace content
|