使用 Win32 API 拒绝 UWP 文件夹访问权限

标签 uwp

我正在移植 libgit2得到 Git GUI for UWP .一个场景是让用户使用 FolderPicker::PickSingleFolderAsync 选择一个存储库文件夹然后将其添加到 FutureAccessList以便应用程序可以访问它以获取常用的 Git 功能。问题在于底层 Git 代码严重依赖 Win32 API 文件和文件夹访问,例如 FindFirstFileMoveFileEx、... 用于文件和文件夹 stat, open, ... 并且应用在尝试访问它时报告Permission denied。 (该代码适用于应用程序本地存储中的文件夹。我还检查了通常的 stdio 只能在那里工作。不能在外面使用 fopen。)

是否有可行的解决方案?难道不应该在不同的受支持 API 之间尊重许可,我可能错过了什么? (我不敢尝试移植 libgit2 所需的所有 POSIX。一方面,它会保证效率低下。另一方面,它极易出错,例如编写 mmapopen 配合良好。)

最佳答案

不可能实现这样的目标!

根据@RobCaplan https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/ ,Microsoft 的天才发明了一种既不安全也不向后兼容的安全存储解决方案,也就是让开发人员的生活更轻松:一旦用户授予StorageFolder 应用程序,该应用程序可以使用提供的 StorageFile API 对其造成严重破坏。以下代码

auto folderPicker = ref new Windows::Storage::Pickers::FolderPicker();
folderPicker->FileTypeFilter->Clear();
folderPicker->FileTypeFilter->Append("*");

create_task(folderPicker->PickSingleFolderAsync()).then([](Windows::Storage::StorageFolder^ folder)
{
    if (folder == nullptr)
        cancel_current_task();
    Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList->Add(folder);
    create_task(folder->GetItemsAsync()).then([](IVectorView<IStorageItem^>^ items)
    {
        // Delete the folder content or encrypt it and demand money
        auto iter = items->First();
        while (iter->HasCurrent)
        {
            create_task(iter->Current->DeleteAsync(StorageDeleteOption::PermanentDelete));
            iter->MoveNext();
        }
    });
});

将愉快地清除不幸的用户选择的文件夹。恶意应用程序甚至不需要使用 Win32 API 来执行此操作。从逻辑上讲,API 不是安全问题的原因。现有的 UWP Win32 API 显然可以正确处理本地存储访问,因此在 Win32 API 中支持 FutureAccessList 应该花费最少的精力;这种让 UWP 开发变得困难的愿望一定是故意的。 (毫无疑问,Centenial 不会会飞起来。没有人愿意从 Win32 的巨大灵 active 转移到 UWP jail 。)

编辑:我应该写

It is IMPOSSIBLE to achieve such thing the way I wanted!

因为这篇文章确实提出了一个快速且非常聪明的解决方案

If the library doesn’t have such an interface and you cannot add one then you will need to copy the StorageFile contents into the application data folder (likely in the TemporaryFolder) and then pass the path to the temporary copy to the library.

所以在我的情况下,每次用户选择一个存储库文件夹时,我都可以将整个文件夹复制到本地存储,对其进行操作,然后将整个文件夹复制回其原始位置。当然,上面的“我想要的方式”指的是高效的方式,您不必来回复制东西。

关于使用 Win32 API 拒绝 UWP 文件夹访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090054/

相关文章:

c++ - Microsoft Visual C++、Visual Studio,如何修复错误 : identifier "GetAsyncKeyState" is undefined

c# - 我可以在 UWP 应用程序中使用 SQL CE 数据库吗?

c# - 如何使用VisualState AdaptiveTrigger改变ListView Item FontSize

webview - UWP 应用程序 WebView 泄漏内存,无法清除图像

uwp - 如何将文件复制到另一个文件夹并删除文件

visual-studio - 将 Assets 复制到 UWP 应用程序包中

c# - 缺少 UWP 样式触发器

c# - DataTemplate 无法解析 DataType 前缀数据

Xamarin.Forms map 自动关注用户位置

c# - 使用 UWP 将信息保存到 SOAP