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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/policy/dlp/dlp_download_observer.h"
#include <memory>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chromeos/dbus/dlp/dlp_client.h"
#include "chromeos/dbus/dlp/dlp_service.pb.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/common/download_save_item_data.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/file_access/scoped_file_access.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
using testing::_;
namespace {
constexpr char kFilePath[] = "/path/to/file";
constexpr char kTabUrl[] = "https://example.com/tab";
constexpr char kDataUrl[] = "data:text/plain;charset=UTF-8,test";
constexpr char kHttpsUrl[] = "https://example.com/https";
constexpr char kReferrerUrl[] = "https://referrer.example.com";
constexpr char kBlobUrl[] = "blob:https://example.com/uuid";
constexpr char kOriginUrl[] = "https://example.com/";
} // namespace
class DlpDownloadObserverTest : public testing::Test {
public:
void SetUp() override { chromeos::DlpClient::Get()->InitializeFake(); }
void TearDown() override { chromeos::DlpClient::Get()->Shutdown(); }
};
// Tests if we correctly request dlp file access for a data: url with the tab
// url.
TEST_F(DlpDownloadObserverTest, TestDataSchemeRewrite) {
base::MockRepeatingCallback<void(const dlp::AddFilesRequest,
chromeos::DlpClient::AddFilesCallback)>
add_files_cb;
EXPECT_CALL(
add_files_cb,
Run(testing::Property(&dlp::AddFilesRequest::add_file_requests,
testing::ElementsAre(testing::Property(
&dlp::AddFileRequest::source_url, kTabUrl))),
_))
.WillOnce(base::test::RunOnceCallback<1>(
dlp::AddFilesResponse::default_instance()));
auto* dlp_client = chromeos::DlpClient::Get()->GetTestInterface();
dlp_client->SetAddFilesMock(add_files_cb.Get());
auto key = SimpleFactoryKey(base::FilePath(), false);
DlpDownloadObserver observer(&key);
testing::NiceMock<download::MockDownloadItem> item;
base::FilePath file_path(kFilePath);
GURL data_url(kDataUrl);
GURL referrer_url(kReferrerUrl);
GURL tab_url(kTabUrl);
ON_CALL(item, IsSavePackageDownload).WillByDefault(testing::Return(false));
ON_CALL(item, GetState)
.WillByDefault(
testing::Return(download::DownloadItem::DownloadState::COMPLETE));
ON_CALL(item, GetFullPath).WillByDefault(testing::ReturnRef(file_path));
ON_CALL(item, GetURL).WillByDefault(testing::ReturnRef(data_url));
ON_CALL(item, GetReferrerUrl).WillByDefault(testing::ReturnRef(referrer_url));
ON_CALL(item, GetTabUrl).WillByDefault(testing::ReturnRef(tab_url));
observer.OnDownloadUpdated(&item);
}
// Test if we request file access with the original url in the default case.
TEST_F(DlpDownloadObserverTest, TestNoSchemeRewrite) {
base::MockRepeatingCallback<void(const dlp::AddFilesRequest,
chromeos::DlpClient::AddFilesCallback)>
add_files_cb;
EXPECT_CALL(
add_files_cb,
Run(testing::Property(&dlp::AddFilesRequest::add_file_requests,
testing::ElementsAre(testing::Property(
&dlp::AddFileRequest::source_url, kHttpsUrl))),
_))
.WillOnce(base::test::RunOnceCallback<1>(
dlp::AddFilesResponse::default_instance()));
auto* dlp_client = chromeos::DlpClient::Get()->GetTestInterface();
dlp_client->SetAddFilesMock(add_files_cb.Get());
auto key = SimpleFactoryKey(base::FilePath(), false);
DlpDownloadObserver observer(&key);
testing::NiceMock<download::MockDownloadItem> item;
base::FilePath file_path(kFilePath);
GURL https_url(kHttpsUrl);
GURL referrer_url(kReferrerUrl);
GURL tab_url(kTabUrl);
ON_CALL(item, IsSavePackageDownload).WillByDefault(testing::Return(false));
ON_CALL(item, GetState)
.WillByDefault(
testing::Return(download::DownloadItem::DownloadState::COMPLETE));
ON_CALL(item, GetFullPath).WillByDefault(testing::ReturnRef(file_path));
ON_CALL(item, GetURL).WillByDefault(testing::ReturnRef(https_url));
ON_CALL(item, GetReferrerUrl).WillByDefault(testing::ReturnRef(referrer_url));
ON_CALL(item, GetTabUrl).WillByDefault(testing::ReturnRef(tab_url));
observer.OnDownloadUpdated(&item);
}
// Test if we request the file access for a blob: url with its origin. We do
// this as the url matcher would not recognize the host in the blob: url.
TEST_F(DlpDownloadObserverTest, TestBlobSchemeRewrite) {
base::MockRepeatingCallback<void(const dlp::AddFilesRequest,
chromeos::DlpClient::AddFilesCallback)>
add_files_cb;
EXPECT_CALL(
add_files_cb,
Run(testing::Property(&dlp::AddFilesRequest::add_file_requests,
testing::ElementsAre(testing::Property(
&dlp::AddFileRequest::source_url, kOriginUrl))),
_))
.WillOnce(base::test::RunOnceCallback<1>(
dlp::AddFilesResponse::default_instance()));
auto* dlp_client = chromeos::DlpClient::Get()->GetTestInterface();
dlp_client->SetAddFilesMock(add_files_cb.Get());
auto key = SimpleFactoryKey(base::FilePath(), false);
DlpDownloadObserver observer(&key);
testing::NiceMock<download::MockDownloadItem> item;
base::FilePath file_path(kFilePath);
GURL blob_url(kBlobUrl);
GURL referrer_url(kReferrerUrl);
GURL tab_url(kTabUrl);
ON_CALL(item, IsSavePackageDownload).WillByDefault(testing::Return(false));
ON_CALL(item, GetState)
.WillByDefault(
testing::Return(download::DownloadItem::DownloadState::COMPLETE));
ON_CALL(item, GetFullPath).WillByDefault(testing::ReturnRef(file_path));
ON_CALL(item, GetURL).WillByDefault(testing::ReturnRef(blob_url));
ON_CALL(item, GetReferrerUrl).WillByDefault(testing::ReturnRef(referrer_url));
ON_CALL(item, GetTabUrl).WillByDefault(testing::ReturnRef(tab_url));
observer.OnDownloadUpdated(&item);
}
// Test if we request the file access for a data: url, while the tab is a blob:
// url, the origin of the tab is used as source.
TEST_F(DlpDownloadObserverTest, TestDataSchemeInBlobTabRewrite) {
base::MockRepeatingCallback<void(const dlp::AddFilesRequest,
chromeos::DlpClient::AddFilesCallback)>
add_files_cb;
EXPECT_CALL(
add_files_cb,
Run(testing::Property(&dlp::AddFilesRequest::add_file_requests,
testing::ElementsAre(testing::Property(
&dlp::AddFileRequest::source_url, kOriginUrl))),
_))
.WillOnce(base::test::RunOnceCallback<1>(
dlp::AddFilesResponse::default_instance()));
auto* dlp_client = chromeos::DlpClient::Get()->GetTestInterface();
dlp_client->SetAddFilesMock(add_files_cb.Get());
auto key = SimpleFactoryKey(base::FilePath(), false);
DlpDownloadObserver observer(&key);
testing::NiceMock<download::MockDownloadItem> item;
base::FilePath file_path(kFilePath);
GURL blob_url(kBlobUrl);
GURL referrer_url(kReferrerUrl);
GURL data_url(kDataUrl);
ON_CALL(item, IsSavePackageDownload).WillByDefault(testing::Return(false));
ON_CALL(item, GetState)
.WillByDefault(
testing::Return(download::DownloadItem::DownloadState::COMPLETE));
ON_CALL(item, GetFullPath).WillByDefault(testing::ReturnRef(file_path));
ON_CALL(item, GetURL).WillByDefault(testing::ReturnRef(data_url));
ON_CALL(item, GetReferrerUrl).WillByDefault(testing::ReturnRef(referrer_url));
ON_CALL(item, GetTabUrl).WillByDefault(testing::ReturnRef(blob_url));
observer.OnDownloadUpdated(&item);
}
} // namespace policy
|