Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <wininet.h> | 7 #include <wininet.h> |
| 8 | 8 |
| 9 #include "content/browser/download/quarantine.h" | |
| 10 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "base/test/test_file_util.h" | |
| 15 #include "content/browser/download/quarantine.h" | |
| 14 #include "net/base/filename_util.h" | 16 #include "net/base/filename_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 namespace content { | 20 namespace content { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 const char kDummySourceUrl[] = "https://example.com/foo"; | 24 const char kDummySourceUrl[] = "https://example.com/foo"; |
| 23 const char kDummyReferrerUrl[] = "https://example.com/referrer"; | 25 const char kDummyReferrerUrl[] = "https://example.com/referrer"; |
| 24 const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7"; | 26 const char kDummyClientGuid[] = "A1B69307-8FA2-4B6F-9181-EA06051A48A7"; |
| 25 | 27 |
| 26 const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; | 28 const char kMotwForInternetZone[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; |
| 27 const base::FilePath::CharType kMotwStreamSuffix[] = | 29 const base::FilePath::CharType kMotwStreamSuffix[] = |
| 28 FILE_PATH_LITERAL(":Zone.Identifier"); | 30 FILE_PATH_LITERAL(":Zone.Identifier"); |
| 29 | 31 |
| 32 const char kTestData[] = { | |
|
svaldez
2016/09/27 20:11:03
Any reason not to do (if it works):
const char kT
asanka
2016/09/27 20:19:17
None other than avoiding the terminating NUL.
I'l
| |
| 33 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\r', '\n', | |
| 34 }; | |
| 35 | |
| 30 const char* const kUntrustedURLs[] = { | 36 const char* const kUntrustedURLs[] = { |
| 31 "http://example.com/foo", | 37 "http://example.com/foo", |
| 32 "https://example.com/foo", | 38 "https://example.com/foo", |
| 33 "ftp://example.com/foo", | 39 "ftp://example.com/foo", |
| 34 "ftp://example.com:2121/foo", | 40 "ftp://example.com:2121/foo", |
| 35 "data:text/plain,Hello%20world", | 41 "data:text/plain,Hello%20world", |
| 36 "blob://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6", | 42 "blob://example.com/126278b3-58f3-4b4a-a914-1d1185d634f6", |
| 37 "about:internet", | 43 "about:internet", |
| 38 ""}; | 44 ""}; |
| 39 | 45 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 61 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 67 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 62 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 68 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 63 | 69 |
| 64 const char* const kLocalSourceURLs[] = { | 70 const char* const kLocalSourceURLs[] = { |
| 65 "http://localhost/foo", | 71 "http://localhost/foo", |
| 66 "file:///C:/some-local-dir/foo.exe" | 72 "file:///C:/some-local-dir/foo.exe" |
| 67 }; | 73 }; |
| 68 | 74 |
| 69 for (const auto source_url : kLocalSourceURLs) { | 75 for (const auto source_url : kLocalSourceURLs) { |
| 70 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | 76 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); |
| 71 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 77 ASSERT_EQ(arraysize(kTestData), |
| 78 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 72 | 79 |
| 73 EXPECT_EQ( | 80 EXPECT_EQ( |
| 74 QuarantineFileResult::OK, | 81 QuarantineFileResult::OK, |
| 75 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); | 82 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); |
| 76 | 83 |
| 77 std::string motw_contents; | 84 std::string motw_contents; |
| 78 base::ReadFileToString( | 85 base::ReadFileToString( |
| 79 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents); | 86 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents); |
| 80 | 87 |
| 81 // These warnings aren't displayed on successful test runs. They are there | 88 // These warnings aren't displayed on successful test runs. They are there |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 100 // configuration. But no sane configuration should be treating the dummy URL as | 107 // configuration. But no sane configuration should be treating the dummy URL as |
| 101 // a trusted source for anything. | 108 // a trusted source for anything. |
| 102 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { | 109 TEST(QuarantineWinTest, DownloadedFile_DependsOnLocalConfig) { |
| 103 base::HistogramTester histogram_tester; | 110 base::HistogramTester histogram_tester; |
| 104 base::ScopedTempDir test_dir; | 111 base::ScopedTempDir test_dir; |
| 105 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 112 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 106 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 113 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 107 | 114 |
| 108 for (const auto source_url : kUntrustedURLs) { | 115 for (const auto source_url : kUntrustedURLs) { |
| 109 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); | 116 SCOPED_TRACE(::testing::Message() << "Trying URL " << source_url); |
| 110 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 117 ASSERT_EQ(arraysize(kTestData), |
| 118 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 111 EXPECT_EQ( | 119 EXPECT_EQ( |
| 112 QuarantineFileResult::OK, | 120 QuarantineFileResult::OK, |
| 113 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); | 121 QuarantineFile(test_file, GURL(source_url), GURL(), kDummyClientGuid)); |
| 114 std::string motw_contents; | 122 std::string motw_contents; |
| 115 ASSERT_TRUE(base::ReadFileToString( | 123 ASSERT_TRUE(base::ReadFileToString( |
| 116 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 124 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 117 // The actual assigned zone could be anything. So only testing that there is | 125 // The actual assigned zone could be anything. So only testing that there is |
| 118 // a zone annotation. | 126 // a zone annotation. |
| 119 EXPECT_FALSE(motw_contents.empty()); | 127 EXPECT_FALSE(motw_contents.empty()); |
| 120 | 128 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 138 | 146 |
| 139 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs), | 147 std::vector<std::string> unsafe_referrers(std::begin(kUntrustedURLs), |
| 140 std::end(kUntrustedURLs)); | 148 std::end(kUntrustedURLs)); |
| 141 | 149 |
| 142 std::string huge_referrer = "http://example.com/"; | 150 std::string huge_referrer = "http://example.com/"; |
| 143 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); | 151 huge_referrer.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); |
| 144 unsafe_referrers.push_back(huge_referrer); | 152 unsafe_referrers.push_back(huge_referrer); |
| 145 | 153 |
| 146 for (const auto referrer_url : unsafe_referrers) { | 154 for (const auto referrer_url : unsafe_referrers) { |
| 147 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url); | 155 SCOPED_TRACE(::testing::Message() << "Trying URL " << referrer_url); |
| 148 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 156 ASSERT_EQ(arraysize(kTestData), |
| 157 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 149 EXPECT_EQ(QuarantineFileResult::OK, | 158 EXPECT_EQ(QuarantineFileResult::OK, |
| 150 QuarantineFile(test_file, GURL("http://example.com/good"), | 159 QuarantineFile(test_file, GURL("http://example.com/good"), |
| 151 GURL(referrer_url), kDummyClientGuid)); | 160 GURL(referrer_url), kDummyClientGuid)); |
| 152 std::string motw_contents; | 161 std::string motw_contents; |
| 153 ASSERT_TRUE(base::ReadFileToString( | 162 ASSERT_TRUE(base::ReadFileToString( |
| 154 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 163 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 155 // The actual assigned zone could be anything. So only testing that there is | 164 // The actual assigned zone could be anything. So only testing that there is |
| 156 // a zone annotation. | 165 // a zone annotation. |
| 157 EXPECT_FALSE(motw_contents.empty()); | 166 EXPECT_FALSE(motw_contents.empty()); |
| 158 | 167 |
| 159 // These warnings aren't displayed on successful test runs. They are there | 168 // These warnings aren't displayed on successful test runs. They are there |
| 160 // so that we can check for deviations in behavior during manual testing. | 169 // so that we can check for deviations in behavior during manual testing. |
| 161 if (motw_contents != kMotwForInternetZone) | 170 if (motw_contents != kMotwForInternetZone) |
| 162 LOG(WARNING) << "Unexpected zone marker: " << motw_contents; | 171 LOG(WARNING) << "Unexpected zone marker: " << motw_contents; |
| 163 base::DeleteFile(test_file, false); | 172 base::DeleteFile(test_file, false); |
| 164 } | 173 } |
| 165 | 174 |
| 166 // Bucket 0 is SUCCESS_WITH_MOTW. | 175 // Bucket 0 is SUCCESS_WITH_MOTW. |
| 167 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 176 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
| 168 unsafe_referrers.size()); | 177 unsafe_referrers.size()); |
| 169 } | 178 } |
| 170 | 179 |
| 171 // An empty source URL should result in a file that's treated the same as one | 180 // An empty source URL should result in a file that's treated the same as one |
| 172 // downloaded from the internet. | 181 // downloaded from the internet. |
| 173 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { | 182 TEST(QuarantineWinTest, EmptySource_DependsOnLocalConfig) { |
| 174 base::HistogramTester histogram_tester; | 183 base::HistogramTester histogram_tester; |
| 175 base::ScopedTempDir test_dir; | 184 base::ScopedTempDir test_dir; |
| 176 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 185 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 177 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 186 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 178 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 187 ASSERT_EQ(arraysize(kTestData), |
| 188 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 179 | 189 |
| 180 EXPECT_EQ(QuarantineFileResult::OK, | 190 EXPECT_EQ(QuarantineFileResult::OK, |
| 181 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); | 191 QuarantineFile(test_file, GURL(), GURL(), kDummyClientGuid)); |
| 182 std::string motw_contents; | 192 std::string motw_contents; |
| 183 ASSERT_TRUE(base::ReadFileToString( | 193 ASSERT_TRUE(base::ReadFileToString( |
| 184 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 194 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 185 // The actual assigned zone could be anything. So only testing that there is a | 195 // The actual assigned zone could be anything. So only testing that there is a |
| 186 // zone annotation. | 196 // zone annotation. |
| 187 EXPECT_FALSE(motw_contents.empty()); | 197 EXPECT_FALSE(motw_contents.empty()); |
| 188 | 198 |
| 189 // Bucket 0 is SUCCESS_WITH_MOTW. | 199 // Bucket 0 is SUCCESS_WITH_MOTW. |
| 190 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, | 200 histogram_tester.ExpectUniqueSample("Download.AttachmentServices.Result", 0, |
| 191 1); | 201 1); |
| 192 } | 202 } |
| 193 | 203 |
| 194 // Empty files aren't passed to AVScanFile. They are instead marked manually. If | 204 // Empty files aren't passed to AVScanFile. They are instead marked manually. If |
| 195 // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to | 205 // the file is passed to AVScanFile, then there wouldn't be a MOTW attached to |
| 196 // it and the test would fail. | 206 // it and the test would fail. |
| 197 TEST(QuarantineWinTest, EmptyFile) { | 207 TEST(QuarantineWinTest, EmptyFile) { |
| 208 base::HistogramTester histogram_tester; | |
| 198 base::ScopedTempDir test_dir; | 209 base::ScopedTempDir test_dir; |
| 199 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 210 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 200 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 211 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 201 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u)); | 212 ASSERT_EQ(0, base::WriteFile(test_file, "", 0u)); |
| 202 | 213 |
| 203 EXPECT_EQ(QuarantineFileResult::OK, | 214 EXPECT_EQ(QuarantineFileResult::OK, |
| 204 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 215 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), |
| 205 kDummyClientGuid)); | 216 kDummyClientGuid)); |
| 206 std::string motw_contents; | 217 std::string motw_contents; |
| 207 ASSERT_TRUE(base::ReadFileToString( | 218 ASSERT_TRUE(base::ReadFileToString( |
| 208 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 219 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 209 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 220 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 221 | |
| 222 // Attachment services shouldn't have been invoked at all. | |
| 223 histogram_tester.ExpectTotalCount("Download.AttachmentServices.Result", 0); | |
| 210 } | 224 } |
| 211 | 225 |
| 212 // If there is no client GUID supplied to the QuarantineFile() call, then rather | 226 // If there is no client GUID supplied to the QuarantineFile() call, then rather |
| 213 // than invoking AVScanFile, the MOTW will be applied manually. If the file is | 227 // than invoking AVScanFile, the MOTW will be applied manually. If the file is |
| 214 // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the | 228 // passed to AVScanFile, then there wouldn't be a MOTW attached to it and the |
| 215 // test would fail. | 229 // test would fail. |
| 216 TEST(QuarantineWinTest, NoClientGuid) { | 230 TEST(QuarantineWinTest, NoClientGuid) { |
| 217 base::ScopedTempDir test_dir; | 231 base::ScopedTempDir test_dir; |
| 218 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 232 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 219 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 233 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 220 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 234 ASSERT_EQ(arraysize(kTestData), |
| 235 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 221 | 236 |
| 222 EXPECT_EQ(QuarantineFileResult::OK, | 237 EXPECT_EQ(QuarantineFileResult::OK, |
| 223 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), | 238 QuarantineFile(test_file, net::FilePathToFileURL(test_file), GURL(), |
| 224 std::string())); | 239 std::string())); |
| 225 std::string motw_contents; | 240 std::string motw_contents; |
| 226 ASSERT_TRUE(base::ReadFileToString( | 241 ASSERT_TRUE(base::ReadFileToString( |
| 227 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 242 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 228 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 243 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 229 } | 244 } |
| 230 | 245 |
| 231 // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a | 246 // URLs longer than INTERNET_MAX_URL_LENGTH are known to break URLMon. Such a |
| 232 // URL, when used as a source URL shouldn't break QuarantineFile() which should | 247 // URL, when used as a source URL shouldn't break QuarantineFile() which should |
| 233 // mark the file as being from the internet zone as a safe fallback. | 248 // mark the file as being from the internet zone as a safe fallback. |
| 234 TEST(QuarantineWinTest, SuperLongURL) { | 249 TEST(QuarantineWinTest, SuperLongURL) { |
| 235 base::ScopedTempDir test_dir; | 250 base::ScopedTempDir test_dir; |
| 236 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 251 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
| 237 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); | 252 base::FilePath test_file = test_dir.path().AppendASCII("foo.exe"); |
| 238 ASSERT_EQ(5, base::WriteFile(test_file, "Hello", 5u)); | 253 ASSERT_EQ(arraysize(kTestData), |
| 254 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | |
| 239 | 255 |
| 240 std::string source_url("http://example.com/"); | 256 std::string source_url("http://example.com/"); |
| 241 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); | 257 source_url.append(INTERNET_MAX_URL_LENGTH * 2, 'a'); |
| 242 EXPECT_EQ(QuarantineFileResult::OK, | 258 EXPECT_EQ(QuarantineFileResult::OK, |
| 243 QuarantineFile(test_file, GURL(source_url), GURL(), std::string())); | 259 QuarantineFile(test_file, GURL(source_url), GURL(), std::string())); |
| 244 | 260 |
| 245 std::string motw_contents; | 261 std::string motw_contents; |
| 246 ASSERT_TRUE(base::ReadFileToString( | 262 ASSERT_TRUE(base::ReadFileToString( |
| 247 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); | 263 base::FilePath(test_file.value() + kMotwStreamSuffix), &motw_contents)); |
| 248 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); | 264 EXPECT_STREQ(kMotwForInternetZone, motw_contents.c_str()); |
| 249 } | 265 } |
| 250 | 266 |
| 251 } // content | 267 } // content |
| OLD | NEW |