Commit d8f45b46 authored by James Cook's avatar James Cook Committed by Chromium LUCI CQ

Clean up ChromeNetworkDelegate helper functions

The main IsAccessAllowedInternal function has several #if blocks.
Extract the per-platform logic into IsAccessAllowedChromeOS() and
IsAccessAllowAndroid() helper functions.

This simplifies the #if blocks in the main function, and allows
all #ifs to be positive-sense (no !defined anymore).

No functional changes -- this is just cleanup before I add yet
another #if for lacros.

Bug: 1150934
Test: existing unit_tests
Change-Id: Ic0fb5ad7cc2d8b1d566c12baf8a7c8cb0d8898f2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2568813
Commit-Queue: James Cook <jamescook@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#832684}
parent efc0b7c3
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include "chrome/browser/net/chrome_network_delegate.h" #include "chrome/browser/net/chrome_network_delegate.h"
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/logging.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "build/chromeos_buildflags.h" #include "build/chromeos_buildflags.h"
...@@ -24,18 +23,38 @@ namespace { ...@@ -24,18 +23,38 @@ namespace {
bool g_access_to_all_files_enabled = false; bool g_access_to_all_files_enabled = false;
bool IsAccessAllowedInternal(const base::FilePath& path, #if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS) || \
const base::FilePath& profile_path) { defined(OS_ANDROID)
if (g_access_to_all_files_enabled) // Returns true if |allowlist| contains |path| or a parent of |path|.
return true; bool IsPathOnAllowlist(const base::FilePath& path,
const std::vector<base::FilePath>& allowlist) {
#if !BUILDFLAG(IS_CHROMEOS_ASH) && !BUILDFLAG(IS_CHROMEOS_LACROS) && \ for (const auto& allowlisted_path : allowlist) {
!defined(OS_ANDROID) // base::FilePath::operator== should probably handle trailing separators.
return true; if (allowlisted_path == path.StripTrailingSeparators() ||
#else allowlisted_path.IsParent(path)) {
return true;
}
}
return false;
}
#endif
std::vector<base::FilePath> allowlist;
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS) #if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
// Returns true if access is allowed for |path| for a user with |profile_path).
bool IsAccessAllowedChromeOS(const base::FilePath& path,
const base::FilePath& profile_path) {
// Allow access to DriveFS logs. These reside in
// $PROFILE_PATH/GCache/v2/<opaque id>/Logs.
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(&components);
if (components.size() > 1 && components[1] == "Logs") {
return true;
}
}
// Use an allowlist to only allow access to files residing in the list of // Use an allowlist to only allow access to files residing in the list of
// directories below. // directories below.
static const base::FilePath::CharType* const kLocalAccessAllowList[] = { static const base::FilePath::CharType* const kLocalAccessAllowList[] = {
...@@ -49,6 +68,9 @@ bool IsAccessAllowedInternal(const base::FilePath& path, ...@@ -49,6 +68,9 @@ bool IsAccessAllowedInternal(const base::FilePath& path,
"/usr/share/chromeos-assets", "/usr/share/chromeos-assets",
"/var/log", "/var/log",
}; };
std::vector<base::FilePath> allowlist;
for (const auto* allowlisted_path : kLocalAccessAllowList)
allowlist.emplace_back(allowlisted_path);
base::FilePath temp_dir; base::FilePath temp_dir;
if (base::PathService::Get(base::DIR_TEMP, &temp_dir)) if (base::PathService::Get(base::DIR_TEMP, &temp_dir))
...@@ -71,7 +93,13 @@ bool IsAccessAllowedInternal(const base::FilePath& path, ...@@ -71,7 +93,13 @@ bool IsAccessAllowedInternal(const base::FilePath& path,
if (!base::SysInfo::IsRunningOnChromeOS()) if (!base::SysInfo::IsRunningOnChromeOS())
allowlist.push_back(DownloadPrefs::GetDefaultDownloadDirectory()); allowlist.push_back(DownloadPrefs::GetDefaultDownloadDirectory());
#elif defined(OS_ANDROID) return IsPathOnAllowlist(path, allowlist);
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
#if defined(OS_ANDROID)
// Returns true if access is allowed for |path|.
bool IsAccessAllowedAndroid(const base::FilePath& path) {
// Access to files in external storage is allowed. // Access to files in external storage is allowed.
base::FilePath external_storage_path; base::FilePath external_storage_path;
base::PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, base::PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE,
...@@ -79,6 +107,7 @@ bool IsAccessAllowedInternal(const base::FilePath& path, ...@@ -79,6 +107,7 @@ bool IsAccessAllowedInternal(const base::FilePath& path,
if (external_storage_path.IsParent(path)) if (external_storage_path.IsParent(path))
return true; return true;
std::vector<base::FilePath> allowlist;
std::vector<base::FilePath> all_download_dirs = std::vector<base::FilePath> all_download_dirs =
base::android::GetAllPrivateDownloadsDirectories(); base::android::GetAllPrivateDownloadsDirectories();
allowlist.insert(allowlist.end(), all_download_dirs.begin(), allowlist.insert(allowlist.end(), all_download_dirs.begin(),
...@@ -98,36 +127,25 @@ bool IsAccessAllowedInternal(const base::FilePath& path, ...@@ -98,36 +127,25 @@ bool IsAccessAllowedInternal(const base::FilePath& path,
"/sdcard", "/sdcard",
"/mnt/sdcard", "/mnt/sdcard",
}; };
#endif
for (const auto* allowlisted_path : kLocalAccessAllowList) for (const auto* allowlisted_path : kLocalAccessAllowList)
allowlist.push_back(base::FilePath(allowlisted_path)); allowlist.emplace_back(allowlisted_path);
for (const auto& allowlisted_path : allowlist) { return IsPathOnAllowlist(path, allowlist);
// base::FilePath::operator== should probably handle trailing separators. }
if (allowlisted_path == path.StripTrailingSeparators() || #endif // defined(OS_ANDROID)
allowlisted_path.IsParent(path)) {
return true;
}
}
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS) bool IsAccessAllowedInternal(const base::FilePath& path,
// Allow access to DriveFS logs. These reside in const base::FilePath& profile_path) {
// $PROFILE_PATH/GCache/v2/<opaque id>/Logs. if (g_access_to_all_files_enabled)
base::FilePath path_within_gcache_v2; return true;
if (profile_path.Append("GCache/v2")
.AppendRelativePath(path, &path_within_gcache_v2)) {
std::vector<std::string> components;
path_within_gcache_v2.GetComponents(&components);
if (components.size() > 1 && components[1] == "Logs") {
return true;
}
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
DVLOG(1) << "File access denied - " << path.value().c_str(); #if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_CHROMEOS_LACROS)
return false; return IsAccessAllowedChromeOS(path, profile_path);
#endif // !BUILDFLAG(IS_CHROMEOS_ASH) && !defined(OS_ANDROID) #elif defined(OS_ANDROID)
return IsAccessAllowedAndroid(path);
#else
return true;
#endif
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment