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