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

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

Issue 2060923002: Neutralize dangerous subresource files during Save Page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@save-package-cleanup-1
Patch Set: Catch up with ToT Created 4 years, 6 months 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 26 matching lines...) Expand all
37 37
38 // This constant copied from save_package.cc. 38 // This constant copied from save_package.cc.
39 #if defined(OS_WIN) 39 #if defined(OS_WIN)
40 const uint32_t kMaxFilePathLength = MAX_PATH - 1; 40 const uint32_t kMaxFilePathLength = MAX_PATH - 1;
41 const uint32_t kMaxFileNameLength = MAX_PATH - 1; 41 const uint32_t kMaxFileNameLength = MAX_PATH - 1;
42 #elif defined(OS_POSIX) 42 #elif defined(OS_POSIX)
43 const uint32_t kMaxFilePathLength = PATH_MAX - 1; 43 const uint32_t kMaxFilePathLength = PATH_MAX - 1;
44 const uint32_t kMaxFileNameLength = NAME_MAX; 44 const uint32_t kMaxFileNameLength = NAME_MAX;
45 #endif 45 #endif
46 46
47 // Used to make long filenames.
48 std::string long_file_name(
49 "EFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz01234567"
50 "89ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz012345"
51 "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123"
52 "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a");
53
54 bool HasOrdinalNumber(const base::FilePath::StringType& filename) { 47 bool HasOrdinalNumber(const base::FilePath::StringType& filename) {
55 base::FilePath::StringType::size_type r_paren_index = 48 base::FilePath::StringType::size_type r_paren_index =
56 filename.rfind(FPL(')')); 49 filename.rfind(FPL(')'));
57 base::FilePath::StringType::size_type l_paren_index = 50 base::FilePath::StringType::size_type l_paren_index =
58 filename.rfind(FPL('(')); 51 filename.rfind(FPL('('));
59 if (l_paren_index >= r_paren_index) 52 if (l_paren_index >= r_paren_index)
60 return false; 53 return false;
61 54
62 for (base::FilePath::StringType::size_type i = l_paren_index + 1; 55 for (base::FilePath::StringType::size_type i = l_paren_index + 1;
63 i != r_paren_index; ++i) { 56 i != r_paren_index; ++i) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 98
106 // Do the initialization in SetUp so contents() is initialized by 99 // Do the initialization in SetUp so contents() is initialized by
107 // RenderViewHostImplTestHarness::SetUp. 100 // RenderViewHostImplTestHarness::SetUp.
108 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
109 102
110 save_package_success_ = 103 save_package_success_ =
111 new SavePackage(contents(), SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 104 new SavePackage(contents(), SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
112 temp_dir_.path().AppendASCII("testfile" HTML_EXTENSION), 105 temp_dir_.path().AppendASCII("testfile" HTML_EXTENSION),
113 temp_dir_.path().AppendASCII("testfile_files")); 106 temp_dir_.path().AppendASCII("testfile_files"));
114 107
115 // We need to construct a path that is *almost* kMaxFilePathLength long 108 base::FilePath::StringType long_file_name = GetLongFileName();
116 long_file_name.reserve(kMaxFilePathLength + long_file_name.length());
117 while (long_file_name.length() < kMaxFilePathLength)
118 long_file_name += long_file_name;
119 long_file_name.resize(
120 kMaxFilePathLength - 9 - temp_dir_.path().value().length());
121
122 save_package_fail_ = new SavePackage( 109 save_package_fail_ = new SavePackage(
123 contents(), SAVE_PAGE_TYPE_AS_COMPLETE_HTML, 110 contents(), SAVE_PAGE_TYPE_AS_COMPLETE_HTML,
124 temp_dir_.path().AppendASCII(long_file_name + HTML_EXTENSION), 111 temp_dir_.path().Append(long_file_name + FPL_HTML_EXTENSION),
125 temp_dir_.path().AppendASCII(long_file_name + "_files")); 112 temp_dir_.path().Append(long_file_name + FPL("_files")));
126 } 113 }
127 114
128 BrowserContext* CreateBrowserContext() override { 115 BrowserContext* CreateBrowserContext() override {
129 // This method is invoked after the browser threads have been created and 116 // This method is invoked after the browser threads have been created and
130 // obviously before the BrowserContext is created. This is the correct time 117 // obviously before the BrowserContext is created. This is the correct time
131 // to create a ResourceDispatcherHostImpl so that our SavePackage objects 118 // to create a ResourceDispatcherHostImpl so that our SavePackage objects
132 // can initialize correctly. 119 // can initialize correctly.
133 rdh_.reset(new ResourceDispatcherHostImpl); 120 rdh_.reset(new ResourceDispatcherHostImpl);
134 return RenderViewHostImplTestHarness::CreateBrowserContext(); 121 return RenderViewHostImplTestHarness::CreateBrowserContext();
135 } 122 }
136 123
137 void TearDown() override { 124 void TearDown() override {
138 DeleteContents(); 125 DeleteContents();
139 base::RunLoop().RunUntilIdle(); 126 base::RunLoop().RunUntilIdle();
140 127
141 save_package_success_ = nullptr; 128 save_package_success_ = nullptr;
142 save_package_fail_ = nullptr; 129 save_package_fail_ = nullptr;
143 rdh_.reset(); 130 rdh_.reset();
144 131
145 RenderViewHostImplTestHarness::TearDown(); 132 RenderViewHostImplTestHarness::TearDown();
146 } 133 }
147 134
135 // Returns a path that is *almost* kMaxFilePathLength long
136 base::FilePath::StringType GetLongFileName() const {
137 size_t target_length =
138 kMaxFilePathLength - 9 - temp_dir_.path().value().length();
139 return base::FilePath::StringType(target_length, FPL('a'));
140 }
141
148 private: 142 private:
149 // SavePackage for successfully generating file name. 143 // SavePackage for successfully generating file name.
150 scoped_refptr<SavePackage> save_package_success_; 144 scoped_refptr<SavePackage> save_package_success_;
151 // SavePackage for failed generating file name. 145 // SavePackage for failed generating file name.
152 scoped_refptr<SavePackage> save_package_fail_; 146 scoped_refptr<SavePackage> save_package_fail_;
153 147
154 base::ScopedTempDir temp_dir_; 148 base::ScopedTempDir temp_dir_;
155 149
156 std::unique_ptr<ResourceDispatcherHostImpl> rdh_; 150 std::unique_ptr<ResourceDispatcherHostImpl> rdh_;
157 }; 151 };
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 } 224 }
231 225
232 // Crashing on Windows, see http://crbug.com/79365 226 // Crashing on Windows, see http://crbug.com/79365
233 #if defined(OS_WIN) 227 #if defined(OS_WIN)
234 #define MAYBE_TestLongSavePackageFilename DISABLED_TestLongSavePackageFilename 228 #define MAYBE_TestLongSavePackageFilename DISABLED_TestLongSavePackageFilename
235 #else 229 #else
236 #define MAYBE_TestLongSavePackageFilename TestLongSavePackageFilename 230 #define MAYBE_TestLongSavePackageFilename TestLongSavePackageFilename
237 #endif 231 #endif
238 TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) { 232 TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) {
239 const std::string base_url("http://www.google.com/"); 233 const std::string base_url("http://www.google.com/");
240 const std::string long_file = long_file_name + ".css"; 234 const base::FilePath::StringType long_file_name =
241 const std::string url = base_url + long_file; 235 GetLongFileName() + FPL(".css");
236 const std::string url =
237 base_url + base::FilePath(long_file_name).AsUTF8Unsafe();
242 238
243 base::FilePath::StringType filename; 239 base::FilePath::StringType filename;
244 // Test that the filename is successfully shortened to fit. 240 // Test that the filename is successfully shortened to fit.
245 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename)); 241 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename));
246 EXPECT_TRUE(filename.length() < long_file.length()); 242 EXPECT_TRUE(filename.length() < long_file_name.length());
247 EXPECT_FALSE(HasOrdinalNumber(filename)); 243 EXPECT_FALSE(HasOrdinalNumber(filename));
248 244
249 // Test that the filename is successfully shortened to fit, and gets an 245 // Test that the filename is successfully shortened to fit, and gets an
250 // an ordinal appended. 246 // an ordinal appended.
251 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename)); 247 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename));
252 EXPECT_TRUE(filename.length() < long_file.length()); 248 EXPECT_TRUE(filename.length() < long_file_name.length());
253 EXPECT_TRUE(HasOrdinalNumber(filename)); 249 EXPECT_TRUE(HasOrdinalNumber(filename));
254 250
255 // Test that the filename is successfully shortened to fit, and gets a 251 // Test that the filename is successfully shortened to fit, and gets a
256 // different ordinal appended. 252 // different ordinal appended.
257 base::FilePath::StringType filename2; 253 base::FilePath::StringType filename2;
258 ASSERT_TRUE( 254 ASSERT_TRUE(
259 GetGeneratedFilename(true, std::string(), url, false, &filename2)); 255 GetGeneratedFilename(true, std::string(), url, false, &filename2));
260 EXPECT_TRUE(filename2.length() < long_file.length()); 256 EXPECT_TRUE(filename2.length() < long_file_name.length());
261 EXPECT_TRUE(HasOrdinalNumber(filename2)); 257 EXPECT_TRUE(HasOrdinalNumber(filename2));
262 EXPECT_NE(filename, filename2); 258 EXPECT_NE(filename, filename2);
263 } 259 }
264 260
265 // Crashing on Windows, see http://crbug.com/79365 261 // Crashing on Windows, see http://crbug.com/79365
266 #if defined(OS_WIN) 262 #if defined(OS_WIN)
267 #define MAYBE_TestLongSafePureFilename DISABLED_TestLongSafePureFilename 263 #define MAYBE_TestLongSafePureFilename DISABLED_TestLongSafePureFilename
268 #else 264 #else
269 #define MAYBE_TestLongSafePureFilename TestLongSafePureFilename 265 #define MAYBE_TestLongSafePureFilename TestLongSafePureFilename
270 #endif 266 #endif
271 TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) { 267 TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) {
272 const base::FilePath save_dir(FPL("test_dir")); 268 const base::FilePath save_dir(FPL("test_dir"));
273 const base::FilePath::StringType ext(FPL_HTML_EXTENSION); 269 const base::FilePath::StringType ext(FPL_HTML_EXTENSION);
274 base::FilePath::StringType filename = 270 base::FilePath::StringType filename = GetLongFileName();
275 #if defined(OS_WIN)
276 base::ASCIIToUTF16(long_file_name);
277 #else
278 long_file_name;
279 #endif
280 271
281 // Test that the filename + extension doesn't exceed kMaxFileNameLength 272 // Test that the filename + extension doesn't exceed kMaxFileNameLength
282 uint32_t max_path = SavePackage::GetMaxPathLengthForDirectory(save_dir); 273 uint32_t max_path = SavePackage::GetMaxPathLengthForDirectory(save_dir);
283 ASSERT_TRUE(SavePackage::TruncateBaseNameToFitPathConstraints( 274 ASSERT_TRUE(SavePackage::TruncateBaseNameToFitPathConstraints(
284 save_dir, ext, max_path, &filename)); 275 save_dir, ext, max_path, &filename));
285 EXPECT_TRUE(filename.length() <= kMaxFileNameLength-ext.length()); 276 EXPECT_TRUE(filename.length() <= kMaxFileNameLength-ext.length());
286 } 277 }
287 278
288 static const struct { 279 static const struct {
289 const base::FilePath::CharType* page_title; 280 const base::FilePath::CharType* page_title;
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 GURL mock_url = net::URLRequestMockHTTPJob::GetMockUrl("save_page/a.htm"); 445 GURL mock_url = net::URLRequestMockHTTPJob::GetMockUrl("save_page/a.htm");
455 GURL view_source_url = 446 GURL view_source_url =
456 GURL(kViewSourceScheme + std::string(":") + mock_url.spec()); 447 GURL(kViewSourceScheme + std::string(":") + mock_url.spec());
457 GURL actual_url = net::URLRequestMockHTTPJob::GetMockUrl("save_page/a.htm"); 448 GURL actual_url = net::URLRequestMockHTTPJob::GetMockUrl("save_page/a.htm");
458 NavigateAndCommit(view_source_url); 449 NavigateAndCommit(view_source_url);
459 EXPECT_EQ(actual_url, GetUrlToBeSaved()); 450 EXPECT_EQ(actual_url, GetUrlToBeSaved());
460 EXPECT_EQ(view_source_url, contents()->GetLastCommittedURL()); 451 EXPECT_EQ(view_source_url, contents()->GetLastCommittedURL());
461 } 452 }
462 453
463 } // namespace content 454 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/save_package.cc ('k') | content/public/browser/download_manager_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698