使用 MEF 并在接口上使用继承的导出是否会让我的代码对 vunerability 开放

本文关键字:是否 我的 代码 开放 vunerability MEF 接口 继承 使用 | 更新日期: 2023-09-27 18:30:54

所以我使用 MEF 将插件动态加载到我的应用程序中。

我建立了这样的DirectoryCatalogue

//An aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();
foreach (var path in Directory.EnumerateDirectories(Properties.Settings.Default.PluginDirectory, "*", System.IO.SearchOption.TopDirectoryOnly))
{
     catalog.Catalogs.Add(new DirectoryCatalog(path));
}
// Create the CompositionContainer with all parts in the catalog (links Exports and Imports)
var container = new CompositionContainer(catalog);
//Fill the imports of this object
container.ComposeParts(this);

并具有标记为导入的属性:

    [ImportMany]
    public ObservableCollection<ISyncPlugin> SyncPlugins
    {
        get;
        set;
    }

但是我不禁觉得导出一个接口让我的程序对 vunerability 开放。如果编码人员发现我的接口正在导出,他们可以实现我的接口并编写将由我的程序加载和运行的恶意代码。有没有更好的方法可以做到这一点?

使用 MEF 并在接口上使用继承的导出是否会让我的代码对 vunerability 开放

插件

结构背后的想法是允许其他人为您的程序编写扩展。也就是说,您正在授予对您的程序的访问权限,并授予其他人他们想要的任何权限。如果您需要阻止插件执行某些操作,则必须这样做。

请参阅此处有关"安全性"的说明:
如何控制谁可以为 MEF 应用程序编写扩展