Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [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 | |
[email protected] | 62885ab | 2013-01-23 03:55:16 | [diff] [blame] | 5 | #include "components/navigation_interception/intercept_navigation_delegate.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 6 | |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 9 | #include "base/android/jni_android.h" |
| 10 | #include "base/android/jni_string.h" |
Avi Drissman | 12be031 | 2023-01-11 09:16:09 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
| 12 | #include "base/functional/callback.h" |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 13 | #include "base/strings/escape.h" |
Mohamed Heikal | bd64131 | 2019-06-22 00:14:37 | [diff] [blame] | 14 | #include "components/navigation_interception/jni_headers/InterceptNavigationDelegate_jni.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 16 | #include "content/public/browser/navigation_handle.h" |
clamy | 40c9e14 | 2015-09-29 11:18:47 | [diff] [blame] | 17 | #include "content/public/browser/navigation_throttle.h" |
jaekyun | 03890319 | 2015-03-31 14:15:59 | [diff] [blame] | 18 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 19 | #include "content/public/browser/render_view_host.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 20 | #include "content/public/browser/web_contents.h" |
Michael Thiessen | 41821c98 | 2023-08-14 21:45:54 | [diff] [blame] | 21 | #include "content/public/common/page_visibility_state.h" |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 22 | #include "mojo/public/cpp/bindings/self_owned_receiver.h" |
| 23 | #include "net/http/http_status_code.h" |
| 24 | #include "net/http/http_util.h" |
| 25 | #include "net/url_request/redirect_info.h" |
| 26 | #include "net/url_request/redirect_util.h" |
| 27 | #include "services/network/public/cpp/parsed_headers.h" |
| 28 | #include "services/network/public/cpp/resource_request.h" |
| 29 | #include "services/network/public/cpp/single_request_url_loader_factory.h" |
| 30 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 31 | #include "url/android/gurl_android.h" |
[email protected] | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 32 | #include "url/gurl.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 33 | |
| 34 | using base::android::ConvertUTF8ToJavaString; |
| 35 | using base::android::ScopedJavaLocalRef; |
| 36 | using content::BrowserThread; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 37 | using content::RenderViewHost; |
| 38 | using content::WebContents; |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 39 | using ui::PageTransition; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 40 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 41 | namespace navigation_interception { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 42 | |
| 43 | namespace { |
| 44 | |
thestig | 3b6a2f1 | 2015-09-25 08:17:20 | [diff] [blame] | 45 | const void* const kInterceptNavigationDelegateUserDataKey = |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 46 | &kInterceptNavigationDelegateUserDataKey; |
| 47 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 48 | bool CheckIfShouldIgnoreNavigationOnUIThread( |
| 49 | content::NavigationHandle* navigation_handle) { |
mostynb | ad1e8c96 | 2015-03-25 21:51:12 | [diff] [blame] | 50 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 51 | DCHECK(navigation_handle); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 52 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 53 | InterceptNavigationDelegate* intercept_navigation_delegate = |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 54 | InterceptNavigationDelegate::Get(navigation_handle->GetWebContents()); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 55 | if (!intercept_navigation_delegate) |
| 56 | return false; |
| 57 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 58 | return intercept_navigation_delegate->ShouldIgnoreNavigation( |
| 59 | navigation_handle); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 60 | } |
| 61 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 62 | class RedirectURLLoader : public network::mojom::URLLoader { |
| 63 | public: |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 64 | RedirectURLLoader(const network::ResourceRequest& resource_request, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 65 | mojo::PendingRemote<network::mojom::URLLoaderClient> client) |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 66 | : client_(std::move(client)), request_(resource_request) {} |
| 67 | |
| 68 | void DoRedirect(std::unique_ptr<GURL> url) { |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 69 | net::HttpStatusCode response_code = net::HTTP_TEMPORARY_REDIRECT; |
| 70 | auto response_head = network::mojom::URLResponseHead::New(); |
| 71 | response_head->encoded_data_length = 0; |
| 72 | response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 73 | net::HttpUtil::AssembleRawHeaders("HTTP/1.1 307 Temporary Redirect")); |
| 74 | |
| 75 | // Avoid a round-trip to the network service by pre-parsing headers. |
| 76 | // This doesn't violate: `docs/security/rule-of-2.md`, because the input is |
| 77 | // trusted, before appending the Location: <url> header. |
| 78 | response_head->parsed_headers = |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 79 | network::PopulateParsedHeaders(response_head->headers.get(), *url); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 80 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 81 | response_head->headers->AddHeader("Location", url->spec()); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 82 | |
| 83 | auto first_party_url_policy = |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 84 | request_.update_first_party_url_on_redirect |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 85 | ? net::RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT |
| 86 | : net::RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL; |
| 87 | |
| 88 | client_->OnReceiveRedirect( |
| 89 | net::RedirectInfo::ComputeRedirectInfo( |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 90 | request_.method, request_.url, request_.site_for_cookies, |
| 91 | first_party_url_policy, request_.referrer_policy, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 92 | request_.referrer.spec(), response_code, *url, std::nullopt, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 93 | /*insecure_scheme_was_upgraded=*/false, |
| 94 | /*copy_fragment=*/false), |
| 95 | std::move(response_head)); |
| 96 | } |
| 97 | |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 98 | void OnNonRedirectAsyncAction() { |
| 99 | client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED)); |
| 100 | } |
| 101 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 102 | RedirectURLLoader(const RedirectURLLoader&) = delete; |
| 103 | RedirectURLLoader& operator=(const RedirectURLLoader&) = delete; |
| 104 | |
| 105 | ~RedirectURLLoader() override = default; |
| 106 | |
| 107 | private: |
| 108 | // network::mojom::URLLoader overrides: |
| 109 | void FollowRedirect( |
| 110 | const std::vector<std::string>& removed_headers, |
| 111 | const net::HttpRequestHeaders& modified_headers, |
| 112 | const net::HttpRequestHeaders& modified_cors_exempt_headers, |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 113 | const std::optional<GURL>& new_url) override { |
Peter Boström | aaf19db | 2024-05-14 22:08:09 | [diff] [blame^] | 114 | NOTREACHED_IN_MIGRATION(); |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 115 | } |
| 116 | void SetPriority(net::RequestPriority priority, |
| 117 | int intra_priority_value) override {} |
| 118 | void PauseReadingBodyFromNet() override {} |
| 119 | void ResumeReadingBodyFromNet() override {} |
| 120 | |
| 121 | mojo::Remote<network::mojom::URLLoaderClient> client_; |
Michael Thiessen | 7b53117 | 2023-01-28 05:25:59 | [diff] [blame] | 122 | network::ResourceRequest request_; |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame] | 123 | }; |
| 124 | |
[email protected] | a8e69a74 | 2013-10-15 10:58:55 | [diff] [blame] | 125 | } // namespace |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 126 | |
| 127 | // static |
| 128 | void InterceptNavigationDelegate::Associate( |
| 129 | WebContents* web_contents, |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 130 | std::unique_ptr<InterceptNavigationDelegate> delegate) { |
[email protected] |
|