1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SAFE_BROWSING_CONTENT_RENDERER_RENDERER_URL_LOADER_THROTTLE_H_
#define COMPONENTS_SAFE_BROWSING_CONTENT_RENDERER_RENDERER_URL_LOADER_THROTTLE_H_
#include <memory>
#include <optional>
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/types/optional_ref.h"
#include "components/safe_browsing/content/common/safe_browsing.mojom.h"
#include "components/safe_browsing/core/common/safe_browsing_url_checker.mojom.h"
#include "extensions/buildflags/buildflags.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/common/loader/url_loader_throttle.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "url/gurl.h"
namespace safe_browsing {
// RendererURLLoaderThrottle is used in renderer processes to query
// SafeBrowsing and determine whether a URL and its redirect URLs are safe to
// load. It defers response processing until all URL checks are completed;
// cancels the load if any URLs turn out to be bad.
class RendererURLLoaderThrottle : public blink::URLLoaderThrottle {
public:
// |safe_browsing| must stay alive until WillStartRequest() (if it is called)
// or the end of this object.
// |local_frame_token| is used for displaying SafeBrowsing UI when necessary.
RendererURLLoaderThrottle(
mojom::SafeBrowsing* safe_browsing,
base::optional_ref<const blink::LocalFrameToken> local_frame_token);
#if BUILDFLAG(ENABLE_EXTENSIONS)
// |extension_web_request_reporter_pending_remote| is used for sending
// extension web requests to the browser.
RendererURLLoaderThrottle(
mojom::SafeBrowsing* safe_browsing,
base::optional_ref<const blink::LocalFrameToken> local_frame_token,
mojom::ExtensionWebRequestReporter* extension_web_request_reporter);
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
~RendererURLLoaderThrottle() override;
private:
FRIEND_TEST_ALL_PREFIXES(SBRendererUrlLoaderThrottleTest,
DoesNotDeferHttpsImageUrl);
FRIEND_TEST_ALL_PREFIXES(SBRendererUrlLoaderThrottleTest,
DoesNotDeferHttpsScriptUrl);
FRIEND_TEST_ALL_PREFIXES(SBRendererUrlLoaderThrottleTest,
DoesNotDeferChromeUrl);
FRIEND_TEST_ALL_PREFIXES(SBRendererUrlLoaderThrottleTest,
DoesNotDeferIframeUrl);
// blink::URLLoaderThrottle implementation.
void DetachFromCurrentSequence() override;
void WillStartRequest(network::ResourceRequest* request,
bool* defer) override;
void WillRedirectRequest(
net::RedirectInfo* redirect_info,
const network::mojom::URLResponseHead& response_head,
bool* defer,
std::vector<std::string>* to_be_removed_headers,
net::HttpRequestHeaders* modified_headers,
net::HttpRequestHeaders* modified_cors_exempt_headers) override;
void WillProcessResponse(const GURL& response_url,
network::mojom::URLResponseHead* response_head,
bool* defer) override;
const char* NameForLoggingWillProcessResponse() override;
void OnCheckUrlResult(
bool proceed,
bool showed_interstitial);
void OnMojoDisconnect();
// TODO(crbug.com/324108312): Remove `safe_browsing_`, `frame_token_`,
// `safe_browsing_pending_remote_`, `safe_browsing_remote_` that are unused.
raw_ptr<mojom::SafeBrowsing, DanglingUntriaged> safe_browsing_;
const std::optional<blink::LocalFrameToken> frame_token_;
// These fields hold the connection to this instance's private connection to
// the Safe Browsing service if DetachFromCurrentThread has been called.
mojo::PendingRemote<mojom::SafeBrowsing> safe_browsing_pending_remote_;
mojo::Remote<mojom::SafeBrowsing> safe_browsing_remote_;
mojo::Remote<mojom::SafeBrowsingUrlChecker> url_checker_;
size_t pending_checks_ = 0;
bool blocked_ = false;
bool deferred_ = false;
GURL original_url_;
#if BUILDFLAG(ENABLE_EXTENSIONS)
// Bind the pipe created in DetachFromCurrentSequence to the current
// sequence.
void BindExtensionWebRequestReporterPipeIfDetached();
// Send web request data to the browser if the request
// originated from an extension and destination is HTTP/HTTPS scheme only.
void MaybeSendExtensionWebRequestData(network::ResourceRequest* request);
raw_ptr<mojom::ExtensionWebRequestReporter, DanglingUntriaged>
extension_web_request_reporter_;
mojo::PendingRemote<mojom::ExtensionWebRequestReporter>
extension_web_request_reporter_pending_remote_;
mojo::Remote<mojom::ExtensionWebRequestReporter>
extension_web_request_reporter_remote_;
// Tracks if the request originated from an extension, used during redirects
// to send web request data to the telemetry service.
std::string origin_extension_id_;
bool initiated_from_content_script_ = false;
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
base::WeakPtrFactory<RendererURLLoaderThrottle> weak_factory_{this};
};
} // namespace safe_browsing
#endif // COMPONENTS_SAFE_BROWSING_CONTENT_RENDERER_RENDERER_URL_LOADER_THROTTLE_H_
|