blob: 02f99bb60b0ed8c6f6efd87da29730dfeabcac3f [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2012 The Chromium Authors
[email protected]4360ae72012-10-09 22:10:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]62885ab2013-01-23 03:55:165#include "components/navigation_interception/intercept_navigation_delegate.h"
[email protected]4360ae72012-10-09 22:10:466
Gyuyoung Kimcb7965e2018-01-25 00:39:017#include <memory>
8
[email protected]4360ae72012-10-09 22:10:469#include "base/android/jni_android.h"
10#include "base/android/jni_string.h"
Avi Drissman12be0312023-01-11 09:16:0911#include "base/functional/bind.h"
12#include "base/functional/callback.h"
Ryan Hamilton7f3bd3d2022-04-23 00:07:3913#include "base/strings/escape.h"
[email protected]4360ae72012-10-09 22:10:4614#include "content/public/browser/browser_thread.h"
David Bokan2a48f7bb2021-07-09 13:21:3615#include "content/public/browser/navigation_handle.h"
clamy40c9e142015-09-29 11:18:4716#include "content/public/browser/navigation_throttle.h"
jaekyun038903192015-03-31 14:15:5917#include "content/public/browser/render_frame_host.h"
[email protected]4360ae72012-10-09 22:10:4618#include "content/public/browser/render_view_host.h"
19#include "content/public/browser/web_contents.h"
Michael Thiessen41821c982023-08-14 21:45:5420#include "content/public/common/page_visibility_state.h"
Michael Thiessen1a49e4d52022-12-02 21:54:4021#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 Thiessenca245a382022-02-21 16:11:1730#include "url/android/gurl_android.h"
[email protected]e3b599e2013-07-05 07:15:1731#include "url/gurl.h"
[email protected]4360ae72012-10-09 22:10:4632
Andrew Grieveecb885bb2024-05-29 18:14:1933// Must come after all headers that specialize FromJniType() / ToJniType().
34#include "components/navigation_interception/jni_headers/InterceptNavigationDelegate_jni.h"
35
[email protected]4360ae72012-10-09 22:10:4636using base::android::ConvertUTF8ToJavaString;
37using base::android::ScopedJavaLocalRef;
38using content::BrowserThread;
39using content::RenderViewHost;
40using content::WebContents;
Michael Thiessenca245a382022-02-21 16:11:1741using ui::PageTransition;
[email protected]4360ae72012-10-09 22:10:4642
[email protected]8812e3d02013-05-22 12:38:5343namespace navigation_interception {
[email protected]4360ae72012-10-09 22:10:4644
45namespace {
46
thestig3b6a2f12015-09-25 08:17:2047const void* const kInterceptNavigationDelegateUserDataKey =
[email protected]4360ae72012-10-09 22:10:4648 &kInterceptNavigationDelegateUserDataKey;
49
Michael Thiessenca245a382022-02-21 16:11:1750bool CheckIfShouldIgnoreNavigationOnUIThread(
51 content::NavigationHandle* navigation_handle) {
mostynbad1e8c962015-03-25 21:51:1252 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Michael Thiessenca245a382022-02-21 16:11:1753 DCHECK(navigation_handle);
[email protected]4360ae72012-10-09 22:10:4654
[email protected]4360ae72012-10-09 22:10:4655 InterceptNavigationDelegate* intercept_navigation_delegate =
Michael Thiessenca245a382022-02-21 16:11:1756 InterceptNavigationDelegate::Get(navigation_handle->GetWebContents());
[email protected]4360ae72012-10-09 22:10:4657 if (!intercept_navigation_delegate)
58 return false;
59
Michael Thiessenca245a382022-02-21 16:11:1760 return intercept_navigation_delegate->ShouldIgnoreNavigation(
61 navigation_handle);
[email protected]4360ae72012-10-09 22:10:4662}
63
Michael Thiessen1a49e4d52022-12-02 21:54:4064class RedirectURLLoader : public network::mojom::URLLoader {
65 public:
Michael Thiessen7b531172023-01-28 05:25:5966 RedirectURLLoader(const network::ResourceRequest& resource_request,
Michael Thiessen1a49e4d52022-12-02 21:54:4067 mojo::PendingRemote<network::mojom::URLLoaderClient> client)
Michael Thiessen7b531172023-01-28 05:25:5968 : client_(std::move(client)), request_(resource_request) {}
69
70 void DoRedirect(std::unique_ptr<GURL> url) {
Michael Thiessen1a49e4d52022-12-02 21:54:4071 net::HttpStatusCode response_code = net::HTTP_TEMPORARY_REDIRECT;
72 auto response_head = network::mojom::URLResponseHead::New();
73 response_head->encoded_data_length = 0;
74 response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
75 net::HttpUtil::AssembleRawHeaders("HTTP/1.1 307 Temporary Redirect"));
76
77 // Avoid a round-trip to the network service by pre-parsing headers.
78 // This doesn't violate: `docs/security/rule-of-2.md`, because the input is
79 // trusted, before appending the Location: <url> header.
80 response_head->parsed_headers =
Michael Thiessen7b531172023-01-28 05:25:5981 network::PopulateParsedHeaders(response_head->headers.get(), *url);
Michael Thiessen1a49e4d52022-12-02 21:54:4082
Michael Thiessen7b531172023-01-28 05:25:5983 response_head->headers->AddHeader("Location", url->spec());
Michael Thiessen1a49e4d52022-12-02 21:54:4084
85 auto first_party_url_policy =
Michael Thiessen7b531172023-01-28 05:25:5986 request_.update_first_party_url_on_redirect
Michael Thiessen1a49e4d52022-12-02 21:54:4087 ? net::RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT
88 : net::RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
89
90 client_->OnReceiveRedirect(
91 net::RedirectInfo::ComputeRedirectInfo(
Michael Thiessen7b531172023-01-28 05:25:5992 request_.method, request_.url, request_.site_for_cookies,
93 first_party_url_policy, request_.referrer_policy,
Arthur Sonzognic571efb2024-01-26 20:26:1894 request_.referrer.spec(), response_code, *url, std::nullopt,
Michael Thiessen1a49e4d52022-12-02 21:54:4095 /*insecure_scheme_was_upgraded=*/false,
96 /*copy_fragment=*/false),
97 std::move(response_head));
98 }
99
Michael Thiessen7b531172023-01-28 05:25:59100 void OnNonRedirectAsyncAction() {
101 client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED));
102 }
103
Michael Thiessen1a49e4d52022-12-02 21:54:40104 RedirectURLLoader(const RedirectURLLoader&) = delete;
105 RedirectURLLoader& operator=(const RedirectURLLoader&) = delete;
106
107 ~RedirectURLLoader() override = default;
108
109 private:
110 // network::mojom::URLLoader overrides:
111 void FollowRedirect(
112 const std::vector<std::string>& removed_headers,
113 const net::HttpRequestHeaders& modified_headers,
114 const net::HttpRequestHeaders& modified_cors_exempt_headers,
Arthur Sonzognic571efb2024-01-26 20:26:18115 const std::optional<GURL>& new_url) override {
Peter Boströmaaf19db2024-05-14 22:08:09116 NOTREACHED_IN_MIGRATION();
Michael Thiessen1a49e4d52022-12-02 21:54:40117 }
118 void SetPriority(net::RequestPriority priority,
119 int intra_priority_value) override {}
120 void PauseReadingBodyFromNet() override {}
121 void ResumeReadingBodyFromNet() override {}
122
123 mojo::Remote<network::mojom::URLLoaderClient> client_;
Michael Thiessen7b531172023-01-28 05:25:59124 network::ResourceRequest request_;
Michael Thiessen1a49e4d52022-12-02 21:54:40125};
126
[email protected]a8e69a742013-10-15 10:58:55127} // namespace
[email protected]4360ae72012-10-09 22:10:46128
129// static
130void InterceptNavigationDelegate::Associate(
131 WebContents* web_contents,
dcheng84c358e2016-04-26 07:05:53132 std::unique_ptr<InterceptNavigationDelegate> delegate) {
[email protected]4360ae72012-10-09 22:10:46133 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
avi8945fc92017-05-02 16:03:23134 std::move(delegate));
[email protected]4360ae72012-10-09 22:10:46135}
136
137// static
138InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
139 WebContents* web_contents) {
dtrainor037df0d2014-10-08 18:05:24140 return static_cast<InterceptNavigationDelegate*>(
[email protected]4360ae72012-10-09 22:10:46141 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
142}
143
144// static
dcheng84c358e2016-04-26 07:05:53145std::unique_ptr<content::NavigationThrottle>
David Bokan2a48f7bb2021-07-09 13:21:36146InterceptNavigationDelegate::MaybeCreateThrottleFor(
Charlie Harrison3286ab72019-02-13 20:13:30147 content::NavigationHandle* handle,
148 navigation_interception::SynchronyMode mode) {
David Bokan2a48f7bb2021-07-09 13:21:36149 // Navigations in a subframe or non-primary frame tree should not be
150 // intercepted. As examples of a non-primary frame tree, a navigation
151 // occurring in a Portal element or an unactivated prerendering page should
152 // not launch an app.
153 // TODO(bokan): This is a bit of a stopgap approach since we won't run
154 // throttles again when the prerender is activated which means links that are
155 // prerendered will avoid launching an app intent that a regular navigation
156 // would have. Longer term we'll want prerender activation to check for app
157 // intents, or have this throttle cancel the prerender if an intent would
158 // have been launched (without launching the intent). It's also not clear
159 // what the right behavior for <portal> elements is.
160 // https://crbug.com/1227659.
161 if (!handle->IsInPrimaryMainFrame())
162 return nullptr;
163
Gyuyoung Kimcb7965e2018-01-25 00:39:01164 return std::make_unique<InterceptNavigationThrottle>(
Ken Rockotae24ce92019-12-19 20:00:25165 handle, base::BindRepeating(&CheckIfShouldIgnoreNavigationOnUIThread),
166 mode);
[email protected]4360ae72012-10-09 22:10:46167}
168
169InterceptNavigationDelegate::InterceptNavigationDelegate(
Colin Blundell4695e8142020-03-16 11:13:12170 JNIEnv* env,
Andrew Grievedf3420a4e2024-08-30 21:02:48171 const jni_zero::JavaRef<jobject>& jdelegate,
Colin Blundell4695e8142020-03-16 11:13:12172 bool escape_external_handler_value)
173 : weak_jdelegate_(env, jdelegate),
174 escape_external_handler_value_(escape_external_handler_value) {}
[email protected]4360ae72012-10-09 22:10:46175
Michael Thiessen1a49e4d52022-12-02 21:54:40176InterceptNavigationDelegate::~InterceptNavigationDelegate() = default;
[email protected]4360ae72012-10-09 22:10:46177
178bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
Michael Thiessenca245a382022-02-21 16:11:17179 content::NavigationHandle* navigation_handle) {
180 GURL escaped_url = escape_external_handler_value_
Ryan Hamilton7f3bd3d2022-04-23 00:07:39181 ? GURL(base::EscapeExternalHandlerValue(
Michael Thiessenca245a382022-02-21 16:11:17182 navigation_handle->GetURL().spec()))
183 : navigation_handle->GetURL();
Colin Blundell4695e8142020-03-16 11:13:12184
Michael Thiessenca245a382022-02-21 16:11:17185 if (!escaped_url.is_valid())
[email protected]4360ae72012-10-09 22:10:46186 return false;
187
188 JNIEnv* env = base::android::AttachCurrentThread();
189 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
190
191 if (jdelegate.is_null())
192 return false;
193
Michael Thiessen41821c982023-08-14 21:45:54194 bool hidden_cross_frame = false;
Michael Thiessen884e6172023-02-13 17:50:31195 // Only main frame navigations use this path, so we only need to check if the
196 // navigation is cross-frame to the main frame.
Michael Thiessen41821c982023-08-14 21:45:54197 if (navigation_handle->GetInitiatorFrameToken() &&
198 navigation_handle->GetInitiatorFrameToken() !=
199 navigation_handle->GetWebContents()
200 ->GetPrimaryMainFrame()
201 ->GetFrameToken()) {
202 content::RenderFrameHost* initiator_frame_host =
203 content::RenderFrameHost::FromFrameToken(
Dave Tapuskae9b7c0f72023-11-06 16:38:01204 content::GlobalRenderFrameHostToken(
205 navigation_handle->GetInitiatorProcessId(),
206 navigation_handle->GetInitiatorFrameToken().value()));
Michael Thiessen41821c982023-08-14 21:45:54207 // If the initiator is gone treat it as not visible.
208 hidden_cross_frame =
209 !initiator_frame_host || initiator_frame_host->GetVisibilityState() !=
210 content::PageVisibilityState::kVisible;
211 }
Michael Thiessen884e6172023-02-13 17:50:31212
Michael Thiessen8fd3520b2023-03-10 20:55:47213 // We don't care which sandbox flags are present, only that any sandbox flags
214 // are present, as we don't support persisting sandbox flags through fallback
215 // URL navigation.
216 bool is_sandboxed = navigation_handle->SandboxFlagsInherited() !=
Michael Thiessend80a1af12023-08-21 17:17:35217 network::mojom::WebSandboxFlags::kNone ||
218 navigation_handle->SandboxFlagsInitiator() !=
219 network::mojom::WebSandboxFlags::kNone;
Arthur Sonzognidc8508ab2023-08-17 08:42:42220
[email protected]4360ae72012-10-09 22:10:46221 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
Michael Thiessenca245a382022-02-21 16:11:17222 env, jdelegate, navigation_handle->GetJavaNavigationHandle(),
Michael Thiessen41821c982023-08-14 21:45:54223 url::GURLAndroid::FromNativeGURL(env, escaped_url), hidden_cross_frame,
Michael Thiessen8fd3520b2023-03-10 20:55:47224 is_sandboxed);
Michael Thiessenca245a382022-02-21 16:11:17225}
226
Michael Thiessen7cb129e2022-11-08 17:24:51227void InterceptNavigationDelegate::HandleSubframeExternalProtocol(
Michael Thiessenca245a382022-02-21 16:11:17228 const GURL& url,
229 ui::PageTransition page_transition,
230 bool has_user_gesture,
Arthur Sonzognic571efb2024-01-26 20:26:18231 const std::optional<url::Origin>& initiating_origin,
Michael Thiessen1a49e4d52022-12-02 21:54:40232 mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory) {
Michael Thiessen7b531172023-01-28 05:25:59233 // If there's a pending async subframe action, don't consider external
234 // navigation for the current navigation.
235 if (subframe_redirect_url_ || url_loader_) {
236 return;
237 }
238
Michael Thiessenca245a382022-02-21 16:11:17239 GURL escaped_url = escape_external_handler_value_
Ryan Hamilton7f3bd3d2022-04-23 00:07:39240 ? GURL(base::EscapeExternalHandlerValue(url.spec()))
Michael Thiessenca245a382022-02-21 16:11:17241 : url;
242 if (!escaped_url.is_valid())
243 return;
244
245 JNIEnv* env = base::android::AttachCurrentThread();
246 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
247
248 if (jdelegate.is_null())
249 return;
Michael Thiessen1a49e4d52022-12-02 21:54:40250 ScopedJavaLocalRef<jobject> j_gurl =
251 Java_InterceptNavigationDelegate_handleSubframeExternalProtocol(
252 env, jdelegate, url::GURLAndroid::FromNativeGURL(env, escaped_url),
253 page_transition, has_user_gesture,
Andrew Grieve5cec6392023-09-06 14:46:01254 initiating_origin ? initiating_origin->ToJavaObject() : nullptr);
Michael Thiessen1a49e4d52022-12-02 21:54:40255 if (j_gurl.is_null())
256 return;
Andrew Grieve7912fd32024-04-15 17:23:05257 subframe_redirect_url_ =
258 std::make_unique<GURL>(url::GURLAndroid::ToNativeGURL(env, j_gurl));
Michael Thiessen1a49e4d52022-12-02 21:54:40259
260 mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver =
261 out_factory->InitWithNewPipeAndPassReceiver();
262 scoped_refptr<network::SharedURLLoaderFactory> loader_factory =
263 base::MakeRefCounted<network::SingleRequestURLLoaderFactory>(
Michael Thiessen7b531172023-01-28 05:25:59264 base::BindOnce(&InterceptNavigationDelegate::LoaderCallback,
265 weak_ptr_factory_.GetWeakPtr()));
Michael Thiessen1a49e4d52022-12-02 21:54:40266 loader_factory->Clone(std::move(receiver));
[email protected]4360ae72012-10-09 22:10:46267}
268
Michael Thiessen7b531172023-01-28 05:25:59269void InterceptNavigationDelegate::LoaderCallback(
270 const network::ResourceRequest& resource_request,
271 mojo::PendingReceiver<network::mojom::URLLoader> pending_receiver,
272 mojo::PendingRemote<network::mojom::URLLoaderClient> pending_client) {
273 url_loader_ = mojo::MakeSelfOwnedReceiver(
274 std::make_unique<RedirectURLLoader>(resource_request,
275 std::move(pending_client)),
276 std::move(pending_receiver));
277 MaybeHandleSubframeAction();
278}
279
280void InterceptNavigationDelegate::MaybeHandleSubframeAction() {
281 // An empty subframe_redirect_url_ implies a pending async action.
282 if (!url_loader_ ||
283 (subframe_redirect_url_ && subframe_redirect_url_->is_empty())) {
284 return;
285 }
286 RedirectURLLoader* loader =
287 static_cast<RedirectURLLoader*>(url_loader_->impl());
288 if (!subframe_redirect_url_) {
289 loader->OnNonRedirectAsyncAction();
290 } else {
291 loader->DoRedirect(std::move(subframe_redirect_url_));
292 }
293 url_loader_.reset();
294}
295
Michael Thiessen332dadb62022-07-13 14:44:07296void InterceptNavigationDelegate::OnResourceRequestWithGesture() {
297 JNIEnv* env = base::android::AttachCurrentThread();
298 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
299 if (jdelegate.is_null())
300 return;
301 Java_InterceptNavigationDelegate_onResourceRequestWithGesture(env, jdelegate);
Michael Thiessene5663522022-05-25 21:23:28302}
303
Michael Thiessen7b531172023-01-28 05:25:59304void InterceptNavigationDelegate::OnSubframeAsyncActionTaken(
305 JNIEnv* env,
306 const base::android::JavaParamRef<jobject>& j_gurl) {
307 // subframe_redirect_url_ no longer empty indicates the async action has been
308 // taken.
309 subframe_redirect_url_ =
Andrew Grieve7912fd32024-04-15 17:23:05310 j_gurl.is_null()
311 ? nullptr
312 : std::make_unique<GURL>(url::GURLAndroid::ToNativeGURL(env, j_gurl));
Michael Thiessen7b531172023-01-28 05:25:59313 MaybeHandleSubframeAction();
314}
315
[email protected]8812e3d02013-05-22 12:38:53316} // namespace navigation_interception