[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 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 "chrome/browser/net/chrome_network_delegate.h" |
| 6 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 7 | #include "base/base_paths.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 9 | #include "base/path_service.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 11 | |
zpeng | db4a58e | 2017-01-10 17:40:32 | [diff] [blame] | 12 | #if defined(OS_ANDROID) |
Xing Liu | b9456c1 | 2018-05-11 01:46:17 | [diff] [blame] | 13 | #include "base/android/path_utils.h" |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 14 | #endif |
| 15 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 16 | namespace { |
| 17 | |
satorux | d18e61a | 2017-06-08 06:38:46 | [diff] [blame] | 18 | bool g_access_to_all_files_enabled = false; |
| 19 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 20 | bool IsAccessAllowedInternal(const base::FilePath& path, |
| 21 | const base::FilePath& profile_path) { |
Chong Zhang | a7f5b32 | 2018-09-20 00:05:24 | [diff] [blame] | 22 | if (g_access_to_all_files_enabled) |
| 23 | return true; |
| 24 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 25 | #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
| 26 | return true; |
| 27 | #else |
| 28 | |
| 29 | std::vector<base::FilePath> whitelist; |
| 30 | #if defined(OS_CHROMEOS) |
| 31 | // Use a whitelist to only allow access to files residing in the list of |
| 32 | // directories below. |
| 33 | static const base::FilePath::CharType* const kLocalAccessWhiteList[] = { |
| 34 | "/home/chronos/user/Downloads", |
Luciano Pacheco | 857feaa | 2018-12-11 20:19:13 | [diff] [blame] | 35 | "/home/chronos/user/MyFiles", |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 36 | "/home/chronos/user/log", |
| 37 | "/home/chronos/user/WebRTC Logs", |
| 38 | "/media", |
| 39 | "/opt/oem", |
Naoki Fukino | ba401a5 | 2018-08-14 06:48:01 | [diff] [blame] | 40 | "/run/arc/sdcard/write/emulated/0", |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 41 | "/usr/share/chromeos-assets", |
| 42 | "/var/log", |
| 43 | }; |
| 44 | |
| 45 | base::FilePath temp_dir; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 46 | if (base::PathService::Get(base::DIR_TEMP, &temp_dir)) |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 47 | whitelist.push_back(temp_dir); |
| 48 | |
| 49 | // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under |
| 50 | // the profile path ("/home/chronos/user' is a hard link to current primary |
| 51 | // logged in profile.) For the support of multi-profile sessions, we are |
| 52 | // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such |
| 53 | // access. |
| 54 | if (!profile_path.empty()) { |
| 55 | const base::FilePath downloads = profile_path.AppendASCII("Downloads"); |
| 56 | whitelist.push_back(downloads); |
Luciano Pacheco | 857feaa | 2018-12-11 20:19:13 | [diff] [blame] | 57 | whitelist.push_back(profile_path.AppendASCII("MyFiles")); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 58 | const base::FilePath webrtc_logs = profile_path.AppendASCII("WebRTC Logs"); |
| 59 | whitelist.push_back(webrtc_logs); |
| 60 | } |
| 61 | #elif defined(OS_ANDROID) |
| 62 | // Access to files in external storage is allowed. |
| 63 | base::FilePath external_storage_path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 64 | base::PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, |
| 65 | &external_storage_path); |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 66 | if (external_storage_path.IsParent(path)) |
| 67 | return true; |
| 68 | |
Xing Liu | b9456c1 | 2018-05-11 01:46:17 | [diff] [blame] | 69 | auto all_download_dirs = base::android::GetAllPrivateDownloadsDirectories(); |
| 70 | for (const auto& dir : all_download_dirs) |
| 71 | whitelist.push_back(dir); |
| 72 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 73 | // Whitelist of other allowed directories. |
| 74 | static const base::FilePath::CharType* const kLocalAccessWhiteList[] = { |
| 75 | "/sdcard", "/mnt/sdcard", |
| 76 | }; |
| 77 | #endif |
| 78 | |
| 79 | for (const auto* whitelisted_path : kLocalAccessWhiteList) |
| 80 | whitelist.push_back(base::FilePath(whitelisted_path)); |
| 81 | |
| 82 | for (const auto& whitelisted_path : whitelist) { |
| 83 | // base::FilePath::operator== should probably handle trailing separators. |
| 84 | if (whitelisted_path == path.StripTrailingSeparators() || |
| 85 | whitelisted_path.IsParent(path)) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
Sam McNally | 1c16a4a | 2018-10-06 03:51:23 | [diff] [blame] | 90 | #if defined(OS_CHROMEOS) |
| 91 | // Allow access to DriveFS logs. These reside in |
| 92 | // $PROFILE_PATH/GCache/v2/<opaque id>/Logs. |
| 93 | base::FilePath path_within_gcache_v2; |
| 94 | if (profile_path.Append("GCache/v2") |
| 95 | .AppendRelativePath(path, &path_within_gcache_v2)) { |
| 96 | std::vector<std::string> components; |
| 97 | path_within_gcache_v2.GetComponents(&components); |
| 98 | if (components.size() > 1 && components[1] == "Logs") { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | #endif // defined(OS_CHROMEOS) |
| 103 | |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 104 | DVLOG(1) << "File access denied - " << path.value().c_str(); |
| 105 | return false; |
| 106 | #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
| 107 | } |
| 108 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 109 | } // namespace |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 110 | |
satorux | 7c536058 | 2017-01-27 07:24:29 | [diff] [blame] | 111 | // static |
| 112 | bool ChromeNetworkDelegate::IsAccessAllowed( |
| 113 | const base::FilePath& path, |
| 114 | const base::FilePath& profile_path) { |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 115 | return IsAccessAllowedInternal(path, profile_path); |
| 116 | } |
| 117 | |
| 118 | // static |
| 119 | bool ChromeNetworkDelegate::IsAccessAllowed( |
| 120 | const base::FilePath& path, |
| 121 | const base::FilePath& absolute_path, |
| 122 | const base::FilePath& profile_path) { |
| 123 | #if defined(OS_ANDROID) |
| 124 | // Android's whitelist relies on symbolic links (ex. /sdcard is whitelisted |
| 125 | // and commonly a symbolic link), thus do not check absolute paths. |
| 126 | return IsAccessAllowedInternal(path, profile_path); |
satorux | 7c536058 | 2017-01-27 07:24:29 | [diff] [blame] | 127 | #else |
Ken Rockot | 314714c | 2017-11-05 23:36:24 | [diff] [blame] | 128 | return (IsAccessAllowedInternal(path, profile_path) && |
| 129 | IsAccessAllowedInternal(absolute_path, profile_path)); |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 130 | #endif |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 131 | } |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 132 | |
satorux | d18e61a | 2017-06-08 06:38:46 | [diff] [blame] | 133 | // static |
| 134 | void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) { |
| 135 | g_access_to_all_files_enabled = enabled; |
| 136 | } |