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

Side by Side Diff: base/file_util.cc

Issue 12223014: Add path traversal protection to Move and CopyFile too. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util.h ('k') | base/file_util_mac.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #endif 9 #endif
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 (last_separator != std::wstring::npos && last_dot < last_separator)) { 70 (last_separator != std::wstring::npos && last_dot < last_separator)) {
71 // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". 71 // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo".
72 // We should just append the suffix to the entire path. 72 // We should just append the suffix to the entire path.
73 value.append(suffix); 73 value.append(suffix);
74 return; 74 return;
75 } 75 }
76 76
77 value.insert(last_dot, suffix); 77 value.insert(last_dot, suffix);
78 } 78 }
79 79
80 bool Move(const FilePath& from_path, const FilePath& to_path) {
81 if (from_path.ReferencesParent() || to_path.ReferencesParent())
82 return false;
83 return MoveUnsafe(from_path, to_path);
84 }
85
86 bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
87 if (from_path.ReferencesParent() || to_path.ReferencesParent())
88 return false;
89 return CopyFileUnsafe(from_path, to_path);
90 }
91
80 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { 92 bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
81 // We open the file in binary format even if they are text files because 93 // We open the file in binary format even if they are text files because
82 // we are just comparing that bytes are exactly same in both files and not 94 // we are just comparing that bytes are exactly same in both files and not
83 // doing anything smart with text formatting. 95 // doing anything smart with text formatting.
84 std::ifstream file1(filename1.value().c_str(), 96 std::ifstream file1(filename1.value().c_str(),
85 std::ios::in | std::ios::binary); 97 std::ios::in | std::ios::binary);
86 std::ifstream file2(filename2.value().c_str(), 98 std::ifstream file2(filename2.value().c_str(),
87 std::ios::in | std::ios::binary); 99 std::ios::in | std::ios::binary);
88 100
89 // Even if both files aren't openable (and thus, in some sense, "equal"), 101 // Even if both files aren't openable (and thus, in some sense, "equal"),
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 // FileEnumerator 402 // FileEnumerator
391 // 403 //
392 // Note: the main logic is in file_util_<platform>.cc 404 // Note: the main logic is in file_util_<platform>.cc
393 405
394 bool FileEnumerator::ShouldSkip(const FilePath& path) { 406 bool FileEnumerator::ShouldSkip(const FilePath& path) {
395 FilePath::StringType basename = path.BaseName().value(); 407 FilePath::StringType basename = path.BaseName().value();
396 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); 408 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_));
397 } 409 }
398 410
399 } // namespace 411 } // namespace
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698