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" |
Sebastien Marchand | 53801a3 | 2019-01-25 16:26:11 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 12 | #include "base/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 | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame^] | 21 | #include "mojo/public/cpp/bindings/self_owned_receiver.h" |
| 22 | #include "net/http/http_status_code.h" |
| 23 | #include "net/http/http_util.h" |
| 24 | #include "net/url_request/redirect_info.h" |
| 25 | #include "net/url_request/redirect_util.h" |
| 26 | #include "services/network/public/cpp/parsed_headers.h" |
| 27 | #include "services/network/public/cpp/resource_request.h" |
| 28 | #include "services/network/public/cpp/single_request_url_loader_factory.h" |
| 29 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 30 | #include "url/android/gurl_android.h" |
[email protected] | e3b599e | 2013-07-05 07:15:17 | [diff] [blame] | 31 | #include "url/gurl.h" |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 32 | |
| 33 | using base::android::ConvertUTF8ToJavaString; |
| 34 | using base::android::ScopedJavaLocalRef; |
| 35 | using content::BrowserThread; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 36 | using content::RenderViewHost; |
| 37 | using content::WebContents; |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 38 | using ui::PageTransition; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 39 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 40 | namespace navigation_interception { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
thestig | 3b6a2f1 | 2015-09-25 08:17:20 | [diff] [blame] | 44 | const void* const kInterceptNavigationDelegateUserDataKey = |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 45 | &kInterceptNavigationDelegateUserDataKey; |
| 46 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 47 | bool CheckIfShouldIgnoreNavigationOnUIThread( |
| 48 | content::NavigationHandle* navigation_handle) { |
mostynb | ad1e8c96 | 2015-03-25 21:51:12 | [diff] [blame] | 49 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 50 | DCHECK(navigation_handle); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 51 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 52 | InterceptNavigationDelegate* intercept_navigation_delegate = |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 53 | InterceptNavigationDelegate::Get(navigation_handle->GetWebContents()); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 54 | if (!intercept_navigation_delegate) |
| 55 | return false; |
| 56 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 57 | return intercept_navigation_delegate->ShouldIgnoreNavigation( |
| 58 | navigation_handle); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 59 | } |
| 60 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame^] | 61 | class RedirectURLLoader : public network::mojom::URLLoader { |
| 62 | public: |
| 63 | RedirectURLLoader(const GURL& url, |
| 64 | const network::ResourceRequest& resource_request, |
| 65 | mojo::PendingRemote<network::mojom::URLLoaderClient> client) |
| 66 | : client_(std::move(client)) { |
| 67 | net::HttpStatusCode response_code = net::HTTP_TEMPORARY_REDIRECT; |
| 68 | auto response_head = network::mojom::URLResponseHead::New(); |
| 69 | response_head->encoded_data_length = 0; |
| 70 | response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 71 | net::HttpUtil::AssembleRawHeaders("HTTP/1.1 307 Temporary Redirect")); |
| 72 | |
| 73 | // Avoid a round-trip to the network service by pre-parsing headers. |
| 74 | // This doesn't violate: `docs/security/rule-of-2.md`, because the input is |
| 75 | // trusted, before appending the Location: <url> header. |
| 76 | response_head->parsed_headers = |
| 77 | network::PopulateParsedHeaders(response_head->headers.get(), url); |
| 78 | |
| 79 | response_head->headers->AddHeader("Location", url.spec()); |
| 80 | |
| 81 | auto first_party_url_policy = |
| 82 | resource_request.update_first_party_url_on_redirect |
| 83 | ? net::RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT |
| 84 | : net::RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL; |
| 85 | |
| 86 | client_->OnReceiveRedirect( |
| 87 | net::RedirectInfo::ComputeRedirectInfo( |
| 88 | resource_request.method, resource_request.url, |
| 89 | resource_request.site_for_cookies, first_party_url_policy, |
| 90 | resource_request.referrer_policy, resource_request.referrer.spec(), |
| 91 | response_code, url, absl::nullopt, |
| 92 | /*insecure_scheme_was_upgraded=*/false, |
| 93 | /*copy_fragment=*/false), |
| 94 | std::move(response_head)); |
| 95 | } |
| 96 | |
| 97 | RedirectURLLoader(const RedirectURLLoader&) = delete; |
| 98 | RedirectURLLoader& operator=(const RedirectURLLoader&) = delete; |
| 99 | |
| 100 | ~RedirectURLLoader() override = default; |
| 101 | |
| 102 | private: |
| 103 | // network::mojom::URLLoader overrides: |
| 104 | void FollowRedirect( |
| 105 | const std::vector<std::string>& removed_headers, |
| 106 | const net::HttpRequestHeaders& modified_headers, |
| 107 | const net::HttpRequestHeaders& modified_cors_exempt_headers, |
| 108 | const absl::optional<GURL>& new_url) override { |
| 109 | NOTREACHED(); |
| 110 | } |
| 111 | void SetPriority(net::RequestPriority priority, |
| 112 | int intra_priority_value) override {} |
| 113 | void PauseReadingBodyFromNet() override {} |
| 114 | void ResumeReadingBodyFromNet() override {} |
| 115 | |
| 116 | mojo::Remote<network::mojom::URLLoaderClient> client_; |
| 117 | }; |
| 118 | |
| 119 | void RedirectToCallback( |
| 120 | GURL url, |
| 121 | const network::ResourceRequest& resource_request, |
| 122 | mojo::PendingReceiver<network::mojom::URLLoader> pending_receiver, |
| 123 | mojo::PendingRemote<network::mojom::URLLoaderClient> pending_client) { |
| 124 | mojo::MakeSelfOwnedReceiver( |
| 125 | std::make_unique<RedirectURLLoader>(url, resource_request, |
| 126 | std::move(pending_client)), |
| 127 | std::move(pending_receiver)); |
| 128 | } |
| 129 | |
[email protected] | a8e69a74 | 2013-10-15 10:58:55 | [diff] [blame] | 130 | } // namespace |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 131 | |
| 132 | // static |
| 133 | void InterceptNavigationDelegate::Associate( |
| 134 | WebContents* web_contents, |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 135 | std::unique_ptr<InterceptNavigationDelegate> delegate) { |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 136 | web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey, |
avi | 8945fc9 | 2017-05-02 16:03:23 | [diff] [blame] | 137 | std::move(delegate)); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // static |
| 141 | InterceptNavigationDelegate* InterceptNavigationDelegate::Get( |
| 142 | WebContents* web_contents) { |
dtrainor | 037df0d | 2014-10-08 18:05:24 | [diff] [blame] | 143 | return static_cast<InterceptNavigationDelegate*>( |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 144 | web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey)); |
| 145 | } |
| 146 | |
| 147 | // static |
dcheng | 84c358e | 2016-04-26 07:05:53 | [diff] [blame] | 148 | std::unique_ptr<content::NavigationThrottle> |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 149 | InterceptNavigationDelegate::MaybeCreateThrottleFor( |
Charlie Harrison | 3286ab7 | 2019-02-13 20:13:30 | [diff] [blame] | 150 | content::NavigationHandle* handle, |
| 151 | navigation_interception::SynchronyMode mode) { |
David Bokan | 2a48f7bb | 2021-07-09 13:21:36 | [diff] [blame] | 152 | // Navigations in a subframe or non-primary frame tree should not be |
| 153 | // intercepted. As examples of a non-primary frame tree, a navigation |
| 154 | // occurring in a Portal element or an unactivated prerendering page should |
| 155 | // not launch an app. |
| 156 | // TODO(bokan): This is a bit of a stopgap approach since we won't run |
| 157 | // throttles again when the prerender is activated which means links that are |
| 158 | // prerendered will avoid launching an app intent that a regular navigation |
| 159 | // would have. Longer term we'll want prerender activation to check for app |
| 160 | // intents, or have this throttle cancel the prerender if an intent would |
| 161 | // have been launched (without launching the intent). It's also not clear |
| 162 | // what the right behavior for <portal> elements is. |
| 163 | // https://crbug.com/1227659. |
| 164 | if (!handle->IsInPrimaryMainFrame()) |
| 165 | return nullptr; |
| 166 | |
Gyuyoung Kim | cb7965e | 2018-01-25 00:39:01 | [diff] [blame] | 167 | return std::make_unique<InterceptNavigationThrottle>( |
Ken Rockot | ae24ce9 | 2019-12-19 20:00:25 | [diff] [blame] | 168 | handle, base::BindRepeating(&CheckIfShouldIgnoreNavigationOnUIThread), |
| 169 | mode); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 170 | } |
| 171 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 172 | InterceptNavigationDelegate::InterceptNavigationDelegate( |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 173 | JNIEnv* env, |
| 174 | jobject jdelegate, |
| 175 | bool escape_external_handler_value) |
| 176 | : weak_jdelegate_(env, jdelegate), |
| 177 | escape_external_handler_value_(escape_external_handler_value) {} |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 178 | |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame^] | 179 | InterceptNavigationDelegate::~InterceptNavigationDelegate() = default; |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 180 | |
| 181 | bool InterceptNavigationDelegate::ShouldIgnoreNavigation( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 182 | content::NavigationHandle* navigation_handle) { |
| 183 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 184 | ? GURL(base::EscapeExternalHandlerValue( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 185 | navigation_handle->GetURL().spec())) |
| 186 | : navigation_handle->GetURL(); |
Colin Blundell | 4695e814 | 2020-03-16 11:13:12 | [diff] [blame] | 187 | |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 188 | if (!escaped_url.is_valid()) |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 189 | return false; |
| 190 | |
| 191 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 192 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 193 | |
| 194 | if (jdelegate.is_null()) |
| 195 | return false; |
| 196 | |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 197 | return Java_InterceptNavigationDelegate_shouldIgnoreNavigation( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 198 | env, jdelegate, navigation_handle->GetJavaNavigationHandle(), |
Michael Thiessen | 332dadb6 | 2022-07-13 14:44:07 | [diff] [blame] | 199 | url::GURLAndroid::FromNativeGURL(env, escaped_url)); |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 200 | } |
| 201 | |
Michael Thiessen | 7cb129e | 2022-11-08 17:24:51 | [diff] [blame] | 202 | void InterceptNavigationDelegate::HandleSubframeExternalProtocol( |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 203 | const GURL& url, |
| 204 | ui::PageTransition page_transition, |
| 205 | bool has_user_gesture, |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame^] | 206 | const absl::optional<url::Origin>& initiating_origin, |
| 207 | mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory) { |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 208 | GURL escaped_url = escape_external_handler_value_ |
Ryan Hamilton | 7f3bd3d | 2022-04-23 00:07:39 | [diff] [blame] | 209 | ? GURL(base::EscapeExternalHandlerValue(url.spec())) |
Michael Thiessen | ca245a38 | 2022-02-21 16:11:17 | [diff] [blame] | 210 | : url; |
| 211 | if (!escaped_url.is_valid()) |
| 212 | return; |
| 213 | |
| 214 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 215 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 216 | |
| 217 | if (jdelegate.is_null()) |
| 218 | return; |
Michael Thiessen | 1a49e4d5 | 2022-12-02 21:54:40 | [diff] [blame^] | 219 | ScopedJavaLocalRef<jobject> j_gurl = |
| 220 | Java_InterceptNavigationDelegate_handleSubframeExternalProtocol( |
| 221 | env, jdelegate, url::GURLAndroid::FromNativeGURL(env, escaped_url), |
| 222 | page_transition, has_user_gesture, |
| 223 | initiating_origin ? initiating_origin->CreateJavaObject() : nullptr); |
| 224 | if (j_gurl.is_null()) |
| 225 | return; |
| 226 | std::unique_ptr<GURL> gurl = url::GURLAndroid::ToNativeGURL(env, j_gurl); |
| 227 | |
| 228 | mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver = |
| 229 | out_factory->InitWithNewPipeAndPassReceiver(); |
| 230 | scoped_refptr<network::SharedURLLoaderFactory> loader_factory = |
| 231 | base::MakeRefCounted<network::SingleRequestURLLoaderFactory>( |
| 232 | base::BindOnce(&RedirectToCallback, *gurl)); |
| 233 | loader_factory->Clone(std::move(receiver)); |
[email protected] | 4360ae7 | 2012-10-09 22:10:46 | [diff] [blame] | 234 | } |
| 235 | |
Michael Thiessen | 332dadb6 | 2022-07-13 14:44:07 | [diff] [blame] | 236 | void InterceptNavigationDelegate::OnResourceRequestWithGesture() { |
| 237 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 238 | ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env); |
| 239 | if (jdelegate.is_null()) |
| 240 | return; |
| 241 | Java_InterceptNavigationDelegate_onResourceRequestWithGesture(env, jdelegate); |
Michael Thiessen | e566352 | 2022-05-25 21:23:28 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 8812e3d0 | 2013-05-22 12:38:53 | [diff] [blame] | 244 | } // namespace navigation_interception |