test_support_win.cc 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <windows.h>
  5. #include <string>
  6. #include <vector>
  7. #include "base/files/file_path.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/string_split.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/win/scoped_handle.h"
  12. #include "base/win/windows_version.h"
  13. #include "components/services/quarantine/common.h"
  14. #include "components/services/quarantine/common_win.h"
  15. #include "components/services/quarantine/test_support.h"
  16. namespace quarantine {
  17. namespace {
  18. bool ZoneIdentifierPresentForFile(const base::FilePath& path,
  19. const GURL source_url,
  20. const GURL referrer_url) {
  21. const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  22. base::FilePath::StringType zone_identifier_path =
  23. path.value() + kZoneIdentifierStreamSuffix;
  24. base::win::ScopedHandle file(
  25. ::CreateFile(zone_identifier_path.c_str(), GENERIC_READ, kShare, nullptr,
  26. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
  27. if (!file.IsValid())
  28. return false;
  29. // During testing, the zone identifier is expected to be under this limit.
  30. std::vector<char> zone_identifier_contents_buffer(4096);
  31. DWORD actual_length = 0;
  32. if (!::ReadFile(file.Get(), &zone_identifier_contents_buffer.front(),
  33. zone_identifier_contents_buffer.size(), &actual_length,
  34. nullptr))
  35. return false;
  36. zone_identifier_contents_buffer.resize(actual_length);
  37. std::string zone_identifier_contents(zone_identifier_contents_buffer.begin(),
  38. zone_identifier_contents_buffer.end());
  39. std::vector<base::StringPiece> lines =
  40. base::SplitStringPiece(zone_identifier_contents, "\n",
  41. base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  42. if (lines.size() < 2 || lines[0] != "[ZoneTransfer]" != 0)
  43. return false;
  44. base::StringPiece found_zone_id;
  45. base::StringPiece found_host_url;
  46. base::StringPiece found_referrer_url;
  47. // Note that we don't try too hard to parse the zone identifier here. This is
  48. // a test. If Windows starts adding whitespace or doing anything fancier than
  49. // ASCII, then we'd have to update this.
  50. for (const auto& line : lines) {
  51. if (base::StartsWith(line, "ZoneId="))
  52. found_zone_id = line.substr(7);
  53. else if (base::StartsWith(line, "HostUrl="))
  54. found_host_url = line.substr(8);
  55. else if (base::StartsWith(line, "ReferrerUrl="))
  56. found_referrer_url = line.substr(12);
  57. }
  58. return !found_zone_id.empty() &&
  59. (source_url.is_empty() ||
  60. SanitizeUrlForQuarantine(source_url).spec() == found_host_url) &&
  61. (referrer_url.is_empty() ||
  62. SanitizeUrlForQuarantine(referrer_url).spec() == found_referrer_url);
  63. }
  64. } // namespace
  65. bool IsFileQuarantined(const base::FilePath& file,
  66. const GURL& source_url,
  67. const GURL& referrer_url) {
  68. if (base::win::GetVersion() >= base::win::Version::WIN10)
  69. return ZoneIdentifierPresentForFile(file, source_url, referrer_url);
  70. else
  71. return ZoneIdentifierPresentForFile(file, GURL::EmptyGURL(),
  72. GURL::EmptyGURL());
  73. }
  74. } // namespace quarantine