зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1847572 - Expand home directory paths for arbitrary users r=xpcom-reviewers,nika
Differential Revision: https://phabricator.services.mozilla.com/D185999
This commit is contained in:
Родитель
b36e4cef47
Коммит
ac308994f4
|
@ -803,6 +803,7 @@ if (AppConstants.platform == "win") {
|
|||
});
|
||||
} else {
|
||||
const homeDir = Services.dirsvc.get("Home", Ci.nsIFile).path;
|
||||
const homeBase = AppConstants.platform == "macosx" ? "/Users" : "/home";
|
||||
|
||||
testcases.push({
|
||||
input: "~",
|
||||
|
@ -814,6 +815,16 @@ if (AppConstants.platform == "win") {
|
|||
fixedURI: `file://${homeDir}/foo`,
|
||||
protocolChange: true,
|
||||
});
|
||||
testcases.push({
|
||||
input: "~foo",
|
||||
fixedURI: `file://${homeBase}/foo`,
|
||||
protocolChange: true,
|
||||
});
|
||||
testcases.push({
|
||||
input: "~foo/bar",
|
||||
fixedURI: `file://${homeBase}/foo/bar`,
|
||||
protocolChange: true,
|
||||
});
|
||||
testcases.push({
|
||||
input: "/some/file.txt",
|
||||
fixedURI: "file:///some/file.txt",
|
||||
|
|
|
@ -307,19 +307,35 @@ nsLocalFile::Clone(nsIFile** aFile) {
|
|||
|
||||
NS_IMETHODIMP
|
||||
nsLocalFile::InitWithNativePath(const nsACString& aFilePath) {
|
||||
if (aFilePath.EqualsLiteral("~") ||
|
||||
Substring(aFilePath, 0, 2).EqualsLiteral("~/")) {
|
||||
nsCOMPtr<nsIFile> homeDir;
|
||||
nsAutoCString homePath;
|
||||
if (NS_FAILED(
|
||||
NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(homeDir))) ||
|
||||
NS_FAILED(homeDir->GetNativePath(homePath))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
if (!aFilePath.IsEmpty() && aFilePath.First() == '~') {
|
||||
if (aFilePath.Length() == 1 || aFilePath.CharAt(1) == '/') {
|
||||
// Home dir for the current user
|
||||
|
||||
mPath = homePath;
|
||||
if (aFilePath.Length() > 2) {
|
||||
mPath.Append(Substring(aFilePath, 1, aFilePath.Length() - 1));
|
||||
nsCOMPtr<nsIFile> homeDir;
|
||||
nsAutoCString homePath;
|
||||
if (NS_FAILED(NS_GetSpecialDirectory(NS_OS_HOME_DIR,
|
||||
getter_AddRefs(homeDir))) ||
|
||||
NS_FAILED(homeDir->GetNativePath(homePath))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
mPath = homePath;
|
||||
if (aFilePath.Length() > 2) {
|
||||
mPath.Append(Substring(aFilePath, 1));
|
||||
}
|
||||
} else {
|
||||
// Home dir for an arbitrary user e.g. `~foo/bar` -> `/home/foo/bar`
|
||||
// (`/Users/foo/bar` on Mac). The accurate way to get this directory
|
||||
// is with `getpwnam`, but we would like to avoid doing blocking
|
||||
// filesystem I/O while creating an `nsIFile`.
|
||||
|
||||
mPath =
|
||||
#ifdef XP_MACOSX
|
||||
"/Users/"_ns
|
||||
#else
|
||||
"/home/"_ns
|
||||
#endif
|
||||
+ Substring(aFilePath, 1);
|
||||
}
|
||||
} else {
|
||||
if (aFilePath.IsEmpty() || aFilePath.First() != '/') {
|
||||
|
|
|
@ -160,6 +160,31 @@ TEST(TestFilePreferencesUnix, Simple)
|
|||
rv = newPath->AppendNative(".."_ns);
|
||||
ASSERT_EQ(rv, NS_ERROR_FILE_UNRECOGNIZED_PATH);
|
||||
|
||||
#ifdef XP_UNIX
|
||||
nsAutoCString homePath;
|
||||
NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(newPath));
|
||||
newPath->GetNativePath(homePath);
|
||||
|
||||
newPath->InitWithNativePath("~"_ns);
|
||||
ASSERT_TRUE(newPath->NativePath().Equals(homePath));
|
||||
|
||||
newPath->InitWithNativePath("~/foo"_ns);
|
||||
ASSERT_TRUE(newPath->NativePath().Equals(homePath + "/foo"_ns));
|
||||
|
||||
nsLiteralCString homeBase =
|
||||
# ifdef XP_MACOSX
|
||||
"/Users"_ns;
|
||||
# else
|
||||
"/home"_ns;
|
||||
# endif
|
||||
|
||||
newPath->InitWithNativePath("~foo"_ns);
|
||||
ASSERT_TRUE(newPath->NativePath().Equals(homeBase + "/foo"_ns));
|
||||
|
||||
newPath->InitWithNativePath("~foo/bar"_ns);
|
||||
ASSERT_TRUE(newPath->NativePath().Equals(homeBase + "/foo/bar"_ns));
|
||||
#endif
|
||||
|
||||
nsAutoCString trickyPath(tempPath);
|
||||
trickyPath.AppendLiteral("/allowed/../forbidden_dir/file");
|
||||
rv = newPath->InitWithNativePath(trickyPath);
|
||||
|
|
Загрузка…
Ссылка в новой задаче