[DOM] Filter out symlinks for webkitdirectory.

This is effectively a back-out of the following patches from Bug 1274959
except we add comments and test coverage:

- Part 1 which made the DirectoryListingTask include symlinks in the
  results as exposed by Directory.getFilesAndDirectories.
- Part 3 which made GetFilesHelper include symlinks in the results.

Test coverage for getFilesAndDirectories is provided by
dom/filesystem/tests/test_basic.html by way of changes to its included
file dom/filesystem/tests/filesystem_commons.js and changes to the
createTreeFile helper in dom/filesystem/tests/script_fileList.js.
Test coverage for GetFilesHelper is provided by
dom/filesystem/tests/test_webkitdirectory.html and changes to the
createTestFile helper in dom/filesystem/tests/script_fileList.js.

Commenting out either of the `isLink` test in the relevant C++ code will
cause the given tests to fail on non-windows platforms.
This commit is contained in:
Moonchild 2023-07-05 21:55:00 +02:00 committed by roytam1
commit a299eaa965
6 changed files with 134 additions and 87 deletions

View file

@ -333,13 +333,8 @@ GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
return NS_OK;
}
nsresult rv = AddExploredDirectory(aFile);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsISimpleEnumerator> entries;
rv = aFile->GetDirectoryEntries(getter_AddRefs(entries));
nsresult rv = aFile->GetDirectoryEntries(getter_AddRefs(entries));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -361,7 +356,12 @@ GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
bool isLink, isSpecial, isFile, isDir;
if (NS_WARN_IF(NS_FAILED(currFile->IsSymlink(&isLink)) ||
NS_FAILED(currFile->IsSpecial(&isSpecial))) ||
isSpecial) {
isSpecial ||
// Although we allow explicit individual selection of symlinks via the
// file picker, we do not process symlinks in directory traversal. Our
// specific policy decision is documented at
// https://bugzilla.mozilla.org/show_bug.cgi?id=1813299#c20
isLink) {
continue;
}
@ -371,11 +371,6 @@ GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
continue;
}
// We don't want to explore loops of links.
if (isDir && isLink && !ShouldFollowSymLink(currFile)) {
continue;
}
// The new domPath
nsAutoString domPath;
domPath.Assign(aDOMPath);
@ -415,69 +410,6 @@ GetFilesHelperBase::ExploreDirectory(const nsAString& aDOMPath, nsIFile* aFile)
return NS_OK;
}
nsresult
GetFilesHelperBase::AddExploredDirectory(nsIFile* aDir)
{
nsresult rv;
#ifdef DEBUG
bool isDir;
rv = aDir->IsDirectory(&isDir);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_ASSERT(isDir, "Why are we here?");
#endif
bool isLink;
rv = aDir->IsSymlink(&isLink);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsAutoCString path;
if (!isLink) {
nsAutoString path16;
rv = aDir->GetPath(path16);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
path = NS_ConvertUTF16toUTF8(path16);
} else {
rv = aDir->GetNativeTarget(path);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
mExploredDirectories.PutEntry(path);
return NS_OK;
}
bool
GetFilesHelperBase::ShouldFollowSymLink(nsIFile* aDir)
{
#ifdef DEBUG
bool isLink, isDir;
if (NS_WARN_IF(NS_FAILED(aDir->IsSymlink(&isLink)) ||
NS_FAILED(aDir->IsDirectory(&isDir)))) {
return false;
}
MOZ_ASSERT(isLink && isDir, "Why are we here?");
#endif
nsAutoCString targetPath;
if (NS_WARN_IF(NS_FAILED(aDir->GetNativeTarget(targetPath)))) {
return false;
}
return !mExploredDirectories.Contains(targetPath);
}
void
GetFilesHelper::ResolveOrRejectPromise(Promise* aPromise)
{