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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/safe_browsing/content/renderer/renderer_url_loader_throttle.h"
#include <string_view>
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/safe_browsing/content/common/safe_browsing.mojom.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_url_checker.mojom.h"
#include "ipc/ipc_message.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace safe_browsing {
class FakeSafeBrowsing : public mojom::SafeBrowsing {
public:
FakeSafeBrowsing() = default;
void CreateCheckerAndCheck(
const std::optional<blink::LocalFrameToken>& frame_token,
mojo::PendingReceiver<mojom::SafeBrowsingUrlChecker> receiver,
const GURL& url,
const std::string& method,
const net::HttpRequestHeaders& headers,
int32_t load_flags,
bool has_user_gesture,
bool originated_from_service_worker,
CreateCheckerAndCheckCallback callback) override {
if (should_delay_callback_) {
pending_callback_ = std::move(callback);
receiver_ = std::move(receiver);
} else {
std::move(callback).Run(/*proceed=*/true, /*show_interstitial=*/false);
}
}
void Clone(mojo::PendingReceiver<mojom::SafeBrowsing> receiver) override {
NOTREACHED();
}
void EnableDelayCallback() { should_delay_callback_ = true; }
private:
bool should_delay_callback_ = false;
mojo::PendingReceiver<mojom::SafeBrowsingUrlChecker> receiver_;
CreateCheckerAndCheckCallback pending_callback_;
};
class MockThrottleDelegate : public blink::URLLoaderThrottle::Delegate {
public:
~MockThrottleDelegate() override = default;
void CancelWithError(int error_code,
std::string_view custom_reason) override {}
void Resume() override {}
};
class SBRendererUrlLoaderThrottleTest : public ::testing::Test {
protected:
SBRendererUrlLoaderThrottleTest() : mojo_receiver_(&safe_browsing_) {
mojo_receiver_.Bind(safe_browsing_remote_.BindNewPipeAndPassReceiver());
throttle_delegate_ = std::make_unique<MockThrottleDelegate>();
throttle_ = std::make_unique<RendererURLLoaderThrottle>(
safe_browsing_remote_.get(), std::nullopt);
throttle_->set_delegate(throttle_delegate_.get());
}
network::ResourceRequest GetResourceRequest(
const GURL& url,
network::mojom::RequestDestination destination) {
network::ResourceRequest request;
request.url = url;
request.destination = destination;
return request;
}
base::test::TaskEnvironment message_loop_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
FakeSafeBrowsing safe_browsing_;
mojo::Receiver<mojom::SafeBrowsing> mojo_receiver_;
mojo::Remote<mojom::SafeBrowsing> safe_browsing_remote_;
std::unique_ptr<RendererURLLoaderThrottle> throttle_;
std::unique_ptr<MockThrottleDelegate> throttle_delegate_;
};
TEST_F(SBRendererUrlLoaderThrottleTest, DoesNotDeferHttpsImageUrl) {
safe_browsing_.EnableDelayCallback();
GURL url("https://example.com/");
bool defer = false;
network::ResourceRequest request =
GetResourceRequest(url, network::mojom::RequestDestination::kImage);
throttle_->WillStartRequest(&request, &defer);
message_loop_.RunUntilIdle();
auto response_head = network::mojom::URLResponseHead::New();
throttle_->WillProcessResponse(url, response_head.get(), &defer);
EXPECT_FALSE(defer);
}
TEST_F(SBRendererUrlLoaderThrottleTest, DoesNotDeferHttpsScriptUrl) {
safe_browsing_.EnableDelayCallback();
GURL url("https://example.com/");
bool defer = false;
network::ResourceRequest request =
GetResourceRequest(url, network::mojom::RequestDestination::kScript);
throttle_->WillStartRequest(&request, &defer);
message_loop_.RunUntilIdle();
auto response_head = network::mojom::URLResponseHead::New();
throttle_->WillProcessResponse(url, response_head.get(), &defer);
EXPECT_FALSE(defer);
}
TEST_F(SBRendererUrlLoaderThrottleTest, DoesNotDeferChromeUrl) {
GURL url("chrome://settings/");
bool defer = false;
network::ResourceRequest request =
GetResourceRequest(url, network::mojom::RequestDestination::kScript);
throttle_->WillStartRequest(&request, &defer);
auto response_head = network::mojom::URLResponseHead::New();
throttle_->WillProcessResponse(url, response_head.get(), &defer);
EXPECT_FALSE(defer);
}
TEST_F(SBRendererUrlLoaderThrottleTest, DoesNotDeferIframeUrl) {
GURL url("https://example.com/");
bool defer = false;
network::ResourceRequest request =
GetResourceRequest(url, network::mojom::RequestDestination::kIframe);
throttle_->WillStartRequest(&request, &defer);
message_loop_.RunUntilIdle();
auto response_head = network::mojom::URLResponseHead::New();
throttle_->WillProcessResponse(url, response_head.get(), &defer);
EXPECT_FALSE(defer);
}
} // namespace safe_browsing
|