File: test_support_win.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (85 lines) | stat: -rw-r--r-- 3,143 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 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/services/quarantine/test_support.h"

#include <windows.h>

#include <string>
#include <string_view>
#include <vector>

#include "base/files/file_path.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/win/scoped_handle.h"
#include "components/services/quarantine/common.h"
#include "components/services/quarantine/common_win.h"

namespace quarantine {

namespace {

bool ZoneIdentifierPresentForFile(const base::FilePath& path,
                                  const GURL source_url,
                                  const GURL referrer_url) {
  const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  base::FilePath::StringType zone_identifier_path =
      path.value() + kZoneIdentifierStreamSuffix;
  base::win::ScopedHandle file(
      ::CreateFile(zone_identifier_path.c_str(), GENERIC_READ, kShare, nullptr,
                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
  if (!file.IsValid())
    return false;

  // During testing, the zone identifier is expected to be under this limit.
  std::vector<char> zone_identifier_contents_buffer(4096);
  DWORD actual_length = 0;
  if (!::ReadFile(file.Get(), &zone_identifier_contents_buffer.front(),
                  zone_identifier_contents_buffer.size(), &actual_length,
                  nullptr))
    return false;
  zone_identifier_contents_buffer.resize(actual_length);

  std::string zone_identifier_contents(zone_identifier_contents_buffer.begin(),
                                       zone_identifier_contents_buffer.end());

  std::vector<std::string_view> lines =
      base::SplitStringPiece(zone_identifier_contents, "\n",
                             base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (lines.size() < 2 || lines[0] != "[ZoneTransfer]" != 0)
    return false;

  std::string_view found_zone_id;
  std::string_view found_host_url;
  std::string_view found_referrer_url;

  // Note that we don't try too hard to parse the zone identifier here. This is
  // a test. If Windows starts adding whitespace or doing anything fancier than
  // ASCII, then we'd have to update this.
  for (const auto& line : lines) {
    if (base::StartsWith(line, "ZoneId="))
      found_zone_id = line.substr(7);
    else if (base::StartsWith(line, "HostUrl="))
      found_host_url = line.substr(8);
    else if (base::StartsWith(line, "ReferrerUrl="))
      found_referrer_url = line.substr(12);
  }

  return !found_zone_id.empty() &&
         (source_url.is_empty() ||
          SanitizeUrlForQuarantine(source_url).spec() == found_host_url) &&
         (referrer_url.is_empty() ||
          SanitizeUrlForQuarantine(referrer_url).spec() == found_referrer_url);
}

}  // namespace

bool IsFileQuarantined(const base::FilePath& file,
                       const GURL& source_url,
                       const GURL& referrer_url) {
  return ZoneIdentifierPresentForFile(file, source_url, referrer_url);
}

}  // namespace quarantine