| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/fileapi/file_system_path_manager.h" | 5 #include "webkit/fileapi/file_system_path_manager.h" |
| 6 | 6 |
| 7 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_callback_factory.h" | 9 #include "base/memory/scoped_callback_factory.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 local_provider_->GetFileSystemRootPathOnFileThread( | 93 local_provider_->GetFileSystemRootPathOnFileThread( |
| 94 origin_url, type, virtual_path, create) : | 94 origin_url, type, virtual_path, create) : |
| 95 FilePath(); | 95 FilePath(); |
| 96 case kFileSystemTypeUnknown: | 96 case kFileSystemTypeUnknown: |
| 97 default: | 97 default: |
| 98 NOTREACHED(); | 98 NOTREACHED(); |
| 99 return FilePath(); | 99 return FilePath(); |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool FileSystemPathManager::CrackFileSystemPath( | |
| 104 const FilePath& path, GURL* origin_url, FileSystemType* type, | |
| 105 FilePath* virtual_path) const { | |
| 106 // TODO(ericu): | |
| 107 // Paths come in here [for now] as a URL, followed by a virtual path in | |
| 108 // platform format. For example, on Windows, this will look like | |
| 109 // filesystem:http://www.example.com/temporary/\path\to\file.txt. | |
| 110 // A potentially dangerous malicious path on Windows might look like: | |
| 111 // filesystem:http://www.example.com/temporary/foo/../../\path\to\file.txt. | |
| 112 // This code is ugly, but will get cleaned up as we fix the calling side. | |
| 113 // Eventually there won't be a distinction between a filesystem path and a | |
| 114 // filesystem URL--they'll all be URLs. | |
| 115 // We should be passing these to WebKit as string, not FilePath, for ease of | |
| 116 // manipulation, or possibly as GURL/KURL. | |
| 117 | |
| 118 std::string path_as_string; | |
| 119 #ifdef OS_WIN | |
| 120 path_as_string = WideToUTF8(path.value()); | |
| 121 #else | |
| 122 path_as_string = path.value(); | |
| 123 #endif | |
| 124 GURL path_as_url(path_as_string); | |
| 125 | |
| 126 FilePath local_path; | |
| 127 GURL local_url; | |
| 128 FileSystemType local_type; | |
| 129 if (!CrackFileSystemURL(path_as_url, &local_url, &local_type, &local_path)) | |
| 130 return false; | |
| 131 | |
| 132 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 133 // TODO(ericu): This puts the separators back to windows-standard; they come | |
| 134 // out of the above code as '/' no matter the platform. Long-term, we'll | |
| 135 // want to let the underlying FileSystemFileUtil implementation do this part, | |
| 136 // since they won't all need it. | |
| 137 local_path = local_path.NormalizeWindowsPathSeparators(); | |
| 138 #endif | |
| 139 | |
| 140 // Check if file access to this type of file system is allowed | |
| 141 // for this origin. | |
| 142 switch (local_type) { | |
| 143 case kFileSystemTypeTemporary: | |
| 144 case kFileSystemTypePersistent: | |
| 145 if (!sandbox_provider_->IsAccessAllowed(local_url)) | |
| 146 return false; | |
| 147 break; | |
| 148 case kFileSystemTypeLocal: | |
| 149 if (!local_provider_.get() || | |
| 150 !local_provider_->IsAccessAllowed(local_url)) { | |
| 151 return false; | |
| 152 } | |
| 153 break; | |
| 154 case kFileSystemTypeUnknown: | |
| 155 default: | |
| 156 NOTREACHED(); | |
| 157 return false; | |
| 158 } | |
| 159 // Any paths that include parent references are considered invalid. | |
| 160 // These should have been taken care of in CrackFileSystemURL. | |
| 161 DCHECK(!local_path.ReferencesParent()); | |
| 162 | |
| 163 // The given |local_path| seems valid. Populates the |origin_url|, |type| | |
| 164 // and |virtual_path| if they are given. | |
| 165 | |
| 166 if (origin_url) { | |
| 167 *origin_url = local_url; | |
| 168 } | |
| 169 | |
| 170 if (type) | |
| 171 *type = local_type; | |
| 172 | |
| 173 if (virtual_path) { | |
| 174 *virtual_path = local_path; | |
| 175 } | |
| 176 | |
| 177 return true; | |
| 178 } | |
| 179 | |
| 180 bool FileSystemPathManager::IsAllowedScheme(const GURL& url) const { | 103 bool FileSystemPathManager::IsAllowedScheme(const GURL& url) const { |
| 181 // Basically we only accept http or https. We allow file:// URLs | 104 // Basically we only accept http or https. We allow file:// URLs |
| 182 // only if --allow-file-access-from-files flag is given. | 105 // only if --allow-file-access-from-files flag is given. |
| 183 return url.SchemeIs("http") || url.SchemeIs("https") || | 106 return url.SchemeIs("http") || url.SchemeIs("https") || |
| 184 url.SchemeIs(kExtensionScheme) || | 107 url.SchemeIs(kExtensionScheme) || |
| 185 (url.SchemeIsFile() && allow_file_access_from_files_); | 108 (url.SchemeIsFile() && allow_file_access_from_files_); |
| 186 } | 109 } |
| 187 | 110 |
| 188 // static | 111 // static |
| 189 std::string FileSystemPathManager::GetFileSystemTypeString( | 112 std::string FileSystemPathManager::GetFileSystemTypeString( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 205 case kFileSystemTypeLocal: | 128 case kFileSystemTypeLocal: |
| 206 return local_provider_.get() ? | 129 return local_provider_.get() ? |
| 207 local_provider_->IsRestrictedFileName(filename) : true; | 130 local_provider_->IsRestrictedFileName(filename) : true; |
| 208 case kFileSystemTypeUnknown: | 131 case kFileSystemTypeUnknown: |
| 209 default: | 132 default: |
| 210 NOTREACHED(); | 133 NOTREACHED(); |
| 211 return true; | 134 return true; |
| 212 } | 135 } |
| 213 } | 136 } |
| 214 | 137 |
| 138 // Checks if an origin has access to a particular filesystem type. |
| 139 bool FileSystemPathManager::IsAllowedFileSystemType( |
| 140 GURL origin, FileSystemType type) { |
| 141 switch (type) { |
| 142 case kFileSystemTypeTemporary: |
| 143 case kFileSystemTypePersistent: |
| 144 if (!sandbox_provider_->IsAccessAllowed(origin)) |
| 145 return false; |
| 146 break; |
| 147 case kFileSystemTypeLocal: |
| 148 if (!local_provider_.get() || |
| 149 !local_provider_->IsAccessAllowed(origin)) { |
| 150 return false; |
| 151 } |
| 152 break; |
| 153 case kFileSystemTypeUnknown: |
| 154 default: |
| 155 NOTREACHED(); |
| 156 return false; |
| 157 } |
| 158 return true; |
| 159 } |
| 160 |
| 215 } // namespace fileapi | 161 } // namespace fileapi |
| 216 | 162 |
| 217 COMPILE_ASSERT(int(WebFileSystem::TypeTemporary) == \ | 163 COMPILE_ASSERT(int(WebFileSystem::TypeTemporary) == \ |
| 218 int(fileapi::kFileSystemTypeTemporary), mismatching_enums); | 164 int(fileapi::kFileSystemTypeTemporary), mismatching_enums); |
| 219 COMPILE_ASSERT(int(WebFileSystem::TypePersistent) == \ | 165 COMPILE_ASSERT(int(WebFileSystem::TypePersistent) == \ |
| 220 int(fileapi::kFileSystemTypePersistent), mismatching_enums); | 166 int(fileapi::kFileSystemTypePersistent), mismatching_enums); |
| OLD | NEW |