#include "chrome/browser/net/chrome_network_delegate.h"
#include "base/base_paths.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#if BUILDFLAG(IS_CHROMEOS)
#include <fnmatch.h>
#include "base/files/file_util.h"
#include "base/system/sys_info.h"
#include "chrome/common/chrome_paths.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ambient_time_of_day_constants.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "base/android/path_utils.h"
#endif
namespace {
bool g_access_to_all_files_enabled = …;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
bool IsPathOnAllowlist(const base::FilePath& path,
const std::vector<base::FilePath>& allowlist) {
for (const auto& allowlisted_path : allowlist) {
if (allowlisted_path == path.StripTrailingSeparators() ||
allowlisted_path.IsParent(path)) {
return true;
}
}
return false;
}
#endif
#if BUILDFLAG(IS_CHROMEOS)
bool IsLacrosLogFile(const base::FilePath& path) {
return (fnmatch("/home/chronos/user/lacros/lacros*.log", path.value().c_str(),
FNM_NOESCAPE) == 0) ||
(fnmatch("/var/log/lacros/lacros*.log", path.value().c_str(),
FNM_NOESCAPE) == 0);
}
bool IsAccessAllowedChromeOS(const base::FilePath& path,
const base::FilePath& profile_path) {
base::FilePath path_within_gcache_v2;
if (profile_path.Append("GCache/v2")
.AppendRelativePath(path, &path_within_gcache_v2)) {
std::vector<std::string> components = path_within_gcache_v2.GetComponents();
if (components.size() > 1 && components[1] == "Logs") {
return true;
}
}
if (IsLacrosLogFile(path))
return true;
static const base::FilePath::CharType* const kLocalAccessAllowList[] = {
"/home/chronos/user/MyFiles",
"/home/chronos/user/WebRTC Logs",
"/home/chronos/user/google-assistant-library/log",
"/home/chronos/user/lacros/Crash Reports",
"/home/chronos/user/log",
"/home/chronos/user/crostini.icons",
"/media",
"/opt/oem",
"/run/arc/sdcard/write/emulated/0",
"/usr/share/chromeos-assets",
"/var/log",
};
std::vector<base::FilePath> allowlist;
for (const auto* allowlisted_path : kLocalAccessAllowList)
allowlist.emplace_back(allowlisted_path);
base::FilePath temp_dir;
if (base::PathService::Get(base::DIR_TEMP, &temp_dir))
allowlist.push_back(temp_dir);
#if BUILDFLAG(IS_CHROMEOS_ASH)
if (!profile_path.empty()) {
allowlist.push_back(profile_path.AppendASCII("MyFiles"));
const base::FilePath webrtc_logs = profile_path.AppendASCII("WebRTC Logs");
allowlist.push_back(webrtc_logs);
}
if (!base::SysInfo::IsRunningOnChromeOS()) {
base::FilePath downloads_dir;
if (base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_dir))
allowlist.push_back(downloads_dir);
}
allowlist.push_back(
base::FilePath("/run/imageloader").Append(ash::kTimeOfDayDlcId));
#else
base::FilePath documents_dir;
if (base::PathService::Get(chrome::DIR_USER_DOCUMENTS, &documents_dir))
allowlist.push_back(documents_dir);
base::FilePath downloads_dir;
if (base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &downloads_dir))
allowlist.push_back(downloads_dir);
if (!profile_path.empty())
allowlist.push_back(profile_path.AppendASCII("WebRTC Logs"));
#endif
return IsPathOnAllowlist(path, allowlist);
}
#endif
#if BUILDFLAG(IS_ANDROID)
bool IsAccessAllowedAndroid(const base::FilePath& path) {
base::FilePath external_storage_path;
base::PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE,
&external_storage_path);
if (external_storage_path.IsParent(path))
return true;
std::vector<base::FilePath> allowlist;
std::vector<base::FilePath> all_download_dirs =
base::android::GetAllPrivateDownloadsDirectories();
allowlist.insert(allowlist.end(), all_download_dirs.begin(),
all_download_dirs.end());
base::android::BuildInfo* build_info =
base::android::BuildInfo::GetInstance();
if (build_info->sdk_int() > base::android::SDK_VERSION_Q) {
std::vector<base::FilePath> all_external_download_volumes =
base::android::GetSecondaryStorageDownloadDirectories();
allowlist.insert(allowlist.end(), all_external_download_volumes.begin(),
all_external_download_volumes.end());
}
static const base::FilePath::CharType* const kLocalAccessAllowList[] = {
"/sdcard",
"/mnt/sdcard",
};
for (const auto* allowlisted_path : kLocalAccessAllowList)
allowlist.emplace_back(allowlisted_path);
return IsPathOnAllowlist(path, allowlist);
}
#endif
bool IsAccessAllowedInternal(const base::FilePath& path,
const base::FilePath& profile_path) { … }
}
bool ChromeNetworkDelegate::IsAccessAllowed(
const base::FilePath& path,
const base::FilePath& profile_path) { … }
bool ChromeNetworkDelegate::IsAccessAllowed(
const base::FilePath& path,
const base::FilePath& absolute_path,
const base::FilePath& profile_path) { … }
void ChromeNetworkDelegate::EnableAccessToAllFilesForTesting(bool enabled) { … }