Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 7 | #include <optional> |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 8 | #include <tuple> |
| 9 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 10 | #include "base/command_line.h" |
Jun Cai | 53c54b22 | 2018-08-16 00:14:04 | [diff] [blame] | 11 | #include "base/feature_list.h" |
Alex Ilin | cf96655e1 | 2019-11-21 16:13:52 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
| 14 | #include "base/functional/callback_helpers.h" |
Gabriel Charette | 9f60dd1 | 2020-03-06 20:48:04 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 16 | #include "base/memory/raw_ptr.h" |
Andrew Williams | 39151a7 | 2022-10-18 23:12:41 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
Lei Zhang | f40ac9a61 | 2025-06-02 22:35:10 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
Lei Zhang | e02299a | 2021-04-26 23:12:24 | [diff] [blame] | 19 | #include "base/strings/stringprintf.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
Liam Brady | ce3383b | 2023-08-16 18:37:21 | [diff] [blame] | 21 | #include "base/synchronization/waitable_event.h" |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 22 | #include "base/test/bind.h" |
Nasko Oskov | 1b7c562 | 2024-04-16 19:47:25 | [diff] [blame] | 23 | #include "base/test/gtest_util.h" |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 24 | #include "base/test/scoped_feature_list.h" |
Ari Chivukula | a29eb3a | 2021-07-21 02:57:48 | [diff] [blame] | 25 | #include "base/unguessable_token.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 26 | #include "build/build_config.h" |
Liam Brady | 56f7d25 | 2023-02-28 17:40:18 | [diff] [blame] | 27 | #include "content/browser/attribution_reporting/attribution_manager.h" |
ananta | a5721fb | 2016-08-10 19:12:26 | [diff] [blame] | 28 | #include "content/browser/bad_message.h" |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 29 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 30 | #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
| 31 | #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 32 | #include "content/browser/fenced_frame/fenced_frame.h" |
Liam Brady | 56f7d25 | 2023-02-28 17:40:18 | [diff] [blame] | 33 | #include "content/browser/private_aggregation/private_aggregation_manager.h" |
danakj | 10f3237 | 2020-09-15 22:25:16 | [diff] [blame] | 34 | #include "content/browser/renderer_host/navigator.h" |
| 35 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 36 | #include "content/browser/renderer_host/render_frame_proxy_host.h" |
Yutaka Hirano | 12c224a1 | 2017-12-19 06:12:35 | [diff] [blame] | 37 | #include "content/browser/renderer_host/render_process_host_impl.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 38 | #include "content/browser/renderer_host/render_view_host_factory.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 39 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Avi Drissman | bd3e98644 | 2020-05-20 21:09:20 | [diff] [blame] | 40 | #include "content/browser/web_contents/file_chooser_impl.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 41 | #include "content/browser/web_contents/web_contents_impl.h" |
Nasko Oskov | fa20eae0 | 2023-05-05 22:41:56 | [diff] [blame] | 42 | #include "content/common/features.h" |
csharrison | 95f01e92 | 2017-04-24 18:52:35 | [diff] [blame] | 43 | #include "content/common/frame.mojom.h" |
Hans Wennborg | 78b5218 | 2021-06-15 13:42:15 | [diff] [blame] | 44 | #include "content/common/frame_messages.mojom.h" |
rockot | 5c478a7 | 2016-09-28 23:14:18 | [diff] [blame] | 45 | #include "content/common/render_message_filter.mojom.h" |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 46 | #include "content/public/browser/blob_handle.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 47 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 48 | #include "content/public/browser/browser_task_traits.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 49 | #include "content/public/browser/browser_thread.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 50 | #include "content/public/browser/content_browser_client.h" |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 51 | #include "content/public/browser/file_select_listener.h" |
Hiroki Nakagawa | 080f86c | 2020-05-13 10:56:42 | [diff] [blame] | 52 | #include "content/public/browser/navigation_handle.h" |
ananta | a5721fb | 2016-08-10 19:12:26 | [diff] [blame] | 53 | #include "content/public/browser/resource_context.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 54 | #include "content/public/browser/storage_partition.h" |
Nasko Oskov | 6773006 | 2020-05-08 16:49:23 | [diff] [blame] | 55 | #include "content/public/common/bindings_policy.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 56 | #include "content/public/common/content_switches.h" |
Avi Drissman | 73cb985 | 2024-08-26 18:10:58 | [diff] [blame] | 57 | #include "content/public/common/isolated_world_ids.h" |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 58 | #include "content/public/common/url_constants.h" |
Sreeja Kamishetty | ce8d594 | 2020-08-19 11:25:51 | [diff] [blame] | 59 | #include "content/public/test/back_forward_cache_util.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 60 | #include "content/public/test/browser_test.h" |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 61 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 6e9def1 | 2014-03-27 20:23:28 | [diff] [blame] | 62 | #include "content/public/test/content_browser_test.h" |
| 63 | #include "content/public/test/content_browser_test_utils.h" |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 64 | #include "content/public/test/fenced_frame_test_util.h" |
arthursonzogni | 85bc116 | 2019-10-15 14:44:13 | [diff] [blame] | 65 | #include "content/public/test/navigation_handle_observer.h" |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 66 | #include "content/public/test/test_frame_navigation_observer.h" |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 67 | #include "content/public/test/test_navigation_observer.h" |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 68 | #include "content/public/test/test_renderer_host.h" |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 69 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 70 | #include "content/shell/browser/shell.h" |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 71 | #include "content/test/content_browser_test_utils_internal.h" |
Arthur Hemery | 2a0a28be | 2019-03-06 17:51:36 | [diff] [blame] | 72 | #include "content/test/did_commit_navigation_interceptor.h" |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 73 | #include "content/test/frame_host_interceptor.h" |
creis | 3710b238 | 2015-08-18 00:12:15 | [diff] [blame] | 74 | #include "content/test/test_content_browser_client.h" |
Tom Sepez | cc69c44 | 2025-07-17 02:00:30 | [diff] [blame] | 75 | #include "ipc/constants.mojom.h" |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 76 | #include "mojo/core/embedder/embedder.h" |
Julie Jeongeun Kim | ed2e5ba7 | 2019-09-12 10:14:17 | [diff] [blame] | 77 | #include "mojo/public/cpp/bindings/pending_associated_remote.h" |
Julie Jeongeun Kim | e003de5 | 2019-10-29 05:14:27 | [diff] [blame] | 78 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
Miyoung Shin | da5da32 | 2019-09-02 10:07:37 | [diff] [blame] | 79 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Henrique Ferreiro | 3bb206b | 2019-09-18 12:48:23 | [diff] [blame] | 80 | #include "mojo/public/cpp/bindings/remote.h" |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 81 | #include "mojo/public/cpp/test_support/test_utils.h" |
Andrew Williams | 39151a7 | 2022-10-18 23:12:41 | [diff] [blame] | 82 | #include "net/base/features.h" |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 83 | #include "net/base/filename_util.h" |
Alex Moshchuk | 375f4de8 | 2025-05-13 17:30:48 | [diff] [blame] | 84 | #include "net/base/net_errors.h" |
Shivani Sharma | 11c23f55 | 2019-06-11 18:18:39 | [diff] [blame] | 85 | #include "net/base/network_isolation_key.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 86 | #include "net/dns/mock_host_resolver.h" |
Chris Fredrickson | 9ffdf5b | 2024-07-09 20:05:09 | [diff] [blame] | 87 | #include "net/storage_access_api/status.h" |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 88 | #include "net/test/embedded_test_server/controllable_http_response.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 89 | #include "net/test/embedded_test_server/embedded_test_server.h" |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 90 | #include "net/test/embedded_test_server/http_request.h" |
Ramin Halavati | 81fdc8fb | 2017-07-09 17:15:42 | [diff] [blame] | 91 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
John Abd-El-Malek | 91243b3 | 2018-01-19 06:08:33 | [diff] [blame] | 92 | #include "services/network/public/cpp/network_switches.h" |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 93 | #include "services/network/public/cpp/resource_request.h" |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 94 | #include "services/network/public/mojom/fetch_api.mojom.h" |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 95 | #include "services/network/public/mojom/trust_tokens.mojom.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 96 | #include "services/network/public/mojom/url_loader.mojom.h" |
John Abd-El-Malek | d0cd7c5c | 2018-01-30 07:20:02 | [diff] [blame] | 97 | #include "services/network/test/test_url_loader_client.h" |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 98 | #include "storage/browser/blob/blob_registry_impl.h" |
David Sanders | b355ac4 | 2025-07-25 23:32:39 | [diff] [blame] | 99 | #include "storage/browser/blob/blob_url_registry.h" |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 100 | #include "testing/gmock/include/gmock/gmock.h" |
| 101 | #include "testing/gtest/include/gtest/gtest.h" |
Kinuko Yasuda | 04a82ab1 | 2019-07-26 06:13:39 | [diff] [blame] | 102 | #include "third_party/blink/public/common/blob/blob_utils.h" |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 103 | #include "third_party/blink/public/common/fenced_frame/fenced_frame_utils.h" |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 104 | #include "third_party/blink/public/common/frame/fenced_frame_sandbox_flags.h" |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 105 | #include "third_party/blink/public/common/navigation/navigation_policy.h" |
Andrew Williams | 39151a7 | 2022-10-18 23:12:41 | [diff] [blame] | 106 | #include "third_party/blink/public/mojom/blob/blob_url_store.mojom.h" |
Henrique Ferreiro | 3bb206b | 2019-09-18 12:48:23 | [diff] [blame] | 107 | #include "third_party/blink/public/mojom/choosers/file_chooser.mojom.h" |
Nan Lin | e376738a | 2022-03-25 22:05:41 | [diff] [blame] | 108 | #include "third_party/blink/public/mojom/fenced_frame/fenced_frame.mojom.h" |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 109 | #include "third_party/blink/public/mojom/frame/frame.mojom-test-utils.h" |
Gyuyoung Kim | 16a12f5 | 2020-12-19 04:24:26 | [diff] [blame] | 110 | #include "third_party/blink/public/mojom/frame/frame.mojom.h" |
Takuto Ikuta | 247f72f | 2024-02-21 04:46:06 | [diff] [blame] | 111 | #include "third_party/blink/public/mojom/frame/remote_frame.mojom-test-utils.h" |
Henrique Ferreiro | 621b3ad | 2021-02-15 12:51:41 | [diff] [blame] | 112 | #include "third_party/blink/public/mojom/loader/mixed_content.mojom.h" |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 113 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 114 | using ::testing::HasSubstr; |
| 115 | using ::testing::Optional; |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 116 | |
| 117 | namespace content { |
| 118 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 119 | namespace { |
| 120 | |
| 121 | // This is a helper function for the tests which attempt to create a |
| 122 | // duplicate RenderViewHost or RenderWidgetHost. It tries to create two objects |
| 123 | // with the same process and routing ids, which causes a collision. |
| 124 | // It creates a couple of windows in process 1, which causes a few routing ids |
| 125 | // to be allocated. Then a cross-process navigation is initiated, which causes a |
| 126 | // new process 2 to be created and have a pending RenderViewHost for it. The |
| 127 | // routing id of the RenderViewHost which is target for a duplicate is set |
Alex Moshchuk | 27caae8 | 2017-09-11 23:11:18 | [diff] [blame] | 128 | // into |target_routing_id| and the pending RenderFrameHost which is used for |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 129 | // the attempt is the return value. |
Alex Moshchuk | 27caae8 | 2017-09-11 23:11:18 | [diff] [blame] | 130 | RenderFrameHostImpl* PrepareToDuplicateHosts(Shell* shell, |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 131 | net::EmbeddedTestServer* server, |
Alex Moshchuk | 27caae8 | 2017-09-11 23:11:18 | [diff] [blame] | 132 | int* target_routing_id) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 133 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 134 | |
Aaron Colwell | 5fb87804 | 2020-12-17 19:48:44 | [diff] [blame] | 135 | if (IsIsolatedOriginRequiredToGuaranteeDedicatedProcess()) { |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 136 | // Isolate "bar.com" so we are guaranteed to get a different process |
| 137 | // for navigations to this origin. |
| 138 | IsolateOriginsForTesting(server, shell->web_contents(), {"bar.com"}); |
| 139 | } |
| 140 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 141 | // Start off with initial navigation, so we get the first process allocated. |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 142 | EXPECT_TRUE(NavigateToURL(shell, foo)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 143 | EXPECT_EQ(u"OK", shell->web_contents()->GetTitle()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 144 | |
| 145 | // Open another window, so we generate some more routing ids. |
| 146 | ShellAddedObserver shell2_observer; |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 147 | EXPECT_TRUE(ExecJs(shell, "window.open(document.URL + '#2');")); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 148 | Shell* shell2 = shell2_observer.GetShell(); |
| 149 | |
| 150 | // The new window must be in the same process, but have a new routing id. |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 151 | EXPECT_EQ(shell->web_contents() |
| 152 | ->GetPrimaryMainFrame() |
| 153 | ->GetProcess() |
| 154 | ->GetDeprecatedID(), |
| 155 | shell2->web_contents() |
| 156 | ->GetPrimaryMainFrame() |
| 157 | ->GetProcess() |
| 158 | ->GetDeprecatedID()); |
Lucas Furukawa Gadani | e764942 | 2021-02-03 03:04:32 | [diff] [blame] | 159 | *target_routing_id = shell2->web_contents() |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 160 | ->GetPrimaryMainFrame() |
Lucas Furukawa Gadani | e764942 | 2021-02-03 03:04:32 | [diff] [blame] | 161 | ->GetRenderViewHost() |
| 162 | ->GetRoutingID(); |
| 163 | EXPECT_NE(*target_routing_id, shell->web_contents() |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 164 | ->GetPrimaryMainFrame() |
Lucas Furukawa Gadani | e764942 | 2021-02-03 03:04:32 | [diff] [blame] | 165 | ->GetRenderViewHost() |
| 166 | ->GetRoutingID()); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 167 | |
| 168 | // Now, simulate a link click coming from the renderer. |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 169 | GURL extension_url("http://bar.com/simple_page.html"); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 170 | WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell->web_contents()); |
Jiacheng Guo | 4bdd0be | 2024-06-11 23:35:21 | [diff] [blame] | 171 | TestNavigationManager navigation_manager(wc, extension_url); |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 172 | wc->GetPrimaryFrameTree().root()->navigator().RequestOpenURL( |
| 173 | wc->GetPrimaryFrameTree().root()->current_frame_host(), extension_url, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 174 | nullptr /* initiator_frame_token */, |
| 175 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 176 | url::Origin::Create(foo), /* initiator_base_url= */ std::nullopt, nullptr, |
| 177 | std::string(), Referrer(), WindowOpenDisposition::CURRENT_TAB, |
Garrett Tanzer | 6c807e3 | 2022-04-14 17:33:58 | [diff] [blame] | 178 | false /* should_replace_current_entry */, true /* user_gesture */, |
Gyuyoung Kim | 16a12f5 | 2020-12-19 04:24:26 | [diff] [blame] | 179 | blink::mojom::TriggeringEventInfo::kFromTrustedEvent, std::string(), |
Kevin McNee | 6455638a | 2024-06-27 22:05:03 | [diff] [blame] | 180 | nullptr /* blob_url_loader_factory */, std::nullopt /* impression */, |
| 181 | false /* has_rel_opener */); |
Jiacheng Guo | 4bdd0be | 2024-06-11 23:35:21 | [diff] [blame] | 182 | navigation_manager.WaitForSpeculativeRenderFrameHostCreation(); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 183 | |
| 184 | // Since the navigation above requires a cross-process swap, there will be a |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 185 | // speculative/pending RenderFrameHost. Ensure it exists and is in a different |
| 186 | // process than the initial page. |
Sharon Yang | f60bc94 | 2022-12-01 02:14:07 | [diff] [blame] | 187 | RenderFrameHostImpl* next_rfh = wc->GetPrimaryFrameTree() |
| 188 | .root() |
| 189 | ->render_manager() |
| 190 | ->speculative_frame_host(); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 191 | |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 192 | EXPECT_TRUE(next_rfh); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 193 | EXPECT_NE(shell->web_contents() |
| 194 | ->GetPrimaryMainFrame() |
| 195 | ->GetProcess() |
| 196 | ->GetDeprecatedID(), |
| 197 | next_rfh->GetProcess()->GetDeprecatedID()); |
carlosk | c49005eb | 2015-06-16 11:25:07 | [diff] [blame] | 198 | |
Alex Moshchuk | 27caae8 | 2017-09-11 23:11:18 | [diff] [blame] | 199 | return next_rfh; |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 200 | } |
| 201 | |
Yeunjoo Choi | 9232ea3 | 2021-03-08 16:25:17 | [diff] [blame] | 202 | blink::mojom::OpenURLParamsPtr CreateOpenURLParams(const GURL& url) { |
| 203 | auto params = blink::mojom::OpenURLParams::New(); |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 204 | params->url = url; |
| 205 | params->disposition = WindowOpenDisposition::CURRENT_TAB; |
| 206 | params->should_replace_current_entry = false; |
| 207 | params->user_gesture = true; |
| 208 | return params; |
| 209 | } |
| 210 | |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 211 | std::unique_ptr<content::BlobHandle> CreateMemoryBackedBlob( |
| 212 | BrowserContext* browser_context, |
| 213 | const std::string& contents, |
| 214 | const std::string& content_type) { |
| 215 | std::unique_ptr<content::BlobHandle> result; |
| 216 | base::RunLoop loop; |
Lukasz Anforowicz | 7ef1cfd | 2021-05-04 02:18:37 | [diff] [blame] | 217 | browser_context->CreateMemoryBackedBlob( |
Peter Kasting | 5f6928c | 2024-11-29 21:25:11 | [diff] [blame] | 218 | base::as_byte_span(contents), content_type, |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 219 | base::BindOnce( |
| 220 | [](std::unique_ptr<content::BlobHandle>* out_blob, |
| 221 | base::OnceClosure done, |
| 222 | std::unique_ptr<content::BlobHandle> blob) { |
| 223 | *out_blob = std::move(blob); |
| 224 | std::move(done).Run(); |
| 225 | }, |
| 226 | &result, loop.QuitClosure())); |
| 227 | loop.Run(); |
| 228 | EXPECT_TRUE(result); |
| 229 | return result; |
| 230 | } |
| 231 | |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 232 | // Constructs a WebContentsDelegate that mocks a file dialog. |
| 233 | // Unlike content::FileChooserDelegate, this class doesn't make a response in |
| 234 | // RunFileChooser(), and a user needs to call Choose(). |
| 235 | class DelayedFileChooserDelegate : public WebContentsDelegate { |
| 236 | public: |
| 237 | void Choose(const base::FilePath& file) { |
| 238 | auto file_info = blink::mojom::FileChooserFileInfo::NewNativeFile( |
Joel Hockey | 0174ba7 | 2024-11-12 00:23:47 | [diff] [blame] | 239 | blink::mojom::NativeFileInfo::New(file, std::u16string(), |
| 240 | std::vector<std::u16string>())); |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 241 | std::vector<blink::mojom::FileChooserFileInfoPtr> files; |
| 242 | files.push_back(std::move(file_info)); |
| 243 | listener_->FileSelected(std::move(files), base::FilePath(), |
| 244 | blink::mojom::FileChooserParams::Mode::kOpen); |
| 245 | listener_.reset(); |
| 246 | } |
| 247 | |
| 248 | // WebContentsDelegate overrides |
| 249 | void RunFileChooser(RenderFrameHost* render_frame_host, |
Kent Tamura | 3abb32d | 2020-07-02 00:23:01 | [diff] [blame] | 250 | scoped_refptr<FileSelectListener> listener, |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 251 | const blink::mojom::FileChooserParams& params) override { |
| 252 | listener_ = std::move(listener); |
| 253 | } |
| 254 | |
| 255 | void EnumerateDirectory(WebContents* web_contents, |
Kent Tamura | 3abb32d | 2020-07-02 00:23:01 | [diff] [blame] | 256 | scoped_refptr<FileSelectListener> listener, |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 257 | const base::FilePath& directory_path) override { |
| 258 | listener->FileSelectionCanceled(); |
| 259 | } |
| 260 | |
| 261 | private: |
Kent Tamura | 3abb32d | 2020-07-02 00:23:01 | [diff] [blame] | 262 | scoped_refptr<FileSelectListener> listener_; |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | void FileChooserCallback(base::RunLoop* run_loop, |
| 266 | blink::mojom::FileChooserResultPtr result) { |
| 267 | run_loop->Quit(); |
| 268 | } |
| 269 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 270 | } // namespace |
| 271 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 272 | // The goal of these tests will be to "simulate" exploited renderer processes, |
| 273 | // which can send arbitrary IPC messages and confuse browser process internal |
| 274 | // state, leading to security bugs. We are trying to verify that the browser |
| 275 | // doesn't perform any dangerous operations in such cases. |
| 276 | class SecurityExploitBrowserTest : public ContentBrowserTest { |
| 277 | public: |
| 278 | SecurityExploitBrowserTest() {} |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 279 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 280 | void SetUpCommandLine(base::CommandLine* command_line) override { |
gab | 0fa95f0d | 2017-01-14 01:38:06 | [diff] [blame] | 281 | // EmbeddedTestServer::InitializeAndListen() initializes its |base_url_| |
| 282 | // which is required below. This cannot invoke Start() however as that kicks |
| 283 | // off the "EmbeddedTestServer IO Thread" which then races with |
| 284 | // initialization in ContentBrowserTest::SetUp(), http://crbug.com/674545. |
| 285 | ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 286 | |
| 287 | // Add a host resolver rule to map all outgoing requests to the test server. |
| 288 | // This allows us to use "real" hostnames in URLs, which we can use to |
| 289 | // create arbitrary SiteInstances. |
| 290 | command_line->AppendSwitchASCII( |
John Abd-El-Malek | 91243b3 | 2018-01-19 06:08:33 | [diff] [blame] | 291 | network::switches::kHostResolverRules, |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 292 | "MAP * " + |
| 293 | net::HostPortPair::FromURL(embedded_test_server()->base_url()) |
| 294 | .ToString() + |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 295 | ",EXCLUDE localhost"); |
| 296 | } |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 297 | |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 298 | void SetUpOnMainThread() override { |
gab | 0fa95f0d | 2017-01-14 01:38:06 | [diff] [blame] | 299 | // Complete the manual Start() after ContentBrowserTest's own |
| 300 | // initialization, ref. comment on InitializeAndListen() above. |
| 301 | embedded_test_server()->StartAcceptingConnections(); |
gzobqq | 1af4fad | 2016-01-30 13:07:06 | [diff] [blame] | 302 | } |
| 303 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 304 | protected: |
nasko | ada75b2 | 2016-06-11 16:09:46 | [diff] [blame] | 305 | // Tests that a given file path sent in a FrameHostMsg_RunFileChooser will |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 306 | // cause renderer to be killed. |
| 307 | void TestFileChooserWithPath(const base::FilePath& path); |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 308 | |
| 309 | void IsolateOrigin(const std::string& hostname) { |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 310 | IsolateOriginsForTesting(embedded_test_server(), shell()->web_contents(), |
| 311 | {hostname}); |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 312 | } |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 313 | }; |
| 314 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 315 | void SecurityExploitBrowserTest::TestFileChooserWithPath( |
| 316 | const base::FilePath& path) { |
| 317 | GURL foo("http://foo.com/simple_page.html"); |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 318 | EXPECT_TRUE(NavigateToURL(shell(), foo)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 319 | EXPECT_EQ(u"OK", shell()->web_contents()->GetTitle()); |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 320 | |
nasko | ada75b2 | 2016-06-11 16:09:46 | [diff] [blame] | 321 | RenderFrameHost* compromised_renderer = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 322 | shell()->web_contents()->GetPrimaryMainFrame(); |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 323 | blink::mojom::FileChooserParamsPtr params = |
| 324 | blink::mojom::FileChooserParams::New(); |
| 325 | params->default_file_name = path; |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 326 | |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 327 | mojo::test::BadMessageObserver bad_message_observer; |
Avi Drissman | bd3e98644 | 2020-05-20 21:09:20 | [diff] [blame] | 328 | mojo::Remote<blink::mojom::FileChooser> chooser = |
| 329 | FileChooserImpl::CreateBoundForTesting( |
| 330 | static_cast<RenderFrameHostImpl*>(compromised_renderer)); |
| 331 | chooser->OpenFileChooser( |
Kent Tamura | 8c9e056 | 2018-11-06 07:02:19 | [diff] [blame] | 332 | std::move(params), blink::mojom::FileChooser::OpenFileChooserCallback()); |
Avi Drissman | bd3e98644 | 2020-05-20 21:09:20 | [diff] [blame] | 333 | chooser.FlushForTesting(); |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 334 | EXPECT_THAT(bad_message_observer.WaitForBadMessage(), |
| 335 | ::testing::StartsWith("FileChooser: The default file name")); |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 338 | // Ensure that we kill the renderer process if we try to give it WebUI |
| 339 | // properties and it doesn't have enabled WebUI bindings. |
jaekyun | 37e572a3 | 2014-12-04 23:33:35 | [diff] [blame] | 340 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, SetWebUIProperty) { |
nick | 4c8dfd4 | 2014-11-14 04:11:49 | [diff] [blame] | 341 | GURL foo("http://foo.com/simple_page.html"); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 342 | |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 343 | EXPECT_TRUE(NavigateToURL(shell(), foo)); |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 344 | EXPECT_EQ(u"OK", shell()->web_contents()->GetTitle()); |
Avi Drissman | 78865bbb | 2024-08-22 20:57:19 | [diff] [blame] | 345 | EXPECT_TRUE(shell() |
| 346 | ->web_contents() |
| 347 | ->GetPrimaryMainFrame() |
| 348 | ->GetEnabledBindings() |
| 349 | .empty()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 350 | |
Nasko Oskov | dce1a62 | 2019-11-06 23:58:33 | [diff] [blame] | 351 | RenderFrameHost* compromised_renderer = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 352 | shell()->web_contents()->GetPrimaryMainFrame(); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 353 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
| 354 | compromised_renderer->GetProcess()); |
Lukasz Anforowicz | 6f74628 | 2018-01-04 23:24:51 | [diff] [blame] | 355 | compromised_renderer->SetWebUIProperty("toolkit", "views"); |
| 356 | EXPECT_EQ(bad_message::RVH_WEB_UI_BINDINGS_MISMATCH, kill_waiter.Wait()); |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 359 | // This is a test for crbug.com/312016 attempting to create duplicate |
| 360 | // RenderViewHosts. SetupForDuplicateHosts sets up this test case and leaves |
| 361 | // it in a state with pending RenderViewHost. Before the commit of the new |
| 362 | // pending RenderViewHost, this test case creates a new window through the new |
| 363 | // process. |
| 364 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
jam | 5f61aa0 | 2017-03-21 20:30:39 | [diff] [blame] | 365 | AttemptDuplicateRenderViewHost) { |
Tom Sepez | cc69c44 | 2025-07-17 02:00:30 | [diff] [blame] | 366 | int32_t duplicate_routing_id = IPC::mojom::kRoutingIdNone; |
Aaron Colwell | e953e56 | 2019-07-24 16:47:36 | [diff] [blame] | 367 | RenderFrameHostImpl* pending_rfh = PrepareToDuplicateHosts( |
| 368 | shell(), embedded_test_server(), &duplicate_routing_id); |
Tom Sepez | cc69c44 | 2025-07-17 02:00:30 | [diff] [blame] | 369 | EXPECT_NE(IPC::mojom::kRoutingIdNone, duplicate_routing_id); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 370 | |
nick | b374042 | 2017-04-24 22:27:45 | [diff] [blame] | 371 | mojom::CreateNewWindowParamsPtr params = mojom::CreateNewWindowParams::New(); |
| 372 | params->target_url = GURL("about:blank"); |
Alex Moshchuk | 27caae8 | 2017-09-11 23:11:18 | [diff] [blame] | 373 | pending_rfh->CreateNewWindow( |
Daniel Cheng | 8bb30c6 | 2017-10-23 20:40:47 | [diff] [blame] | 374 | std::move(params), base::BindOnce([](mojom::CreateNewWindowStatus, |
| 375 | mojom::CreateNewWindowReplyPtr) {})); |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 376 | // If the above operation doesn't cause a crash, the test has succeeded! |
[email protected] | 81374f2 | 2013-02-07 02:03:45 | [diff] [blame] | 377 | } |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 378 | |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 379 | // This is a test for crbug.com/444198. It tries to send a |
nasko | ada75b2 | 2016-06-11 16:09:46 | [diff] [blame] | 380 | // FrameHostMsg_RunFileChooser containing an invalid path. The browser should |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 381 | // correctly terminate the renderer in these cases. |
| 382 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, AttemptRunFileChoosers) { |
| 383 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("../../*.txt"))); |
| 384 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("/etc/*.conf"))); |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 385 | #if BUILDFLAG(IS_WIN) |
wfh | 815c487 | 2015-02-25 21:01:31 | [diff] [blame] | 386 | TestFileChooserWithPath( |
| 387 | base::FilePath(FILE_PATH_LITERAL("\\\\evilserver\\evilshare\\*.txt"))); |
| 388 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("c:\\*.txt"))); |
| 389 | TestFileChooserWithPath(base::FilePath(FILE_PATH_LITERAL("..\\..\\*.txt"))); |
| 390 | #endif |
| 391 | } |
| 392 | |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 393 | // A test for crbug.com/941008. |
| 394 | // Calling OpenFileChooser() and EnumerateChosenDirectory() for a single |
| 395 | // FileChooser instance had a problem. |
| 396 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, UnexpectedMethodsSequence) { |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 397 | EXPECT_TRUE(NavigateToURL(shell(), GURL("http://foo.com/simple_page.html"))); |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 398 | RenderFrameHost* compromised_renderer = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 399 | shell()->web_contents()->GetPrimaryMainFrame(); |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 400 | auto delegate = std::make_unique<DelayedFileChooserDelegate>(); |
| 401 | shell()->web_contents()->SetDelegate(delegate.get()); |
| 402 | |
Henrique Ferreiro | 3bb206b | 2019-09-18 12:48:23 | [diff] [blame] | 403 | mojo::Remote<blink::mojom::FileChooser> chooser = |
Avi Drissman | bd3e98644 | 2020-05-20 21:09:20 | [diff] [blame] | 404 | FileChooserImpl::CreateBoundForTesting( |
| 405 | static_cast<RenderFrameHostImpl*>(compromised_renderer)); |
Kent Tamura | 4a348269 | 2019-03-14 01:44:27 | [diff] [blame] | 406 | base::RunLoop run_loop1; |
| 407 | base::RunLoop run_loop2; |
| 408 | chooser->OpenFileChooser(blink::mojom::FileChooserParams::New(), |
| 409 | base::BindOnce(FileChooserCallback, &run_loop2)); |
| 410 | // The following EnumerateChosenDirectory() runs the specified callback |
| 411 | // immediately regardless of the content of the first argument FilePath. |
| 412 | chooser->EnumerateChosenDirectory( |
| 413 | base::FilePath(FILE_PATH_LITERAL(":*?\"<>|")), |
| 414 | base::BindOnce(FileChooserCallback, &run_loop1)); |
| 415 | run_loop1.Run(); |
| 416 | |
| 417 | delegate->Choose(base::FilePath(FILE_PATH_LITERAL("foo.txt"))); |
| 418 | run_loop2.Run(); |
| 419 | |
| 420 | // The test passes if it doesn't crash. |
| 421 | } |
| 422 | |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 423 | class CorsExploitBrowserTest : public ContentBrowserTest { |
| 424 | public: |
Yutaka Hirano | 235e892 | 2020-03-17 23:44:32 | [diff] [blame] | 425 | CorsExploitBrowserTest() = default; |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 426 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 427 | CorsExploitBrowserTest(const CorsExploitBrowserTest&) = delete; |
| 428 | CorsExploitBrowserTest& operator=(const CorsExploitBrowserTest&) = delete; |
| 429 | |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 430 | void SetUpOnMainThread() override { |
| 431 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 432 | SetupCrossSiteRedirector(embedded_test_server()); |
| 433 | } |
Hiroshige Hayashizaki | 8ee3f7e6 | 2019-09-05 07:17:47 | [diff] [blame] | 434 | }; |
| 435 | |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 436 | // Test that receiving a commit with incorrect origin properly terminates the |
| 437 | // renderer process. |
| 438 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, MismatchedOriginOnCommit) { |
| 439 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 440 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 441 | |
| 442 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 443 | ->GetPrimaryFrameTree() |
| 444 | .root(); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 445 | |
Charlie Reis | e2c2c49 | 2018-06-15 21:34:04 | [diff] [blame] | 446 | // Navigate to a new URL, with an interceptor that replaces the origin with |
| 447 | // one that does not match params.url. |
| 448 | GURL url(embedded_test_server()->GetURL("/title2.html")); |
| 449 | PwnCommitIPC(shell()->web_contents(), url, url, |
| 450 | url::Origin::Create(GURL("http://bar.com/"))); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 451 | |
| 452 | // Use LoadURL, as the test shouldn't wait for navigation commit. |
| 453 | NavigationController& controller = shell()->web_contents()->GetController(); |
| 454 | controller.LoadURL(url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
| 455 | EXPECT_NE(nullptr, controller.GetPendingEntry()); |
| 456 | EXPECT_EQ(url, controller.GetPendingEntry()->GetURL()); |
| 457 | |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 458 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Lukasz Anforowicz | 6f74628 | 2018-01-04 23:24:51 | [diff] [blame] | 459 | root->current_frame_host()->GetProcess()); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 460 | |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 461 | // When the IPC message is received and validation fails, the process is |
| 462 | // terminated. However, the notification for that should be processed in a |
| 463 | // separate task of the message loop, so ensure that the process is still |
| 464 | // considered alive. |
Lukasz Anforowicz | 5510ed65 | 2018-06-06 16:16:19 | [diff] [blame] | 465 | EXPECT_TRUE( |
| 466 | root->current_frame_host()->GetProcess()->IsInitializedAndNotDead()); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 467 | |
Lukasz Anforowicz | 6f74628 | 2018-01-04 23:24:51 | [diff] [blame] | 468 | EXPECT_EQ(bad_message::RFH_INVALID_ORIGIN_ON_COMMIT, kill_waiter.Wait()); |
nasko | 402cc29 | 2016-03-11 09:16:11 | [diff] [blame] | 469 | } |
| 470 | |
Rakina Zata Amni | d09b611 | 2021-06-05 06:20:14 | [diff] [blame] | 471 | // Test that receiving a document.open() URL update with incorrect origin |
| 472 | // properly terminates the renderer process. |
| 473 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 474 | MismatchedOriginOnDocumentOpenURLUpdate) { |
| 475 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 476 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 477 | |
| 478 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 479 | shell()->web_contents()->GetPrimaryMainFrame()); |
Rakina Zata Amni | d09b611 | 2021-06-05 06:20:14 | [diff] [blame] | 480 | |
| 481 | // Simulate a document.open() URL update with incorrect origin. |
| 482 | RenderProcessHostBadIpcMessageWaiter kill_waiter(rfh->GetProcess()); |
| 483 | static_cast<mojom::FrameHost*>(rfh)->DidOpenDocumentInputStream( |
| 484 | embedded_test_server()->GetURL("evil.com", "/title1.html")); |
| 485 | |
| 486 | // Ensure that the renderer process gets killed. |
| 487 | EXPECT_EQ(AreAllSitesIsolatedForTesting() |
| 488 | ? bad_message::RFH_CAN_COMMIT_URL_BLOCKED |
| 489 | : bad_message::RFH_INVALID_ORIGIN_ON_COMMIT, |
| 490 | kill_waiter.Wait()); |
| 491 | } |
| 492 | |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 493 | // Test that same-document navigations cannot go cross-origin (even within the |
| 494 | // same site). |
| 495 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 496 | CrossOriginSameDocumentCommit) { |
| 497 | GURL start_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 498 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 499 | |
| 500 | // Do a same-document navigation to a cross-origin URL/Origin (which match |
| 501 | // each other, unlike the MismatchedOriginOnCommit), using an interceptor that |
| 502 | // replaces the origin and URL. This intentionally uses a cross-origin but |
| 503 | // same-site destination, to avoid failing Site Isolation checks. |
| 504 | GURL dest_url(embedded_test_server()->GetURL("bar.foo.com", "/title2.html")); |
| 505 | PwnCommitIPC(shell()->web_contents(), start_url, dest_url, |
| 506 | url::Origin::Create(dest_url)); |
| 507 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 508 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 509 | // ExecJs will sometimes finish before the renderer gets killed, so we must |
| 510 | // ignore the result. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 511 | std::ignore = ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 512 | "history.pushState({}, '', location.href);"); |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 513 | EXPECT_EQ(bad_message::RFH_INVALID_ORIGIN_ON_COMMIT, kill_waiter.Wait()); |
| 514 | } |
| 515 | |
| 516 | // Test that same-document navigations cannot go cross-origin from about:blank |
| 517 | // (even within the same site). Uses a subframe to inherit an existing origin. |
| 518 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 519 | CrossOriginSameDocumentCommitFromAboutBlank) { |
| 520 | GURL start_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 521 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 522 | |
| 523 | // Create an about:blank iframe that inherits the origin. |
| 524 | RenderFrameHost* subframe = |
| 525 | CreateSubframe(static_cast<WebContentsImpl*>(shell()->web_contents()), |
| 526 | "child1", GURL(), false /* wait_for_navigation */); |
| 527 | EXPECT_EQ(url::Origin::Create(start_url), subframe->GetLastCommittedOrigin()); |
| 528 | |
| 529 | // Do a same-document navigation to another about:blank URL, but using a |
| 530 | // different origin. This intentionally uses a cross-origin but same-site |
| 531 | // origin to avoid triggering Site Isolation checks. |
| 532 | GURL blank_url("about:blank#foo"); |
| 533 | GURL fake_url(embedded_test_server()->GetURL("bar.foo.com", "/")); |
| 534 | PwnCommitIPC(shell()->web_contents(), blank_url, blank_url, |
| 535 | url::Origin::Create(fake_url)); |
| 536 | RenderProcessHostBadIpcMessageWaiter kill_waiter(subframe->GetProcess()); |
| 537 | // ExecJs will sometimes finish before the renderer gets killed, so we must |
| 538 | // ignore the result. |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 539 | std::ignore = ExecJs(subframe, "location.hash='foo';"); |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 540 | EXPECT_EQ(bad_message::RFH_INVALID_ORIGIN_ON_COMMIT, kill_waiter.Wait()); |
| 541 | } |
| 542 | |
| 543 | // Test that same-document navigations cannot go cross-origin (even within the |
| 544 | // same site), in the case that allow_universal_access_from_file_urls is enabled |
| 545 | // but the last committed origin is not a file URL. See also |
| 546 | // RenderFrameHostManagerTest.EnsureUniversalAccessFromFileSchemeSucceeds for |
| 547 | // the intended case that file URLs are allowed to go cross-origin. |
| 548 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 549 | CrossOriginSameDocumentCommitUniversalAccessNonFile) { |
| 550 | auto prefs = shell()->web_contents()->GetOrCreateWebPreferences(); |
| 551 | prefs.allow_universal_access_from_file_urls = true; |
| 552 | shell()->web_contents()->SetWebPreferences(prefs); |
| 553 | |
| 554 | GURL start_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 555 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 556 | |
| 557 | // Do a same-document navigation to a cross-origin URL, using an interceptor |
| 558 | // that replaces the URL but not the origin (to simulate the universal access |
| 559 | // case, but for a non-file committed origin). This intentionally uses a |
| 560 | // cross-origin but same-site destination, to avoid failing Site Isolation |
| 561 | // checks. |
| 562 | GURL dest_url(embedded_test_server()->GetURL("bar.foo.com", "/title2.html")); |
| 563 | PwnCommitIPC(shell()->web_contents(), start_url, dest_url, |
| 564 | url::Origin::Create(start_url)); |
| 565 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 566 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 567 | // ExecJs will sometimes finish before the renderer gets killed, so we must |
| 568 | // ignore the result. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 569 | std::ignore = ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 570 | "history.pushState({}, '', location.href);"); |
Charlie Reis | 95fbca44 | 2021-05-21 21:38:24 | [diff] [blame] | 571 | EXPECT_EQ(bad_message::RFH_INVALID_ORIGIN_ON_COMMIT, kill_waiter.Wait()); |
| 572 | } |
| 573 | |
Charlie Reis | c8e511e | 2024-02-14 17:23:06 | [diff] [blame] | 574 | // Test that receiving a commit with a URL with an invalid scheme properly |
| 575 | // terminates the renderer process. See https://crbug.com/324934416. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 576 | // TODO(crbug.com/40092527): This test can be removed once the browser |
Charlie Reis | c8e511e | 2024-02-14 17:23:06 | [diff] [blame] | 577 | // stops using cross-document URLs computed by the renderer process. |
| 578 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, BadUrlSchemeOnCommit) { |
| 579 | GURL start_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 580 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 581 | |
| 582 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 583 | ->GetPrimaryFrameTree() |
| 584 | .root(); |
| 585 | |
| 586 | // Navigate to a new URL, with an interceptor that replaces the URL with one |
| 587 | // that has an illegal scheme. Note that most cross-document navigations where |
| 588 | // the renderer's commit URL disagrees with the browser's expectation will |
| 589 | // currently be caught by a DCHECK in debug builds, but this case still works |
| 590 | // in release builds until the browser process becomes the authority for |
| 591 | // cross-document URLs in https://crbug.com/888079. For now, we can test this |
| 592 | // case and avoid the DCHECK by claiming to commit about:blank#blocked, which |
| 593 | // is given an exception in RenderFrameHostImpl's CalculateLoadingURL. |
| 594 | GURL url("about:blank#blocked"); |
| 595 | GURL bad_scheme_url("bar:com"); |
| 596 | PwnCommitIPC(shell()->web_contents(), url, bad_scheme_url, |
| 597 | url::Origin::Create(url)); |
| 598 | |
| 599 | RenderProcessHost* process = root->current_frame_host()->GetProcess(); |
| 600 | RenderProcessHostBadIpcMessageWaiter kill_waiter(process); |
| 601 | |
| 602 | // ExecJs will sometimes finish before the renderer gets killed, so we must |
| 603 | // ignore the result. |
| 604 | std::ignore = ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
| 605 | "location.href = 'about:blank#blocked';"); |
| 606 | |
| 607 | // When the IPC message is received and validation fails, the process is |
| 608 | // terminated. However, the notification for that should be processed in a |
| 609 | // separate task of the message loop, so ensure that the process is still |
| 610 | // considered alive. |
| 611 | EXPECT_TRUE(process->IsInitializedAndNotDead()); |
| 612 | |
| 613 | EXPECT_EQ(bad_message::RFH_CAN_COMMIT_URL_BLOCKED, kill_waiter.Wait()); |
| 614 | } |
| 615 | |
| 616 | // Test that receiving a same-document commit with a URL with an invalid scheme |
| 617 | // properly terminates the renderer process. See https://crbug.com/324934416. |
| 618 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 619 | BadUrlSchemeOnSameDocumentCommit) { |
| 620 | GURL start_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 621 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 622 | |
| 623 | // Do a same-document navigation to a URL with an incorrect scheme, but with |
| 624 | // the expected origin, using an interceptor that replaces the URL. |
| 625 | GURL dest_url("bar:com"); |
| 626 | PwnCommitIPC(shell()->web_contents(), start_url, dest_url, |
| 627 | url::Origin::Create(start_url)); |
| 628 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
| 629 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
| 630 | // ExecJs will sometimes finish before the renderer gets killed, so we must |
| 631 | // ignore the result. |
| 632 | std::ignore = ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
| 633 | "history.pushState({}, '', location.href);"); |
| 634 | EXPECT_EQ(bad_message::RFH_CAN_COMMIT_URL_BLOCKED, kill_waiter.Wait()); |
| 635 | } |
| 636 | |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 637 | namespace { |
| 638 | |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 639 | // Interceptor that replaces |interface_params| with the specified |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 640 | // value for the first DidCommitProvisionalLoad message it observes in the given |
| 641 | // |web_contents| while in scope. |
Arthur Hemery | 2a0a28be | 2019-03-06 17:51:36 | [diff] [blame] | 642 | class ScopedInterfaceParamsReplacer : public DidCommitNavigationInterceptor { |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 643 | public: |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 644 | ScopedInterfaceParamsReplacer( |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 645 | WebContents* web_contents, |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 646 | mojom::DidCommitProvisionalLoadInterfaceParamsPtr params_override) |
Arthur Hemery | 2a0a28be | 2019-03-06 17:51:36 | [diff] [blame] | 647 | : DidCommitNavigationInterceptor(web_contents), |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 648 | params_override_(std::move(params_override)) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 649 | |
| 650 | ScopedInterfaceParamsReplacer(const ScopedInterfaceParamsReplacer&) = delete; |
| 651 | ScopedInterfaceParamsReplacer& operator=( |
| 652 | const ScopedInterfaceParamsReplacer&) = delete; |
| 653 | |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 654 | ~ScopedInterfaceParamsReplacer() override = default; |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 655 | |
| 656 | protected: |
Arthur Hemery | 2a0a28be | 2019-03-06 17:51:36 | [diff] [blame] | 657 | bool WillProcessDidCommitNavigation( |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 658 | RenderFrameHost* render_frame_host, |
Arthur Hemery | 2a0a28be | 2019-03-06 17:51:36 | [diff] [blame] | 659 | NavigationRequest* navigation_request, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 660 | mojom::DidCommitProvisionalLoadParamsPtr*, |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 661 | mojom::DidCommitProvisionalLoadInterfaceParamsPtr* interface_params) |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 662 | override { |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 663 | interface_params->Swap(¶ms_override_); |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 664 | |
clamy | d3bfdb0 | 2018-07-12 13:52:18 | [diff] [blame] | 665 | return true; |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | private: |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 669 | mojom::DidCommitProvisionalLoadInterfaceParamsPtr params_override_; |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | } // namespace |
| 673 | |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 674 | // Test that, as a general rule, not receiving new |
| 675 | // DidCommitProvisionalLoadInterfaceParamsPtr for a cross-document navigation |
| 676 | // properly terminates the renderer process. There is one exception to this |
| 677 | // rule, see: RenderFrameHostImplBrowserTest. |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 678 | // InterfaceProviderRequestIsOptionalForFirstCommit. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 679 | // TODO(crbug.com/40519010): when all clients are converted to use |
Julie Jeongeun Kim | 641f475 | 2019-12-04 01:53:22 | [diff] [blame] | 680 | // BrowserInterfaceBroker, PendingReceiver<InterfaceProvider>-related code will |
| 681 | // be removed. |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 682 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 683 | MissingInterfaceProviderOnNonSameDocumentCommit) { |
| 684 | const GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 685 | const GURL non_same_document_url( |
| 686 | embedded_test_server()->GetURL("/title2.html")); |
| 687 | |
| 688 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 689 | |
Lukasz Anforowicz | 9904bfa3 | 2018-07-31 22:19:55 | [diff] [blame] | 690 | RenderFrameHostImpl* frame = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 691 | shell()->web_contents()->GetPrimaryMainFrame()); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 692 | RenderProcessHostBadIpcMessageWaiter kill_waiter(frame->GetProcess()); |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 693 | |
arthursonzogni | 85bc116 | 2019-10-15 14:44:13 | [diff] [blame] | 694 | NavigationHandleObserver navigation_observer(shell()->web_contents(), |
| 695 | non_same_document_url); |
Oksana Zhuravlova | 8b88e57 | 2019-01-07 21:54:00 | [diff] [blame] | 696 | ScopedInterfaceParamsReplacer replacer(shell()->web_contents(), nullptr); |
Alex Moshchuk | 8969de2 | 2019-09-28 01:12:26 | [diff] [blame] | 697 | EXPECT_TRUE(NavigateToURLAndExpectNoCommit(shell(), non_same_document_url)); |
Lukasz Anforowicz | 6f74628 | 2018-01-04 23:24:51 | [diff] [blame] | 698 | EXPECT_EQ(bad_message::RFH_INTERFACE_PROVIDER_MISSING, kill_waiter.Wait()); |
Lukasz Anforowicz | 9904bfa3 | 2018-07-31 22:19:55 | [diff] [blame] | 699 | |
arthursonzogni | 85bc116 | 2019-10-15 14:44:13 | [diff] [blame] | 700 | // Verify that the death of the renderer process doesn't leave behind and |
| 701 | // leak NavigationRequests - see https://crbug.com/869193. |
Alexander Timin | 26864e1 | 2019-10-18 02:00:06 | [diff] [blame] | 702 | EXPECT_FALSE(frame->HasPendingCommitNavigation()); |
arthursonzogni | 85bc116 | 2019-10-15 14:44:13 | [diff] [blame] | 703 | EXPECT_FALSE(navigation_observer.has_committed()); |
| 704 | EXPECT_TRUE(navigation_observer.is_error()); |
| 705 | EXPECT_TRUE(navigation_observer.last_committed_url().is_empty()); |
| 706 | EXPECT_EQ(net::OK, navigation_observer.net_error_code()); |
Balazs Engedy | 0c8d550b | 2017-12-06 21:30:23 | [diff] [blame] | 707 | } |
| 708 | |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 709 | // Test that a compromised renderer cannot ask to upload an arbitrary file in |
| 710 | // OpenURL. This is a regression test for https://crbug.com/726067. |
| 711 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 712 | OpenUrl_ResourceRequestBody) { |
| 713 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 714 | GURL target_url(embedded_test_server()->GetURL("/echoall")); |
| 715 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 716 | |
| 717 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 718 | ->GetPrimaryFrameTree() |
| 719 | .root(); |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 720 | |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 721 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Lukasz Anforowicz | 6f74628 | 2018-01-04 23:24:51 | [diff] [blame] | 722 | root->current_frame_host()->GetProcess()); |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 723 | |
| 724 | // Prepare a file to upload. |
Francois Doray | 21cd5319 | 2018-08-21 13:32:35 | [diff] [blame] | 725 | base::ScopedAllowBlockingForTesting allow_blocking; |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 726 | base::ScopedTempDir temp_dir; |
| 727 | base::FilePath file_path; |
| 728 | std::string file_content("test-file-content"); |
| 729 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 730 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.GetPath(), &file_path)); |
Lei Zhang | 9989f27 | 2020-05-11 19:21:21 | [diff] [blame] | 731 | ASSERT_TRUE(base::WriteFile(file_path, file_content)); |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 732 | |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 733 | // Simulate an OpenURL Mojo method asking to POST a file that the renderer |
| 734 | // shouldn't have access to. |
| 735 | auto params = CreateOpenURLParams(target_url); |
| 736 | params->post_body = new network::ResourceRequestBody; |
| 737 | params->post_body->AppendFileRange(file_path, 0, file_content.size(), |
| 738 | base::Time()); |
| 739 | params->should_replace_current_entry = true; |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 740 | |
Mario Sanchez Prada | cb83d6c8 | 2020-09-11 13:11:51 | [diff] [blame] | 741 | static_cast<mojom::FrameHost*>(root->current_frame_host()) |
| 742 | ->OpenURL(std::move(params)); |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 743 | |
| 744 | // Verify that the malicious navigation did not commit the navigation to |
| 745 | // |target_url|. |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 746 | EXPECT_EQ(start_url, root->current_frame_host()->GetLastCommittedURL()); |
| 747 | |
| 748 | // Verify that the malicious renderer got killed. |
Lukasz Anforowicz | 445abd4 | 2019-01-25 21:27:27 | [diff] [blame] | 749 | EXPECT_EQ(bad_message::ILLEGAL_UPLOAD_PARAMS, kill_waiter.Wait()); |
lukasza | 4ec2e757 | 2017-05-26 23:18:10 | [diff] [blame] | 750 | } |
| 751 | |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 752 | // Forging a navigation commit after the initial empty document will result in a |
| 753 | // renderer kill, even if the URL used is about:blank. |
| 754 | // See https://crbug.com/766262 for an example advanced case that involves |
| 755 | // forging a frame's unique name. |
| 756 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 757 | NonInitialAboutBlankRendererKill) { |
| 758 | // Navigate normally. |
| 759 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
| 760 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 761 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 762 | shell()->web_contents()->GetPrimaryMainFrame()); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 763 | |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 764 | // Simulate an about:blank commit without a NavigationRequest. It will fail |
| 765 | // because only initial commits are allowed to do this. |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 766 | auto params = mojom::DidCommitProvisionalLoadParams::New(); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 767 | params->did_create_new_entry = false; |
| 768 | params->url = GURL("about:blank"); |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 769 | params->referrer = blink::mojom::Referrer::New(); |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 770 | params->transition = ui::PAGE_TRANSITION_LINK; |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 771 | params->should_update_history = false; |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 772 | params->method = "GET"; |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 773 | params->page_state = blink::PageState::CreateFromURL(GURL("about:blank")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 774 | params->origin = url::Origin::Create(GURL("about:blank")); |
ckitagawa | 223004a | 2020-06-23 16:10:17 | [diff] [blame] | 775 | params->embedding_token = base::UnguessableToken::Create(); |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 776 | RenderProcessHostBadIpcMessageWaiter kill_waiter(rfh->GetProcess()); |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 777 | static_cast<mojom::FrameHost*>(rfh)->DidCommitProvisionalLoad( |
| 778 | std::move(params), |
| 779 | mojom::DidCommitProvisionalLoadInterfaceParams::New( |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 780 | mojo::PendingRemote<blink::mojom::BrowserInterfaceBroker>() |
| 781 | .InitWithNewPipeAndPassReceiver())); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 782 | |
Rakina Zata Amni | a8866d2 | 2020-11-19 10:05:10 | [diff] [blame] | 783 | // Verify that the malicious renderer got killed. |
| 784 | EXPECT_EQ(bad_message::RFH_NO_MATCHING_NAVIGATION_REQUEST_ON_COMMIT, |
| 785 | kill_waiter.Wait()); |
Charles Reis | c050720 | 2017-09-21 00:40:02 | [diff] [blame] | 786 | } |
| 787 | |
Alex Moshchuk | 375f4de8 | 2025-05-13 17:30:48 | [diff] [blame] | 788 | // Make sure that a renderer is terminated if it sends an invalid net error code |
| 789 | // in a DidFailLoadWithError() IPC. See https://crbug.com/407069514. |
| 790 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 791 | DidFailLoadWithInvalidErrorCode) { |
| 792 | GURL url(embedded_test_server()->GetURL("/title1.html")); |
| 793 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 794 | |
| 795 | // Navigate normally and wait for commit (but not full load). |
| 796 | TestFrameNavigationObserver commit_observer(shell()); |
| 797 | GURL failed_url(embedded_test_server()->GetURL("/title2.html")); |
| 798 | ASSERT_TRUE(ExecJs(shell(), JsReplace("location.href = $1;", failed_url))); |
| 799 | commit_observer.WaitForCommit(); |
| 800 | |
| 801 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
| 802 | shell()->web_contents()->GetPrimaryMainFrame()); |
| 803 | |
| 804 | // Simulate a DidFailLoadWithError message with an invalid error code. |
| 805 | RenderProcessHostBadIpcMessageWaiter kill_waiter(rfh->GetProcess()); |
| 806 | int32_t invalid_error_code = INT_MAX; |
| 807 | ASSERT_FALSE(net::IsOkOrDefinedError(invalid_error_code)); |
| 808 | static_cast<blink::mojom::LocalFrameHost*>(rfh)->DidFailLoadWithError( |
| 809 | failed_url, invalid_error_code); |
| 810 | |
| 811 | // Verify that the malicious renderer got killed. |
| 812 | EXPECT_EQ(bad_message::RFHI_INVALID_NET_ERROR_CODE, kill_waiter.Wait()); |
| 813 | } |
| 814 | |
Andrew Williams | 2bb0d8e | 2024-08-07 21:37:36 | [diff] [blame] | 815 | class SecurityExploitBrowserTestMojoBlobURLs |
| 816 | : public SecurityExploitBrowserTest { |
| 817 | public: |
| 818 | SecurityExploitBrowserTestMojoBlobURLs() = default; |
| 819 | |
| 820 | void TearDown() override { |
| 821 | storage::BlobUrlRegistry::SetURLStoreCreationHookForTesting(nullptr); |
| 822 | } |
| 823 | }; |
| 824 | |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 825 | // Check that when site isolation is enabled, an origin can't create a blob URL |
| 826 | // for a different origin. Similar to the test above, but checks the |
| 827 | // mojo-based Blob URL implementation. See https://crbug.com/886976. |
Andrew Williams | 2bb0d8e | 2024-08-07 21:37:36 | [diff] [blame] | 828 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTestMojoBlobURLs, |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 829 | CreateMojoBlobURLInDifferentOrigin) { |
| 830 | IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess()); |
| 831 | |
| 832 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 833 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 834 | RenderFrameHost* rfh = shell()->web_contents()->GetPrimaryMainFrame(); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 835 | |
| 836 | // Intercept future blob URL registrations and overwrite the blob URL origin |
| 837 | // with b.com. |
| 838 | std::string target_origin = "http://b.com"; |
| 839 | std::string blob_path = "5881f76e-10d2-410d-8c61-ef210502acfd"; |
Andrew Williams | 39151a7 | 2022-10-18 23:12:41 | [diff] [blame] | 840 | |
| 841 | base::RepeatingCallback<void(storage::BlobUrlRegistry*, mojo::ReceiverId)> |
| 842 | blob_url_registry_intercept_hook; |
Andrew Williams | 39151a7 | 2022-10-18 23:12:41 | [diff] [blame] | 843 | |
janiceliu | 2457d49d | 2024-07-09 18:15:57 | [diff] [blame] | 844 | blob_url_registry_intercept_hook = |
| 845 | base::BindRepeating(&BlobURLStoreInterceptor::Intercept, |
| 846 | GURL("blob:" + target_origin + "/" + blob_path)); |
| 847 | storage::BlobUrlRegistry::SetURLStoreCreationHookForTesting( |
| 848 | &blob_url_registry_intercept_hook); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 849 | |
| 850 | // Register a blob URL from the a.com main frame, which will go through the |
| 851 | // interceptor above and be rewritten to register the blob URL with the b.com |
| 852 | // origin. This should result in a kill because a.com should not be allowed |
| 853 | // to create blob URLs outside of its own origin. |
Lukasz Anforowicz | 110cda3 | 2020-03-23 22:32:03 | [diff] [blame] | 854 | content::RenderProcessHostBadMojoMessageWaiter crash_observer( |
| 855 | rfh->GetProcess()); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 856 | |
Chris Fredrickson | d3bb268 | 2023-05-10 03:32:26 | [diff] [blame] | 857 | // The renderer should always get killed, but sometimes ExecJs returns |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 858 | // true anyway, so just ignore the result. |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 859 | std::ignore = ExecJs(rfh, "URL.createObjectURL(new Blob(['foo']))"); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 860 | |
| 861 | // If the process is killed, this test passes. |
Lukasz Anforowicz | 110cda3 | 2020-03-23 22:32:03 | [diff] [blame] | 862 | EXPECT_EQ( |
| 863 | "Received bad user message: " |
Marijn Kruisselbrink | 15cfa2b | 2021-11-09 21:39:27 | [diff] [blame] | 864 | "URL with invalid origin passed to BlobURLStore::Register", |
Lukasz Anforowicz | 110cda3 | 2020-03-23 22:32:03 | [diff] [blame] | 865 | crash_observer.Wait()); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Check that with site isolation enabled, an origin can't create a filesystem |
| 869 | // URL for a different origin. See https://crbug.com/888001. |
| 870 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 871 | CreateFilesystemURLInDifferentOrigin) { |
| 872 | IsolateAllSitesForTesting(base::CommandLine::ForCurrentProcess()); |
| 873 | |
| 874 | GURL main_url(embedded_test_server()->GetURL( |
| 875 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 876 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
kyraseevers | 235fd48 | 2021-10-25 17:33:26 | [diff] [blame] | 877 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 878 | shell()->web_contents()->GetPrimaryMainFrame()); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 879 | |
| 880 | // Block the renderer on operation that never completes, to shield it from |
| 881 | // receiving unexpected browser->renderer IPCs that might CHECK. |
| 882 | rfh->ExecuteJavaScriptWithUserGestureForTests( |
Peter Kasting | 65491ff | 2021-04-29 16:40:31 | [diff] [blame] | 883 | u"var r = new XMLHttpRequest();" |
| 884 | u"r.open('GET', '/slow?99999', false);" |
| 885 | u"r.send(null);" |
Stephen McGruer | a37eac1 | 2022-04-11 16:17:17 | [diff] [blame] | 886 | u"while (1);", |
Avi Drissman | 73cb985 | 2024-08-26 18:10:58 | [diff] [blame] | 887 | base::NullCallback(), ISOLATED_WORLD_ID_GLOBAL); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 888 | |
| 889 | // Set up a blob ID and populate it with attacker-controlled value. This |
| 890 | // is just using the blob APIs directly since creating arbitrary blobs is not |
| 891 | // what is prohibited; this data is not in any origin. |
| 892 | std::string payload = "<html><body>pwned.</body></html>"; |
| 893 | std::string payload_type = "text/html"; |
| 894 | std::unique_ptr<content::BlobHandle> blob = CreateMemoryBackedBlob( |
| 895 | rfh->GetSiteInstance()->GetBrowserContext(), payload, payload_type); |
| 896 | std::string blob_id = blob->GetUUID(); |
| 897 | |
| 898 | // Target a different origin. |
| 899 | std::string target_origin = "http://b.com"; |
| 900 | GURL target_url = |
| 901 | GURL("filesystem:" + target_origin + "/temporary/exploit.html"); |
| 902 | |
| 903 | // Note: a well-behaved renderer would always call Open first before calling |
| 904 | // Create and Write, but it's actually not necessary for the original attack |
| 905 | // to succeed, so we omit it. As a result there are some log warnings from the |
| 906 | // quota observer. |
| 907 | |
| 908 | PwnMessageHelper::FileSystemCreate(rfh->GetProcess(), 23, target_url, false, |
Mariam Ali | 8338d9fa | 2023-07-24 16:57:31 | [diff] [blame] | 909 | false, false, rfh->GetStorageKey()); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 910 | |
| 911 | // Write the blob into the file. If successful, this places an |
| 912 | // attacker-controlled value in a resource on the target origin. |
| 913 | PwnMessageHelper::FileSystemWrite(rfh->GetProcess(), 24, target_url, blob_id, |
Mariam Ali | 8338d9fa | 2023-07-24 16:57:31 | [diff] [blame] | 914 | 0, rfh->GetStorageKey()); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 915 | |
kyraseevers | 2c703e5 | 2021-11-12 17:06:23 | [diff] [blame] | 916 | // Now navigate to `target_url` in a subframe. It should not succeed, and the |
| 917 | // subframe should not contain `payload`. |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 918 | TestNavigationObserver observer(shell()->web_contents()); |
| 919 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 920 | ->GetPrimaryFrameTree() |
| 921 | .root(); |
Alex Moshchuk | 2078096 | 2018-09-27 23:17:04 | [diff] [blame] | 922 | NavigateFrameToURL(root->child_at(0), target_url); |
| 923 | EXPECT_FALSE(observer.last_navigation_succeeded()); |
| 924 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, observer.last_net_error_code()); |
| 925 | |
| 926 | RenderFrameHost* attacked_rfh = root->child_at(0)->current_frame_host(); |
| 927 | std::string body = |
| 928 | EvalJs(attacked_rfh, "document.body.innerText").ExtractString(); |
| 929 | EXPECT_TRUE(base::StartsWith(body, "Could not load the requested resource", |
| 930 | base::CompareCase::INSENSITIVE_ASCII)) |
| 931 | << " body=" << body; |
| 932 | } |
| 933 | |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 934 | // Verify that when a compromised renderer tries to navigate a remote frame to |
| 935 | // a disallowed URL (e.g., file URL), that navigation is blocked. |
| 936 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 937 | BlockIllegalOpenURLFromRemoteFrame) { |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 938 | // Explicitly isolating a.com helps ensure that this test is applicable on |
| 939 | // platforms without site-per-process. |
| 940 | IsolateOrigin("a.com"); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 941 | |
| 942 | GURL main_url(embedded_test_server()->GetURL( |
| 943 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 944 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 945 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 946 | ->GetPrimaryFrameTree() |
| 947 | .root(); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 948 | FrameTreeNode* child = root->child_at(0); |
| 949 | |
| 950 | // Simulate an IPC message where the top frame asks the remote subframe to |
| 951 | // navigate to a file: URL. |
Sharon Yang | 7424bda | 2021-11-04 20:27:43 | [diff] [blame] | 952 | SiteInstanceImpl* a_com_instance = |
| 953 | root->current_frame_host()->GetSiteInstance(); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 954 | RenderFrameProxyHost* proxy = |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 955 | child->current_frame_host() |
| 956 | ->browsing_context_state() |
| 957 | ->GetRenderFrameProxyHost(a_com_instance->group()); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 958 | EXPECT_TRUE(proxy); |
| 959 | |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 960 | TestNavigationObserver observer(shell()->web_contents()); |
Mario Sanchez Prada | cb83d6c8 | 2020-09-11 13:11:51 | [diff] [blame] | 961 | static_cast<mojom::FrameHost*>(proxy->frame_tree_node()->current_frame_host()) |
| 962 | ->OpenURL(CreateOpenURLParams(GURL("file:///"))); |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 963 | observer.Wait(); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 964 | |
| 965 | // Verify that the malicious navigation was blocked. Currently, this happens |
Nasko Oskov | 413468f8 | 2018-10-29 23:22:01 | [diff] [blame] | 966 | // by rewriting the target URL to about:blank#blocked. |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 967 | // |
| 968 | // TODO(alexmos): Consider killing the renderer process in this case, since |
| 969 | // this security check is already enforced in the renderer process. |
Nasko Oskov | 413468f8 | 2018-10-29 23:22:01 | [diff] [blame] | 970 | EXPECT_EQ(GURL(kBlockedURL), |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 971 | child->current_frame_host()->GetLastCommittedURL()); |
| 972 | |
| 973 | // Navigate to the starting page again to recreate the proxy, then try the |
| 974 | // same malicious navigation with a chrome:// URL. |
| 975 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 976 | child = root->child_at(0); |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 977 | proxy = child->current_frame_host() |
| 978 | ->browsing_context_state() |
| 979 | ->GetRenderFrameProxyHost(a_com_instance->group()); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 980 | EXPECT_TRUE(proxy); |
| 981 | |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 982 | TestNavigationObserver observer_2(shell()->web_contents()); |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 983 | GURL chrome_url(std::string(kChromeUIScheme) + "://" + |
| 984 | std::string(kChromeUIGpuHost)); |
Mario Sanchez Prada | cb83d6c8 | 2020-09-11 13:11:51 | [diff] [blame] | 985 | static_cast<mojom::FrameHost*>(proxy->frame_tree_node()->current_frame_host()) |
| 986 | ->OpenURL(CreateOpenURLParams(chrome_url)); |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 987 | observer_2.Wait(); |
Nasko Oskov | 413468f8 | 2018-10-29 23:22:01 | [diff] [blame] | 988 | EXPECT_EQ(GURL(kBlockedURL), |
Alex Moshchuk | 5f8671e | 2018-10-19 02:10:11 | [diff] [blame] | 989 | child->current_frame_host()->GetLastCommittedURL()); |
| 990 | } |
| 991 | |
Gyuyoung Kim | eff73ad5 | 2021-03-25 22:59:01 | [diff] [blame] | 992 | class RemoteFrameHostInterceptor |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 993 | : public blink::mojom::RemoteFrameHostInterceptorForTesting { |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 994 | public: |
Gyuyoung Kim | eff73ad5 | 2021-03-25 22:59:01 | [diff] [blame] | 995 | explicit RemoteFrameHostInterceptor( |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 996 | RenderFrameProxyHost* render_frame_proxy_host, |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 997 | const url::Origin& evil_origin) |
Daniel Cheng | f693d88 | 2024-05-07 16:48:37 | [diff] [blame] | 998 | : evil_origin_(evil_origin), |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 999 | swapped_impl_( |
Daniel Cheng | f693d88 | 2024-05-07 16:48:37 | [diff] [blame] | 1000 | render_frame_proxy_host->frame_host_receiver_for_testing(), |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 1001 | this) {} |
| 1002 | |
| 1003 | ~RemoteFrameHostInterceptor() override = default; |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1004 | |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1005 | RemoteFrameHost* GetForwardingInterface() override { |
Daniel Cheng | f693d88 | 2024-05-07 16:48:37 | [diff] [blame] | 1006 | return swapped_impl_.old_impl(); |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void RouteMessageEvent( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1010 | const std::optional<blink::LocalFrameToken>& source_frame_token, |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1011 | const url::Origin& source_origin, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 1012 | const std::u16string& target_origin, |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1013 | blink::TransferableMessage message) override { |
| 1014 | // Forward the message to the actual RFPH replacing |source_origin| with the |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1015 | // "evil origin". |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1016 | GetForwardingInterface()->RouteMessageEvent( |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1017 | std::move(source_frame_token), evil_origin_, std::move(target_origin), |
| 1018 | std::move(message)); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1019 | } |
| 1020 | |
Gyuyoung Kim | eff73ad5 | 2021-03-25 22:59:01 | [diff] [blame] | 1021 | void OpenURL(blink::mojom::OpenURLParamsPtr params) override { |
| 1022 | intercepted_params_ = std::move(params); |
| 1023 | } |
| 1024 | |
| 1025 | blink::mojom::OpenURLParamsPtr GetInterceptedParams() { |
| 1026 | return std::move(intercepted_params_); |
| 1027 | } |
| 1028 | |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1029 | private: |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1030 | url::Origin evil_origin_; |
Gyuyoung Kim | eff73ad5 | 2021-03-25 22:59:01 | [diff] [blame] | 1031 | blink::mojom::OpenURLParamsPtr intercepted_params_; |
Daniel Cheng | 304b8f5 | 2024-05-07 16:48:11 | [diff] [blame] | 1032 | mojo::test::ScopedSwapImplForTesting<blink::mojom::RemoteFrameHost> |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 1033 | swapped_impl_; |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1034 | }; |
| 1035 | |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1036 | // Test verifying that a compromised renderer can't lie about the source_origin |
| 1037 | // passed along with the RouteMessageEvent() mojo message. See also |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1038 | // https://crbug.com/915721. |
| 1039 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, PostMessageSourceOrigin) { |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1040 | // Explicitly isolating a.com helps ensure that this test is applicable on |
| 1041 | // platforms without site-per-process. |
| 1042 | IsolateOrigin("b.com"); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1043 | |
| 1044 | // Navigate to a page with an OOPIF. |
| 1045 | GURL main_url(embedded_test_server()->GetURL( |
| 1046 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 1047 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1048 | |
| 1049 | // Sanity check of test setup: main frame and subframe should be isolated. |
| 1050 | WebContents* web_contents = shell()->web_contents(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1051 | RenderFrameHost* main_frame = web_contents->GetPrimaryMainFrame(); |
Dave Tapuska | 217f04d | 2021-09-22 13:35:41 | [diff] [blame] | 1052 | RenderFrameHost* subframe = ChildFrameAt(main_frame, 0); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1053 | EXPECT_NE(main_frame->GetProcess(), subframe->GetProcess()); |
| 1054 | |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1055 | // We need to get ahold of the RenderFrameProxyHost representing the main |
| 1056 | // frame for the subframe's process, to install the mojo interceptor. |
| 1057 | FrameTreeNode* main_frame_node = |
| 1058 | static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1059 | ->GetPrimaryFrameTree() |
| 1060 | .root(); |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1061 | FrameTreeNode* subframe_node = main_frame_node->child_at(0); |
Sharon Yang | 7424bda | 2021-11-04 20:27:43 | [diff] [blame] | 1062 | SiteInstanceImpl* b_com_instance = |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1063 | subframe_node->current_frame_host()->GetSiteInstance(); |
| 1064 | RenderFrameProxyHost* main_frame_proxy_host = |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1065 | main_frame_node->current_frame_host() |
| 1066 | ->browsing_context_state() |
| 1067 | ->GetRenderFrameProxyHost(b_com_instance->group()); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1068 | |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1069 | // Prepare to intercept the RouteMessageEvent IPC message that will come |
| 1070 | // from the subframe process. |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1071 | url::Origin evil_source_origin = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1072 | web_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin(); |
Gyuyoung Kim | eff73ad5 | 2021-03-25 22:59:01 | [diff] [blame] | 1073 | RemoteFrameHostInterceptor mojo_interceptor(main_frame_proxy_host, |
| 1074 | evil_source_origin); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1075 | |
Mario Sanchez Prada | e54aa9e | 2020-05-20 15:08:31 | [diff] [blame] | 1076 | // Post a message from the subframe to the cross-site parent and intercept the |
| 1077 | // associated IPC message, changing it to simulate a compromised subframe |
| 1078 | // renderer lying that the |source_origin| of the postMessage is the origin of |
| 1079 | // the parent (not of the subframe). |
| 1080 | RenderProcessHostBadIpcMessageWaiter kill_waiter(subframe->GetProcess()); |
| 1081 | EXPECT_TRUE(ExecJs(subframe, "parent.postMessage('blah', '*')")); |
Lukasz Anforowicz | 3ae9799d | 2019-01-11 01:01:32 | [diff] [blame] | 1082 | EXPECT_EQ(bad_message::RFPH_POST_MESSAGE_INVALID_SOURCE_ORIGIN, |
| 1083 | kill_waiter.Wait()); |
| 1084 | } |
| 1085 | |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1086 | // Test verifying that a compromised renderer can't lie about the source_origin |
| 1087 | // passed along with the RouteMessageEvent() mojo message. Similar to the test |
| 1088 | // above, but exercises a scenario where the source origin is opaque and the |
| 1089 | // precursor needs to be validated. This provides coverage for messages sent |
| 1090 | // from sandboxed frames; see https://crbug.com/40606810 and |
| 1091 | // https://crbug.com/325410297. |
| 1092 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1093 | PostMessageOpaqueSourceOrigin) { |
| 1094 | // This test requires opaque origin enforcements to be turned on; otherwise, |
| 1095 | // there would be no renderer kill to check for. |
| 1096 | if (!base::FeatureList::IsEnabled( |
| 1097 | features::kAdditionalOpaqueOriginEnforcements)) { |
| 1098 | GTEST_SKIP(); |
| 1099 | } |
| 1100 | |
| 1101 | // Explicitly isolating b.com helps ensure that this test is applicable on |
| 1102 | // platforms without site-per-process. |
| 1103 | IsolateOrigin("b.com"); |
| 1104 | |
| 1105 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1106 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 1107 | WebContentsImpl* web_contents = |
| 1108 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1109 | FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root(); |
| 1110 | RenderFrameHostImpl* main_frame = root->current_frame_host(); |
| 1111 | |
| 1112 | // Create cross-site sandboxed child frame. |
| 1113 | GURL child_url(embedded_test_server()->GetURL("b.com", "/title1.html")); |
| 1114 | { |
| 1115 | std::string js_str = base::StringPrintf( |
| 1116 | "var frame = document.createElement('iframe'); " |
| 1117 | "frame.sandbox = 'allow-scripts'; " |
| 1118 | "frame.src = '%s'; " |
| 1119 | "document.body.appendChild(frame);", |
| 1120 | child_url.spec().c_str()); |
| 1121 | EXPECT_TRUE(ExecJs(main_frame, js_str)); |
| 1122 | ASSERT_TRUE(WaitForLoadStop(web_contents)); |
| 1123 | } |
| 1124 | |
| 1125 | // Sanity check of test setup: main frame and subframe should be in separate |
| 1126 | // processes, and subframe should be sandboxed. |
| 1127 | FrameTreeNode* subframe_node = root->child_at(0); |
| 1128 | RenderFrameHostImpl* subframe = subframe_node->current_frame_host(); |
| 1129 | EXPECT_NE(main_frame->GetProcess(), subframe->GetProcess()); |
| 1130 | EXPECT_TRUE(subframe->GetSiteInstance()->GetSiteInfo().is_sandboxed()); |
| 1131 | |
| 1132 | // Retrieve the RenderFrameProxyHost representing the main frame for the |
| 1133 | // subframe's process. |
| 1134 | RenderFrameProxyHost* main_frame_proxy_host = |
| 1135 | main_frame->browsing_context_state()->GetRenderFrameProxyHost( |
| 1136 | subframe->GetSiteInstance()->group()); |
| 1137 | |
| 1138 | // Prepare to intercept the RouteMessageEvent IPC message that will come from |
| 1139 | // the subframe process. Set the fake source origin to an opaque origin with |
| 1140 | // a.com as the precursor. |
| 1141 | url::Origin precursor_origin = main_frame->GetLastCommittedOrigin(); |
| 1142 | url::Origin evil_source_origin = precursor_origin.DeriveNewOpaqueOrigin(); |
| 1143 | EXPECT_TRUE(evil_source_origin.opaque()); |
| 1144 | EXPECT_EQ("a.com", |
| 1145 | evil_source_origin.GetTupleOrPrecursorTupleIfOpaque().host()); |
| 1146 | |
| 1147 | RemoteFrameHostInterceptor mojo_interceptor(main_frame_proxy_host, |
| 1148 | evil_source_origin); |
| 1149 | |
| 1150 | // Post a message from the subframe to the cross-site parent and intercept the |
| 1151 | // associated IPC message, changing it to simulate a compromised subframe |
| 1152 | // renderer lying that the |source_origin| of the postMessage has an incorrect |
| 1153 | // precursor of a.com, rather than b.com. This should result in a renderer |
| 1154 | // kill. |
| 1155 | RenderProcessHostBadIpcMessageWaiter kill_waiter(subframe->GetProcess()); |
| 1156 | EXPECT_TRUE(ExecJs(subframe, "parent.postMessage('blah', '*')")); |
| 1157 | EXPECT_EQ(bad_message::RFPH_POST_MESSAGE_INVALID_SOURCE_ORIGIN, |
| 1158 | kill_waiter.Wait()); |
| 1159 | } |
| 1160 | |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1161 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1162 | InvalidRemoteNavigationInitiator) { |
| 1163 | // Explicitly isolating a.com helps ensure that this test is applicable on |
| 1164 | // platforms without site-per-process. |
| 1165 | IsolateOrigin("a.com"); |
| 1166 | |
| 1167 | // Navigate to a test page where the subframe is cross-site (and because of |
| 1168 | // IsolateOrigin call above in a separate process) from the main frame. |
| 1169 | GURL main_url(embedded_test_server()->GetURL( |
| 1170 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 1171 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
Mario Sanchez Prada | cb83d6c8 | 2020-09-11 13:11:51 | [diff] [blame] | 1172 | RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1173 | shell()->web_contents()->GetPrimaryMainFrame()); |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1174 | RenderProcessHost* main_process = main_frame->GetProcess(); |
Dave Tapuska | 217f04d | 2021-09-22 13:35:41 | [diff] [blame] | 1175 | RenderFrameHost* subframe = ChildFrameAt(main_frame, 0); |
| 1176 | ASSERT_TRUE(subframe); |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1177 | RenderProcessHost* subframe_process = subframe->GetProcess(); |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 1178 | EXPECT_NE(main_process->GetDeprecatedID(), |
| 1179 | subframe_process->GetDeprecatedID()); |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1180 | |
Gyuyoung Kim | 0028790a | 2020-06-26 00:09:00 | [diff] [blame] | 1181 | // Prepare to intercept OpenURL Mojo message that will come from |
| 1182 | // the main frame. |
Gyuyoung Kim | bfcb9fc | 2020-09-02 14:13:53 | [diff] [blame] | 1183 | FrameTreeNode* main_frame_node = |
| 1184 | static_cast<WebContentsImpl*>(shell()->web_contents()) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1185 | ->GetPrimaryFrameTree() |
| 1186 | .root(); |
Gyuyoung Kim | bfcb9fc | 2020-09-02 14:13:53 | [diff] [blame] | 1187 | FrameTreeNode* child_node = main_frame_node->child_at(0); |
Sharon Yang | 7424bda | 2021-11-04 20:27:43 | [diff] [blame] | 1188 | SiteInstanceImpl* a_com_instance = |
Gyuyoung Kim | bfcb9fc | 2020-09-02 14:13:53 | [diff] [blame] | 1189 | main_frame_node->current_frame_host()->GetSiteInstance(); |
| 1190 | RenderFrameProxyHost* proxy = |
Harkiran Bolaria | d22a1dca | 2022-02-22 17:01:12 | [diff] [blame] | 1191 | child_node->current_frame_host() |
| 1192 | ->browsing_context_state() |
| 1193 | ->GetRenderFrameProxyHost(a_com_instance->group()); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1194 | RenderProcessHostBadIpcMessageWaiter kill_waiter(main_process); |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 1195 | { |
Alex Moshchuk | 1005cd9 | 2024-06-05 21:00:40 | [diff] [blame] | 1196 | RemoteFrameHostInterceptor interceptor(proxy, url::Origin()); |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 1197 | |
| 1198 | // Have the main frame request navigation in the "remote" subframe. This |
| 1199 | // will result in OpenURL Mojo message being sent to the |
| 1200 | // RenderFrameProxyHost. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1201 | EXPECT_TRUE(ExecJs(shell()->web_contents()->GetPrimaryMainFrame(), |
Will Harris | eb6cca01 | 2022-02-11 01:23:17 | [diff] [blame] | 1202 | "window.frames[0].location = '/title1.html';")); |
| 1203 | |
| 1204 | // Change the intercepted message to simulate a compromised subframe |
| 1205 | // renderer lying that the |initiator_origin| is the origin of the |
| 1206 | // |subframe|. |
| 1207 | auto evil_params = interceptor.GetInterceptedParams(); |
| 1208 | evil_params->initiator_origin = subframe->GetLastCommittedOrigin(); |
| 1209 | |
| 1210 | // Inject the invalid IPC and verify that the renderer gets terminated. |
| 1211 | static_cast<mojom::FrameHost*>(main_frame)->OpenURL(std::move(evil_params)); |
| 1212 | } |
| 1213 | |
Lukasz Anforowicz | 56211d45 | 2019-02-05 18:05:51 | [diff] [blame] | 1214 | EXPECT_EQ(bad_message::INVALID_INITIATOR_ORIGIN, kill_waiter.Wait()); |
| 1215 | } |
| 1216 | |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1217 | class BeginNavigationInitiatorReplacer : public FrameHostInterceptor { |
| 1218 | public: |
Lukasz Anforowicz | 2294ca8 | 2019-02-25 18:26:05 | [diff] [blame] | 1219 | BeginNavigationInitiatorReplacer( |
| 1220 | WebContents* web_contents, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1221 | std::optional<url::Origin> initiator_to_inject) |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1222 | : FrameHostInterceptor(web_contents), |
| 1223 | initiator_to_inject_(initiator_to_inject) {} |
| 1224 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 1225 | BeginNavigationInitiatorReplacer(const BeginNavigationInitiatorReplacer&) = |
| 1226 | delete; |
| 1227 | BeginNavigationInitiatorReplacer& operator=( |
| 1228 | const BeginNavigationInitiatorReplacer&) = delete; |
| 1229 | |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1230 | bool WillDispatchBeginNavigation( |
| 1231 | RenderFrameHost* render_frame_host, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1232 | blink::mojom::CommonNavigationParamsPtr* common_params, |
| 1233 | blink::mojom::BeginNavigationParamsPtr* begin_params, |
Julie Jeongeun Kim | 249cfbb | 2019-08-30 06:44:05 | [diff] [blame] | 1234 | mojo::PendingRemote<blink::mojom::BlobURLToken>* blob_url_token, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 1235 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) |
| 1236 | override { |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1237 | if (is_activated_) { |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 1238 | (*common_params)->initiator_origin = initiator_to_inject_; |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1239 | is_activated_ = false; |
| 1240 | } |
| 1241 | |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | void Activate() { is_activated_ = true; } |
| 1246 | |
| 1247 | private: |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1248 | std::optional<url::Origin> initiator_to_inject_; |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1249 | bool is_activated_ = false; |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1250 | }; |
| 1251 | |
| 1252 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1253 | InvalidBeginNavigationInitiator) { |
arthursonzogni | 0a164974 | 2019-10-11 08:35:20 | [diff] [blame] | 1254 | WebContentsImpl* web_contents = |
| 1255 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
arthursonzogni | 0a164974 | 2019-10-11 08:35:20 | [diff] [blame] | 1256 | |
arthursonzogni | b64b584 | 2019-10-25 09:09:51 | [diff] [blame] | 1257 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1258 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1259 | BeginNavigationInitiatorReplacer injector( |
| 1260 | web_contents, url::Origin::Create(GURL("http://b.com"))); |
| 1261 | |
arthursonzogni | b64b584 | 2019-10-25 09:09:51 | [diff] [blame] | 1262 | // Explicitly isolating a.com helps ensure that this test is applicable on |
| 1263 | // platforms without site-per-process. |
| 1264 | IsolateOrigin("a.com"); |
| 1265 | |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1266 | // Navigate to a test page that will be locked to a.com. |
| 1267 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1268 | EXPECT_TRUE(NavigateToURL(web_contents, main_url)); |
| 1269 | |
| 1270 | // Start monitoring for renderer kills. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1271 | RenderProcessHost* main_process = |
| 1272 | web_contents->GetPrimaryMainFrame()->GetProcess(); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1273 | RenderProcessHostBadIpcMessageWaiter kill_waiter(main_process); |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1274 | |
| 1275 | // Have the main frame navigate and lie that the initiator origin is b.com. |
| 1276 | injector.Activate(); |
Nate Chapin | 3501550b | 2019-04-23 20:58:47 | [diff] [blame] | 1277 | // Don't expect a response for the script, as the process may be killed |
| 1278 | // before the script sends its completion message. |
| 1279 | ExecuteScriptAsync(web_contents, "window.location = '/title2.html';"); |
Lukasz Anforowicz | 3cfc1efd | 2019-02-22 02:29:29 | [diff] [blame] | 1280 | |
| 1281 | // Verify that the renderer was terminated. |
| 1282 | EXPECT_EQ(bad_message::INVALID_INITIATOR_ORIGIN, kill_waiter.Wait()); |
| 1283 | } |
| 1284 | |
Alex Moshchuk | 4043b73 | 2024-06-14 06:02:39 | [diff] [blame] | 1285 | // Similar to the test above, but ensure that initiator origins are validated |
| 1286 | // even for opaque origins. |
| 1287 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1288 | InvalidBeginNavigationOpaqueInitiator) { |
| 1289 | // This test requires opaque origin enforcements to be turned on; otherwise, |
| 1290 | // there would be no renderer kill to check for. |
| 1291 | if (!base::FeatureList::IsEnabled( |
| 1292 | features::kAdditionalOpaqueOriginEnforcements)) { |
| 1293 | GTEST_SKIP(); |
| 1294 | } |
| 1295 | |
| 1296 | WebContentsImpl* web_contents = |
| 1297 | static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 1298 | |
| 1299 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 1300 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 1301 | url::Origin injected_origin(url::Origin::Create(GURL("http://evil.com"))); |
| 1302 | injected_origin = injected_origin.DeriveNewOpaqueOrigin(); |
| 1303 | BeginNavigationInitiatorReplacer injector(web_contents, injected_origin); |
| 1304 | |
| 1305 | // Explicitly isolating b.com helps ensure that this test is applicable on |
| 1306 | // platforms without site-per-process. |
| 1307 | IsolateOrigin("b.com"); |
| 1308 | |
| 1309 | // Navigate to a test page at a.com. |
| 1310 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1311 | EXPECT_TRUE(NavigateToURL(web_contents, main_url)); |
| 1312 | |
| 1313 | // Add a cross-site sandboxed child frame at b.com. |
| 1314 | FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root(); |
| 1315 | RenderFrameHostImpl* main_frame = root->current_frame_host(); |
| 1316 | GURL child_url(embedded_test_server()->GetURL("b.com", "/title1.html")); |
| 1317 | { |
| 1318 | std::string js_str = base::StringPrintf( |
| 1319 | "var frame = document.createElement('iframe'); " |
| 1320 | "frame.sandbox = 'allow-scripts'; " |
| 1321 | "frame.src = '%s'; " |
| 1322 | "document.body.appendChild(frame);", |
| 1323 | child_url.spec().c_str()); |
| 1324 | EXPECT_TRUE(ExecJs(main_frame, js_str)); |
| 1325 | ASSERT_TRUE(WaitForLoadStop(web_contents)); |
| 1326 | } |
| 1327 | |
| 1328 | // Sanity check of test setup: main frame and subframe should be in separate |
| 1329 | // processes, and subframe should be sandboxed. |
| 1330 | FrameTreeNode* subframe_node = root->child_at(0); |
| 1331 | RenderFrameHostImpl* subframe = subframe_node->current_frame_host(); |
| 1332 | EXPECT_NE(main_frame->GetProcess(), subframe->GetProcess()); |
| 1333 | EXPECT_TRUE(subframe->GetSiteInstance()->GetSiteInfo().is_sandboxed()); |
| 1334 | |
| 1335 | // Start monitoring for renderer kills. |
| 1336 | RenderProcessHostBadIpcMessageWaiter kill_waiter(subframe->GetProcess()); |
| 1337 | |
| 1338 | // Have the sandboxed subframe navigate and lie that the initiator origin is |
| 1339 | // an opaque origin with the precursor of evil.com instead of b.com. |
| 1340 | injector.Activate(); |
| 1341 | // Don't expect a response for the script, as the process may be killed |
| 1342 | // before the script sends its completion message. |
| 1343 | ExecuteScriptAsync(subframe, "window.location = '/title2.html';"); |
| 1344 | |
| 1345 | // Verify that the renderer was terminated. |
| 1346 | EXPECT_EQ(bad_message::INVALID_INITIATOR_ORIGIN, kill_waiter.Wait()); |
| 1347 | } |
| 1348 | |
Lukasz Anforowicz | 2294ca8 | 2019-02-25 18:26:05 | [diff] [blame] | 1349 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1350 | MissingBeginNavigationInitiator) { |
| 1351 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 1352 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 1353 | WebContents* web_contents = shell()->web_contents(); |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1354 | BeginNavigationInitiatorReplacer injector(web_contents, std::nullopt); |
Lukasz Anforowicz | 2294ca8 | 2019-02-25 18:26:05 | [diff] [blame] | 1355 | |
| 1356 | // Navigate to a test page. |
| 1357 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1358 | EXPECT_TRUE(NavigateToURL(web_contents, main_url)); |
| 1359 | |
| 1360 | // Start monitoring for renderer kills. |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1361 | RenderProcessHost* main_process = |
| 1362 | web_contents->GetPrimaryMainFrame()->GetProcess(); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1363 | RenderProcessHostBadIpcMessageWaiter kill_waiter(main_process); |
Lukasz Anforowicz | 2294ca8 | 2019-02-25 18:26:05 | [diff] [blame] | 1364 | |
| 1365 | // Have the main frame submit a BeginNavigation IPC with a missing initiator. |
| 1366 | injector.Activate(); |
Nate Chapin | 3501550b | 2019-04-23 20:58:47 | [diff] [blame] | 1367 | // Don't expect a response for the script, as the process may be killed |
| 1368 | // before the script sends its completion message. |
| 1369 | ExecuteScriptAsync(web_contents, "window.location = '/title2.html';"); |
Lukasz Anforowicz | 2294ca8 | 2019-02-25 18:26:05 | [diff] [blame] | 1370 | |
| 1371 | // Verify that the renderer was terminated. |
| 1372 | EXPECT_EQ(bad_message::RFHI_BEGIN_NAVIGATION_MISSING_INITIATOR_ORIGIN, |
| 1373 | kill_waiter.Wait()); |
| 1374 | } |
| 1375 | |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1376 | namespace { |
| 1377 | |
| 1378 | // An interceptor class that allows replacing the URL of the commit IPC from |
| 1379 | // the renderer process to the browser process. |
| 1380 | class DidCommitUrlReplacer : public DidCommitNavigationInterceptor { |
| 1381 | public: |
| 1382 | DidCommitUrlReplacer(WebContents* web_contents, const GURL& replacement_url) |
| 1383 | : DidCommitNavigationInterceptor(web_contents), |
| 1384 | replacement_url_(replacement_url) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 1385 | |
| 1386 | DidCommitUrlReplacer(const DidCommitUrlReplacer&) = delete; |
| 1387 | DidCommitUrlReplacer& operator=(const DidCommitUrlReplacer&) = delete; |
| 1388 | |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1389 | ~DidCommitUrlReplacer() override = default; |
| 1390 | |
| 1391 | protected: |
| 1392 | bool WillProcessDidCommitNavigation( |
| 1393 | RenderFrameHost* render_frame_host, |
| 1394 | NavigationRequest* navigation_request, |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1395 | mojom::DidCommitProvisionalLoadParamsPtr* params, |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1396 | mojom::DidCommitProvisionalLoadInterfaceParamsPtr* interface_params) |
| 1397 | override { |
arthursonzogni | 73fe321 | 2020-11-17 13:24:07 | [diff] [blame] | 1398 | (**params).url = replacement_url_; |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | private: |
| 1403 | GURL replacement_url_; |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1404 | }; |
| 1405 | |
| 1406 | } // namespace |
| 1407 | |
| 1408 | // Test which verifies that when an exploited renderer process sends a commit |
| 1409 | // message with URL that the process is not allowed to commit. |
Nasko Oskov | 8e0e1ee | 2020-07-20 23:50:01 | [diff] [blame] | 1410 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, DidCommitInvalidURL) { |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1411 | // Explicitly isolating foo.com helps ensure that this test is applicable on |
| 1412 | // platforms without site-per-process. |
| 1413 | IsolateOrigin("foo.com"); |
| 1414 | |
Nasko Oskov | 8e0e1ee | 2020-07-20 23:50:01 | [diff] [blame] | 1415 | RenderFrameDeletedObserver initial_frame_deleted_observer( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1416 | shell()->web_contents()->GetPrimaryMainFrame()); |
Nasko Oskov | 8e0e1ee | 2020-07-20 23:50:01 | [diff] [blame] | 1417 | |
Sreeja Kamishetty | ce8d594 | 2020-08-19 11:25:51 | [diff] [blame] | 1418 | // Test assumes the initial RenderFrameHost to be deleted. Disable |
| 1419 | // back-forward cache to ensure that it doesn't get preserved in the cache. |
| 1420 | DisableBackForwardCacheForTesting(shell()->web_contents(), |
Rakina Zata Amni | 30af706 | 2022-01-19 23:46:36 | [diff] [blame] | 1421 | BackForwardCache::TEST_REQUIRES_NO_CACHING); |
Sreeja Kamishetty | ce8d594 | 2020-08-19 11:25:51 | [diff] [blame] | 1422 | |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1423 | // Navigate to foo.com initially. |
| 1424 | GURL foo_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 1425 | EXPECT_TRUE(NavigateToURL(shell(), foo_url)); |
| 1426 | |
Nasko Oskov | 8e0e1ee | 2020-07-20 23:50:01 | [diff] [blame] | 1427 | // Wait for the RenderFrameHost which was current before the navigation to |
| 1428 | // foo.com to be deleted. This is necessary, since on a slow system the |
| 1429 | // UnloadACK event can arrive after the DidCommitUrlReplacer instance below |
| 1430 | // is created. The replacer code has checks to ensure that all frames being |
| 1431 | // deleted it has seen being created, which with delayed UnloadACK is |
| 1432 | // violated. |
| 1433 | initial_frame_deleted_observer.WaitUntilDeleted(); |
| 1434 | |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1435 | // Create the interceptor object which will replace the URL of the subsequent |
| 1436 | // navigation with bar.com based URL. |
| 1437 | GURL bar_url(embedded_test_server()->GetURL("bar.com", "/title3.html")); |
| 1438 | DidCommitUrlReplacer url_replacer(shell()->web_contents(), bar_url); |
| 1439 | |
| 1440 | // Navigate to another URL within foo.com, which would usually be committed |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 1441 | // successfully, but when the URL is modified it should result in the |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1442 | // termination of the renderer process. |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1443 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1444 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
Alex Moshchuk | 4174c19 | 2019-08-20 16:58:09 | [diff] [blame] | 1445 | EXPECT_FALSE(NavigateToURL( |
| 1446 | shell(), embedded_test_server()->GetURL("foo.com", "/title2.html"))); |
Nasko Oskov | 7d94539 | 2019-04-20 01:59:30 | [diff] [blame] | 1447 | EXPECT_EQ(bad_message::RFH_CAN_COMMIT_URL_BLOCKED, kill_waiter.Wait()); |
| 1448 | } |
| 1449 | |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1450 | // Test which verifies that when an exploited renderer process sends a commit |
| 1451 | // message with URL that the process is not allowed to commit. |
| 1452 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
Ritika Gupta | af55214 | 2024-11-18 23:20:47 | [diff] [blame] | 1453 | DISABLED_DidCommitInvalidURLWithOpaqueOrigin) { |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1454 | // Explicitly isolating foo.com helps ensure that this test is applicable on |
| 1455 | // platforms without site-per-process. |
| 1456 | IsolateOrigin("foo.com"); |
| 1457 | |
| 1458 | RenderFrameDeletedObserver initial_frame_deleted_observer( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1459 | shell()->web_contents()->GetPrimaryMainFrame()); |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1460 | |
| 1461 | // Test assumes the initial RenderFrameHost to be deleted. Disable |
| 1462 | // back-forward cache to ensure that it doesn't get preserved in the cache. |
| 1463 | DisableBackForwardCacheForTesting(shell()->web_contents(), |
Rakina Zata Amni | 30af706 | 2022-01-19 23:46:36 | [diff] [blame] | 1464 | BackForwardCache::TEST_REQUIRES_NO_CACHING); |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1465 | |
| 1466 | // Navigate to foo.com initially. |
| 1467 | GURL foo_url(embedded_test_server()->GetURL("foo.com", |
| 1468 | "/page_with_blank_iframe.html")); |
| 1469 | EXPECT_TRUE(NavigateToURL(shell(), foo_url)); |
| 1470 | |
| 1471 | // Wait for the RenderFrameHost which was current before the navigation to |
| 1472 | // foo.com to be deleted. This is necessary, since on a slow system the |
| 1473 | // UnloadACK event can arrive after the DidCommitUrlReplacer instance below |
| 1474 | // is created. The replacer code has checks to ensure that all frames being |
| 1475 | // deleted it has seen being created, which with delayed UnloadACK is |
| 1476 | // violated. |
| 1477 | initial_frame_deleted_observer.WaitUntilDeleted(); |
| 1478 | |
| 1479 | // Create the interceptor object which will replace the URL of the subsequent |
| 1480 | // navigation with bar.com based URL. |
| 1481 | GURL bar_url(embedded_test_server()->GetURL("bar.com", "/title3.html")); |
| 1482 | DidCommitUrlReplacer url_replacer(shell()->web_contents(), bar_url); |
| 1483 | |
| 1484 | // Navigate the subframe to a data URL, which would usually be committed |
| 1485 | // successfully in the same process as foo.com, but when the URL is modified |
| 1486 | // it should result in the termination of the renderer process. |
| 1487 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1488 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
Nasko Oskov | 4ef7cb1 | 2022-01-14 06:13:47 | [diff] [blame] | 1489 | |
| 1490 | // Using BeginNavigateIframeToURL is necessary here, since the process |
| 1491 | // termination will result in DidFinishNavigation notification with the |
| 1492 | // navigation not in "committed" state. NavigateIframeToURL waits for the |
| 1493 | // navigation to complete and ignores non-committed navigations, therefore |
| 1494 | // it will wait indefinitely. |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1495 | GURL data_url(R"(data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E)"); |
Nasko Oskov | 4ef7cb1 | 2022-01-14 06:13:47 | [diff] [blame] | 1496 | EXPECT_TRUE(BeginNavigateIframeToURL(shell()->web_contents(), "test_iframe", |
| 1497 | data_url)); |
Lukasz Anforowicz | e2077a9a | 2020-08-21 23:45:20 | [diff] [blame] | 1498 | EXPECT_EQ(bad_message::RFH_CAN_COMMIT_URL_BLOCKED, kill_waiter.Wait()); |
| 1499 | } |
| 1500 | |
Nasko Oskov | a4acefa | 2020-02-12 23:30:32 | [diff] [blame] | 1501 | // Test which verifies that a WebUI process cannot send a commit message with |
| 1502 | // URL for a web document. |
| 1503 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 1504 | WebUIProcessDidCommitWebURL) { |
| 1505 | // Navigate to a WebUI document. |
| 1506 | GURL webui_url(GetWebUIURL(kChromeUIGpuHost)); |
| 1507 | EXPECT_TRUE(NavigateToURL(shell(), webui_url)); |
| 1508 | |
| 1509 | // Create the interceptor object which will replace the URL of the subsequent |
| 1510 | // navigation with |web_url|. |
| 1511 | GURL web_url(embedded_test_server()->GetURL("foo.com", "/title3.html")); |
| 1512 | DidCommitUrlReplacer url_replacer(shell()->web_contents(), web_url); |
| 1513 | |
| 1514 | // Navigate to another URL within the WebUI, which would usually be committed |
| 1515 | // successfully, but when the URL is modified it should result in the |
| 1516 | // termination of the renderer process. |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1517 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1518 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
Nasko Oskov | a4acefa | 2020-02-12 23:30:32 | [diff] [blame] | 1519 | GURL second_webui_url(webui_url.Resolve("/foo")); |
| 1520 | EXPECT_FALSE(NavigateToURL(shell(), second_webui_url)); |
| 1521 | EXPECT_EQ(bad_message::RFH_CAN_COMMIT_URL_BLOCKED, kill_waiter.Wait()); |
| 1522 | } |
| 1523 | |
Nasko Oskov | 6773006 | 2020-05-08 16:49:23 | [diff] [blame] | 1524 | // Test that verifies that if a RenderFrameHost is incorrectly given WebUI |
Nasko Oskov | 1b7c562 | 2024-04-16 19:47:25 | [diff] [blame] | 1525 | // bindings the browser process crashes due to CHECK enforcements. |
Nasko Oskov | 6773006 | 2020-05-08 16:49:23 | [diff] [blame] | 1526 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
Nasko Oskov | 1b7c562 | 2024-04-16 19:47:25 | [diff] [blame] | 1527 | AllowBindingsForNonWebUIProcess) { |
Nasko Oskov | 6773006 | 2020-05-08 16:49:23 | [diff] [blame] | 1528 | // Navigate to a web URL. |
| 1529 | GURL initial_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); |
| 1530 | EXPECT_TRUE(NavigateToURL(shell(), initial_url)); |
| 1531 | |
Nasko Oskov | 1b7c562 | 2024-04-16 19:47:25 | [diff] [blame] | 1532 | // Grant WebUI bindings to the frame to simulate a bug in the code that |
| 1533 | // incorrectly does it and verify the browser process crashes. |
Peter Boström | b41d6ee | 2024-11-20 15:49:39 | [diff] [blame] | 1534 | EXPECT_NOTREACHED_DEATH( |
Nasko Oskov | 1b7c562 | 2024-04-16 19:47:25 | [diff] [blame] | 1535 | shell()->web_contents()->GetPrimaryMainFrame()->AllowBindings( |
Avi Drissman | 78865bbb | 2024-08-22 20:57:19 | [diff] [blame] | 1536 | BindingsPolicySet({BindingsPolicyValue::kWebUi}))); |
Nasko Oskov | 6773006 | 2020-05-08 16:49:23 | [diff] [blame] | 1537 | } |
| 1538 | |
Charlie Reis | f4b6b3ec | 2021-07-09 07:31:38 | [diff] [blame] | 1539 | // Tests that a web page cannot bind to a WebUI interface if a WebUI page is the |
| 1540 | // currently committed RenderFrameHost in the tab (https://crbug.com/1225929). |
| 1541 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, BindToWebUIFromWebViaMojo) { |
| 1542 | // Navigate to a non-privileged web page, and simulate a renderer compromise |
| 1543 | // by granting MojoJS. |
| 1544 | GURL web_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1545 | TestNavigationManager navigation(shell()->web_contents(), web_url); |
| 1546 | shell()->LoadURL(web_url); |
| 1547 | EXPECT_TRUE(navigation.WaitForResponse()); |
| 1548 | RenderFrameHostImpl* main_frame = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1549 | shell()->web_contents()->GetPrimaryMainFrame()); |
Jiewei Qian | cc639a66 | 2022-04-01 02:43:34 | [diff] [blame] | 1550 | main_frame->GetFrameBindingsControl()->EnableMojoJsBindings(nullptr); |
Fergal Daly | 83bc3cd | 2023-01-18 00:22:54 | [diff] [blame] | 1551 | ASSERT_TRUE(navigation.WaitForNavigationFinished()); |
Charlie Reis | f4b6b3ec | 2021-07-09 07:31:38 | [diff] [blame] | 1552 | |
| 1553 | // Open a popup so that the process won't exit on its own when leaving. |
| 1554 | OpenBlankWindow(static_cast<WebContentsImpl*>(shell()->web_contents())); |
| 1555 | |
| 1556 | // When the page unloads (after the cross-process navigation to an actual |
| 1557 | // WebUI page below), try to bind to a WebUI interface from the web |
| 1558 | // RenderFrameHost. Ensure the unload timer and bfcache are disabled so that |
| 1559 | // the handler has a chance to run. |
Fergal Daly | da98f8e1 | 2023-11-22 04:54:26 | [diff] [blame] | 1560 | // This test uses `pagehide` rather than `unload` since they occur at the |
| 1561 | // same timing but `unload` is being deprecated. |
Charlie Reis | f4b6b3ec | 2021-07-09 07:31:38 | [diff] [blame] | 1562 | main_frame->DisableUnloadTimerForTesting(); |
| 1563 | DisableBackForwardCacheForTesting(shell()->web_contents(), |
Fergal Daly | da98f8e1 | 2023-11-22 04:54:26 | [diff] [blame] | 1564 | BackForwardCache::TEST_REQUIRES_NO_CACHING); |
Charlie Reis | f4b6b3ec | 2021-07-09 07:31:38 | [diff] [blame] | 1565 | ASSERT_TRUE(ExecJs(main_frame, R"( |
| 1566 | // Intentionally leak pipe as a global so it doesn't get GCed. |
| 1567 | newMessagePipe = Mojo.createMessagePipe(); |
Fergal Daly | da98f8e1 | 2023-11-22 04:54:26 | [diff] [blame] | 1568 | onpagehide = function () { |
Charlie Reis | f4b6b3ec | 2021-07-09 07:31:38 | [diff] [blame] | 1569 | Mojo.bindInterface('mojom.ProcessInternalsHandler', |
| 1570 | newMessagePipe.handle0); |
| 1571 | }; |
| 1572 | )")); |
| 1573 | |
| 1574 | // Now navigate to a WebUI page and expect the previous renderer process to be |
| 1575 | // killed when asking to bind to the WebUI interface. |
| 1576 | GURL webui_url( |
| 1577 | GetWebUIURL(kChromeUIProcessInternalsHost).Resolve("#general")); |
| 1578 | RenderProcessHostBadIpcMessageWaiter kill_waiter(main_frame->GetProcess()); |
| 1579 | EXPECT_TRUE(NavigateToURL(shell(), webui_url)); |
| 1580 | |
| 1581 | // Verify that the previous renderer was terminated. |
| 1582 | EXPECT_EQ(bad_message::RFH_INVALID_WEB_UI_CONTROLLER, kill_waiter.Wait()); |
| 1583 | } |
| 1584 | |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1585 | class BeginNavigationTransitionReplacer : public FrameHostInterceptor { |
| 1586 | public: |
| 1587 | BeginNavigationTransitionReplacer(WebContents* web_contents, |
| 1588 | ui::PageTransition transition_to_inject) |
| 1589 | : FrameHostInterceptor(web_contents), |
| 1590 | transition_to_inject_(transition_to_inject) {} |
| 1591 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 1592 | BeginNavigationTransitionReplacer(const BeginNavigationTransitionReplacer&) = |
| 1593 | delete; |
| 1594 | BeginNavigationTransitionReplacer& operator=( |
| 1595 | const BeginNavigationTransitionReplacer&) = delete; |
| 1596 | |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1597 | bool WillDispatchBeginNavigation( |
| 1598 | RenderFrameHost* render_frame_host, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1599 | blink::mojom::CommonNavigationParamsPtr* common_params, |
| 1600 | blink::mojom::BeginNavigationParamsPtr* begin_params, |
Julie Jeongeun Kim | 249cfbb | 2019-08-30 06:44:05 | [diff] [blame] | 1601 | mojo::PendingRemote<blink::mojom::BlobURLToken>* blob_url_token, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 1602 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) |
| 1603 | override { |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1604 | if (is_activated_) { |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 1605 | (*common_params)->transition = transition_to_inject_; |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1606 | is_activated_ = false; |
| 1607 | } |
| 1608 | |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | void Activate() { is_activated_ = true; } |
| 1613 | |
| 1614 | private: |
| 1615 | ui::PageTransition transition_to_inject_; |
| 1616 | bool is_activated_ = false; |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1617 | }; |
| 1618 | |
| 1619 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, NonWebbyTransition) { |
| 1620 | const ui::PageTransition test_cases[] = { |
| 1621 | ui::PAGE_TRANSITION_TYPED, |
| 1622 | ui::PAGE_TRANSITION_AUTO_BOOKMARK, |
| 1623 | ui::PAGE_TRANSITION_GENERATED, |
| 1624 | ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 1625 | ui::PAGE_TRANSITION_RELOAD, |
| 1626 | ui::PAGE_TRANSITION_KEYWORD, |
| 1627 | ui::PAGE_TRANSITION_KEYWORD_GENERATED}; |
| 1628 | |
| 1629 | for (ui::PageTransition transition : test_cases) { |
| 1630 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done |
| 1631 | // before the test creates the RenderFrameHostImpl that is the target of the |
| 1632 | // IPC. |
| 1633 | WebContents* web_contents = shell()->web_contents(); |
| 1634 | BeginNavigationTransitionReplacer injector(web_contents, transition); |
| 1635 | |
| 1636 | // Navigate to a test page. |
| 1637 | GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html")); |
| 1638 | EXPECT_TRUE(NavigateToURL(web_contents, main_url)); |
| 1639 | |
| 1640 | // Start monitoring for renderer kills. |
| 1641 | RenderProcessHost* main_process = |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1642 | web_contents->GetPrimaryMainFrame()->GetProcess(); |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1643 | RenderProcessHostBadIpcMessageWaiter kill_waiter(main_process); |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1644 | |
| 1645 | // Have the main frame submit a BeginNavigation IPC with a missing |
| 1646 | // initiator. |
| 1647 | injector.Activate(); |
Nate Chapin | e6fd9fb | 2019-04-22 18:06:02 | [diff] [blame] | 1648 | // Don't expect a response for the script, as the process may be killed |
| 1649 | // before the script sends its completion message. |
| 1650 | ExecuteScriptAsync(web_contents, "window.location = '/title2.html';"); |
Mike West | 9286cf1 | 2019-04-01 13:44:58 | [diff] [blame] | 1651 | |
| 1652 | // Verify that the renderer was terminated. |
| 1653 | EXPECT_EQ(bad_message::RFHI_BEGIN_NAVIGATION_NON_WEBBY_TRANSITION, |
| 1654 | kill_waiter.Wait()); |
| 1655 | } |
| 1656 | } |
| 1657 | |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1658 | class SecurityExploitViaDisabledWebSecurityTest |
| 1659 | : public SecurityExploitBrowserTest { |
| 1660 | public: |
| 1661 | SecurityExploitViaDisabledWebSecurityTest() { |
| 1662 | // To get around BlockedSchemeNavigationThrottle. Other attempts at getting |
| 1663 | // around it don't work, i.e.: |
| 1664 | // -if the request is made in a child frame then the frame is torn down |
| 1665 | // immediately on process killing so the navigation doesn't complete |
| 1666 | // -if it's classified as same document, then a DCHECK in |
| 1667 | // NavigationRequest::CreateRendererInitiated fires |
| 1668 | feature_list_.InitAndEnableFeature( |
| 1669 | features::kAllowContentInitiatedDataUrlNavigations); |
| 1670 | } |
| 1671 | |
| 1672 | protected: |
| 1673 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 1674 | // Simulate a compromised renderer, otherwise the cross-origin request to |
| 1675 | // file: is blocked. |
| 1676 | command_line->AppendSwitch(switches::kDisableWebSecurity); |
| 1677 | SecurityExploitBrowserTest::SetUpCommandLine(command_line); |
| 1678 | } |
| 1679 | |
| 1680 | private: |
| 1681 | base::test::ScopedFeatureList feature_list_; |
| 1682 | }; |
| 1683 | |
| 1684 | // Test to verify that an exploited renderer process trying to specify a |
| 1685 | // non-empty URL for base_url_for_data_url on navigation is correctly |
| 1686 | // terminated. |
| 1687 | IN_PROC_BROWSER_TEST_F(SecurityExploitViaDisabledWebSecurityTest, |
| 1688 | ValidateBaseUrlForDataUrl) { |
| 1689 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 1690 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 1691 | |
| 1692 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1693 | shell()->web_contents()->GetPrimaryMainFrame()); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1694 | |
| 1695 | GURL data_url("data:text/html,foo"); |
| 1696 | base::FilePath file_path = GetTestFilePath("", "simple_page.html"); |
| 1697 | GURL file_url = net::FilePathToFileURL(file_path); |
| 1698 | |
| 1699 | // Setup a BeginNavigate IPC with non-empty base_url_for_data_url. |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1700 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 1701 | blink::mojom::CommonNavigationParams::New( |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1702 | data_url, url::Origin::Create(data_url), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1703 | /* initiator_base_url= */ std::nullopt, blink::mojom::Referrer::New(), |
| 1704 | ui::PAGE_TRANSITION_LINK, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1705 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT, |
Yeunjoo Choi | 3df791a | 2021-02-17 07:07:25 | [diff] [blame] | 1706 | blink::NavigationDownloadPolicy(), |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1707 | false /* should_replace_current_entry */, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 1708 | file_url /* base_url_for_data_url */, |
| 1709 | base::TimeTicks::Now() /* actual_navigation_start */, |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1710 | base::TimeTicks::Now() /* navigation_start */, "GET", |
arthursonzogni | af7c62c5 | 2020-02-12 10:49:41 | [diff] [blame] | 1711 | nullptr /* post_data */, network::mojom::SourceLocation::New(), |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1712 | false /* started_from_context_menu */, false /* has_user_gesture */, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 1713 | false /* text_fragment_token */, |
| 1714 | network::mojom::CSPDisposition::CHECK, |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1715 | std::vector<int>() /* initiator_origin_trial_features */, |
| 1716 | std::string() /* href_translate */, |
| 1717 | false /* is_history_navigation_in_new_child_frame */, |
Nan Lin | d91c815 | 2021-10-21 16:22:37 | [diff] [blame] | 1718 | base::TimeTicks() /* input_start */, |
Chris Fredrickson | b52bcd0a | 2023-03-28 14:48:05 | [diff] [blame] | 1719 | network::mojom::RequestDestination::kDocument); |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1720 | blink::mojom::BeginNavigationParamsPtr begin_params = |
| 1721 | blink::mojom::BeginNavigationParams::New( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1722 | std::nullopt /* initiator_frame_token */, std::string() /* headers */, |
| 1723 | net::LOAD_NORMAL, false /* skip_service_worker */, |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1724 | blink::mojom::RequestContextType::LOCATION, |
Henrique Ferreiro | 621b3ad | 2021-02-15 12:51:41 | [diff] [blame] | 1725 | blink::mojom::MixedContentContextType::kBlockable, |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1726 | false /* is_form_submission */, |
| 1727 | false /* was_initiated_by_link_click */, |
Nate Chapin | df6356e | 2023-02-01 01:11:42 | [diff] [blame] | 1728 | blink::mojom::ForceHistoryPush::kNo, GURL() /* searchable_form_url */, |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1729 | std::string() /* searchable_form_encoding */, |
| 1730 | GURL() /* client_side_redirect_url */, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1731 | std::nullopt /* devtools_initiator_info */, |
| 1732 | nullptr /* trust_token_params */, std::nullopt /* impression */, |
Katie Dillon | eb6c6981 | 2020-07-10 23:29:20 | [diff] [blame] | 1733 | base::TimeTicks() /* renderer_before_unload_start */, |
Kunihiko Sakamoto | 7d9429d7 | 2021-02-01 07:07:41 | [diff] [blame] | 1734 | base::TimeTicks() /* renderer_before_unload_end */, |
Yao Xiao | 720ef9d6 | 2022-12-09 05:18:29 | [diff] [blame] | 1735 | blink::mojom::NavigationInitiatorActivationAndAdStatus:: |
Sergey Poromov | dd557c1 | 2023-03-01 11:28:45 | [diff] [blame] | 1736 | kDidNotStartWithTransientActivation, |
Chris Fredrickson | 9ffdf5b | 2024-07-09 20:05:09 | [diff] [blame] | 1737 | false /* is_container_initiated */, |
| 1738 | net::StorageAccessApiStatus::kNone, false /* has_rel_opener */); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1739 | |
| 1740 | // Receiving the invalid IPC message should lead to renderer process |
| 1741 | // termination. |
Lukasz Anforowicz | 1034c088 | 2020-03-21 01:24:53 | [diff] [blame] | 1742 | RenderProcessHostBadIpcMessageWaiter process_kill_waiter(rfh->GetProcess()); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1743 | |
| 1744 | mojo::PendingAssociatedRemote<mojom::NavigationClient> navigation_client; |
Arthur Hemery | d6297f2 | 2019-10-22 14:34:41 | [diff] [blame] | 1745 | auto navigation_client_receiver = |
| 1746 | navigation_client.InitWithNewEndpointAndPassReceiver(); |
| 1747 | rfh->frame_host_receiver_for_testing().impl()->BeginNavigation( |
| 1748 | std::move(common_params), std::move(begin_params), mojo::NullRemote(), |
Rakina Zata Amni | af55b5b6 | 2022-07-19 23:11:03 | [diff] [blame] | 1749 | std::move(navigation_client), mojo::NullRemote(), mojo::NullReceiver()); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1750 | EXPECT_EQ(bad_message::RFH_BASE_URL_FOR_DATA_URL_SPECIFIED, |
| 1751 | process_kill_waiter.Wait()); |
| 1752 | |
| 1753 | EXPECT_FALSE(ChildProcessSecurityPolicyImpl::GetInstance()->CanReadFile( |
Emily Andrews | d15fd76 | 2024-12-10 20:41:54 | [diff] [blame] | 1754 | rfh->GetProcess()->GetDeprecatedID(), file_path)); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1755 | |
| 1756 | // Reload the page to create another renderer process. |
| 1757 | TestNavigationObserver tab_observer(shell()->web_contents(), 1); |
| 1758 | shell()->web_contents()->GetController().Reload(ReloadType::NORMAL, false); |
| 1759 | tab_observer.Wait(); |
| 1760 | |
| 1761 | // Make an XHR request to check if the page has access. |
| 1762 | std::string script = base::StringPrintf( |
| 1763 | "var xhr = new XMLHttpRequest()\n" |
| 1764 | "xhr.open('GET', '%s', false);\n" |
| 1765 | "try { xhr.send(); } catch (e) {}\n" |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1766 | "xhr.responseText;", |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1767 | file_url.spec().c_str()); |
Avi Drissman | c91bd8e | 2021-04-19 23:58:44 | [diff] [blame] | 1768 | std::string result = EvalJs(shell()->web_contents(), script).ExtractString(); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1769 | EXPECT_TRUE(result.empty()); |
| 1770 | } |
| 1771 | |
W. James MacLean | ead730a9 | 2024-06-21 18:34:20 | [diff] [blame] | 1772 | // Test to verify that an exploited renderer process trying to specify a |
| 1773 | // empty URL for initiator_base_url on navigation is correctly terminated. |
| 1774 | IN_PROC_BROWSER_TEST_F(SecurityExploitViaDisabledWebSecurityTest, |
| 1775 | ValidateInitiatorBaseUrlNotEmpty) { |
| 1776 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 1777 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 1778 | |
| 1779 | RenderFrameHostImpl* rfh = static_cast<RenderFrameHostImpl*>( |
| 1780 | shell()->web_contents()->GetPrimaryMainFrame()); |
| 1781 | |
| 1782 | GURL url("about:blank"); |
| 1783 | |
| 1784 | // Setup a BeginNavigate IPC with empty, but not nullopt, initiator_base_url. |
| 1785 | blink::mojom::CommonNavigationParamsPtr common_params = |
| 1786 | blink::mojom::CommonNavigationParams::New( |
| 1787 | url, url::Origin::Create(start_url), |
| 1788 | /* initiator_base_url= */ GURL(), blink::mojom::Referrer::New(), |
| 1789 | ui::PAGE_TRANSITION_LINK, |
| 1790 | blink::mojom::NavigationType::DIFFERENT_DOCUMENT, |
| 1791 | blink::NavigationDownloadPolicy(), |
| 1792 | false /* should_replace_current_entry */, |
Charlie Reis | e1d9b818 | 2025-04-02 04:32:12 | [diff] [blame] | 1793 | GURL() /* base_url_for_data_url */, |
| 1794 | base::TimeTicks::Now() /* actual_navigation_start */, |
W. James MacLean | ead730a9 | 2024-06-21 18:34:20 | [diff] [blame] | 1795 | base::TimeTicks::Now() /* navigation_start */, "GET", |
| 1796 | nullptr /* post_data */, network::mojom::SourceLocation::New(), |
| 1797 | false /* started_from_context_menu */, false /* has_user_gesture */, |
| 1798 | false /* text_fragment_token */, |
| 1799 | network::mojom::CSPDisposition::CHECK, |
| 1800 | std::vector<int>() /* initiator_origin_trial_features */, |
| 1801 | std::string() /* href_translate */, |
| 1802 | false /* is_history_navigation_in_new_child_frame */, |
| 1803 | base::TimeTicks() /* input_start */, |
| 1804 | network::mojom::RequestDestination::kDocument); |
| 1805 | blink::mojom::BeginNavigationParamsPtr begin_params = |
| 1806 | blink::mojom::BeginNavigationParams::New( |
| 1807 | std::nullopt /* initiator_frame_token */, std::string() /* headers */, |
| 1808 | net::LOAD_NORMAL, false /* skip_service_worker */, |
| 1809 | blink::mojom::RequestContextType::LOCATION, |
| 1810 | blink::mojom::MixedContentContextType::kBlockable, |
| 1811 | false /* is_form_submission */, |
| 1812 | false /* was_initiated_by_link_click */, |
| 1813 | blink::mojom::ForceHistoryPush::kNo, GURL() /* searchable_form_url */, |
| 1814 | std::string() /* searchable_form_encoding */, |
| 1815 | GURL() /* client_side_redirect_url */, |
| 1816 | std::nullopt /* devtools_initiator_info */, |
| 1817 | nullptr /* trust_token_params */, std::nullopt /* impression */, |
| 1818 | base::TimeTicks() /* renderer_before_unload_start */, |
| 1819 | base::TimeTicks() /* renderer_before_unload_end */, |
| 1820 | blink::mojom::NavigationInitiatorActivationAndAdStatus:: |
| 1821 | kDidNotStartWithTransientActivation, |
Chris Fredrickson | 9ffdf5b | 2024-07-09 20:05:09 | [diff] [blame] | 1822 | false /* is_container_initiated */, |
| 1823 | net::StorageAccessApiStatus::kNone, false /* has_rel_opener */); |
W. James MacLean | ead730a9 | 2024-06-21 18:34:20 | [diff] [blame] | 1824 | |
| 1825 | // Receiving the invalid IPC message should lead to renderer process |
| 1826 | // termination. |
| 1827 | RenderProcessHostBadIpcMessageWaiter process_kill_waiter(rfh->GetProcess()); |
| 1828 | |
| 1829 | mojo::PendingAssociatedRemote<mojom::NavigationClient> navigation_client; |
| 1830 | auto navigation_client_receiver = |
| 1831 | navigation_client.InitWithNewEndpointAndPassReceiver(); |
| 1832 | rfh->frame_host_receiver_for_testing().impl()->BeginNavigation( |
| 1833 | std::move(common_params), std::move(begin_params), mojo::NullRemote(), |
| 1834 | std::move(navigation_client), mojo::NullRemote(), mojo::NullReceiver()); |
| 1835 | EXPECT_EQ(bad_message::RFH_INITIATOR_BASE_URL_IS_EMPTY, |
| 1836 | process_kill_waiter.Wait()); |
| 1837 | } |
| 1838 | |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1839 | // Tests what happens when a web renderer asks to begin navigating to a file |
| 1840 | // url. |
| 1841 | IN_PROC_BROWSER_TEST_F(SecurityExploitViaDisabledWebSecurityTest, |
| 1842 | WebToFileNavigation) { |
| 1843 | // Navigate to a web page. |
| 1844 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 1845 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 1846 | |
| 1847 | // Have the webpage attempt to open a window with a file URL. |
| 1848 | // |
| 1849 | // Note that such attempt would normally be blocked in the renderer ("Not |
| 1850 | // allowed to load local resource: file:///..."), but the test here simulates |
| 1851 | // a compromised renderer by using --disable-web-security cmdline flag. |
| 1852 | GURL file_url = GetTestUrl("", "simple_page.html"); |
| 1853 | WebContentsAddedObserver new_window_observer; |
| 1854 | TestNavigationObserver nav_observer(nullptr); |
| 1855 | nav_observer.StartWatchingNewWebContents(); |
| 1856 | ASSERT_TRUE(ExecJs(shell()->web_contents(), |
| 1857 | JsReplace("window.open($1, '_blank')", file_url))); |
| 1858 | WebContents* new_window = new_window_observer.GetWebContents(); |
| 1859 | nav_observer.WaitForNavigationFinished(); |
| 1860 | |
| 1861 | // Verify that the navigation got blocked. |
| 1862 | EXPECT_TRUE(nav_observer.last_navigation_succeeded()); |
| 1863 | EXPECT_EQ(GURL(kBlockedURL), nav_observer.last_navigation_url()); |
| 1864 | EXPECT_EQ(GURL(kBlockedURL), |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1865 | new_window->GetPrimaryMainFrame()->GetLastCommittedURL()); |
| 1866 | EXPECT_EQ( |
| 1867 | shell()->web_contents()->GetPrimaryMainFrame()->GetLastCommittedOrigin(), |
| 1868 | new_window->GetPrimaryMainFrame()->GetLastCommittedOrigin()); |
| 1869 | EXPECT_EQ(shell()->web_contents()->GetPrimaryMainFrame()->GetProcess(), |
| 1870 | new_window->GetPrimaryMainFrame()->GetProcess()); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1871 | |
| 1872 | // Even though the navigation is blocked, we expect the opener relationship to |
| 1873 | // be established between the 2 windows. |
| 1874 | EXPECT_EQ(true, ExecJs(new_window, "!!window.opener")); |
| 1875 | } |
| 1876 | |
| 1877 | // Tests what happens when a web renderer asks to begin navigating to a |
| 1878 | // view-source url. |
| 1879 | IN_PROC_BROWSER_TEST_F(SecurityExploitViaDisabledWebSecurityTest, |
| 1880 | WebToViewSourceNavigation) { |
| 1881 | // Navigate to a web page. |
| 1882 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 1883 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 1884 | |
| 1885 | // Have the webpage attempt to open a window with a view-source URL. |
| 1886 | // |
| 1887 | // Note that such attempt would normally be blocked in the renderer ("Not |
| 1888 | // allowed to load local resource: view-source:///..."), but the test here |
| 1889 | // simulates a compromised renderer by using --disable-web-security flag. |
| 1890 | base::FilePath file_path = GetTestFilePath("", "simple_page.html"); |
| 1891 | GURL view_source_url = |
| 1892 | GURL(std::string(kViewSourceScheme) + ":" + start_url.spec()); |
| 1893 | WebContentsAddedObserver new_window_observer; |
| 1894 | TestNavigationObserver nav_observer(nullptr); |
| 1895 | nav_observer.StartWatchingNewWebContents(); |
| 1896 | ASSERT_TRUE(ExecJs(shell()->web_contents(), |
| 1897 | JsReplace("window.open($1, '_blank')", view_source_url))); |
| 1898 | WebContents* new_window = new_window_observer.GetWebContents(); |
| 1899 | nav_observer.WaitForNavigationFinished(); |
| 1900 | |
| 1901 | // Verify that the navigation got blocked. |
| 1902 | EXPECT_TRUE(nav_observer.last_navigation_succeeded()); |
| 1903 | EXPECT_EQ(GURL(kBlockedURL), nav_observer.last_navigation_url()); |
| 1904 | EXPECT_EQ(GURL(kBlockedURL), |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1905 | new_window->GetPrimaryMainFrame()->GetLastCommittedURL()); |
| 1906 | EXPECT_EQ( |
| 1907 | shell()->web_contents()->GetPrimaryMainFrame()->GetLastCommittedOrigin(), |
| 1908 | new_window->GetPrimaryMainFrame()->GetLastCommittedOrigin()); |
| 1909 | EXPECT_EQ(shell()->web_contents()->GetPrimaryMainFrame()->GetProcess(), |
| 1910 | new_window->GetPrimaryMainFrame()->GetProcess()); |
Lukasz Anforowicz | 9666a16 | 2019-10-12 00:54:20 | [diff] [blame] | 1911 | |
| 1912 | // Even though the navigation is blocked, we expect the opener relationship to |
| 1913 | // be established between the 2 windows. |
| 1914 | EXPECT_EQ(true, ExecJs(new_window, "!!window.opener")); |
| 1915 | } |
| 1916 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1917 | class BeginNavigationTrustTokenParamsReplacer : public FrameHostInterceptor { |
| 1918 | public: |
| 1919 | BeginNavigationTrustTokenParamsReplacer( |
| 1920 | WebContents* web_contents, |
| 1921 | network::mojom::TrustTokenParamsPtr params_to_inject) |
| 1922 | : FrameHostInterceptor(web_contents), |
| 1923 | params_to_inject_(std::move(params_to_inject)) {} |
| 1924 | |
| 1925 | BeginNavigationTrustTokenParamsReplacer( |
| 1926 | const BeginNavigationTrustTokenParamsReplacer&) = delete; |
| 1927 | BeginNavigationTrustTokenParamsReplacer& operator=( |
| 1928 | const BeginNavigationTrustTokenParamsReplacer&) = delete; |
| 1929 | |
| 1930 | bool WillDispatchBeginNavigation( |
| 1931 | RenderFrameHost* render_frame_host, |
Minggang Wang | b9f3fa9 | 2021-07-01 15:30:31 | [diff] [blame] | 1932 | blink::mojom::CommonNavigationParamsPtr* common_params, |
| 1933 | blink::mojom::BeginNavigationParamsPtr* begin_params, |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1934 | mojo::PendingRemote<blink::mojom::BlobURLToken>* blob_url_token, |
Antonio Sartori | 636adba | 2021-03-09 12:15:27 | [diff] [blame] | 1935 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) |
| 1936 | override { |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1937 | if (is_activated_) { |
| 1938 | (*begin_params)->trust_token_params = params_to_inject_.Clone(); |
| 1939 | is_activated_ = false; |
| 1940 | } |
| 1941 | |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
| 1945 | void Activate() { is_activated_ = true; } |
| 1946 | |
| 1947 | private: |
| 1948 | network::mojom::TrustTokenParamsPtr params_to_inject_; |
| 1949 | bool is_activated_ = false; |
| 1950 | }; |
| 1951 | |
| 1952 | class SecurityExploitBrowserTestWithTrustTokensEnabled |
| 1953 | : public SecurityExploitBrowserTest { |
| 1954 | public: |
Muyao Xu | 943e6a6 | 2024-12-17 02:51:32 | [diff] [blame] | 1955 | SecurityExploitBrowserTestWithTrustTokensEnabled() = default; |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1956 | }; |
| 1957 | |
| 1958 | // Test that the browser correctly reports a bad message when a child frame |
Sergey Kataev | 88cea7bb | 2023-01-06 01:52:53 | [diff] [blame] | 1959 | // attempts to navigate with a Private State Tokens redemption operation |
| 1960 | // associated with the navigation, but its parent lacks the |
Aykut Bulut | 1af9bfa1 | 2023-11-10 21:40:30 | [diff] [blame] | 1961 | // private-state-token-redemption Permissions Policy feature. |
Charlie Hu | bb5943d | 2021-03-09 19:46:12 | [diff] [blame] | 1962 | IN_PROC_BROWSER_TEST_F( |
| 1963 | SecurityExploitBrowserTestWithTrustTokensEnabled, |
| 1964 | BrowserForbidsTrustTokenRedemptionWithoutPermissionsPolicy) { |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1965 | WebContents* web_contents = shell()->web_contents(); |
| 1966 | |
| 1967 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 1968 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 1969 | auto params = network::mojom::TrustTokenParams::New(); |
Aykut Bulut | 832c80f | 2023-01-19 18:37:35 | [diff] [blame] | 1970 | params->operation = network::mojom::TrustTokenOperationType::kRedemption; |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1971 | BeginNavigationTrustTokenParamsReplacer replacer(web_contents, |
| 1972 | std::move(params)); |
| 1973 | |
| 1974 | GURL start_url(embedded_test_server()->GetURL( |
Charlie Hu | 563114f | 2021-03-11 18:56:36 | [diff] [blame] | 1975 | "/page-with-trust-token-permissions-policy-disabled.html")); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1976 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 1977 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 1978 | RenderFrameHost* parent = web_contents->GetPrimaryMainFrame(); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1979 | ASSERT_FALSE(parent->IsFeatureEnabled( |
Sandor «Alex» Major | e9545a7 | 2025-01-31 20:40:46 | [diff] [blame] | 1980 | network::mojom::PermissionsPolicyFeature::kTrustTokenRedemption)); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1981 | |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 1982 | replacer.Activate(); |
| 1983 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1984 | RenderFrameHost* child = static_cast<WebContentsImpl*>(web_contents) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 1985 | ->GetPrimaryFrameTree() |
| 1986 | .root() |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1987 | ->child_at(0) |
| 1988 | ->current_frame_host(); |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 1989 | ExecuteScriptAsync(child, JsReplace("location = $1", "/title2.html")); |
| 1990 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1991 | RenderProcessHostBadMojoMessageWaiter kill_waiter(child->GetProcess()); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1992 | EXPECT_THAT(kill_waiter.Wait(), |
Charlie Hu | 563114f | 2021-03-11 18:56:36 | [diff] [blame] | 1993 | Optional(HasSubstr("Permissions Policy feature is absent"))); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | // Test that the browser correctly reports a bad message when a child frame |
Sergey Kataev | 88cea7bb | 2023-01-06 01:52:53 | [diff] [blame] | 1997 | // attempts to navigate with a Private State Tokens signing operation associated |
Aykut Bulut | 1af9bfa1 | 2023-11-10 21:40:30 | [diff] [blame] | 1998 | // with the navigation, but its parent lacks the private-state-token-redemption |
| 1999 | // (sic) Permissions Policy feature. |
Charlie Hu | bb5943d | 2021-03-09 19:46:12 | [diff] [blame] | 2000 | IN_PROC_BROWSER_TEST_F( |
| 2001 | SecurityExploitBrowserTestWithTrustTokensEnabled, |
| 2002 | BrowserForbidsTrustTokenSigningWithoutPermissionsPolicy) { |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2003 | WebContents* web_contents = shell()->web_contents(); |
| 2004 | |
| 2005 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 2006 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 2007 | auto params = network::mojom::TrustTokenParams::New(); |
Aykut Bulut | 832c80f | 2023-01-19 18:37:35 | [diff] [blame] | 2008 | params->operation = network::mojom::TrustTokenOperationType::kSigning; |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2009 | BeginNavigationTrustTokenParamsReplacer replacer(web_contents, |
| 2010 | std::move(params)); |
| 2011 | |
| 2012 | GURL start_url(embedded_test_server()->GetURL( |
Charlie Hu | 563114f | 2021-03-11 18:56:36 | [diff] [blame] | 2013 | "/page-with-trust-token-permissions-policy-disabled.html")); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2014 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 2015 | |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2016 | RenderFrameHost* parent = web_contents->GetPrimaryMainFrame(); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2017 | ASSERT_FALSE(parent->IsFeatureEnabled( |
Sandor «Alex» Major | e9545a7 | 2025-01-31 20:40:46 | [diff] [blame] | 2018 | network::mojom::PermissionsPolicyFeature::kTrustTokenRedemption)); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2019 | |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2020 | replacer.Activate(); |
| 2021 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2022 | RenderFrameHost* child = static_cast<WebContentsImpl*>(web_contents) |
Carlos Caballero | 15caeeb | 2021-10-27 09:57:55 | [diff] [blame] | 2023 | ->GetPrimaryFrameTree() |
| 2024 | .root() |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2025 | ->child_at(0) |
| 2026 | ->current_frame_host(); |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2027 | ExecuteScriptAsync(child, JsReplace("location = $1", "/title2.html")); |
| 2028 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2029 | RenderProcessHostBadMojoMessageWaiter kill_waiter(child->GetProcess()); |
Aykut Bulut | 871d3d2 | 2023-04-25 21:33:12 | [diff] [blame] | 2030 | EXPECT_THAT(kill_waiter.Wait(), |
| 2031 | Optional(HasSubstr("Permissions Policy feature is absent"))); |
| 2032 | } |
| 2033 | |
| 2034 | // Test that the browser correctly reports a bad message when a child frame |
| 2035 | // attempts to navigate with a Private State Tokens issue operation |
| 2036 | // associated with the navigation, but its parent lacks the |
| 2037 | // private-state-token-issuance Permissions Policy feature. |
| 2038 | IN_PROC_BROWSER_TEST_F( |
| 2039 | SecurityExploitBrowserTestWithTrustTokensEnabled, |
| 2040 | BrowserForbidsTrustTokenIssuanceWithoutPermissionsPolicy) { |
| 2041 | WebContents* web_contents = shell()->web_contents(); |
| 2042 | |
| 2043 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 2044 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 2045 | auto params = network::mojom::TrustTokenParams::New(); |
Aykut Bulut | 871d3d2 | 2023-04-25 21:33:12 | [diff] [blame] | 2046 | params->operation = network::mojom::TrustTokenOperationType::kIssuance; |
| 2047 | BeginNavigationTrustTokenParamsReplacer replacer(web_contents, |
| 2048 | std::move(params)); |
| 2049 | |
| 2050 | GURL start_url(embedded_test_server()->GetURL( |
| 2051 | "/page-with-trust-token-permissions-policy-disabled.html")); |
| 2052 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 2053 | |
| 2054 | RenderFrameHost* parent = web_contents->GetPrimaryMainFrame(); |
| 2055 | ASSERT_FALSE(parent->IsFeatureEnabled( |
Sandor «Alex» Major | e9545a7 | 2025-01-31 20:40:46 | [diff] [blame] | 2056 | network::mojom::PermissionsPolicyFeature::kPrivateStateTokenIssuance)); |
Aykut Bulut | 871d3d2 | 2023-04-25 21:33:12 | [diff] [blame] | 2057 | |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2058 | replacer.Activate(); |
| 2059 | |
Aykut Bulut | 871d3d2 | 2023-04-25 21:33:12 | [diff] [blame] | 2060 | RenderFrameHost* child = static_cast<WebContentsImpl*>(web_contents) |
| 2061 | ->GetPrimaryFrameTree() |
| 2062 | .root() |
| 2063 | ->child_at(0) |
| 2064 | ->current_frame_host(); |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2065 | ExecuteScriptAsync(child, JsReplace("location = $1", "/title2.html")); |
| 2066 | |
Aykut Bulut | 871d3d2 | 2023-04-25 21:33:12 | [diff] [blame] | 2067 | RenderProcessHostBadMojoMessageWaiter kill_waiter(child->GetProcess()); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2068 | EXPECT_THAT(kill_waiter.Wait(), |
Charlie Hu | 563114f | 2021-03-11 18:56:36 | [diff] [blame] | 2069 | Optional(HasSubstr("Permissions Policy feature is absent"))); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTestWithTrustTokensEnabled, |
| 2073 | BrowserForbidsTrustTokenParamsOnMainFrameNav) { |
| 2074 | WebContents* web_contents = shell()->web_contents(); |
| 2075 | |
| 2076 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 2077 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 2078 | BeginNavigationTrustTokenParamsReplacer replacer( |
| 2079 | web_contents, network::mojom::TrustTokenParams::New()); |
| 2080 | |
| 2081 | GURL start_url(embedded_test_server()->GetURL("/title1.html")); |
| 2082 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 2083 | |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2084 | replacer.Activate(); |
| 2085 | |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2086 | RenderFrameHost* compromised_renderer = web_contents->GetPrimaryMainFrame(); |
| 2087 | ExecuteScriptAsync(compromised_renderer, |
| 2088 | JsReplace("location = $1", "/title2.html")); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2089 | |
Daniel Cheng | 6a53935 | 2023-05-09 03:25:20 | [diff] [blame] | 2090 | RenderProcessHostBadMojoMessageWaiter kill_waiter( |
| 2091 | compromised_renderer->GetProcess()); |
Sergey Kataev | 88cea7bb | 2023-01-06 01:52:53 | [diff] [blame] | 2092 | EXPECT_THAT( |
| 2093 | kill_waiter.Wait(), |
| 2094 | Optional(HasSubstr("Private State Token params in main frame nav"))); |
David Van Cleve | ca4ef906 | 2020-04-27 23:46:53 | [diff] [blame] | 2095 | } |
| 2096 | |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2097 | class FencedFrameSecurityExploitBrowserTestWithTrustTokensEnabled |
| 2098 | : public SecurityExploitBrowserTestWithTrustTokensEnabled { |
| 2099 | protected: |
| 2100 | FencedFrameSecurityExploitBrowserTestWithTrustTokensEnabled() = default; |
| 2101 | |
| 2102 | WebContentsImpl* web_contents() { |
| 2103 | return static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 2104 | } |
| 2105 | |
| 2106 | RenderFrameHostImpl* primary_main_frame_host() { |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2107 | return web_contents()->GetPrimaryMainFrame(); |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | test::FencedFrameTestHelper& fenced_frame_test_helper() { |
| 2111 | return fenced_frame_test_helper_; |
| 2112 | } |
| 2113 | |
| 2114 | private: |
| 2115 | test::FencedFrameTestHelper fenced_frame_test_helper_; |
| 2116 | }; |
| 2117 | |
| 2118 | class FencedFrameBeginNavigationTrustTokenParamsReplacer |
| 2119 | : public BeginNavigationTrustTokenParamsReplacer { |
| 2120 | public: |
| 2121 | FencedFrameBeginNavigationTrustTokenParamsReplacer( |
| 2122 | WebContents* web_contents, |
| 2123 | network::mojom::TrustTokenParamsPtr params_to_inject) |
| 2124 | : BeginNavigationTrustTokenParamsReplacer(web_contents, |
| 2125 | std::move(params_to_inject)) {} |
| 2126 | |
| 2127 | FencedFrameBeginNavigationTrustTokenParamsReplacer( |
| 2128 | const FencedFrameBeginNavigationTrustTokenParamsReplacer&) = delete; |
| 2129 | FencedFrameBeginNavigationTrustTokenParamsReplacer& operator=( |
| 2130 | const FencedFrameBeginNavigationTrustTokenParamsReplacer&) = delete; |
| 2131 | |
| 2132 | bool WillDispatchBeginNavigation( |
| 2133 | RenderFrameHost* render_frame_host, |
| 2134 | blink::mojom::CommonNavigationParamsPtr* common_params, |
| 2135 | blink::mojom::BeginNavigationParamsPtr* begin_params, |
| 2136 | mojo::PendingRemote<blink::mojom::BlobURLToken>* blob_url_token, |
| 2137 | mojo::PendingAssociatedRemote<mojom::NavigationClient>* navigation_client) |
| 2138 | override { |
| 2139 | if (render_frame_host->IsFencedFrameRoot()) { |
| 2140 | BeginNavigationTrustTokenParamsReplacer::WillDispatchBeginNavigation( |
| 2141 | render_frame_host, common_params, begin_params, blob_url_token, |
| 2142 | navigation_client); |
| 2143 | } |
| 2144 | return true; |
| 2145 | } |
| 2146 | }; |
| 2147 | |
| 2148 | IN_PROC_BROWSER_TEST_F( |
| 2149 | FencedFrameSecurityExploitBrowserTestWithTrustTokensEnabled, |
| 2150 | BrowserForbidsTrustTokenParamsOnFencedFrameNav) { |
| 2151 | WebContents* web_contents = shell()->web_contents(); |
| 2152 | |
| 2153 | // Prepare to intercept BeginNavigation mojo IPC. This has to be done before |
| 2154 | // the test creates the RenderFrameHostImpl that is the target of the IPC. |
| 2155 | FencedFrameBeginNavigationTrustTokenParamsReplacer replacer( |
| 2156 | web_contents, network::mojom::TrustTokenParams::New()); |
| 2157 | GURL start_url(embedded_test_server()->GetURL("/empty.html")); |
| 2158 | EXPECT_TRUE(NavigateToURL(shell(), start_url)); |
| 2159 | |
| 2160 | RenderFrameHostImplWrapper primary_rfh(primary_main_frame_host()); |
| 2161 | RenderFrameHostImplWrapper inner_fenced_frame_rfh( |
| 2162 | fenced_frame_test_helper().CreateFencedFrame( |
Dominic Farolino | 667161a9 | 2021-11-20 19:29:02 | [diff] [blame] | 2163 | primary_rfh.get(), |
| 2164 | embedded_test_server()->GetURL("/fenced_frames/empty.html"))); |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2165 | |
| 2166 | RenderFrameHost* compromised_renderer = inner_fenced_frame_rfh.get(); |
| 2167 | RenderProcessHostBadMojoMessageWaiter kill_waiter( |
| 2168 | compromised_renderer->GetProcess()); |
| 2169 | replacer.Activate(); |
| 2170 | |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 2171 | std::ignore = ExecJs( |
Dominic Farolino | 667161a9 | 2021-11-20 19:29:02 | [diff] [blame] | 2172 | compromised_renderer, |
| 2173 | JsReplace("location.href=$1", |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 2174 | embedded_test_server()->GetURL("/fenced_frames/title1.html"))); |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2175 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2176 | std::optional<std::string> result = kill_waiter.Wait(); |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2177 | EXPECT_THAT(result, |
Sergey Kataev | 88cea7bb | 2023-01-06 01:52:53 | [diff] [blame] | 2178 | Optional(HasSubstr("Private State Token params in fenced frame " |
| 2179 | "nav"))); |
Liviu Tinta | eb63e145 | 2021-10-28 18:47:02 | [diff] [blame] | 2180 | } |
| 2181 | |
John Delaney | 943f31b | 2022-05-16 18:35:40 | [diff] [blame] | 2182 | class SecurityExploitTestFencedFramesDisabled |
| 2183 | : public SecurityExploitBrowserTest { |
| 2184 | public: |
| 2185 | SecurityExploitTestFencedFramesDisabled() { |
| 2186 | feature_list_.InitAndDisableFeature(blink::features::kFencedFrames); |
| 2187 | } |
| 2188 | |
| 2189 | private: |
| 2190 | base::test::ScopedFeatureList feature_list_; |
| 2191 | }; |
| 2192 | |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2193 | // Ensure that we kill the renderer process if we try to create a |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2194 | // fenced-frame when the blink::features::kFencedFrames feature is not enabled. |
John Delaney | 943f31b | 2022-05-16 18:35:40 | [diff] [blame] | 2195 | IN_PROC_BROWSER_TEST_F(SecurityExploitTestFencedFramesDisabled, |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2196 | CreateFencedFrameWhenFeatureDisabled) { |
| 2197 | GURL foo("http://foo.com/simple_page.html"); |
| 2198 | EXPECT_TRUE(NavigateToURL(shell(), foo)); |
| 2199 | EXPECT_EQ(u"OK", shell()->web_contents()->GetTitle()); |
Brendon Tiszka | 9b74096 | 2021-12-20 08:50:18 | [diff] [blame] | 2200 | EXPECT_FALSE(blink::features::IsFencedFramesEnabled()); |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2201 | |
| 2202 | RenderFrameHostImpl* compromised_rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2203 | shell()->web_contents()->GetPrimaryMainFrame()); |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2204 | |
| 2205 | mojo::PendingAssociatedRemote<blink::mojom::FencedFrameOwnerHost> remote; |
| 2206 | mojo::PendingAssociatedReceiver<blink::mojom::FencedFrameOwnerHost> receiver; |
| 2207 | receiver = remote.InitWithNewEndpointAndPassReceiver(); |
| 2208 | |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2209 | auto remote_frame_interfaces = |
Dave Tapuska | 68024c1 | 2022-07-26 16:09:01 | [diff] [blame] | 2210 | blink::mojom::RemoteFrameInterfacesFromRenderer::New(); |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2211 | remote_frame_interfaces->frame_host_receiver = |
| 2212 | mojo::AssociatedRemote<blink::mojom::RemoteFrameHost>() |
| 2213 | .BindNewEndpointAndPassDedicatedReceiver(); |
| 2214 | mojo::AssociatedRemote<blink::mojom::RemoteFrame> frame; |
| 2215 | std::ignore = frame.BindNewEndpointAndPassDedicatedReceiver(); |
| 2216 | remote_frame_interfaces->frame = frame.Unbind(); |
| 2217 | |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2218 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
| 2219 | compromised_rfh->GetProcess()); |
Dave Tapuska | 68024c1 | 2022-07-26 16:09:01 | [diff] [blame] | 2220 | static_cast<blink::mojom::LocalFrameHost*>(compromised_rfh) |
Tsuyoshi Horo | c733208b | 2022-02-18 17:32:48 | [diff] [blame] | 2221 | ->CreateFencedFrame( |
Garrett Tanzer | 291a2d5 | 2023-03-20 22:41:57 | [diff] [blame] | 2222 | std::move(receiver), std::move(remote_frame_interfaces), |
| 2223 | blink::RemoteFrameToken(), base::UnguessableToken::Create()); |
Brendon Tiszka | 2b3304f | 2021-12-10 19:51:49 | [diff] [blame] | 2224 | EXPECT_EQ(bad_message::RFH_FENCED_FRAME_MOJO_WHEN_DISABLED, |
| 2225 | kill_waiter.Wait()); |
| 2226 | } |
| 2227 | |
Garrett Tanzer | 6c807e3 | 2022-04-14 17:33:58 | [diff] [blame] | 2228 | // Ensure that we kill the renderer process if we try to do a top-level |
| 2229 | // navigation using the special _unfencedTop IPC path when we are not inside |
| 2230 | // a fenced frame. (Test from an iframe instead.) |
| 2231 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, |
| 2232 | UnfencedTopFromOutsideFencedFrame) { |
| 2233 | GURL main_url(embedded_test_server()->GetURL( |
| 2234 | "a.com", "/cross_site_iframe_factory.html?a(b)")); |
| 2235 | EXPECT_TRUE(NavigateToURL(shell(), main_url)); |
| 2236 | FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents()) |
| 2237 | ->GetPrimaryFrameTree() |
| 2238 | .root(); |
Garrett Tanzer | a42fdef | 2022-06-13 16:09:14 | [diff] [blame] | 2239 | RenderFrameHostImpl* compromised_rfh = |
| 2240 | root->child_at(0)->current_frame_host(); |
Garrett Tanzer | 6c807e3 | 2022-04-14 17:33:58 | [diff] [blame] | 2241 | |
| 2242 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
Garrett Tanzer | a42fdef | 2022-06-13 16:09:14 | [diff] [blame] | 2243 | compromised_rfh->GetProcess()); |
Garrett Tanzer | 6c807e3 | 2022-04-14 17:33:58 | [diff] [blame] | 2244 | |
| 2245 | GURL url("http://foo.com/simple_page.html"); |
Garrett Tanzer | a42fdef | 2022-06-13 16:09:14 | [diff] [blame] | 2246 | auto params = CreateOpenURLParams(url); |
| 2247 | params->is_unfenced_top_navigation = true; |
| 2248 | static_cast<mojom::FrameHost*>(compromised_rfh)->OpenURL(std::move(params)); |
Garrett Tanzer | 6c807e3 | 2022-04-14 17:33:58 | [diff] [blame] | 2249 | |
| 2250 | EXPECT_EQ(bad_message::RFHI_UNFENCED_TOP_IPC_OUTSIDE_FENCED_FRAME, |
| 2251 | kill_waiter.Wait()); |
| 2252 | } |
| 2253 | |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2254 | class SecurityExploitBrowserTestFencedFrames |
| 2255 | : public SecurityExploitBrowserTest { |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2256 | public: |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2257 | void SetUpOnMainThread() override { |
| 2258 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 2259 | https_server()->StartAcceptingConnections(); |
| 2260 | } |
| 2261 | |
| 2262 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 2263 | https_server()->AddDefaultHandlers(GetTestDataFilePath()); |
| 2264 | https_server()->ServeFilesFromSourceDirectory(GetTestDataFilePath()); |
| 2265 | https_server()->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES); |
| 2266 | SetupCrossSiteRedirector(https_server()); |
| 2267 | |
| 2268 | // EmbeddedTestServer::InitializeAndListen() initializes its |base_url_| |
| 2269 | // which is required below. This cannot invoke Start() however as that kicks |
| 2270 | // off the "EmbeddedTestServer IO Thread" which then races with |
| 2271 | // initialization in ContentBrowserTest::SetUp(), http://crbug.com/674545. |
| 2272 | ASSERT_TRUE(https_server()->InitializeAndListen()); |
| 2273 | } |
| 2274 | |
| 2275 | test::FencedFrameTestHelper& fenced_frame_test_helper() { |
| 2276 | return fenced_frame_test_helper_; |
| 2277 | } |
| 2278 | |
| 2279 | net::EmbeddedTestServer* https_server() { return &https_server_; } |
| 2280 | |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2281 | private: |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2282 | test::FencedFrameTestHelper fenced_frame_test_helper_{}; |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2283 | net::EmbeddedTestServer https_server_{net::EmbeddedTestServer::TYPE_HTTPS}; |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2284 | }; |
| 2285 | |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2286 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTestFencedFrames, |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2287 | NavigateFencedFrameToInvalidURL) { |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2288 | GURL main_frame_url(https_server()->GetURL("a.test", "/simple_page.html")); |
Liam Brady | 9108c28d | 2022-05-06 21:14:32 | [diff] [blame] | 2289 | std::vector<GURL> invalid_urls = { |
| 2290 | GURL("http://example.com"), |
Dominic Farolino | fd856985 | 2022-08-24 22:26:25 | [diff] [blame] | 2291 | GURL("http://example.com?<\n=block"), |
Liam Brady | 9108c28d | 2022-05-06 21:14:32 | [diff] [blame] | 2292 | GURL("about:srcdoc"), |
| 2293 | GURL("data:text/html,<p>foo"), |
| 2294 | GURL("blob:https://example.com/a9400bf5-aaa8-4166-86e4-492c50f4ca2b"), |
| 2295 | GURL("file://folder"), |
| 2296 | GURL("javascript:console.log('foo');"), |
| 2297 | GetWebUIURL(kChromeUIHistogramHost), |
| 2298 | GURL(blink::kChromeUIHangURL)}; |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2299 | |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2300 | for (const GURL& invalid_url : invalid_urls) { |
| 2301 | EXPECT_FALSE(blink::IsValidFencedFrameURL(invalid_url)); |
| 2302 | EXPECT_TRUE(blink::features::IsFencedFramesEnabled()); |
| 2303 | EXPECT_TRUE(NavigateToURL(shell(), main_frame_url)); |
| 2304 | EXPECT_EQ(u"OK", shell()->web_contents()->GetTitle()); |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2305 | |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2306 | RenderFrameHostImpl* compromised_rfh = static_cast<RenderFrameHostImpl*>( |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 2307 | shell()->web_contents()->GetPrimaryMainFrame()); |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2308 | |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2309 | mojo::AssociatedRemote<blink::mojom::FencedFrameOwnerHost> remote; |
| 2310 | mojo::PendingAssociatedReceiver<blink::mojom::FencedFrameOwnerHost> |
| 2311 | pending_receiver = remote.BindNewEndpointAndPassReceiver(); |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2312 | |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2313 | auto remote_frame_interfaces = |
Dave Tapuska | 68024c1 | 2022-07-26 16:09:01 | [diff] [blame] | 2314 | blink::mojom::RemoteFrameInterfacesFromRenderer::New(); |
Dave Tapuska | 82b5401 | 2022-07-15 23:26:10 | [diff] [blame] | 2315 | remote_frame_interfaces->frame_host_receiver = |
| 2316 | mojo::AssociatedRemote<blink::mojom::RemoteFrameHost>() |
| 2317 | .BindNewEndpointAndPassDedicatedReceiver(); |
| 2318 | mojo::AssociatedRemote<blink::mojom::RemoteFrame> frame; |
| 2319 | std::ignore = frame.BindNewEndpointAndPassDedicatedReceiver(); |
| 2320 | remote_frame_interfaces->frame = frame.Unbind(); |
| 2321 | |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2322 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
| 2323 | compromised_rfh->GetProcess()); |
Dave Tapuska | 68024c1 | 2022-07-26 16:09:01 | [diff] [blame] | 2324 | static_cast<blink::mojom::LocalFrameHost*>(compromised_rfh) |
Garrett Tanzer | 291a2d5 | 2023-03-20 22:41:57 | [diff] [blame] | 2325 | ->CreateFencedFrame( |
| 2326 | std::move(pending_receiver), std::move(remote_frame_interfaces), |
| 2327 | blink::RemoteFrameToken(), base::UnguessableToken::Create()); |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2328 | EXPECT_EQ(compromised_rfh->GetFencedFrames().size(), 1u); |
| 2329 | |
| 2330 | FencedFrame* fenced_frame = compromised_rfh->GetFencedFrames()[0]; |
| 2331 | static_cast<blink::mojom::FencedFrameOwnerHost*>(fenced_frame) |
Camillia Smith Barnes | 6a64396 | 2023-03-03 00:28:58 | [diff] [blame] | 2332 | ->Navigate(invalid_url, base::TimeTicks(), |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 2333 | /*embedder_shared_storage_context=*/std::nullopt); |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 2334 | EXPECT_EQ(bad_message::FF_NAVIGATION_INVALID_URL, kill_waiter.Wait()); |
| 2335 | } |
Dominic Farolino | a185eb7 | 2022-04-15 02:34:29 | [diff] [blame] | 2336 | } |
| 2337 | |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2338 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTestFencedFrames, |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2339 | ChangeFencedFrameSandboxFlags) { |
| 2340 | GURL main_frame_url(https_server()->GetURL("a.test", "/simple_page.html")); |
| 2341 | |
| 2342 | EXPECT_TRUE(NavigateToURL(shell(), main_frame_url)); |
| 2343 | RenderFrameHostImpl* root_rfh = static_cast<RenderFrameHostImpl*>( |
| 2344 | shell()->web_contents()->GetPrimaryMainFrame()); |
| 2345 | |
| 2346 | const GURL fenced_frame_url = |
| 2347 | https_server()->GetURL("a.test", "/fenced_frames/sandbox_flags.html"); |
| 2348 | constexpr char kAddFencedFrameScript[] = R"({ |
| 2349 | const fenced_frame = document.createElement('fencedframe'); |
Liam Brady | 4a33cee | 2023-04-03 21:15:24 | [diff] [blame] | 2350 | fenced_frame.config = new FencedFrameConfig($1); |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2351 | document.body.appendChild(fenced_frame); |
| 2352 | })"; |
| 2353 | EXPECT_TRUE( |
| 2354 | ExecJs(root_rfh, JsReplace(kAddFencedFrameScript, fenced_frame_url))); |
| 2355 | |
| 2356 | RenderFrameHostImpl* fenced_rfh = nullptr; |
| 2357 | RenderFrameHostImpl* parent_rfh = nullptr; |
| 2358 | |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2359 | std::vector<FencedFrame*> fenced_frames = root_rfh->GetFencedFrames(); |
| 2360 | EXPECT_EQ(fenced_frames.size(), 1u); |
| 2361 | FencedFrame* new_fenced_frame = fenced_frames.back(); |
| 2362 | fenced_rfh = new_fenced_frame->GetInnerRoot(); |
| 2363 | parent_rfh = fenced_rfh; |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2364 | |
| 2365 | RenderProcessHostBadIpcMessageWaiter kill_waiter(fenced_rfh->GetProcess()); |
| 2366 | |
| 2367 | blink::FramePolicy first_policy = |
| 2368 | fenced_rfh->frame_tree_node()->pending_frame_policy(); |
| 2369 | |
| 2370 | first_policy.sandbox_flags = blink::kFencedFrameMandatoryUnsandboxedFlags; |
| 2371 | static_cast<blink::mojom::LocalFrameHost*>(parent_rfh) |
| 2372 | ->DidChangeFramePolicy(std::move(fenced_rfh->GetFrameToken()), |
| 2373 | std::move(first_policy)); |
| 2374 | |
Dominic Farolino | 0b06763 | 2022-11-11 02:57:49 | [diff] [blame] | 2375 | EXPECT_EQ(bad_message::RFH_SANDBOX_FLAGS, kill_waiter.Wait()); |
Liam Brady | b0f1f0e | 2022-08-19 21:42:11 | [diff] [blame] | 2376 | } |
| 2377 | |
Liam Brady | ce3383b | 2023-08-16 18:37:21 | [diff] [blame] | 2378 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTestFencedFrames, |
| 2379 | PullFocusAcrossFencedBoundary) { |
| 2380 | base::HistogramTester histogram_tester; |
| 2381 | GURL main_frame_url(https_server()->GetURL("a.test", "/simple_page.html")); |
| 2382 | |
| 2383 | EXPECT_TRUE(NavigateToURL(shell(), main_frame_url)); |
| 2384 | RenderFrameHostImpl* root_rfh = static_cast<RenderFrameHostImpl*>( |
| 2385 | shell()->web_contents()->GetPrimaryMainFrame()); |
| 2386 | |
| 2387 | const GURL fenced_frame_url = |
| 2388 | https_server()->GetURL("a.test", "/fenced_frames/button.html"); |
| 2389 | constexpr char kAddFencedFrameScript[] = R"({ |
| 2390 | const fenced_frame = document.createElement('fencedframe'); |
| 2391 | fenced_frame.config = new FencedFrameConfig($1); |
| 2392 | document.body.appendChild(fenced_frame); |
| 2393 | })"; |
| 2394 | EXPECT_TRUE( |
| 2395 | ExecJs(root_rfh, JsReplace(kAddFencedFrameScript, fenced_frame_url))); |
| 2396 | |
| 2397 | RenderFrameHostImpl* fenced_rfh = nullptr; |
| 2398 | |
| 2399 | std::vector<FencedFrame*> fenced_frames = root_rfh->GetFencedFrames(); |
| 2400 | EXPECT_EQ(fenced_frames.size(), 1u); |
| 2401 | FencedFrame* new_fenced_frame = fenced_frames.back(); |
| 2402 | fenced_rfh = new_fenced_frame->GetInnerRoot(); |
| 2403 | |
| 2404 | root_rfh->DidFocusFrame(); |
| 2405 | root_rfh->GetRenderWidgetHost()->ResetLostFocus(); |
| 2406 | |
| 2407 | // The fenced frame should not be allowed to focus because it won't have |
| 2408 | // user activation, and the RenderWidgetHost won't have recently lost focus. |
| 2409 | RenderProcessHostBadIpcMessageWaiter kill_waiter(fenced_rfh->GetProcess()); |
| 2410 | fenced_rfh->DidFocusFrame(); |
| 2411 | EXPECT_EQ(bad_message::RFH_FOCUS_ACROSS_FENCED_BOUNDARY, kill_waiter.Wait()); |
| 2412 | } |
Rakina Zata Amni | e3bcee24 | 2025-07-16 23:30:43 | [diff] [blame] | 2413 | |
| 2414 | namespace { |
| 2415 | |
| 2416 | // Interceptor that replaces the origin in the DidCommitProvisionalLoadParams |
| 2417 | // with the specified value for the first DidCommitProvisionalLoad message it |
| 2418 | // observes in the given |web_contents| while in scope. |
| 2419 | class DidCommitParamsOriginReplacer : public DidCommitNavigationInterceptor { |
| 2420 | public: |
| 2421 | DidCommitParamsOriginReplacer(WebContents* web_contents, |
| 2422 | url::Origin origin_override) |
| 2423 | : DidCommitNavigationInterceptor(web_contents), |
| 2424 | origin_override_(std::move(origin_override)) {} |
| 2425 | |
| 2426 | DidCommitParamsOriginReplacer(const DidCommitParamsOriginReplacer&) = delete; |
| 2427 | DidCommitParamsOriginReplacer& operator=( |
| 2428 | const DidCommitParamsOriginReplacer&) = delete; |
| 2429 | |
| 2430 | ~DidCommitParamsOriginReplacer() override = default; |
| 2431 | |
| 2432 | protected: |
| 2433 | bool WillProcessDidCommitNavigation( |
| 2434 | RenderFrameHost* render_frame_host, |
| 2435 | NavigationRequest* navigation_request, |
| 2436 | mojom::DidCommitProvisionalLoadParamsPtr* params, |
| 2437 | mojom::DidCommitProvisionalLoadInterfaceParamsPtr*) override { |
| 2438 | (*params)->origin = origin_override_; |
| 2439 | |
| 2440 | return true; |
| 2441 | } |
| 2442 | |
| 2443 | private: |
| 2444 | url::Origin origin_override_; |
| 2445 | }; |
| 2446 | |
| 2447 | } // namespace |
| 2448 | |
| 2449 | IN_PROC_BROWSER_TEST_F(SecurityExploitBrowserTest, MismatchedOriginToCommit) { |
| 2450 | // Navigate normally. |
| 2451 | GURL url(embedded_test_server()->GetURL("a.test", "/title1.html")); |
| 2452 | EXPECT_TRUE(NavigateToURL(shell(), url)); |
| 2453 | |
| 2454 | // Navigate again, but make it so that the DidCommitParams origin will |
| 2455 | // mismatch, causing a renderer kill. |
| 2456 | GURL url_2(embedded_test_server()->GetURL("a.test", "/title2.html")); |
| 2457 | NavigationHandleObserver navigation_observer(shell()->web_contents(), url_2); |
| 2458 | DidCommitParamsOriginReplacer replacer(shell()->web_contents(), |
| 2459 | url::Origin::Create(GURL("b.test"))); |
| 2460 | RenderProcessHostBadIpcMessageWaiter kill_waiter( |
| 2461 | shell()->web_contents()->GetPrimaryMainFrame()->GetProcess()); |
| 2462 | EXPECT_TRUE(NavigateToURLAndExpectNoCommit(shell(), url_2)); |
| 2463 | EXPECT_EQ(bad_message::RFH_ORIGIN_TO_COMMIT_MISMATCH, kill_waiter.Wait()); |
| 2464 | } |
| 2465 | |
[email protected] | 04cbd3d | 2013-12-04 04:58:20 | [diff] [blame] | 2466 | } // namespace content |