123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- // Copyright 2016 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <windows.h>
- #include <wininet.h>
- #include "base/bind.h"
- #include "base/files/file_path.h"
- #include "base/files/file_util.h"
- #include "base/files/scoped_temp_dir.h"
- #include "base/run_loop.h"
- #include "base/strings/stringprintf.h"
- #include "base/strings/utf_string_conversions.h"
- #include "base/test/task_environment.h"
- #include "base/test/test_file_util.h"
- #include "base/test/test_reg_util_win.h"
- #include "base/win/scoped_com_initializer.h"
- #include "base/win/win_util.h"
- #include "base/win/windows_version.h"
- #include "components/services/quarantine/quarantine.h"
- #include "components/services/quarantine/test_support.h"
- #include "net/base/filename_util.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "url/gurl.h"
- namespace quarantine {
- namespace {
- const char kDummySourceUrl[] = "https://example.com/foo";
- const char kDummyReferrerUrl[] = "https://example.com/referrer";
- const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7";
- const char* const kUntrustedURLs[] = {
- "http://example.com/foo",
- "https://example.com/foo",
- "ftp://example.com/foo",
- "ftp://example.com:2121/foo",
- "data:text/plain,Hello%20world",
- "blob:https://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6",
- "about:internet",
- ""};
- // Creates a non-empty file at |file_path|.
- bool CreateFile(const base::FilePath& file_path) {
- constexpr char kTestData[] = "Hello world!";
- return base::WriteFile(file_path, kTestData, std::size(kTestData)) ==
- static_cast<int>(std::size(kTestData));
- }
- base::FilePath GetZoneIdentifierStreamPath(const base::FilePath& file_path) {
- const base::FilePath::CharType kMotwStreamSuffix[] =
- FILE_PATH_LITERAL(":Zone.Identifier");
- return base::FilePath(file_path.value() + kMotwStreamSuffix);
- }
- // Reads the Zone.Identifier alternate data stream from |file_path| into
- // |contents|.
- bool GetZoneIdentifierStreamContents(const base::FilePath& file_path,
- std::string* contents) {
- DCHECK(contents);
- return base::ReadFileToString(GetZoneIdentifierStreamPath(file_path),
- contents);
- }
- // Maps a domain and protocol to a zone.
- class ScopedZoneForSite {
- public:
- enum ZoneIdentifierType : DWORD {
- kMyComputer = 0,
- kLocalIntranetZone = 1,
- kTrustedSitesZone = 2,
- kInternetZone = 3,
- kRestrictedSitesZone = 4,
- };
- ScopedZoneForSite(const std::wstring& domain,
- const std::wstring& protocol,
- ZoneIdentifierType zone_identifier_type);
- ScopedZoneForSite(const ScopedZoneForSite&) = delete;
- ScopedZoneForSite& operator=(const ScopedZoneForSite&) = delete;
- ~ScopedZoneForSite();
- private:
- std::wstring domain_;
- std::wstring protocol_;
- };
- ScopedZoneForSite::ScopedZoneForSite(const std::wstring& domain,
- const std::wstring& protocol,
- ZoneIdentifierType zone_identifier_type)
- : domain_(domain), protocol_(protocol) {
- std::wstring registry_path = base::StringPrintf(
- L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet "
- L"Settings\\ZoneMap\\Domains\\%ls",
- domain_.c_str());
- base::win::RegKey registry_key(HKEY_CURRENT_USER, registry_path.c_str(),
- KEY_SET_VALUE);
- EXPECT_EQ(registry_key.WriteValue(protocol_.c_str(), zone_identifier_type),
- ERROR_SUCCESS);
- }
- ScopedZoneForSite::~ScopedZoneForSite() {
- std::wstring registry_path = base::StringPrintf(
- L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet "
- L"Settings\\ZoneMap\\Domains\\%ls",
- domain_.c_str());
- base::win::RegKey registry_key(HKEY_CURRENT_USER, registry_path.c_str(),
- KEY_SET_VALUE);
- registry_key.DeleteValue(protocol_.c_str());
- }
- // Sets the internet Zone.Identifier alternate data stream for |file_path|.
- bool AddInternetZoneIdentifierDirectly(const base::FilePath& file_path) {
- static const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n";
- return base::WriteFile(GetZoneIdentifierStreamPath(file_path),
- kMotwForInternetZone,
- std::size(kMotwForInternetZone)) ==
- static_cast<int>(std::size(kMotwForInternetZone));
- }
- void CheckQuarantineResult(QuarantineFileResult result,
- QuarantineFileResult expected_result) {
- EXPECT_EQ(expected_result, result);
- }
- } // namespace
- class QuarantineWinTest : public ::testing::Test {
- public:
- QuarantineWinTest() = default;
- QuarantineWinTest(const QuarantineWinTest&) = delete;
- QuarantineWinTest& operator=(const QuarantineWinTest&) = delete;
- ~QuarantineWinTest() override = default;
- void SetUp() override {
- ASSERT_NO_FATAL_FAILURE(
- registry_override_.OverrideRegistry(HKEY_CURRENT_USER));
- ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
- scoped_zone_for_trusted_site_ = std::make_unique<ScopedZoneForSite>(
- GetTrustedSite(), L"https",
- ScopedZoneForSite::ZoneIdentifierType::kTrustedSitesZone);
- scoped_zone_for_restricted_site_ = std::make_unique<ScopedZoneForSite>(
- GetRestrictedSite(), L"https",
- ScopedZoneForSite::ZoneIdentifierType::kRestrictedSitesZone);
- scoped_zone_for_internet_site_ = std::make_unique<ScopedZoneForSite>(
- GetInternetSite(), L"https",
- ScopedZoneForSite::ZoneIdentifierType::kInternetZone);
- }
- base::FilePath GetTempDir() { return scoped_temp_dir_.GetPath(); }
- const wchar_t* GetTrustedSite() { return L"thisisatrustedsite.com"; }
- const wchar_t* GetRestrictedSite() { return L"thisisarestrictedsite.com"; }
- const wchar_t* GetInternetSite() { return L"example.com"; }
- private:
- base::test::SingleThreadTaskEnvironment task_environment_;
- registry_util::RegistryOverrideManager registry_override_;
- base::ScopedTempDir scoped_temp_dir_;
- base::win::ScopedCOMInitializer com_initializer_{
- base::win::ScopedCOMInitializer::Uninitialization::kBlockPremature};
- // Due to caching, these sites zone must be set for all tests, so that the
- // order the tests are run does not matter.
- std::unique_ptr<ScopedZoneForSite> scoped_zone_for_trusted_site_;
- std::unique_ptr<ScopedZoneForSite> scoped_zone_for_internet_site_;
- std::unique_ptr<ScopedZoneForSite> scoped_zone_for_restricted_site_;
- };
- // If the file is missing, the QuarantineFile() call should return FILE_MISSING.
- TEST_F(QuarantineWinTest, MissingFile) {
- QuarantineFile(GetTempDir().AppendASCII("does-not-exist.exe"),
- GURL(kDummySourceUrl), GURL(kDummyReferrerUrl),
- kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult,
- QuarantineFileResult::FILE_MISSING));
- base::RunLoop().RunUntilIdle();
- }
- // On Windows systems, files downloaded from a local source are considered
- // trustworthy. Hence they aren't annotated with source information. This test
- // verifies this behavior since the other tests in this suite would pass with a
- // false positive if local files are being annotated with the MOTW for the
- // internet zone.
- TEST_F(QuarantineWinTest, LocalFile_DependsOnLocalConfig) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- const char* const kLocalSourceURLs[] = {"http://localhost/foo",
- "file:///C:/some-local-dir/foo.exe"};
- for (const char* source_url : kLocalSourceURLs) {
- SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, GURL(source_url), GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- GetZoneIdentifierStreamContents(test_file, &zone_identifier);
- // No zone identifier for local source.
- EXPECT_TRUE(zone_identifier.empty());
- ASSERT_TRUE(base::DeleteFile(test_file));
- }
- }
- // A file downloaded from the internet should be annotated with .. something.
- // The specific zone assigned to our dummy source URL depends on the local
- // configuration. But no sane configuration should be treating the dummy URL as
- // a trusted source for anything.
- TEST_F(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- for (const char* source_url : kUntrustedURLs) {
- SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, GURL(source_url), GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there
- // is a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- ASSERT_TRUE(base::DeleteFile(test_file));
- }
- }
- TEST_F(QuarantineWinTest, UnsafeReferrer_DependsOnLocalConfig) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs),
- std::end(kUntrustedURLs));
- // Add one more test case.
- std::string huge_referrer = "http://example.com/";
- huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
- unsafe_referrers.push_back(huge_referrer);
- for (const auto& referrer_url : unsafe_referrers) {
- SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url);
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, GURL("http://example.com/good"), GURL(referrer_url),
- kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there
- // is a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- ASSERT_TRUE(base::DeleteFile(test_file));
- }
- }
- // An empty source URL should result in a file that's treated the same as one
- // downloaded from the internet.
- TEST_F(QuarantineWinTest, EmptySource_DependsOnLocalConfig) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, GURL(), GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there is
- // a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- }
- // Empty files aren't passed to AVScanFile. They are instead marked manually. If
- // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to
- // it and the test would fail.
- TEST_F(QuarantineWinTest, EmptyFile) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_EQ(0, base::WriteFile(test_file, "", 0u));
- QuarantineFile(
- test_file, net::FilePathToFileURL(test_file), GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there is
- // a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- }
- // If there is no client GUID supplied to the QuarantineFile() call, then rather
- // than invoking AVScanFile, the MOTW will be applied manually. If the file is
- // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the
- // test would fail.
- TEST_F(QuarantineWinTest, NoClientGuid) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, net::FilePathToFileURL(test_file), GURL(), std::string(),
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there is
- // a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- }
- // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a
- // URL, when used as a source URL shouldn't break QuarantineFile() which should
- // mark the file as being from the internet zone as a safe fallback.
- TEST_F(QuarantineWinTest, SuperLongURL) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_TRUE(CreateFile(test_file));
- std::string source_url("http://example.com/");
- source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
- QuarantineFile(
- test_file, GURL(source_url), GURL(), std::string(),
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- ASSERT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- // The actual assigned zone could be anything and the contents of the zone
- // identifier depends on the version of Windows. So only testing that there is
- // a zone annotation.
- EXPECT_FALSE(zone_identifier.empty());
- }
- TEST_F(QuarantineWinTest, TrustedSite) {
- // Test file path and source URL.
- base::FilePath test_file = GetTempDir().AppendASCII("good.exe");
- GURL source_url = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/good.exe", GetTrustedSite())));
- ASSERT_TRUE(CreateFile(test_file));
- QuarantineFile(
- test_file, source_url, GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- // No zone identifier.
- std::string zone_identifier;
- EXPECT_FALSE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- }
- TEST_F(QuarantineWinTest, RestrictedSite) {
- // Test file path and source URL.
- base::FilePath test_file = GetTempDir().AppendASCII("bad.exe");
- GURL source_url = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/bad.exe", GetRestrictedSite())));
- ASSERT_TRUE(CreateFile(test_file));
- // Files from a restricted site are deleted.
- QuarantineFile(test_file, source_url, GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult,
- QuarantineFileResult::BLOCKED_BY_POLICY));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- EXPECT_FALSE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- }
- TEST_F(QuarantineWinTest, TrustedSite_AlreadyQuarantined) {
- // Test file path and source URL.
- base::FilePath test_file = GetTempDir().AppendASCII("good.exe");
- GURL source_url = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/good.exe", GetTrustedSite())));
- ASSERT_TRUE(CreateFile(test_file));
- // Ensure the file already contains a zone identifier.
- ASSERT_TRUE(AddInternetZoneIdentifierDirectly(test_file));
- QuarantineFile(
- test_file, source_url, GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- // The existing zone identifier was not removed.
- std::string zone_identifier;
- EXPECT_TRUE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- EXPECT_FALSE(zone_identifier.empty());
- }
- TEST_F(QuarantineWinTest, RestrictedSite_AlreadyQuarantined) {
- // Test file path and source URL.
- base::FilePath test_file = GetTempDir().AppendASCII("bad.exe");
- GURL source_url = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/bad.exe", GetRestrictedSite())));
- ASSERT_TRUE(CreateFile(test_file));
- // Ensure the file already contains a zone identifier.
- ASSERT_TRUE(AddInternetZoneIdentifierDirectly(test_file));
- // Files from a restricted site are deleted.
- QuarantineFile(test_file, source_url, GURL(), kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult,
- QuarantineFileResult::BLOCKED_BY_POLICY));
- base::RunLoop().RunUntilIdle();
- std::string zone_identifier;
- EXPECT_FALSE(GetZoneIdentifierStreamContents(test_file, &zone_identifier));
- }
- TEST_F(QuarantineWinTest, MetaData_ApplyMOTW_Directly) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_TRUE(CreateFile(test_file));
- GURL host_url = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://user:pass@%ls/folder/foo.exe?x#y", GetInternetSite())));
- GURL host_url_clean = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://%ls/folder/foo.exe?x#y", GetInternetSite())));
- GURL referrer_url = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://user:pass@%ls/folder/index?x#y", GetInternetSite())));
- GURL referrer_url_clean = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/index?x#y", GetInternetSite())));
- // An invalid GUID will cause QuarantineFile() to apply the MOTW directly.
- QuarantineFile(
- test_file, host_url, referrer_url, std::string(),
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(IsFileQuarantined(test_file, host_url_clean, referrer_url_clean));
- }
- TEST_F(QuarantineWinTest, MetaData_InvokeAS) {
- base::FilePath test_file = GetTempDir().AppendASCII("foo.exe");
- ASSERT_TRUE(CreateFile(test_file));
- GURL host_url = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://%ls/folder/foo.exe?x#y", GetInternetSite())));
- GURL host_url_clean = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://%ls/folder/foo.exe?x#y", GetInternetSite())));
- GURL referrer_url = GURL(base::WideToUTF8(base::StringPrintf(
- L"https://user:pass@%ls/folder/index?x#y", GetInternetSite())));
- GURL referrer_url_clean = GURL(base::WideToUTF8(
- base::StringPrintf(L"https://%ls/folder/index?x#y", GetInternetSite())));
- QuarantineFile(
- test_file, host_url, referrer_url, kDummyClientGuid,
- base::BindOnce(&CheckQuarantineResult, QuarantineFileResult::OK));
- base::RunLoop().RunUntilIdle();
- EXPECT_TRUE(IsFileQuarantined(test_file, host_url_clean, referrer_url_clean));
- }
- } // namespace quarantine
|