Commit 4edee6ba authored by Nathan Parker's avatar Nathan Parker Committed by Commit Bot

Remove dead code in safe_browsing/db/util.cc

Left over from pre-Pver4 code.

Change-Id: I4a5845c7a216e485b710479a027ce9f742a008ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1895964Reviewed-by: default avatarDaniel Rubery <drubery@chromium.org>
Reviewed-by: default avatarNathan Parker <nparker@chromium.org>
Commit-Queue: Nathan Parker <nparker@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711947}
parent 3330f161
......@@ -289,7 +289,6 @@ source_set("unit_tests_shared") {
sources = [
"allowlist_checker_client_unittest.cc",
"database_manager_unittest.cc",
"util_unittest.cc",
"v4_get_hash_protocol_manager_unittest.cc",
"v4_protocol_manager_util_unittest.cc",
]
......
......@@ -6,39 +6,8 @@
#include <stddef.h>
#ifndef NDEBUG
#include "base/base64.h"
#endif
#include "base/environment.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/trace_event.h"
#include "components/version_info/version_info.h"
#include "crypto/sha2.h"
#include "google_apis/google_api_keys.h"
#include "net/base/escape.h"
#include "url/gurl.h"
namespace safe_browsing {
// Utility functions -----------------------------------------------------------
namespace {
bool IsKnownList(const std::string& name) {
for (size_t i = 0; i < base::size(kAllLists); ++i) {
if (!strcmp(kAllLists[i], name.c_str())) {
return true;
}
}
return false;
}
} // namespace
// ThreatMetadata ------------------------------------------------------------
ThreatMetadata::ThreatMetadata()
: threat_pattern_type(ThreatPatternType::NONE) {}
......@@ -83,207 +52,4 @@ std::unique_ptr<base::trace_event::TracedValue> ThreatMetadata::ToTracedValue()
return value;
}
// SBCachedFullHashResult ------------------------------------------------------
SBCachedFullHashResult::SBCachedFullHashResult() {}
SBCachedFullHashResult::SBCachedFullHashResult(
const base::Time& in_expire_after)
: expire_after(in_expire_after) {}
SBCachedFullHashResult::SBCachedFullHashResult(
const SBCachedFullHashResult& other) = default;
SBCachedFullHashResult::~SBCachedFullHashResult() {}
// Listnames that browser can process.
const char kMalwareList[] = "goog-malware-shavar";
const char kPhishingList[] = "goog-phish-shavar";
const char kBinUrlList[] = "goog-badbinurl-shavar";
const char kCsdWhiteList[] = "goog-csdwhite-sha256";
const char kDownloadWhiteList[] = "goog-downloadwhite-digest256";
const char kExtensionBlacklist[] = "goog-badcrxids-digestvar";
const char kIPBlacklist[] = "goog-badip-digest256";
const char kUnwantedUrlList[] = "goog-unwanted-shavar";
const char kResourceBlacklist[] = "goog-badresource-shavar";
const char* kAllLists[9] = {
kMalwareList, kPhishingList, kBinUrlList,
kCsdWhiteList, kDownloadWhiteList, kExtensionBlacklist,
kIPBlacklist, kUnwantedUrlList, kResourceBlacklist,
};
ListType GetListId(const base::StringPiece& name) {
ListType id;
if (name == kMalwareList) {
id = MALWARE;
} else if (name == kPhishingList) {
id = PHISH;
} else if (name == kBinUrlList) {
id = BINURL;
} else if (name == kCsdWhiteList) {
id = CSDWHITELIST;
} else if (name == kDownloadWhiteList) {
id = DOWNLOADWHITELIST;
} else if (name == kExtensionBlacklist) {
id = EXTENSIONBLACKLIST;
} else if (name == kIPBlacklist) {
id = IPBLACKLIST;
} else if (name == kUnwantedUrlList) {
id = UNWANTEDURL;
} else if (name == kResourceBlacklist) {
id = RESOURCEBLACKLIST;
} else {
id = INVALID;
}
return id;
}
bool GetListName(ListType list_id, std::string* list) {
switch (list_id) {
case MALWARE:
*list = kMalwareList;
break;
case PHISH:
*list = kPhishingList;
break;
case BINURL:
*list = kBinUrlList;
break;
case CSDWHITELIST:
*list = kCsdWhiteList;
break;
case DOWNLOADWHITELIST:
*list = kDownloadWhiteList;
break;
case EXTENSIONBLACKLIST:
*list = kExtensionBlacklist;
break;
case IPBLACKLIST:
*list = kIPBlacklist;
break;
case UNWANTEDURL:
*list = kUnwantedUrlList;
break;
case RESOURCEBLACKLIST:
*list = kResourceBlacklist;
break;
default:
return false;
}
DCHECK(IsKnownList(*list));
return true;
}
SBFullHash SBFullHashForString(const base::StringPiece& str) {
SBFullHash h;
crypto::SHA256HashString(str, &h.full_hash, sizeof(h.full_hash));
return h;
}
SBFullHash StringToSBFullHash(const std::string& hash_in) {
DCHECK_EQ(crypto::kSHA256Length, hash_in.size());
SBFullHash hash_out;
memcpy(hash_out.full_hash, hash_in.data(), crypto::kSHA256Length);
return hash_out;
}
std::string SBFullHashToString(const SBFullHash& hash) {
DCHECK_EQ(crypto::kSHA256Length, sizeof(hash.full_hash));
return std::string(hash.full_hash, sizeof(hash.full_hash));
}
void UrlToFullHashes(const GURL& url,
bool include_whitelist_hashes,
std::vector<SBFullHash>* full_hashes) {
// Include this function in traces because it's not cheap so it should be
// called sparingly.
TRACE_EVENT2("loading", "safe_browsing::UrlToFullHashes", "url", url.spec(),
"include_whitelist_hashes", include_whitelist_hashes);
std::string canon_host;
std::string canon_path;
std::string canon_query;
V4ProtocolManagerUtil::CanonicalizeUrl(url, &canon_host, &canon_path,
&canon_query);
std::vector<std::string> hosts;
if (url.HostIsIPAddress()) {
hosts.push_back(url.host());
} else {
V4ProtocolManagerUtil::GenerateHostVariantsToCheck(canon_host, &hosts);
}
std::vector<std::string> paths;
V4ProtocolManagerUtil::GeneratePathVariantsToCheck(canon_path, canon_query,
&paths);
for (const std::string& host : hosts) {
for (const std::string& path : paths) {
full_hashes->push_back(SBFullHashForString(host + path));
// We may have /foo as path-prefix in the whitelist which should
// also match with /foo/bar and /foo?bar. Hence, for every path
// that ends in '/' we also add the path without the slash.
if (include_whitelist_hashes && path.size() > 1 && path.back() == '/') {
full_hashes->push_back(
SBFullHashForString(host + path.substr(0, path.size() - 1)));
}
}
}
}
SafeBrowsingProtocolConfig::SafeBrowsingProtocolConfig()
: disable_auto_update(false) {}
SafeBrowsingProtocolConfig::SafeBrowsingProtocolConfig(
const SafeBrowsingProtocolConfig& other) = default;
SafeBrowsingProtocolConfig::~SafeBrowsingProtocolConfig() {}
namespace ProtocolManagerHelper {
std::string Version() {
if (version_info::GetVersionNumber().empty())
return "0.1";
else
return version_info::GetVersionNumber();
}
std::string ComposeUrl(const std::string& prefix,
const std::string& method,
const std::string& client_name,
const std::string& version,
const std::string& additional_query) {
DCHECK(!prefix.empty() && !method.empty() && !client_name.empty() &&
!version.empty());
std::string url =
base::StringPrintf("%s/%s?client=%s&appver=%s&pver=3.0", prefix.c_str(),
method.c_str(), client_name.c_str(), version.c_str());
std::string api_key = google_apis::GetAPIKey();
if (!api_key.empty()) {
base::StringAppendF(&url, "&key=%s",
net::EscapeQueryParamValue(api_key, true).c_str());
}
if (!additional_query.empty()) {
DCHECK(url.find("?") != std::string::npos);
url.append("&");
url.append(additional_query);
}
return url;
}
std::string ComposeUrl(const std::string& prefix,
const std::string& method,
const std::string& client_name,
const std::string& version,
const std::string& additional_query,
ExtendedReportingLevel reporting_level) {
std::string url =
ComposeUrl(prefix, method, client_name, version, additional_query);
url.append(base::StringPrintf("&ext=%d", reporting_level));
return url;
}
} // namespace ProtocolManagerHelper
} // namespace safe_browsing
......@@ -13,7 +13,6 @@
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/strings/string_piece.h"
......@@ -22,8 +21,6 @@
#include "components/safe_browsing/common/safe_browsing_prefs.h"
#include "components/safe_browsing/db/v4_protocol_manager_util.h"
class GURL;
namespace safe_browsing {
// Metadata that indicates what kind of URL match this is.
......@@ -79,151 +76,6 @@ struct ThreatMetadata {
std::string population_id;
};
// A truncated hash's type.
typedef uint32_t SBPrefix;
// A full hash.
union SBFullHash {
char full_hash[32];
SBPrefix prefix;
};
// Used when we get a gethash response.
struct SBFullHashResult {
SBFullHash hash;
// TODO(shess): Refactor to allow ListType here.
int list_id;
ThreatMetadata metadata;
// Used only for V4 results. The cache expire time for this result. The
// response must not be cached after this time to avoid false positives.
base::Time cache_expire_after;
};
// Caches individual response from GETHASH request.
struct SBCachedFullHashResult {
SBCachedFullHashResult();
explicit SBCachedFullHashResult(const base::Time& in_expire_after);
SBCachedFullHashResult(const SBCachedFullHashResult& other);
~SBCachedFullHashResult();
base::Time expire_after;
std::vector<SBFullHashResult> full_hashes;
};
// SafeBrowsing list names.
extern const char kMalwareList[];
extern const char kPhishingList[];
// Binary Download list name.
extern const char kBinUrlList[];
// SafeBrowsing client-side detection whitelist list name.
extern const char kCsdWhiteList[];
// SafeBrowsing download whitelist list name.
extern const char kDownloadWhiteList[];
// SafeBrowsing extension list name.
extern const char kExtensionBlacklist[];
// SafeBrowsing csd malware IP blacklist name.
extern const char kIPBlacklist[];
// SafeBrowsing unwanted URL list.
extern const char kUnwantedUrlList[];
// Blacklisted resource URLs list name.
extern const char kResourceBlacklist[];
/// This array must contain all Safe Browsing lists.
extern const char* kAllLists[9];
enum ListType {
INVALID = -1,
MALWARE = 0,
PHISH = 1,
BINURL = 2,
// Obsolete BINHASH = 3,
CSDWHITELIST = 4,
// SafeBrowsing lists are stored in pairs. Keep ListType 5
// available for a potential second list that we would store in the
// csd-whitelist store file.
DOWNLOADWHITELIST = 6,
// See above comment. Leave 7 available.
EXTENSIONBLACKLIST = 8,
// See above comment. Leave 9 available.
// Obsolete SIDEEFFECTFREEWHITELIST = 10,
// See above comment. Leave 11 available.
IPBLACKLIST = 12,
// See above comment. Leave 13 available.
UNWANTEDURL = 14,
// See above comment. Leave 15 available.
// Obsolete INCLUSIONWHITELIST = 16,
// See above comment. Leave 17 available.
// Obsolete MODULEWHITELIST = 18,
// See above comment. Leave 19 available.
RESOURCEBLACKLIST = 20,
// See above comment. Leave 21 available.
};
inline bool SBFullHashEqual(const SBFullHash& a, const SBFullHash& b) {
return !memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash));
}
inline bool SBFullHashLess(const SBFullHash& a, const SBFullHash& b) {
return memcmp(a.full_hash, b.full_hash, sizeof(a.full_hash)) < 0;
}
// Generate full hash for the given string.
SBFullHash SBFullHashForString(const base::StringPiece& str);
SBFullHash StringToSBFullHash(const std::string& hash_in);
std::string SBFullHashToString(const SBFullHash& hash_out);
// Maps a list name to ListType.
ListType GetListId(const base::StringPiece& name);
// Maps a ListId to list name. Return false if fails.
bool GetListName(ListType list_id, std::string* list);
// Generate the set of full hashes to check for |url|. If
// |include_whitelist_hashes| is true we will generate additional path-prefixes
// to match against the csd whitelist. E.g., if the path-prefix /foo is on the
// whitelist it should also match /foo/bar which is not the case for all the
// other lists. We'll also always add a pattern for the empty path.
void UrlToFullHashes(const GURL& url,
bool include_whitelist_hashes,
std::vector<SBFullHash>* full_hashes);
struct SafeBrowsingProtocolConfig {
SafeBrowsingProtocolConfig();
SafeBrowsingProtocolConfig(const SafeBrowsingProtocolConfig& other);
~SafeBrowsingProtocolConfig();
std::string client_name;
std::string url_prefix;
std::string backup_connect_error_url_prefix;
std::string backup_http_error_url_prefix;
std::string backup_network_error_url_prefix;
std::string version;
bool disable_auto_update;
};
namespace ProtocolManagerHelper {
// returns chrome version.
std::string Version();
// Composes a URL using |prefix|, |method| (e.g.: gethash, download, report).
// |client_name| and |version|. When not empty, |additional_query| is
// appended to the URL with an additional "&" in the front.
std::string ComposeUrl(const std::string& prefix,
const std::string& method,
const std::string& client_name,
const std::string& version,
const std::string& additional_query);
// Similar to above function, and appends "&ext=1" at the end of URL if
// |is_extended_reporting| is true, otherwise, appends "&ext=0".
std::string ComposeUrl(const std::string& prefix,
const std::string& method,
const std::string& client_name,
const std::string& version,
const std::string& additional_query,
ExtendedReportingLevel reporting_level);
} // namespace ProtocolManagerHelper
} // namespace safe_browsing
#endif // COMPONENTS_SAFE_BROWSING_DB_UTIL_H_
// Copyright (c) 2015 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 <stddef.h>
#include <algorithm>
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "components/safe_browsing/db/util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace safe_browsing {
TEST(SafeBrowsingDbUtilTest, UrlToFullHashes) {
std::vector<SBFullHash> results;
GURL url("http://www.evil.com/evil1/evilness.html");
UrlToFullHashes(url, false, &results);
EXPECT_EQ(6UL, results.size());
EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/"), results[0]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("evil.com/evil1/"), results[1]));
EXPECT_TRUE(SBFullHashEqual(
SBFullHashForString("evil.com/evil1/evilness.html"), results[2]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("www.evil.com/"), results[3]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("www.evil.com/evil1/"), results[4]));
EXPECT_TRUE(SBFullHashEqual(
SBFullHashForString("www.evil.com/evil1/evilness.html"), results[5]));
results.clear();
GURL url2("http://www.evil.com/evil1/evilness.html");
UrlToFullHashes(url2, true, &results);
EXPECT_EQ(8UL, results.size());
EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("evil.com/"), results[0]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("evil.com/evil1/"), results[1]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("evil.com/evil1"), results[2]));
EXPECT_TRUE(SBFullHashEqual(
SBFullHashForString("evil.com/evil1/evilness.html"), results[3]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("www.evil.com/"), results[4]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("www.evil.com/evil1/"), results[5]));
EXPECT_TRUE(
SBFullHashEqual(SBFullHashForString("www.evil.com/evil1"), results[6]));
EXPECT_TRUE(SBFullHashEqual(
SBFullHashForString("www.evil.com/evil1/evilness.html"), results[7]));
}
TEST(SafeBrowsingDbUtilTest, ListIdListNameConversion) {
std::string list_name;
EXPECT_FALSE(GetListName(INVALID, &list_name));
EXPECT_TRUE(GetListName(MALWARE, &list_name));
EXPECT_EQ(list_name, std::string(kMalwareList));
EXPECT_EQ(MALWARE, GetListId(list_name));
EXPECT_TRUE(GetListName(PHISH, &list_name));
EXPECT_EQ(list_name, std::string(kPhishingList));
EXPECT_EQ(PHISH, GetListId(list_name));
EXPECT_TRUE(GetListName(BINURL, &list_name));
EXPECT_EQ(list_name, std::string(kBinUrlList));
EXPECT_EQ(BINURL, GetListId(list_name));
}
// Since the ids are saved in file, we need to make sure they don't change.
// Since only the last bit of each id is saved in file together with
// chunkids, this checks only last bit.
TEST(SafeBrowsingDbUtilTest, ListIdVerification) {
EXPECT_EQ(0, MALWARE % 2);
EXPECT_EQ(1, PHISH % 2);
EXPECT_EQ(0, BINURL % 2);
}
TEST(SafeBrowsingDbUtilTest, StringToSBFullHashAndSBFullHashToString) {
// 31 chars plus the last \0 as full_hash.
const std::string hash_in = "12345678902234567890323456789012";
SBFullHash hash_out = StringToSBFullHash(hash_in);
EXPECT_EQ(0x34333231U, hash_out.prefix);
EXPECT_EQ(0, memcmp(hash_in.data(), hash_out.full_hash, sizeof(SBFullHash)));
std::string hash_final = SBFullHashToString(hash_out);
EXPECT_EQ(hash_in, hash_final);
}
TEST(SafeBrowsingDbUtilTest, FullHashOperators) {
const SBFullHash kHash1 = SBFullHashForString("one");
const SBFullHash kHash2 = SBFullHashForString("two");
EXPECT_TRUE(SBFullHashEqual(kHash1, kHash1));
EXPECT_TRUE(SBFullHashEqual(kHash2, kHash2));
EXPECT_FALSE(SBFullHashEqual(kHash1, kHash2));
EXPECT_FALSE(SBFullHashEqual(kHash2, kHash1));
EXPECT_FALSE(SBFullHashLess(kHash1, kHash2));
EXPECT_TRUE(SBFullHashLess(kHash2, kHash1));
EXPECT_FALSE(SBFullHashLess(kHash1, kHash1));
EXPECT_FALSE(SBFullHashLess(kHash2, kHash2));
}
} // namespace safe_browsing
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment