blob: 913cda39d0ecfd03e22cb11ef1ae096fe9ddb733 [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"
Mohamed Heikalbd641312019-06-22 00:14:3714#include "components/navigation_interception/jni_headers/InterceptNavigationDelegate_jni.h"
[email protected]4360ae72012-10-09 22:10:4615#include "content/public/browser/browser_thread.h"
David Bokan2a48f7bb2021-07-09 13:21:3616#include "content/public/browser/navigation_handle.h"
clamy40c9e142015-09-29 11:18:4717#include "content/public/browser/navigation_throttle.h"
jaekyun038903192015-03-31 14:15:5918#include "content/public/browser/render_frame_host.h"
[email protected]4360ae72012-10-09 22:10:4619#include "content/public/browser/render_view_host.h"
[email protected]4360ae72012-10-09 22:10:4620#include "content/public/browser/web_contents.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
33using base::android::ConvertUTF8ToJavaString;
34using base::android::ScopedJavaLocalRef;
35using content::BrowserThread;
[email protected]4360ae72012-10-09 22:10:4636using content::RenderViewHost;
37using content::WebContents;
Michael Thiessenca245a382022-02-21 16:11:1738using ui::PageTransition;
[email protected]4360ae72012-10-09 22:10:4639
[email protected]8812e3d02013-05-22 12:38:5340namespace navigation_interception {
[email protected]4360ae72012-10-09 22:10:4641
42namespace {
43
thestig3b6a2f12015-09-25 08:17:2044const void* const kInterceptNavigationDelegateUserDataKey =
[email protected]4360ae72012-10-09 22:10:4645 &kInterceptNavigationDelegateUserDataKey;
46
Michael Thiessenca245a382022-02-21 16:11:1747bool CheckIfShouldIgnoreNavigationOnUIThread(
48 content::NavigationHandle* navigation_handle) {
mostynbad1e8c962015-03-25 21:51:1249 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Michael Thiessenca245a382022-02-21 16:11:1750 DCHECK(navigation_handle);
[email protected]4360ae72012-10-09 22:10:4651
[email protected]4360ae72012-10-09 22:10:4652 InterceptNavigationDelegate* intercept_navigation_delegate =
Michael Thiessenca245a382022-02-21 16:11:1753 InterceptNavigationDelegate::Get(navigation_handle->GetWebContents());
[email protected]4360ae72012-10-09 22:10:4654 if (!intercept_navigation_delegate)
55 return false;
56
Michael Thiessenca245a382022-02-21 16:11:1757 return intercept_navigation_delegate->ShouldIgnoreNavigation(
58 navigation_handle);
[email protected]4360ae72012-10-09 22:10:4659}
60
Michael Thiessen1a49e4d52022-12-02 21:54:4061class RedirectURLLoader : public network::mojom::URLLoader {
62 public:
Michael Thiessen7b531172023-01-28 05:25:5963 RedirectURLLoader(const network::ResourceRequest& resource_request,
Michael Thiessen1a49e4d52022-12-02 21:54:4064 mojo::PendingRemote<network::mojom::URLLoaderClient> client)
Michael Thiessen7b531172023-01-28 05:25:5965 : client_(std::move(client)), request_(resource_request) {}
66
67 void DoRedirect(std::unique_ptr<GURL> url) {
Michael Thiessen1a49e4d52022-12-02 21:54:4068 net::HttpStatusCode response_code = net::HTTP_TEMPORARY_REDIRECT;
69 auto response_head = network::mojom::URLResponseHead::New();
70 response_head->encoded_data_length = 0;
71 response_head->headers = base::MakeRefCounted<net::HttpResponseHeaders>(
72 net::HttpUtil::AssembleRawHeaders("HTTP/1.1 307 Temporary Redirect"));
73
74 // Avoid a round-trip to the network service by pre-parsing headers.
75 // This doesn't violate: `docs/security/rule-of-2.md`, because the input is
76 // trusted, before appending the Location: <url> header.
77 response_head->parsed_headers =
Michael Thiessen7b531172023-01-28 05:25:5978 network::PopulateParsedHeaders(response_head->headers.get(), *url);
Michael Thiessen1a49e4d52022-12-02 21:54:4079
Michael Thiessen7b531172023-01-28 05:25:5980 response_head->headers->AddHeader("Location", url->spec());
Michael Thiessen1a49e4d52022-12-02 21:54:4081
82 auto first_party_url_policy =
Michael Thiessen7b531172023-01-28 05:25:5983 request_.update_first_party_url_on_redirect
Michael Thiessen1a49e4d52022-12-02 21:54:4084 ? net::RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT
85 : net::RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
86
87 client_->OnReceiveRedirect(
88 net::RedirectInfo::ComputeRedirectInfo(
Michael Thiessen7b531172023-01-28 05:25:5989 request_.method, request_.url, request_.site_for_cookies,
90 first_party_url_policy, request_.referrer_policy,
91 request_.referrer.spec(), response_code, *url, absl::nullopt,
Michael Thiessen1a49e4d52022-12-02 21:54:4092 /*insecure_scheme_was_upgraded=*/false,
93 /*copy_fragment=*/false),
94 std::move(response_head));
95 }
96
Michael Thiessen7b531172023-01-28 05:25:5997 void OnNonRedirectAsyncAction() {
98 client_->OnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED));
99 }
100
Michael Thiessen1a49e4d52022-12-02 21:54:40101 RedirectURLLoader(const RedirectURLLoader&) = delete;
102 RedirectURLLoader& operator=(const RedirectURLLoader&) = delete;
103
104 ~RedirectURLLoader() override = default;
105
106 private:
107 // network::mojom::URLLoader overrides:
108 void FollowRedirect(
109 const std::vector<std::string>& removed_headers,
110 const net::HttpRequestHeaders& modified_headers,
111 const net::HttpRequestHeaders& modified_cors_exempt_headers,
112 const absl::optional<GURL>& new_url) override {
113 NOTREACHED();
114 }
115 void SetPriority(net::RequestPriority priority,
116 int intra_priority_value) override {}
117 void PauseReadingBodyFromNet() override {}
118 void ResumeReadingBodyFromNet() override {}
119
120 mojo::Remote<network::mojom::URLLoaderClient> client_;
Michael Thiessen7b531172023-01-28 05:25:59121 network::ResourceRequest request_;
Michael Thiessen1a49e4d52022-12-02 21:54:40122};
123
[email protected]a8e69a742013-10-15 10:58:55124} // namespace
[email protected]4360ae72012-10-09 22:10:46125
126// static
127void InterceptNavigationDelegate::Associate(
128 WebContents* web_contents,
dcheng84c358e2016-04-26 07:05:53129 std::unique_ptr<InterceptNavigationDelegate> delegate) {
[email protected]4360ae72012-10-09 22:10:46130 web_contents->SetUserData(kInterceptNavigationDelegateUserDataKey,
avi8945fc92017-05-02 16:03:23131 std::move(delegate));
[email protected]4360ae72012-10-09 22:10:46132}
133
134// static
135InterceptNavigationDelegate* InterceptNavigationDelegate::Get(
136 WebContents* web_contents) {
dtrainor037df0d2014-10-08 18:05:24137 return static_cast<InterceptNavigationDelegate*>(
[email protected]4360ae72012-10-09 22:10:46138 web_contents->GetUserData(kInterceptNavigationDelegateUserDataKey));
139}
140
141// static
dcheng84c358e2016-04-26 07:05:53142std::unique_ptr<content::NavigationThrottle>
David Bokan2a48f7bb2021-07-09 13:21:36143InterceptNavigationDelegate::MaybeCreateThrottleFor(
Charlie Harrison3286ab72019-02-13 20:13:30144 content::NavigationHandle* handle,
145 navigation_interception::SynchronyMode mode) {
David Bokan2a48f7bb2021-07-09 13:21:36146 // Navigations in a subframe or non-primary frame tree should not be
147 // intercepted. As examples of a non-primary frame tree, a navigation
148 // occurring in a Portal element or an unactivated prerendering page should
149 // not launch an app.
150 // TODO(bokan): This is a bit of a stopgap approach since we won't run
151 // throttles again when the prerender is activated which means links that are
152 // prerendered will avoid launching an app intent that a regular navigation
153 // would have. Longer term we'll want prerender activation to check for app
154 // intents, or have this throttle cancel the prerender if an intent would
155 // have been launched (without launching the intent). It's also not clear
156 // what the right behavior for <portal> elements is.
157 // https://crbug.com/1227659.
158 if (!handle->IsInPrimaryMainFrame())
159 return nullptr;
160
Gyuyoung Kimcb7965e2018-01-25 00:39:01161 return std::make_unique<InterceptNavigationThrottle>(
Ken Rockotae24ce92019-12-19 20:00:25162 handle, base::BindRepeating(&CheckIfShouldIgnoreNavigationOnUIThread),
163 mode);
[email protected]4360ae72012-10-09 22:10:46164}
165
[email protected]4360ae72012-10-09 22:10:46166InterceptNavigationDelegate::InterceptNavigationDelegate(
Colin Blundell4695e8142020-03-16 11:13:12167 JNIEnv* env,
168 jobject jdelegate,
169 bool escape_external_handler_value)
170 : weak_jdelegate_(env, jdelegate),
171 escape_external_handler_value_(escape_external_handler_value) {}
[email protected]4360ae72012-10-09 22:10:46172
Michael Thiessen1a49e4d52022-12-02 21:54:40173InterceptNavigationDelegate::~InterceptNavigationDelegate() = default;
[email protected]4360ae72012-10-09 22:10:46174
175bool InterceptNavigationDelegate::ShouldIgnoreNavigation(
Michael Thiessenca245a382022-02-21 16:11:17176 content::NavigationHandle* navigation_handle) {
177 GURL escaped_url = escape_external_handler_value_
Ryan Hamilton7f3bd3d2022-04-23 00:07:39178 ? GURL(base::EscapeExternalHandlerValue(
Michael Thiessenca245a382022-02-21 16:11:17179 navigation_handle->GetURL().spec()))
180 : navigation_handle->GetURL();
Colin Blundell4695e8142020-03-16 11:13:12181
Michael Thiessenca245a382022-02-21 16:11:17182 if (!escaped_url.is_valid())
[email protected]4360ae72012-10-09 22:10:46183 return false;
184
185 JNIEnv* env = base::android::AttachCurrentThread();
186 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
187
188 if (jdelegate.is_null())
189 return false;
190
Michael Thiessen884e6172023-02-13 17:50:31191 // Only main frame navigations use this path, so we only need to check if the
192 // navigation is cross-frame to the main frame.
193 bool cross_frame = navigation_handle->GetInitiatorFrameToken() &&
194 navigation_handle->GetInitiatorFrameToken() !=
195 navigation_handle->GetWebContents()
196 ->GetPrimaryMainFrame()
197 ->GetFrameToken();
198
Michael Thiessen8fd3520b2023-03-10 20:55:47199 // We don't care which sandbox flags are present, only that any sandbox flags
200 // are present, as we don't support persisting sandbox flags through fallback
201 // URL navigation.
202 bool is_sandboxed = navigation_handle->SandboxFlagsInherited() !=
203 network::mojom::WebSandboxFlags::kNone;
204
[email protected]4360ae72012-10-09 22:10:46205 return Java_InterceptNavigationDelegate_shouldIgnoreNavigation(
Michael Thiessenca245a382022-02-21 16:11:17206 env, jdelegate, navigation_handle->GetJavaNavigationHandle(),
Michael Thiessen8fd3520b2023-03-10 20:55:47207 url::GURLAndroid::FromNativeGURL(env, escaped_url), cross_frame,
208 is_sandboxed);
Michael Thiessenca245a382022-02-21 16:11:17209}
210
Michael Thiessen7cb129e2022-11-08 17:24:51211void InterceptNavigationDelegate::HandleSubframeExternalProtocol(
Michael Thiessenca245a382022-02-21 16:11:17212 const GURL& url,
213 ui::PageTransition page_transition,
214 bool has_user_gesture,
Michael Thiessen1a49e4d52022-12-02 21:54:40215 const absl::optional<url::Origin>& initiating_origin,
216 mojo::PendingRemote<network::mojom::URLLoaderFactory>* out_factory) {
Michael Thiessen7b531172023-01-28 05:25:59217 // If there's a pending async subframe action, don't consider external
218 // navigation for the current navigation.
219 if (subframe_redirect_url_ || url_loader_) {
220 return;
221 }
222
Michael Thiessenca245a382022-02-21 16:11:17223 GURL escaped_url = escape_external_handler_value_
Ryan Hamilton7f3bd3d2022-04-23 00:07:39224 ? GURL(base::EscapeExternalHandlerValue(url.spec()))
Michael Thiessenca245a382022-02-21 16:11:17225 : url;
226 if (!escaped_url.is_valid())
227 return;
228
229 JNIEnv* env = base::android::AttachCurrentThread();
230 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
231
232 if (jdelegate.is_null())
233 return;
Michael Thiessen1a49e4d52022-12-02 21:54:40234 ScopedJavaLocalRef<jobject> j_gurl =
235 Java_InterceptNavigationDelegate_handleSubframeExternalProtocol(
236 env, jdelegate, url::GURLAndroid::FromNativeGURL(env, escaped_url),
237 page_transition, has_user_gesture,
238 initiating_origin ? initiating_origin->CreateJavaObject() : nullptr);
239 if (j_gurl.is_null())
240 return;
Michael Thiessen7b531172023-01-28 05:25:59241 subframe_redirect_url_ = url::GURLAndroid::ToNativeGURL(env, j_gurl);
Michael Thiessen1a49e4d52022-12-02 21:54:40242
243 mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver =
244 out_factory->InitWithNewPipeAndPassReceiver();
245 scoped_refptr<network::SharedURLLoaderFactory> loader_factory =
246 base::MakeRefCounted<network::SingleRequestURLLoaderFactory>(
Michael Thiessen7b531172023-01-28 05:25:59247 base::BindOnce(&InterceptNavigationDelegate::LoaderCallback,
248 weak_ptr_factory_.GetWeakPtr()));
Michael Thiessen1a49e4d52022-12-02 21:54:40249 loader_factory->Clone(std::move(receiver));
[email protected]4360ae72012-10-09 22:10:46250}
251
Michael Thiessen7b531172023-01-28 05:25:59252void InterceptNavigationDelegate::LoaderCallback(
253 const network::ResourceRequest& resource_request,
254 mojo::PendingReceiver<network::mojom::URLLoader> pending_receiver,
255 mojo::PendingRemote<network::mojom::URLLoaderClient> pending_client) {
256 url_loader_ = mojo::MakeSelfOwnedReceiver(
257 std::make_unique<RedirectURLLoader>(resource_request,
258 std::move(pending_client)),
259 std::move(pending_receiver));
260 MaybeHandleSubframeAction();
261}
262
263void InterceptNavigationDelegate::MaybeHandleSubframeAction() {
264 // An empty subframe_redirect_url_ implies a pending async action.
265 if (!url_loader_ ||
266 (subframe_redirect_url_ && subframe_redirect_url_->is_empty())) {
267 return;
268 }
269 RedirectURLLoader* loader =
270 static_cast<RedirectURLLoader*>(url_loader_->impl());
271 if (!subframe_redirect_url_) {
272 loader->OnNonRedirectAsyncAction();
273 } else {
274 loader->DoRedirect(std::move(subframe_redirect_url_));
275 }
276 url_loader_.reset();
277}
278
Michael Thiessen332dadb62022-07-13 14:44:07279void InterceptNavigationDelegate::OnResourceRequestWithGesture() {
280 JNIEnv* env = base::android::AttachCurrentThread();
281 ScopedJavaLocalRef<jobject> jdelegate = weak_jdelegate_.get(env);
282 if (jdelegate.is_null())
283 return;
284 Java_InterceptNavigationDelegate_onResourceRequestWithGesture(env, jdelegate);
Michael Thiessene5663522022-05-25 21:23:28285}
286
Michael Thiessen7b531172023-01-28 05:25:59287void InterceptNavigationDelegate::OnSubframeAsyncActionTaken(
288 JNIEnv* env,
289 const base::android::JavaParamRef<jobject>& j_gurl) {
290 // subframe_redirect_url_ no longer empty indicates the async action has been
291 // taken.
292 subframe_redirect_url_ =
293 j_gurl.is_null() ? nullptr : url::GURLAndroid::ToNativeGURL(env, j_gurl);
294 MaybeHandleSubframeAction();
295}
296
[email protected]8812e3d02013-05-22 12:38:53297} // namespace navigation_interception