// This is run on exit of this function to make sure we clear the pref // and that behaviour with the pref cleared is correct. auto cleanup = MakeScopeExit([&] {
nsresult rv = Preferences::ClearUser(kForbiddenPathsPref);
ASSERT_EQ(rv, NS_OK);
FilePreferences::InitPrefs();
ASSERT_EQ(FilePreferences::IsAllowedPath(nsLiteralCString(kForbidden)), true);
ASSERT_EQ(FilePreferences::IsAllowedPath(nsLiteralCString(kForbiddenDir)), true);
ASSERT_EQ(FilePreferences::IsAllowedPath(nsLiteralCString(kForbiddenFile)), true);
ASSERT_EQ(FilePreferences::IsAllowedPath(nsLiteralCString(kAllowed)), true);
});
// This is the directory we will forbid
nsCOMPtr<nsIFile> forbiddenDir;
nsresult rv =
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(forbiddenDir));
ASSERT_EQ(rv, NS_OK);
rv = forbiddenDir->GetNativePath(tempPath);
ASSERT_EQ(rv, NS_OK);
rv = forbiddenDir->AppendNative("forbidden_dir"_ns);
ASSERT_EQ(rv, NS_OK);
// This is executed at exit to clean up after ourselves. auto cleanup = MakeScopeExit([&] {
nsresult rv = Preferences::ClearUser(kForbiddenPathsPref);
ASSERT_EQ(rv, NS_OK);
FilePreferences::InitPrefs();
// Create the directory
rv = forbiddenDir->Create(nsIFile::DIRECTORY_TYPE, 0666);
ASSERT_EQ(rv, NS_OK);
// This is the file we will try to access
nsCOMPtr<nsIFile> forbiddenFile;
rv = forbiddenDir->Clone(getter_AddRefs(forbiddenFile));
ASSERT_EQ(rv, NS_OK);
rv = forbiddenFile->AppendNative("test_file"_ns);
// Create the file
ASSERT_EQ(rv, NS_OK);
rv = forbiddenFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
// Get the forbidden path
nsAutoCString forbiddenPath;
rv = forbiddenDir->GetNativePath(forbiddenPath);
ASSERT_EQ(rv, NS_OK);
// Set the pref and make sure it is enforced
rv = Preferences::SetCString(kForbiddenPathsPref, forbiddenPath);
ASSERT_EQ(rv, NS_OK);
FilePreferences::InitPrefs();
// Check that we can't access some of the file attributes
int64_t size;
rv = forbiddenFile->GetFileSize(&size);
ASSERT_EQ(rv, NS_ERROR_FILE_ACCESS_DENIED);
// Check that we can't enumerate the directory
nsCOMPtr<nsIDirectoryEnumerator> dirEnumerator;
rv = forbiddenDir->GetDirectoryEntries(getter_AddRefs(dirEnumerator));
ASSERT_EQ(rv, NS_ERROR_FILE_ACCESS_DENIED);
// Check that ./ does not bypass the filter
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(newPath));
ASSERT_EQ(rv, NS_OK);
rv = newPath->AppendRelativeNativePath("./forbidden_dir/file"_ns);
ASSERT_EQ(rv, NS_OK);
rv = newPath->Exists(&exists);
ASSERT_EQ(rv, NS_ERROR_FILE_ACCESS_DENIED);
// Check that .. does not bypass the filter
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(newPath));
ASSERT_EQ(rv, NS_OK);
rv = newPath->AppendRelativeNativePath("allowed/../forbidden_dir/file"_ns);
ASSERT_EQ(rv, NS_ERROR_FILE_UNRECOGNIZED_PATH);
// Check that we can't construct a path that is functionally the same // as the forbidden one and bypasses the filter.
trickyPath = tempPath;
trickyPath.AppendLiteral("/./forbidden_dir/file");
rv = newPath->InitWithNativePath(trickyPath);
ASSERT_EQ(rv, NS_ERROR_FILE_ACCESS_DENIED);
// Check that if the forbidden string is a directory, we only block access // to subresources, not the directory itself.
nsAutoCString forbiddenDirPath(forbiddenPath);
forbiddenDirPath.Append("/");
rv = Preferences::SetCString(kForbiddenPathsPref, forbiddenDirPath);
ASSERT_EQ(rv, NS_OK);
FilePreferences::InitPrefs();
// This should work, since we only block subresources
rv = forbiddenDir->Exists(&exists);
ASSERT_EQ(rv, NS_OK);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.