clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 5 | #include "content/browser/renderer_host/navigation_request.h" |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 10 | #include "base/bind.h" |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 11 | #include "base/i18n/number_formatting.h" |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 12 | #include "base/test/scoped_feature_list.h" |
Becky Zhou | 9898cb6e | 2019-06-05 00:35:30 | [diff] [blame] | 13 | #include "build/build_config.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 14 | #include "content/public/browser/navigation_throttle.h" |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 15 | #include "content/public/browser/site_isolation_policy.h" |
jam | 6d47c345 | 2016-09-09 18:51:01 | [diff] [blame] | 16 | #include "content/public/browser/ssl_status.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 17 | #include "content/public/common/content_client.h" |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 18 | #include "content/public/common/content_switches.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 19 | #include "content/public/common/url_constants.h" |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 20 | #include "content/public/test/test_navigation_throttle.h" |
Yao Xiao | 1099e4c | 2022-03-24 22:40:37 | [diff] [blame] | 21 | #include "content/test/fenced_frame_test_utils.h" |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 22 | #include "content/test/navigation_simulator_impl.h" |
Charles Harrison | 860f7ef | 2017-06-28 15:31:41 | [diff] [blame] | 23 | #include "content/test/test_content_browser_client.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 24 | #include "content/test/test_render_frame_host.h" |
scottmg | 276753cf | 2016-10-27 18:25:22 | [diff] [blame] | 25 | #include "content/test/test_web_contents.h" |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 26 | #include "net/ssl/ssl_connection_status_flags.h" |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 27 | #include "services/network/public/cpp/content_security_policy/content_security_policy.h" |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 28 | #include "testing/gmock/include/gmock/gmock.h" |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 29 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 30 | #include "third_party/blink/public/common/navigation/navigation_params.h" |
Richard Li | e689995 | 2018-11-30 08:42:00 | [diff] [blame] | 31 | #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h" |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 32 | |
| 33 | namespace content { |
| 34 | |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 35 | // Test version of a NavigationThrottle that will execute a callback when |
| 36 | // called. |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 37 | class DeletingNavigationThrottle : public NavigationThrottle { |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 38 | public: |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 39 | DeletingNavigationThrottle(NavigationHandle* handle, |
| 40 | const base::RepeatingClosure& deletion_callback) |
| 41 | : NavigationThrottle(handle), deletion_callback_(deletion_callback) {} |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 42 | ~DeletingNavigationThrottle() override = default; |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 43 | |
| 44 | NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 45 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 46 | return NavigationThrottle::PROCEED; |
| 47 | } |
| 48 | |
| 49 | NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 50 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 51 | return NavigationThrottle::PROCEED; |
| 52 | } |
| 53 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 54 | NavigationThrottle::ThrottleCheckResult WillFailRequest() override { |
| 55 | deletion_callback_.Run(); |
| 56 | return NavigationThrottle::PROCEED; |
| 57 | } |
| 58 | |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 59 | NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 60 | deletion_callback_.Run(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 61 | return NavigationThrottle::PROCEED; |
| 62 | } |
| 63 | |
| 64 | const char* GetNameForLogging() override { |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 65 | return "DeletingNavigationThrottle"; |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 69 | base::RepeatingClosure deletion_callback_; |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 70 | }; |
| 71 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 72 | class NavigationRequestTest : public RenderViewHostImplTestHarness { |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 73 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 74 | NavigationRequestTest() : callback_result_(NavigationThrottle::DEFER) {} |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 75 | |
| 76 | void SetUp() override { |
| 77 | RenderViewHostImplTestHarness::SetUp(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 78 | CreateNavigationHandle(); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 79 | contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void TearDown() override { |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 83 | RenderViewHostImplTestHarness::TearDown(); |
| 84 | } |
| 85 | |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 86 | void CancelDeferredNavigation( |
| 87 | NavigationThrottle::ThrottleCheckResult result) { |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 88 | GetNavigationRequest()->CancelDeferredNavigationInternal(result); |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 89 | } |
| 90 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 91 | // Helper function to call WillStartRequest on |handle|. If this function |
| 92 | // returns DEFER, |callback_result_| will be set to the actual result of |
| 93 | // the throttle checks when they are finished. |
| 94 | void SimulateWillStartRequest() { |
| 95 | was_callback_called_ = false; |
| 96 | callback_result_ = NavigationThrottle::DEFER; |
| 97 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 98 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 99 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 100 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 101 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 102 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 103 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 104 | GetNavigationRequest()->WillStartRequest(); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Helper function to call WillRedirectRequest on |handle|. If this function |
| 108 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 109 | // throttle checks when they are finished. |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 110 | // TODO(clamy): this should also simulate that WillStartRequest was called if |
| 111 | // it has not been called before. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 112 | void SimulateWillRedirectRequest() { |
| 113 | was_callback_called_ = false; |
| 114 | callback_result_ = NavigationThrottle::DEFER; |
| 115 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 116 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 117 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 118 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 119 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 120 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 121 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 122 | GetNavigationRequest()->WillRedirectRequest( |
Arthur Hemery | 3a991c09 | 2021-12-22 12:04:24 | [diff] [blame] | 123 | GURL(), nullptr /* post_redirect_process */); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 124 | } |
| 125 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 126 | // Helper function to call WillFailRequest on |handle|. If this function |
| 127 | // returns DEFER, |callback_result_| will be set to the actual result of the |
| 128 | // throttle checks when they are finished. |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 129 | void SimulateWillFailRequest( |
| 130 | net::Error net_error_code, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 131 | const absl::optional<net::SSLInfo> ssl_info = absl::nullopt) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 132 | was_callback_called_ = false; |
| 133 | callback_result_ = NavigationThrottle::DEFER; |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 134 | GetNavigationRequest()->set_net_error(net_error_code); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 135 | |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 136 | // It's safe to use base::Unretained since the NavigationRequest is owned by |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 137 | // the NavigationRequestTest. |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 138 | GetNavigationRequest()->set_complete_callback_for_testing( |
Makoto Shimazu | d2aa220 | 2019-10-09 13:57:18 | [diff] [blame] | 139 | base::BindOnce(&NavigationRequestTest::UpdateThrottleCheckResult, |
| 140 | base::Unretained(this))); |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 141 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 142 | GetNavigationRequest()->WillFailRequest(); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 143 | } |
| 144 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 145 | // Whether the callback was called. |
| 146 | bool was_callback_called() const { return was_callback_called_; } |
| 147 | |
| 148 | // Returns the callback_result. |
| 149 | NavigationThrottle::ThrottleCheckResult callback_result() const { |
| 150 | return callback_result_; |
| 151 | } |
| 152 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 153 | NavigationRequest::NavigationState state() { |
| 154 | return GetNavigationRequest()->state(); |
| 155 | } |
Mohamed Abdelhalim | ccd149af | 2019-10-31 14:48:53 | [diff] [blame] | 156 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 157 | bool call_counts_match(TestNavigationThrottle* throttle, |
| 158 | int start, |
| 159 | int redirect, |
| 160 | int failure, |
| 161 | int process) { |
| 162 | return start == throttle->GetCallCount( |
| 163 | TestNavigationThrottle::WILL_START_REQUEST) && |
| 164 | redirect == throttle->GetCallCount( |
| 165 | TestNavigationThrottle::WILL_REDIRECT_REQUEST) && |
| 166 | failure == throttle->GetCallCount( |
| 167 | TestNavigationThrottle::WILL_FAIL_REQUEST) && |
| 168 | process == throttle->GetCallCount( |
| 169 | TestNavigationThrottle::WILL_PROCESS_RESPONSE); |
| 170 | } |
| 171 | |
| 172 | // Creates, register and returns a TestNavigationThrottle that will |
| 173 | // synchronously return |result| on checks by default. |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 174 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 175 | NavigationThrottle::ThrottleCheckResult result) { |
| 176 | TestNavigationThrottle* test_throttle = |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 177 | new TestNavigationThrottle(GetNavigationRequest()); |
Lucas Garron | 79e1a97 | 2017-10-04 22:25:06 | [diff] [blame] | 178 | test_throttle->SetResponseForAllMethods(TestNavigationThrottle::SYNCHRONOUS, |
| 179 | result); |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 180 | GetNavigationRequest()->RegisterThrottleForTesting( |
dcheng | 9bfa516 | 2016-04-09 01:00:57 | [diff] [blame] | 181 | std::unique_ptr<TestNavigationThrottle>(test_throttle)); |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 182 | return test_throttle; |
| 183 | } |
| 184 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 185 | // Creates, register and returns a TestNavigationThrottle that will |
| 186 | // synchronously return |result| on check for the given |method|, and |
| 187 | // NavigationThrottle::PROCEED otherwise. |
| 188 | TestNavigationThrottle* CreateTestNavigationThrottle( |
| 189 | TestNavigationThrottle::ThrottleMethod method, |
| 190 | NavigationThrottle::ThrottleCheckResult result) { |
| 191 | TestNavigationThrottle* test_throttle = |
| 192 | CreateTestNavigationThrottle(NavigationThrottle::PROCEED); |
| 193 | test_throttle->SetResponse(method, TestNavigationThrottle::SYNCHRONOUS, |
| 194 | result); |
| 195 | return test_throttle; |
| 196 | } |
| 197 | |
Mohamed Abdelhalim | 9ef43fc | 2019-04-05 13:09:43 | [diff] [blame] | 198 | // TODO(zetamoo): Use NavigationSimulator instead of creating |
| 199 | // NavigationRequest and NavigationHandleImpl. |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 200 | void CreateNavigationHandle() { |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 201 | auto common_params = blink::CreateCommonNavigationParams(); |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 202 | common_params->initiator_origin = |
Lukasz Anforowicz | 435bcb58 | 2019-07-12 20:50:06 | [diff] [blame] | 203 | url::Origin::Create(GURL("https://initiator.example.com")); |
Minggang Wang | a13c796e | 2021-07-02 05:54:43 | [diff] [blame] | 204 | auto commit_params = blink::CreateCommitNavigationParams(); |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 205 | commit_params->frame_policy = |
| 206 | main_test_rfh()->frame_tree_node()->pending_frame_policy(); |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 207 | auto request = NavigationRequest::CreateBrowserInitiated( |
Lucas Furukawa Gadani | ef8290a | 2019-07-29 20:27:51 | [diff] [blame] | 208 | main_test_rfh()->frame_tree_node(), std::move(common_params), |
arthursonzogni | 70ac730 | 2020-05-28 08:49:05 | [diff] [blame] | 209 | std::move(commit_params), false /* browser-initiated */, |
Takashi Toyoshima | e87b7be | 2021-01-22 11:51:08 | [diff] [blame] | 210 | false /* was_opener_suppressed */, nullptr /* initiator_frame_token */, |
Antonio Sartori | 9a82f6f3 | 2020-12-14 09:22:45 | [diff] [blame] | 211 | ChildProcessHost::kInvalidUniqueID /* initiator_process_id */, |
John Delaney | 50425f8 | 2020-04-07 16:26:21 | [diff] [blame] | 212 | std::string() /* extra_headers */, nullptr /* frame_entry */, |
jongdeok.kim | 5de823b3 | 2022-06-14 04:37:50 | [diff] [blame] | 213 | nullptr /* entry */, false /* is_form_submission */, |
Daniel Hosseinian | f0fbfb4 | 2021-09-08 02:20:47 | [diff] [blame] | 214 | nullptr /* navigation_ui_data */, absl::nullopt /* impression */, |
| 215 | false /* is_pdf */); |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 216 | main_test_rfh()->frame_tree_node()->CreatedNavigationRequest( |
| 217 | std::move(request)); |
| 218 | GetNavigationRequest()->StartNavigation(); |
clamy | 1d4e78fd | 2017-07-11 12:59:19 | [diff] [blame] | 219 | } |
| 220 | |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 221 | FrameTreeNode* AddFrame(FrameTree& frame_tree, |
| 222 | RenderFrameHostImpl* parent, |
| 223 | int process_id, |
| 224 | int new_routing_id, |
| 225 | const blink::FramePolicy& frame_policy, |
| 226 | blink::FrameOwnerElementType owner_type) { |
| 227 | return frame_tree.AddFrame( |
| 228 | parent, process_id, new_routing_id, |
| 229 | TestRenderFrameHost::CreateStubFrameRemote(), |
| 230 | TestRenderFrameHost::CreateStubBrowserInterfaceBrokerReceiver(), |
| 231 | TestRenderFrameHost::CreateStubPolicyContainerBindParams(), |
Dominic Farolino | 12e06d7 | 2022-08-05 02:29:49 | [diff] [blame] | 232 | TestRenderFrameHost::CreateStubAssociatedInterfaceProviderReceiver(), |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 233 | blink::mojom::TreeScopeType::kDocument, std::string(), "uniqueName0", |
| 234 | false, blink::LocalFrameToken(), base::UnguessableToken::Create(), |
| 235 | frame_policy, blink::mojom::FrameOwnerProperties(), false, owner_type, |
| 236 | /*is_dummy_frame_for_inner_tree=*/false); |
| 237 | } |
| 238 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 239 | private: |
Mohamed Abdelhalim | f03d4a2 | 2019-10-01 13:34:31 | [diff] [blame] | 240 | // The callback provided to NavigationRequest::WillStartRequest, |
| 241 | // NavigationRequest::WillRedirectRequest, and |
| 242 | // NavigationRequest::WillFailRequest during the tests. |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 243 | bool UpdateThrottleCheckResult( |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 244 | NavigationThrottle::ThrottleCheckResult result) { |
| 245 | callback_result_ = result; |
| 246 | was_callback_called_ = true; |
Mohamed Abdelhalim | 7e9e9c1 | 2019-11-26 13:48:44 | [diff] [blame] | 247 | return true; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 248 | } |
| 249 | |
Hiroki Nakagawa | adcffc50 | 2021-06-16 10:47:51 | [diff] [blame] | 250 | // This must be called after CreateNavigationHandle(). |
| 251 | NavigationRequest* GetNavigationRequest() { |
| 252 | return main_test_rfh()->frame_tree_node()->navigation_request(); |
| 253 | } |
| 254 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 255 | bool was_callback_called_ = false; |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 256 | NavigationThrottle::ThrottleCheckResult callback_result_; |
| 257 | }; |
| 258 | |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 259 | // Checks that the request_context_type is properly set. |
| 260 | // Note: can be extended to cover more internal members. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 261 | TEST_F(NavigationRequestTest, SimpleDataChecksRedirectAndProcess) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 262 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 263 | const GURL kUrl2 = GURL("http://google.com"); |
| 264 | auto navigation = |
| 265 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 266 | navigation->Start(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 267 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 268 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 269 | ->request_context_type()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 270 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 271 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 272 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 273 | navigation->set_http_connection_info( |
| 274 | net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1); |
| 275 | navigation->Redirect(kUrl2); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 276 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 277 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 278 | ->request_context_type()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 279 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 280 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 281 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 282 | navigation->set_http_connection_info( |
| 283 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 284 | navigation->ReadyToCommit(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 285 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 286 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 287 | ->request_context_type()); |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 288 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 289 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 290 | } |
| 291 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 292 | TEST_F(NavigationRequestTest, SimpleDataCheckNoRedirect) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 293 | const GURL kUrl = GURL("http://chromium.org"); |
| 294 | auto navigation = |
| 295 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 296 | navigation->Start(); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 297 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 298 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
jkarlin | bb15011 | 2016-11-02 17:55:11 | [diff] [blame] | 299 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 300 | navigation->set_http_connection_info( |
| 301 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 302 | navigation->ReadyToCommit(); |
bnc | 90be5dd78 | 2016-11-09 16:28:44 | [diff] [blame] | 303 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 304 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
carlosk | 489d9e2 | 2016-07-25 14:25:43 | [diff] [blame] | 305 | } |
| 306 | |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 307 | TEST_F(NavigationRequestTest, SimpleDataChecksFailure) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 308 | const GURL kUrl = GURL("http://chromium.org"); |
| 309 | auto navigation = |
| 310 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 311 | navigation->Start(); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 312 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 313 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 314 | ->request_context_type()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 315 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 316 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 317 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 318 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Harkiran Bolaria | a2c9f79a | 2021-07-02 09:25:40 | [diff] [blame] | 319 | EXPECT_EQ(blink::mojom::RequestContextType::LOCATION, |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 320 | NavigationRequest::From(navigation->GetNavigationHandle()) |
| 321 | ->request_context_type()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 322 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, |
| 323 | navigation->GetNavigationHandle()->GetNetErrorCode()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 324 | } |
| 325 | |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 326 | TEST_F(NavigationRequestTest, FencedFrameNavigationToPendingMappedURN) { |
| 327 | // Note that we only run this test for the ShadowDOM implementation of fenced |
| 328 | // frames, due to how they add subframes in a way that is very specific to the |
| 329 | // ShadowDOM implementation, and not suitable for the MPArch implementation. |
| 330 | base::test::ScopedFeatureList scoped_feature_list; |
| 331 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
| 332 | blink::features::kFencedFrames, {{"implementation_type", "shadow_dom"}}); |
| 333 | |
| 334 | FrameTree& frame_tree = contents()->GetPrimaryFrameTree(); |
| 335 | FrameTreeNode* root = frame_tree.root(); |
| 336 | int process_id = root->current_frame_host()->GetProcess()->GetID(); |
| 337 | |
| 338 | // Add a fenced frame. |
| 339 | constexpr auto kFencedframeOwnerType = |
| 340 | blink::FrameOwnerElementType::kFencedframe; |
| 341 | blink::FramePolicy policy; |
| 342 | policy.is_fenced = true; |
| 343 | AddFrame(frame_tree, root->current_frame_host(), process_id, 15, policy, |
| 344 | kFencedframeOwnerType); |
| 345 | |
| 346 | FrameTreeNode* fenced_frame_tree_node = root->child_at(0); |
| 347 | EXPECT_TRUE(fenced_frame_tree_node->IsFencedFrameRoot()); |
| 348 | EXPECT_TRUE(fenced_frame_tree_node->IsInFencedFrameTree()); |
| 349 | |
| 350 | FencedFrameURLMapping& fenced_frame_urls_map = |
| 351 | main_test_rfh()->GetPage().fenced_frame_urls_map(); |
| 352 | |
| 353 | const GURL urn_uuid = fenced_frame_urls_map.GeneratePendingMappedURN(); |
Dominic Farolino | e724b533 | 2022-04-26 05:00:06 | [diff] [blame] | 354 | const GURL mapped_url = GURL("https://chromium.org"); |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 355 | |
| 356 | auto navigation_simulator = NavigationSimulatorImpl::CreateRendererInitiated( |
| 357 | urn_uuid, fenced_frame_tree_node->current_frame_host()); |
| 358 | |
| 359 | auto response_headers = |
| 360 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 361 | response_headers->SetHeader("Supports-Loading-Mode", "fenced-frame"); |
| 362 | |
| 363 | navigation_simulator->SetAutoAdvance(false); |
| 364 | navigation_simulator->SetResponseHeaders(response_headers); |
| 365 | navigation_simulator->SetTransition(ui::PAGE_TRANSITION_AUTO_SUBFRAME); |
| 366 | |
| 367 | navigation_simulator->Start(); |
| 368 | |
| 369 | EXPECT_EQ(navigation_simulator->GetNavigationHandle()->GetURL(), urn_uuid); |
| 370 | |
Yao Xiao | 1099e4c | 2022-03-24 22:40:37 | [diff] [blame] | 371 | SimulateSharedStorageURNMappingComplete( |
| 372 | fenced_frame_urls_map, urn_uuid, mapped_url, |
| 373 | /*shared_storage_origin=*/url::Origin::Create(GURL("https://bar.com")), |
| 374 | /*budget_to_charge=*/2.0); |
Yao Xiao | 6e1f7d3 | 2022-01-07 03:28:40 | [diff] [blame] | 375 | |
| 376 | // Expect that the url in the NavigationRequest is already mapped. |
| 377 | EXPECT_EQ(navigation_simulator->GetNavigationHandle()->GetURL(), mapped_url); |
| 378 | |
| 379 | navigation_simulator->Wait(); |
| 380 | |
| 381 | navigation_simulator->SetAutoAdvance(true); |
| 382 | navigation_simulator->ReadyToCommit(); |
| 383 | navigation_simulator->Commit(); |
| 384 | |
| 385 | EXPECT_EQ(fenced_frame_tree_node->current_url(), mapped_url); |
| 386 | } |
| 387 | |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 388 | // Checks that a navigation deferred during WillStartRequest can be properly |
| 389 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 390 | TEST_F(NavigationRequestTest, CancelDeferredWillStart) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 391 | TestNavigationThrottle* test_throttle = |
| 392 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 393 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 394 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 395 | |
| 396 | // Simulate WillStartRequest. The request should be deferred. The callback |
| 397 | // should not have been called. |
| 398 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 399 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 400 | EXPECT_FALSE(was_callback_called()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 401 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 402 | |
| 403 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 404 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 405 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 406 | EXPECT_TRUE(was_callback_called()); |
| 407 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 408 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Checks that a navigation deferred during WillRedirectRequest can be properly |
| 412 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 413 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirect) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 414 | TestNavigationThrottle* test_throttle = |
| 415 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 416 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 417 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 418 | |
| 419 | // Simulate WillRedirectRequest. The request should be deferred. The callback |
| 420 | // should not have been called. |
| 421 | SimulateWillRedirectRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 422 | EXPECT_EQ(NavigationRequest::WILL_REDIRECT_REQUEST, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 423 | EXPECT_FALSE(was_callback_called()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 424 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 425 | |
| 426 | // Cancel the request. The callback should have been called. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 427 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 428 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 429 | EXPECT_TRUE(was_callback_called()); |
| 430 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 431 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 1, 0, 0)); |
| 432 | } |
| 433 | |
| 434 | // Checks that a navigation deferred during WillFailRequest can be properly |
| 435 | // cancelled. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 436 | TEST_F(NavigationRequestTest, CancelDeferredWillFail) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 437 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 438 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 439 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 440 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
| 441 | |
| 442 | // Simulate WillStartRequest. |
| 443 | SimulateWillStartRequest(); |
| 444 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
| 445 | |
| 446 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 447 | // should not have been called. |
| 448 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 449 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 450 | EXPECT_FALSE(was_callback_called()); |
| 451 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 452 | |
| 453 | // Cancel the request. The callback should have been called. |
| 454 | CancelDeferredNavigation(NavigationThrottle::CANCEL_AND_IGNORE); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 455 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 456 | EXPECT_TRUE(was_callback_called()); |
| 457 | EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
| 458 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Checks that a navigation deferred can be canceled and not ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 462 | TEST_F(NavigationRequestTest, CancelDeferredWillRedirectNoIgnore) { |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 463 | TestNavigationThrottle* test_throttle = |
| 464 | CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 465 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 466 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 467 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 468 | // Simulate WillStartRequest. The request should be deferred. The callback |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 469 | // should not have been called. |
| 470 | SimulateWillStartRequest(); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 471 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 472 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 473 | |
| 474 | // Cancel the request. The callback should have been called with CANCEL, and |
| 475 | // not CANCEL_AND_IGNORE. |
Charles Harrison | 4f2bf1a | 2017-07-18 20:21:21 | [diff] [blame] | 476 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 477 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 478 | EXPECT_TRUE(was_callback_called()); |
| 479 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 480 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
clamy | e8853384 | 2015-11-18 12:48:57 | [diff] [blame] | 481 | } |
| 482 | |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 483 | // Checks that a navigation deferred by WillFailRequest can be canceled and not |
| 484 | // ignored. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 485 | TEST_F(NavigationRequestTest, CancelDeferredWillFailNoIgnore) { |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 486 | TestNavigationThrottle* test_throttle = CreateTestNavigationThrottle( |
| 487 | TestNavigationThrottle::WILL_FAIL_REQUEST, NavigationThrottle::DEFER); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 488 | EXPECT_EQ(NavigationRequest::WILL_START_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 489 | EXPECT_TRUE(call_counts_match(test_throttle, 0, 0, 0, 0)); |
| 490 | |
| 491 | // Simulate WillStartRequest. |
| 492 | SimulateWillStartRequest(); |
| 493 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 0, 0)); |
| 494 | |
| 495 | // Simulate WillFailRequest. The request should be deferred. The callback |
| 496 | // should not have been called. |
| 497 | SimulateWillFailRequest(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 498 | EXPECT_EQ(NavigationRequest::WILL_FAIL_REQUEST, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 499 | EXPECT_FALSE(was_callback_called()); |
| 500 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 501 | |
| 502 | // Cancel the request. The callback should have been called with CANCEL, and |
| 503 | // not CANCEL_AND_IGNORE. |
| 504 | CancelDeferredNavigation(NavigationThrottle::CANCEL); |
Mohamed Abdelhalim | b6f9f70 | 2019-11-06 17:23:19 | [diff] [blame] | 505 | EXPECT_EQ(NavigationRequest::CANCELING, state()); |
Lucas Garron | 0cedd96 | 2017-10-17 07:23:33 | [diff] [blame] | 506 | EXPECT_TRUE(was_callback_called()); |
| 507 | EXPECT_EQ(NavigationThrottle::CANCEL, callback_result()); |
| 508 | EXPECT_TRUE(call_counts_match(test_throttle, 1, 0, 1, 0)); |
| 509 | } |
| 510 | |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 511 | // Checks that data from the SSLInfo passed into SimulateWillStartRequest() is |
John Abd-El-Malek | f36e05f | 2017-11-30 16:17:52 | [diff] [blame] | 512 | // stored on the handle. |
Mohamed Abdelhalim | 1e8c582 | 2019-08-02 11:45:43 | [diff] [blame] | 513 | TEST_F(NavigationRequestTest, WillFailRequestSetsSSLInfo) { |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 514 | uint16_t cipher_suite = 0xc02f; // TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 |
| 515 | int connection_status = 0; |
| 516 | net::SSLConnectionStatusSetCipherSuite(cipher_suite, &connection_status); |
| 517 | |
| 518 | // Set some test values. |
| 519 | net::SSLInfo ssl_info; |
| 520 | ssl_info.cert_status = net::CERT_STATUS_AUTHORITY_INVALID; |
| 521 | ssl_info.connection_status = connection_status; |
| 522 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 523 | const GURL kUrl = GURL("https://chromium.org"); |
| 524 | auto navigation = |
| 525 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
Emily Stark | fd6978ad1 | 2019-04-30 21:20:07 | [diff] [blame] | 526 | navigation->SetSSLInfo(ssl_info); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 527 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 528 | |
| 529 | EXPECT_EQ(net::CERT_STATUS_AUTHORITY_INVALID, |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 530 | navigation->GetNavigationHandle()->GetSSLInfo()->cert_status); |
| 531 | EXPECT_EQ(connection_status, |
| 532 | navigation->GetNavigationHandle()->GetSSLInfo()->connection_status); |
Lucas Garron | c1edb5ab | 2017-11-08 03:31:13 | [diff] [blame] | 533 | } |
| 534 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 535 | namespace { |
| 536 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 537 | // Helper throttle which checks that it can access NavigationHandle's |
| 538 | // RenderFrameHost in WillFailRequest() and then defers the failure. |
| 539 | class GetRenderFrameHostOnFailureNavigationThrottle |
| 540 | : public NavigationThrottle { |
| 541 | public: |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 542 | explicit GetRenderFrameHostOnFailureNavigationThrottle( |
| 543 | NavigationHandle* handle) |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 544 | : NavigationThrottle(handle) {} |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 545 | |
| 546 | GetRenderFrameHostOnFailureNavigationThrottle( |
| 547 | const GetRenderFrameHostOnFailureNavigationThrottle&) = delete; |
| 548 | GetRenderFrameHostOnFailureNavigationThrottle& operator=( |
| 549 | const GetRenderFrameHostOnFailureNavigationThrottle&) = delete; |
| 550 | |
Fergal Daly | a1d56997 | 2021-03-16 03:24:53 | [diff] [blame] | 551 | ~GetRenderFrameHostOnFailureNavigationThrottle() override = default; |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 552 | |
| 553 | NavigationThrottle::ThrottleCheckResult WillFailRequest() override { |
| 554 | EXPECT_TRUE(navigation_handle()->GetRenderFrameHost()); |
| 555 | return NavigationThrottle::DEFER; |
| 556 | } |
| 557 | |
| 558 | const char* GetNameForLogging() override { |
| 559 | return "GetRenderFrameHostOnFailureNavigationThrottle"; |
| 560 | } |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 561 | }; |
| 562 | |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 563 | class ThrottleTestContentBrowserClient : public ContentBrowserClient { |
| 564 | std::vector<std::unique_ptr<NavigationThrottle>> CreateThrottlesForNavigation( |
| 565 | NavigationHandle* navigation_handle) override { |
| 566 | std::vector<std::unique_ptr<NavigationThrottle>> throttle; |
| 567 | throttle.push_back( |
| 568 | std::make_unique<GetRenderFrameHostOnFailureNavigationThrottle>( |
| 569 | navigation_handle)); |
| 570 | return throttle; |
| 571 | } |
| 572 | }; |
| 573 | |
| 574 | } // namespace |
| 575 | |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 576 | // Verify that the NavigationHandle::GetRenderFrameHost() can be retrieved by a |
| 577 | // throttle in WillFailRequest(), as well as after deferring the failure. This |
| 578 | // is allowed, since at that point the final RenderFrameHost will have already |
| 579 | // been chosen. See https://crbug.com/817881. |
Mohamed Abdelhalim | ba02067 | 2019-10-31 16:18:53 | [diff] [blame] | 580 | TEST_F(NavigationRequestTest, WillFailRequestCanAccessRenderFrameHost) { |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 581 | std::unique_ptr<ContentBrowserClient> client( |
| 582 | new ThrottleTestContentBrowserClient); |
| 583 | ContentBrowserClient* old_browser_client = |
| 584 | SetBrowserClientForTesting(client.get()); |
| 585 | |
| 586 | const GURL kUrl = GURL("http://chromium.org"); |
| 587 | auto navigation = |
| 588 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 589 | navigation->SetAutoAdvance(false); |
| 590 | navigation->Start(); |
| 591 | navigation->Fail(net::ERR_CERT_DATE_INVALID); |
Mohamed Abdelhalim | 3b23527 | 2019-11-05 15:06:07 | [diff] [blame] | 592 | EXPECT_EQ( |
| 593 | NavigationRequest::WILL_FAIL_REQUEST, |
| 594 | NavigationRequest::From(navigation->GetNavigationHandle())->state()); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 595 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 596 | NavigationRequest::From(navigation->GetNavigationHandle()) |
danakj | f26536bf | 2020-09-10 00:46:13 | [diff] [blame] | 597 | ->GetNavigationThrottleRunnerForTesting() |
Mohamed Abdelhalim | 40c35d2 | 2019-09-19 15:59:05 | [diff] [blame] | 598 | ->CallResumeForTesting(); |
Camille Lamy | 62b82601 | 2019-02-26 09:15:47 | [diff] [blame] | 599 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetRenderFrameHost()); |
| 600 | |
| 601 | SetBrowserClientForTesting(old_browser_client); |
Alex Moshchuk | 1a66b1d | 2018-05-15 21:18:26 | [diff] [blame] | 602 | } |
| 603 | |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 604 | TEST_F(NavigationRequestTest, PolicyContainerInheritance) { |
| 605 | struct TestCase { |
| 606 | const char* url; |
| 607 | bool expect_inherit; |
| 608 | } cases[]{{"about:blank", true}, |
| 609 | {"data:text/plain,hello", true}, |
| 610 | {"file://local", false}, |
| 611 | {"http://chromium.org", false}}; |
| 612 | |
| 613 | const GURL kUrl1 = GURL("http://chromium.org"); |
| 614 | auto navigation = |
| 615 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl1, main_rfh()); |
| 616 | navigation->Commit(); |
| 617 | |
| 618 | for (auto test : cases) { |
| 619 | // We navigate child frames because the BlockedSchemeNavigationThrottle |
| 620 | // restricts navigations in the main frame. |
| 621 | auto* child_frame = static_cast<TestRenderFrameHost*>( |
| 622 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("child")); |
| 623 | |
| 624 | // We set the referrer policy of the frame to "always". We then create a new |
| 625 | // navigation, set as initiator the frame itself, start the navigation, and |
| 626 | // change the referrer policy of the frame to "never". After we commit the |
| 627 | // navigation: |
| 628 | // - If navigating to a local scheme, the target frame should have inherited |
| 629 | // the referrer policy of the initiator ("always"). |
| 630 | // - If navigating to a non-local scheme, the target frame should have a new |
| 631 | // policy container (hence referrer policy set to "default"). |
| 632 | const GURL kUrl = GURL(test.url); |
Peter Kasting | eb8c3ce | 2021-08-20 04:39:35 | [diff] [blame] | 633 | navigation = |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 634 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, child_frame); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 635 | static_cast<blink::mojom::PolicyContainerHost*>( |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 636 | child_frame->policy_container_host()) |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 637 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kAlways); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 638 | navigation->SetInitiatorFrame(child_frame); |
| 639 | navigation->Start(); |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 640 | static_cast<blink::mojom::PolicyContainerHost*>( |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 641 | child_frame->policy_container_host()) |
Antonio Sartori | 5b2f804 | 2020-10-23 18:13:26 | [diff] [blame] | 642 | ->SetReferrerPolicy(network::mojom::ReferrerPolicy::kNever); |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 643 | navigation->Commit(); |
| 644 | EXPECT_EQ( |
| 645 | test.expect_inherit ? network::mojom::ReferrerPolicy::kAlways |
| 646 | : network::mojom::ReferrerPolicy::kDefault, |
| 647 | static_cast<RenderFrameHostImpl*>(navigation->GetFinalRenderFrameHost()) |
Antonio Sartori | 9290b6b | 2020-11-09 10:09:33 | [diff] [blame] | 648 | ->policy_container_host() |
Antonio Sartori | 3cfa3b6 | 2020-10-09 10:42:40 | [diff] [blame] | 649 | ->referrer_policy()); |
| 650 | } |
| 651 | } |
| 652 | |
Cammie Smith Barnes | a0da2cf | 2021-01-11 22:09:37 | [diff] [blame] | 653 | TEST_F(NavigationRequestTest, DnsAliasesCanBeAccessed) { |
| 654 | // Create simulated NavigationRequest for the URL, which has aliases. |
| 655 | const GURL kUrl = GURL("http://chromium.org"); |
| 656 | auto navigation = |
| 657 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 658 | std::vector<std::string> dns_aliases({"alias1", "alias2"}); |
| 659 | navigation->SetResponseDnsAliases(std::move(dns_aliases)); |
| 660 | |
| 661 | // Start the navigation. |
| 662 | navigation->Start(); |
| 663 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
| 664 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 665 | |
| 666 | // Commit the navigation. |
| 667 | navigation->set_http_connection_info( |
| 668 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 669 | navigation->ReadyToCommit(); |
| 670 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
| 671 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 672 | |
| 673 | // Verify that the aliases are accessible from the NavigationRequest. |
| 674 | EXPECT_THAT(navigation->GetNavigationHandle()->GetDnsAliases(), |
| 675 | testing::ElementsAre("alias1", "alias2")); |
| 676 | } |
| 677 | |
| 678 | TEST_F(NavigationRequestTest, NoDnsAliases) { |
| 679 | // Create simulated NavigationRequest for the URL, which does not |
| 680 | // have aliases. (Note the empty alias list.) |
| 681 | const GURL kUrl = GURL("http://chromium.org"); |
| 682 | auto navigation = |
| 683 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 684 | std::vector<std::string> dns_aliases; |
| 685 | navigation->SetResponseDnsAliases(std::move(dns_aliases)); |
| 686 | |
| 687 | // Start the navigation. |
| 688 | navigation->Start(); |
| 689 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_UNKNOWN, |
| 690 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 691 | |
| 692 | // Commit the navigation. |
| 693 | navigation->set_http_connection_info( |
| 694 | net::HttpResponseInfo::CONNECTION_INFO_QUIC_35); |
| 695 | navigation->ReadyToCommit(); |
| 696 | EXPECT_EQ(net::HttpResponseInfo::CONNECTION_INFO_QUIC_35, |
| 697 | navigation->GetNavigationHandle()->GetConnectionInfo()); |
| 698 | |
| 699 | // Verify that there are no aliases in the NavigationRequest. |
| 700 | EXPECT_TRUE(navigation->GetNavigationHandle()->GetDnsAliases().empty()); |
| 701 | } |
| 702 | |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 703 | TEST_F(NavigationRequestTest, StorageKeyToCommit) { |
| 704 | TestRenderFrameHost* child_document = static_cast<TestRenderFrameHost*>( |
| 705 | content::RenderFrameHostTester::For(main_rfh())->AppendChild("")); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 706 | auto attributes = child_document->frame_tree_node()->attributes_->Clone(); |
| 707 | // Set |anonymous| to true. |
| 708 | attributes->anonymous = true; |
| 709 | child_document->frame_tree_node()->SetAttributes(std::move(attributes)); |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 710 | |
| 711 | const GURL kUrl = GURL("http://chromium.org"); |
| 712 | auto navigation = |
| 713 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, child_document); |
| 714 | navigation->ReadyToCommit(); |
| 715 | NavigationRequest* request = |
| 716 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 717 | EXPECT_TRUE(request->commit_params().storage_key.nonce().has_value()); |
Ali Hijazi | 78de14c | 2022-07-21 11:32:19 | [diff] [blame] | 718 | EXPECT_EQ(child_document->GetMainFrame()->anonymous_iframes_nonce(), |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 719 | request->commit_params().storage_key.nonce().value()); |
| 720 | |
| 721 | navigation->Commit(); |
| 722 | child_document = |
| 723 | static_cast<TestRenderFrameHost*>(navigation->GetFinalRenderFrameHost()); |
Arthur Sonzogni | e41678a | 2022-06-16 15:51:19 | [diff] [blame] | 724 | EXPECT_TRUE(child_document->IsAnonymous()); |
Ali Hijazi | 78de14c | 2022-07-21 11:32:19 | [diff] [blame] | 725 | EXPECT_EQ(blink::StorageKey::CreateWithNonce( |
| 726 | url::Origin::Create(kUrl), |
| 727 | child_document->GetMainFrame()->anonymous_iframes_nonce()), |
| 728 | child_document->storage_key()); |
Antonio Sartori | 3e8de6d | 2021-07-26 10:28:41 | [diff] [blame] | 729 | } |
| 730 | |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 731 | TEST_F(NavigationRequestTest, |
| 732 | NavigationToAnonymousDocumentNetworkIsolationInfo) { |
| 733 | auto* child_frame = static_cast<TestRenderFrameHost*>( |
| 734 | content::RenderFrameHostTester::For(main_test_rfh()) |
| 735 | ->AppendChild("child")); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 736 | auto attributes = child_frame->frame_tree_node()->attributes_->Clone(); |
| 737 | // Set |anonymous| to true. |
| 738 | attributes->anonymous = true; |
| 739 | child_frame->frame_tree_node()->SetAttributes(std::move(attributes)); |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 740 | |
| 741 | std::unique_ptr<NavigationSimulator> navigation = |
| 742 | NavigationSimulator::CreateRendererInitiated( |
| 743 | GURL("https://example.com/navigation.html"), child_frame); |
| 744 | navigation->ReadyToCommit(); |
| 745 | |
Ali Hijazi | 78de14c | 2022-07-21 11:32:19 | [diff] [blame] | 746 | EXPECT_EQ(main_test_rfh()->anonymous_iframes_nonce(), |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 747 | static_cast<NavigationRequest*>(navigation->GetNavigationHandle()) |
| 748 | ->isolation_info_for_subresources() |
| 749 | .network_isolation_key() |
| 750 | .GetNonce()); |
Ali Hijazi | 78de14c | 2022-07-21 11:32:19 | [diff] [blame] | 751 | EXPECT_EQ(main_test_rfh()->anonymous_iframes_nonce(), |
Antonio Sartori | bf27cc44 | 2021-08-25 13:08:23 | [diff] [blame] | 752 | static_cast<NavigationRequest*>(navigation->GetNavigationHandle()) |
| 753 | ->GetIsolationInfo() |
| 754 | .network_isolation_key() |
| 755 | .GetNonce()); |
| 756 | } |
| 757 | |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 758 | class ScopedIsolatedAppBrowserClient : public ContentBrowserClient { |
| 759 | public: |
| 760 | ScopedIsolatedAppBrowserClient() |
| 761 | : old_client_(SetBrowserClientForTesting(this)) {} |
| 762 | |
| 763 | ~ScopedIsolatedAppBrowserClient() override { |
| 764 | SetBrowserClientForTesting(old_client_); |
| 765 | } |
| 766 | |
| 767 | bool ShouldUrlUseApplicationIsolationLevel(BrowserContext* browser_context, |
| 768 | const GURL& url) override { |
| 769 | return true; |
| 770 | } |
| 771 | |
| 772 | private: |
| 773 | raw_ptr<ContentBrowserClient> old_client_; |
| 774 | }; |
| 775 | |
| 776 | TEST_F(NavigationRequestTest, IsolatedAppPolicyInjection) { |
| 777 | const GURL kUrl = GURL("https://chromium.org"); |
| 778 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 779 | switches::kIsolatedAppOrigins, kUrl.spec()); |
| 780 | // Disable flag caching so the --isolated-app-origins value takes effect. |
| 781 | SiteIsolationPolicy::DisableFlagCachingForTesting(); |
| 782 | ScopedIsolatedAppBrowserClient client; |
| 783 | |
| 784 | auto navigation = |
| 785 | NavigationSimulatorImpl::CreateRendererInitiated(kUrl, main_rfh()); |
| 786 | navigation->ReadyToCommit(); |
| 787 | |
| 788 | // Validate the COOP/COEP headers. |
| 789 | const PolicyContainerPolicies& policies = |
| 790 | navigation->GetNavigationHandle()->GetPolicyContainerPolicies(); |
| 791 | EXPECT_EQ(network::mojom::CrossOriginOpenerPolicyValue::kSameOriginPlusCoep, |
| 792 | policies.cross_origin_opener_policy.value); |
| 793 | EXPECT_EQ(network::mojom::CrossOriginEmbedderPolicyValue::kRequireCorp, |
| 794 | policies.cross_origin_embedder_policy.value); |
| 795 | |
| 796 | // Validate CSP. |
| 797 | EXPECT_EQ(1UL, policies.content_security_policies.size()); |
| 798 | const auto& csp = policies.content_security_policies[0]; |
Robbie McElrath | b062f3d | 2022-08-09 17:48:44 | [diff] [blame^] | 799 | EXPECT_EQ(7UL, csp->raw_directives.size()); |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 800 | using Directive = network::mojom::CSPDirectiveName; |
| 801 | EXPECT_EQ("'none'", csp->raw_directives[Directive::BaseURI]); |
| 802 | EXPECT_EQ("'none'", csp->raw_directives[Directive::ObjectSrc]); |
| 803 | EXPECT_EQ("'self'", csp->raw_directives[Directive::DefaultSrc]); |
| 804 | EXPECT_EQ("'self' https:", csp->raw_directives[Directive::FrameSrc]); |
| 805 | EXPECT_EQ("'self' https:", csp->raw_directives[Directive::ConnectSrc]); |
Robbie McElrath | b062f3d | 2022-08-09 17:48:44 | [diff] [blame^] | 806 | EXPECT_EQ("'self' 'wasm-unsafe-eval'", |
| 807 | csp->raw_directives[Directive::ScriptSrc]); |
Robbie McElrath | 5641d57 | 2022-05-20 17:15:29 | [diff] [blame] | 808 | EXPECT_EQ("'script'", csp->raw_directives[Directive::RequireTrustedTypesFor]); |
| 809 | } |
| 810 | |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 811 | // Test that the required CSP of every frame is computed/inherited correctly and |
| 812 | // that the Sec-Required-CSP header is set. |
| 813 | class CSPEmbeddedEnforcementUnitTest : public NavigationRequestTest { |
| 814 | protected: |
| 815 | TestRenderFrameHost* main_rfh() { |
| 816 | return static_cast<TestRenderFrameHost*>(NavigationRequestTest::main_rfh()); |
| 817 | } |
| 818 | |
| 819 | // Simulate the |csp| attribute being set in |rfh|'s frame. Then navigate it. |
| 820 | // Returns the request's Sec-Required-CSP header. |
| 821 | std::string NavigateWithRequiredCSP(TestRenderFrameHost** rfh, |
| 822 | std::string required_csp) { |
| 823 | TestRenderFrameHost* document = *rfh; |
| 824 | |
| 825 | if (!required_csp.empty()) { |
| 826 | auto headers = |
| 827 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 828 | headers->SetHeader("Content-Security-Policy", required_csp); |
| 829 | std::vector<network::mojom::ContentSecurityPolicyPtr> policies; |
| 830 | network::AddContentSecurityPolicyFromHeaders( |
| 831 | *headers, GURL("https://example.com/"), &policies); |
Yuzu Saijo | 03dbf9b | 2022-07-22 04:29:45 | [diff] [blame] | 832 | auto attributes = document->frame_tree_node()->attributes_->Clone(); |
| 833 | // Set csp value. |
| 834 | attributes->parsed_csp_attribute = std::move(policies[0]); |
| 835 | document->frame_tree_node()->SetAttributes(std::move(attributes)); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // Chrome blocks a document navigating to a URL if more than one of its |
| 839 | // ancestors have the same URL. Use a different URL every time, to |
| 840 | // avoid blocking navigation of the grandchild frame. |
| 841 | static int nonce = 0; |
| 842 | GURL url("https://www.example.com" + base::NumberToString(nonce++)); |
| 843 | |
| 844 | auto navigation = |
| 845 | content::NavigationSimulator::CreateRendererInitiated(url, *rfh); |
| 846 | navigation->Start(); |
| 847 | NavigationRequest* request = |
| 848 | NavigationRequest::From(navigation->GetNavigationHandle()); |
| 849 | std::string sec_required_csp; |
| 850 | request->GetRequestHeaders().GetHeader("sec-required-csp", |
| 851 | &sec_required_csp); |
| 852 | |
| 853 | // Complete the navigation so that the required csp is stored in the |
| 854 | // RenderFrameHost, so that when we will add children to this document they |
| 855 | // will be able to get the parent's required csp (and hence also test that |
| 856 | // the whole logic works). |
| 857 | auto response_headers = |
| 858 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
| 859 | response_headers->SetHeader("Allow-CSP-From", "*"); |
| 860 | navigation->SetResponseHeaders(response_headers); |
| 861 | navigation->Commit(); |
| 862 | |
| 863 | *rfh = static_cast<TestRenderFrameHost*>( |
| 864 | navigation->GetFinalRenderFrameHost()); |
| 865 | |
| 866 | return sec_required_csp; |
| 867 | } |
| 868 | |
| 869 | TestRenderFrameHost* AddChild(TestRenderFrameHost* parent) { |
| 870 | return static_cast<TestRenderFrameHost*>( |
| 871 | content::RenderFrameHostTester::For(parent)->AppendChild("")); |
| 872 | } |
| 873 | }; |
| 874 | |
| 875 | TEST_F(CSPEmbeddedEnforcementUnitTest, TopLevel) { |
| 876 | TestRenderFrameHost* top_document = main_rfh(); |
| 877 | std::string sec_required_csp = NavigateWithRequiredCSP(&top_document, ""); |
| 878 | EXPECT_EQ("", sec_required_csp); |
| 879 | EXPECT_FALSE(top_document->required_csp()); |
| 880 | } |
| 881 | |
| 882 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildNoCSP) { |
| 883 | TestRenderFrameHost* top_document = main_rfh(); |
| 884 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 885 | std::string sec_required_csp = NavigateWithRequiredCSP(&child_document, ""); |
| 886 | EXPECT_EQ("", sec_required_csp); |
| 887 | EXPECT_FALSE(child_document->required_csp()); |
| 888 | } |
| 889 | |
| 890 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildWithCSP) { |
| 891 | TestRenderFrameHost* top_document = main_rfh(); |
| 892 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 893 | std::string sec_required_csp = |
| 894 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 895 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 896 | EXPECT_TRUE(child_document->required_csp()); |
| 897 | EXPECT_EQ("script-src 'none'", |
| 898 | child_document->required_csp()->header->header_value); |
| 899 | } |
| 900 | |
| 901 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildSiblingNoCSP) { |
| 902 | TestRenderFrameHost* top_document = main_rfh(); |
| 903 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 904 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 905 | TestRenderFrameHost* sibling_document = AddChild(top_document); |
| 906 | std::string sec_required_csp = NavigateWithRequiredCSP(&sibling_document, ""); |
| 907 | EXPECT_FALSE(sibling_document->required_csp()); |
| 908 | } |
| 909 | |
| 910 | TEST_F(CSPEmbeddedEnforcementUnitTest, ChildSiblingCSP) { |
| 911 | TestRenderFrameHost* top_document = main_rfh(); |
| 912 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 913 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 914 | TestRenderFrameHost* sibling_document = AddChild(top_document); |
| 915 | std::string sec_required_csp = |
| 916 | NavigateWithRequiredCSP(&sibling_document, "script-src 'none'"); |
| 917 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 918 | EXPECT_TRUE(sibling_document->required_csp()); |
| 919 | EXPECT_EQ("script-src 'none'", |
| 920 | sibling_document->required_csp()->header->header_value); |
| 921 | } |
| 922 | |
| 923 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildNoCSP) { |
| 924 | TestRenderFrameHost* top_document = main_rfh(); |
| 925 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 926 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 927 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 928 | std::string sec_required_csp = |
| 929 | NavigateWithRequiredCSP(&grand_child_document, ""); |
| 930 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 931 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 932 | EXPECT_EQ("script-src 'none'", |
| 933 | grand_child_document->required_csp()->header->header_value); |
| 934 | } |
| 935 | |
| 936 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildSameCSP) { |
| 937 | TestRenderFrameHost* top_document = main_rfh(); |
| 938 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 939 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 940 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 941 | std::string sec_required_csp = |
| 942 | NavigateWithRequiredCSP(&grand_child_document, "script-src 'none'"); |
| 943 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 944 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 945 | EXPECT_EQ("script-src 'none'", |
| 946 | grand_child_document->required_csp()->header->header_value); |
| 947 | } |
| 948 | |
| 949 | TEST_F(CSPEmbeddedEnforcementUnitTest, GrandChildDifferentCSP) { |
| 950 | TestRenderFrameHost* top_document = main_rfh(); |
| 951 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 952 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 953 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 954 | std::string sec_required_csp = |
| 955 | NavigateWithRequiredCSP(&grand_child_document, "img-src 'none'"); |
| 956 | |
| 957 | // This seems weird, but it is the intended behaviour according to the spec. |
| 958 | // The problem is that "script-src 'none'" does not subsume "img-src 'none'", |
| 959 | // so "img-src 'none'" on the grandchild is an invalid csp attribute, and we |
| 960 | // just discard it in favour of the parent's csp attribute. |
| 961 | // |
| 962 | // This should probably be fixed in the specification: |
| 963 | // https://github.com/w3c/webappsec-cspee/pull/11 |
| 964 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 965 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 966 | EXPECT_EQ("script-src 'none'", |
| 967 | grand_child_document->required_csp()->header->header_value); |
| 968 | } |
| 969 | |
| 970 | TEST_F(CSPEmbeddedEnforcementUnitTest, InvalidCSP) { |
| 971 | TestRenderFrameHost* top_document = main_rfh(); |
| 972 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 973 | std::string sec_required_csp = |
| 974 | NavigateWithRequiredCSP(&child_document, "report-to group"); |
| 975 | EXPECT_EQ("", sec_required_csp); |
| 976 | EXPECT_FALSE(child_document->required_csp()); |
| 977 | } |
| 978 | |
| 979 | TEST_F(CSPEmbeddedEnforcementUnitTest, InvalidCspAndInheritFromParent) { |
| 980 | TestRenderFrameHost* top_document = main_rfh(); |
| 981 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 982 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 983 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 984 | std::string sec_required_csp = |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 985 | NavigateWithRequiredCSP(&grand_child_document, "report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 986 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 987 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 988 | EXPECT_EQ("script-src 'none'", |
| 989 | grand_child_document->required_csp()->header->header_value); |
| 990 | } |
| 991 | |
| 992 | TEST_F(CSPEmbeddedEnforcementUnitTest, |
| 993 | SemiInvalidCspAndInheritSameCspFromParent) { |
| 994 | TestRenderFrameHost* top_document = main_rfh(); |
| 995 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 996 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 997 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 998 | std::string sec_required_csp = NavigateWithRequiredCSP( |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 999 | &grand_child_document, "script-src 'none'; report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1000 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1001 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1002 | EXPECT_EQ("script-src 'none'", |
| 1003 | grand_child_document->required_csp()->header->header_value); |
| 1004 | } |
| 1005 | |
| 1006 | TEST_F(CSPEmbeddedEnforcementUnitTest, |
| 1007 | SemiInvalidCspAndInheritDifferentCspFromParent) { |
| 1008 | TestRenderFrameHost* top_document = main_rfh(); |
| 1009 | TestRenderFrameHost* child_document = AddChild(top_document); |
| 1010 | NavigateWithRequiredCSP(&child_document, "script-src 'none'"); |
| 1011 | TestRenderFrameHost* grand_child_document = AddChild(child_document); |
| 1012 | std::string sec_required_csp = NavigateWithRequiredCSP( |
Antonio Sartori | 4231e93 | 2021-02-04 12:01:14 | [diff] [blame] | 1013 | &grand_child_document, "sandbox; report-to group"); |
arthursonzogni | 898dcda5 | 2021-01-21 08:50:10 | [diff] [blame] | 1014 | EXPECT_EQ("script-src 'none'", sec_required_csp); |
| 1015 | EXPECT_TRUE(grand_child_document->required_csp()); |
| 1016 | EXPECT_EQ("script-src 'none'", |
| 1017 | grand_child_document->required_csp()->header->header_value); |
| 1018 | } |
| 1019 | |
clamy | 4967831 | 2015-10-22 21:59:00 | [diff] [blame] | 1020 | } // namespace content |