blob: 208b68ddfb764a52939795d69ce74b37346e6f24 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ac039522010-06-15 16:39:442// 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]d8e4f132012-09-06 04:28:057#include "base/base_paths.h"
[email protected]7a299a92012-10-24 23:54:508#include "base/logging.h"
[email protected]d8e4f132012-09-06 04:28:059#include "base/path_service.h"
avi6846aef2015-12-26 01:09:3810#include "build/build_config.h"
[email protected]d05ef99c2011-02-01 21:38:1611
zpengdb4a58e2017-01-10 17:40:3212#if defined(OS_ANDROID)
Xing Liub9456c12018-05-11 01:46:1713#include "base/android/path_utils.h"
[email protected]4a2b6232014-06-19 08:44:1414#endif
15
[email protected]d05ef99c2011-02-01 21:38:1616namespace {
17
satoruxd18e61a2017-06-08 06:38:4618bool g_access_to_all_files_enabled = false;
19
Ken Rockot314714c2017-11-05 23:36:2420bool IsAccessAllowedInternal(const base::FilePath& path,
21 const base::FilePath& profile_path) {
Chong Zhanga7f5b322018-09-20 00:05:2422 if (g_access_to_all_files_enabled)
23 return true;
24
Ken Rockot314714c2017-11-05 23:36:2425#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 Pacheco857feaa2018-12-11 20:19:1335 "/home/chronos/user/MyFiles",
Ken Rockot314714c2017-11-05 23:36:2436 "/home/chronos/user/log",
37 "/home/chronos/user/WebRTC Logs",
38 "/media",
39 "/opt/oem",
Naoki Fukinoba401a52018-08-14 06:48:0140 "/run/arc/sdcard/write/emulated/0",
Ken Rockot314714c2017-11-05 23:36:2441 "/usr/share/chromeos-assets",
42 "/var/log",
43 };
44
45 base::FilePath temp_dir;
Avi Drissmanea15ea02018-05-07 18:55:1246 if (base::PathService::Get(base::DIR_TEMP, &temp_dir))
Ken Rockot314714c2017-11-05 23:36:2447 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 Pacheco857feaa2018-12-11 20:19:1357 whitelist.push_back(profile_path.AppendASCII("MyFiles"));
Ken Rockot314714c2017-11-05 23:36:2458 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 Drissman9098f9002018-05-04 00:11:5264 base::PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE,
65 &external_storage_path);
Ken Rockot314714c2017-11-05 23:36:2466 if (external_storage_path.IsParent(path))
67 return true;
68
Xing Liub9456c12018-05-11 01:46:1769 auto all_download_dirs = base::android::GetAllPrivateDownloadsDirectories();
70 for (const auto& dir : all_download_dirs)
71 whitelist.push_back(dir);
72
Ken Rockot314714c2017-11-05 23:36:2473 // 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 McNally1c16a4a2018-10-06 03:51:2390#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 Rockot314714c2017-11-05 23:36:24104 DVLOG(1) << "File access denied - " << path.value().c_str();
105 return false;
106#endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
107}
108
[email protected]d05ef99c2011-02-01 21:38:16109} // namespace
[email protected]ac039522010-06-15 16:39:44110
satorux7c5360582017-01-27 07:24:29111// static
112bool ChromeNetworkDelegate::IsAccessAllowed(
113 const base::FilePath& path,
114 const base::FilePath& profile_path) {
Ken Rockot314714c2017-11-05 23:36:24115 return IsAccessAllowedInternal(path, profile_path);
116}
117
118// static
119bool 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);
satorux7c5360582017-01-27 07:24:29127#else
Ken Rockot314714c2017-11-05 23:36:24128 return (IsAccessAllowedInternal(path, profile_path) &&
129 IsAccessAllowedInternal(absolute_path, profile_path));
[email protected]d8e4f132012-09-06 04:28:05130#endif
[email protected]4c219e22012-05-05 19:41:04131}
[email protected]a1d4ab072012-06-07 13:21:15132
satoruxd18e61a2017-06-08 06:38:46133// static
134void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) {
135 g_access_to_all_files_enabled = enabled;
136}