Commit a38320bf authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

Resolve a leading ~ in extension loading path to the user's home dir.

Bug: 815060
Change-Id: I358fca7bfeb19ef035703f37318f7eb0c90d7015
Reviewed-on: https://chromium-review.googlesource.com/976550Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547512}
parent 013076d5
......@@ -8,6 +8,7 @@
#include "base/callback.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/path_service.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
......@@ -25,6 +26,7 @@
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/install_flag.h"
#include "extensions/browser/path_util.h"
#include "extensions/browser/policy_check.h"
#include "extensions/browser/preload_check_group.h"
#include "extensions/browser/requirements_checker.h"
......@@ -107,7 +109,8 @@ bool UnpackedInstaller::LoadFromCommandLine(const base::FilePath& path_in,
// between extension loading and loading an URL from the command line.
base::ThreadRestrictions::ScopedAllowIO allow_io;
extension_path_ = base::MakeAbsoluteFilePath(path_in);
extension_path_ =
base::MakeAbsoluteFilePath(path_util::ResolveHomeDirectory(path_in));
if (!IsLoadingUnpackedAllowed()) {
ReportExtensionLoadError(kUnpackedExtensionsBlacklistedError);
......
......@@ -115,5 +115,26 @@ void CalculateAndFormatExtensionDirectorySize(
std::move(callback)));
}
base::FilePath ResolveHomeDirectory(const base::FilePath& path) {
#if defined(OS_WIN)
return path;
#else
const auto& value = path.value();
// Look for a path starting with the "~" character. It must be alone or
// followed by a separator.
if (value.empty() || value[0] != FILE_PATH_LITERAL('~') ||
(value.length() > 1 && !base::FilePath::IsSeparator(value[1]))) {
return path;
}
base::FilePath result;
PathService::Get(base::DIR_HOME, &result);
// The user could specify "~" or "~/", so be safe.
if (value.length() > 2) {
result = result.Append(value.substr(2));
}
return result;
#endif
}
} // namespace path_util
} // namespace extensions
......@@ -26,6 +26,10 @@ void CalculateAndFormatExtensionDirectorySize(
int message_id,
base::OnceCallback<void(const base::string16&)> callback);
// Returns a new FilePath with the '~' resolved to the home directory, if
// appropriate. Otherwise, returns the original path.
base::FilePath ResolveHomeDirectory(const base::FilePath& path);
} // namespace path_util
} // namespace extensions
......
......@@ -6,6 +6,8 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::FilePath;
......@@ -44,4 +46,36 @@ TEST(ExtensionPathUtilTest, BasicPrettifyPathTest) {
prettified);
}
TEST(ExtensionPathUtilTest, ResolveHomeDirTest) {
FilePath home_dir;
ASSERT_TRUE(PathService::Get(base::DIR_HOME, &home_dir));
const FilePath abs_path(FILE_PATH_LITERAL("/foo/bar/baz"));
const FilePath rel_path(FILE_PATH_LITERAL("foo/bar/baz"));
const FilePath rel_path_with_tilde(FILE_PATH_LITERAL("~/foo/bar"));
const FilePath rel_path_with_tilde_no_separator(FILE_PATH_LITERAL("~foobar"));
// This function is a no-op on Windows.
#if defined(OS_WIN)
EXPECT_EQ(rel_path_with_tilde,
path_util::ResolveHomeDirectory(rel_path_with_tilde));
#else
EXPECT_EQ(home_dir.Append("foo/bar"),
path_util::ResolveHomeDirectory(rel_path_with_tilde));
// Make sure tilde without any relative path works as expected.
EXPECT_EQ(home_dir,
path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~"))));
EXPECT_EQ(home_dir,
path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~/"))));
#endif
// An absolute path without a ~ should be untouched.
EXPECT_EQ(abs_path, path_util::ResolveHomeDirectory(abs_path));
// A relative path without a ~ should be untouched.
EXPECT_EQ(rel_path, path_util::ResolveHomeDirectory(rel_path));
// A tilde, followed by a non-separator character should not
// expand.
EXPECT_EQ(rel_path_with_tilde_no_separator,
path_util::ResolveHomeDirectory(rel_path_with_tilde_no_separator));
}
} // namespace extensions
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