Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: content/browser/download/quarantine_win_unittest.cc

Issue 2124373002: [PPAPI] Quarantine files that are writeable by a Pepper plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@consolidate-file-metadata
Patch Set: Address comments. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include <windows.h>
6
7 #include <wininet.h>
8
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h"
13 #include "base/test/histogram_tester.h"
14 #include "base/test/test_file_util.h"
15 #include "content/browser/download/quarantine.h"
16 #include "net/base/filename_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
19
20 namespace content {
21
22 namespace {
23
24 const char kDummySourceUrl[] = "https://example.com/foo";
25 const char kDummyReferrerUrl[] = "https://example.com/referrer";
26 const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7";
27
28 const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n";
29 const base::FilePath::CharType kMotwStreamSuffix[] =
30 FILE_PATH_LITERAL(":Zone.Identifier");
31
32 const char kTestData[] = "Hello world!";
33
34 const char* const kUntrustedURLs[] = {
35 "http://example.com/foo",
36 "https://example.com/foo",
37 "ftp://example.com/foo",
38 "ftp://example.com:2121/foo",
39 "data:text/plain,Hello%20world",
40 "blob://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6",
41 "about:internet",
42 ""};
43
44 } // namespace
45
46 // If the file is missing, the QuarantineFile() call should return FILE_MISSING.
47 TEST(QuarantineWinTest, MissingFile) {
48 base::ScopedTempDir test_dir;
49 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
50
51 EXPECT_EQ(QuarantineFileResult::FILE_MISSING,
52 QuarantineFile(test_dir.GetPath().AppendASCII("does-not-exist.exe"),
53 GURL(kDummySourceUrl), GURL(kDummyReferrerUrl),
54 kDummyClientGuid));
55 }
56
57 // On Windows systems, files downloaded from a local source are considered
58 // trustworthy. Hence they aren't annotated with source information. This test
59 // verifies this behavior since the other tests in this suite would pass with a
60 // false positive if local files are being annotated with the MOTW for the
61 // internet zone.
62 TEST(QuarantineWinTest, LocalFile_DependsOnLocalConfig) {
63 base::HistogramTester histogram_tester;
64 base::ScopedTempDir test_dir;
65 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
66 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
67
68 const char* const kLocalSourceURLs[] = {
69 "http://localhost/foo",
70 "file:///C:/some-local-dir/foo.exe"
71 };
72
73 for (const auto source_url : kLocalSourceURLs) {
74 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
75 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
76 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
77
78 EXPECT_EQ(
79 QuarantineFileResult::OK,
80 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid));
81
82 std::string motw_contents;
83 base::ReadFileToString(
84 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents);
85
86 // These warnings aren't displayed on successful test runs. They are there
87 // so that we can check for deviations in behavior during manual testing.
88 if (!motw_contents.empty()) {
89 LOG(WARNING) << "Unexpected zone marker for file " << test_file.value()
90 << " Source URL:" << source_url;
91 if (motw_contents != kMotwForInternetZone)
92 LOG(WARNING) << "Zone marker contents: " << motw_contents;
93 }
94
95 base::DeleteFile(test_file, false);
96 }
97
98 // Bucket 1 is SUCCESS_WITHOUT_MOTW.
99 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 1,
100 arraysize(kLocalSourceURLs));
101 }
102
103 // A file downloaded from the internet should be annotated with .. something.
104 // The specific zone assigned to our dummy source URL depends on the local
105 // configuration. But no sane configuration should be treating the dummy URL as
106 // a trusted source for anything.
107 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) {
108 base::HistogramTester histogram_tester;
109 base::ScopedTempDir test_dir;
110 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
111 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
112
113 for (const auto source_url : kUntrustedURLs) {
114 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url);
115 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
116 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
117 EXPECT_EQ(
118 QuarantineFileResult::OK,
119 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid));
120 std::string motw_contents;
121 ASSERT_TRUE(base::ReadFileToString(
122 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
123 // The actual assigned zone could be anything. So only testing that there is
124 // a zone annotation.
125 EXPECT_FALSE(motw_contents.empty());
126
127 // These warnings aren't displayed on successful test runs. They are there
128 // so that we can check for deviations in behavior during manual testing.
129 if (motw_contents != kMotwForInternetZone)
130 LOG(WARNING) << "Unexpected zone marker: " << motw_contents;
131 base::DeleteFile(test_file, false);
132 }
133
134 // Bucket 0 is SUCCESS_WITH_MOTW.
135 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0,
136 arraysize(kUntrustedURLs));
137 }
138
139 TEST(QuarantineWinTest, UnsafeReferrer_DependsOnLocalConfig) {
140 base::HistogramTester histogram_tester;
141 base::ScopedTempDir test_dir;
142 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
143 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
144
145 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs),
146 std::end(kUntrustedURLs));
147
148 std::string huge_referrer = "http://example.com/";
149 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
150 unsafe_referrers.push_back(huge_referrer);
151
152 for (const auto referrer_url : unsafe_referrers) {
153 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url);
154 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
155 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
156 EXPECT_EQ(QuarantineFileResult::OK,
157 QuarantineFile(test_file, GURL("http://example.com/good"),
158 GURL(referrer_url), kDummyClientGuid));
159 std::string motw_contents;
160 ASSERT_TRUE(base::ReadFileToString(
161 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
162 // The actual assigned zone could be anything. So only testing that there is
163 // a zone annotation.
164 EXPECT_FALSE(motw_contents.empty());
165
166 // These warnings aren't displayed on successful test runs. They are there
167 // so that we can check for deviations in behavior during manual testing.
168 if (motw_contents != kMotwForInternetZone)
169 LOG(WARNING) << "Unexpected zone marker: " << motw_contents;
170 base::DeleteFile(test_file, false);
171 }
172
173 // Bucket 0 is SUCCESS_WITH_MOTW.
174 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0,
175 unsafe_referrers.size());
176 }
177
178 // An empty source URL should result in a file that's treated the same as one
179 // downloaded from the internet.
180 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) {
181 base::HistogramTester histogram_tester;
182 base::ScopedTempDir test_dir;
183 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
184 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
185 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
186 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
187
188 EXPECT_EQ(QuarantineFileResult::OK,
189 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid));
190 std::string motw_contents;
191 ASSERT_TRUE(base::ReadFileToString(
192 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
193 // The actual assigned zone could be anything. So only testing that there is a
194 // zone annotation.
195 EXPECT_FALSE(motw_contents.empty());
196
197 // Bucket 0 is SUCCESS_WITH_MOTW.
198 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0,
199 1);
200 }
201
202 // Empty files aren't passed to AVScanFile. They are instead marked manually. If
203 // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to
204 // it and the test would fail.
205 TEST(QuarantineWinTest, EmptyFile) {
206 base::HistogramTester histogram_tester;
207 base::ScopedTempDir test_dir;
208 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
209 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
210 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u));
211
212 EXPECT_EQ(QuarantineFileResult::OK,
213 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(),
214 kDummyClientGuid));
215 std::string motw_contents;
216 ASSERT_TRUE(base::ReadFileToString(
217 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
218 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
219
220 // Attachment services shouldn't have been invoked at all.
221 histogram_tester.ExpectTotalCount("Download.AttachmentServices.Result", 0);
222 }
223
224 // If there is no client GUID supplied to the QuarantineFile() call, then rather
225 // than invoking AVScanFile, the MOTW will be applied manually. If the file is
226 // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the
227 // test would fail.
228 TEST(QuarantineWinTest, NoClientGuid) {
229 base::ScopedTempDir test_dir;
230 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
231 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
232 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
233 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
234
235 EXPECT_EQ(QuarantineFileResult::OK,
236 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(),
237 std::string()));
238 std::string motw_contents;
239 ASSERT_TRUE(base::ReadFileToString(
240 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
241 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
242 }
243
244 // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a
245 // URL, when used as a source URL shouldn't break QuarantineFile() which should
246 // mark the file as being from the internet zone as a safe fallback.
247 TEST(QuarantineWinTest, SuperLongURL) {
248 base::ScopedTempDir test_dir;
249 ASSERT_TRUE(test_dir.CreateUniqueTempDir());
250 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.exe");
251 ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
252 base::WriteFile(test_file, kTestData, arraysize(kTestData)));
253
254 std::string source_url("http://example.com/");
255 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a');
256 EXPECT_EQ(QuarantineFileResult::OK,
257 QuarantineFile(test_file, GURL(source_url), GURL(), std::string()));
258
259 std::string motw_contents;
260 ASSERT_TRUE(base::ReadFileToString(
261 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents));
262 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str());
263 }
264
265 } // content
OLDNEW
« no previous file with comments | « content/browser/download/quarantine_win.cc ('k') | content/browser/renderer_host/pepper/pepper_file_io_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698