Restrict plugin access to file system and network via appdomain

后端 未结 3 1311
耶瑟儿~
耶瑟儿~ 2020-12-01 13:34

I asked a while ago how to restrict plugins access ( I want to prevent them from writing to the disk or network ) and i was told to use AppDomain. I have searched and tried

3条回答
  •  渐次进展
    2020-12-01 13:43

    I guess this is what you need, if I understand correctly your point.

    System.Security.PermissionSet ps = 
        new System.Security.PermissionSet(System.Security.Permissions.PermissionState.None);
    ps.AddPermission(new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.NoAccess, "C:\\"));
    System.Security.Policy.PolicyLevel pl = System.Security.Policy.PolicyLevel.CreateAppDomainLevel();
    pl.RootCodeGroup.PolicyStatement = new System.Security.Policy.PolicyStatement(ps);
    AppDomain.CurrentDomain.SetAppDomainPolicy(pl);
    System.Reflection.Assembly myPluginAssembly = AppDomain.CurrentDomain.Load("MyPluginAssembly");
    

    Is this more precisely what you meant?

    Notice that you may provide an array of string containg the paths where you don't want the plugin to have access. You may provide if when initializing the new instance of FileIOPermission class.

    Let me know if this helps. :-)

提交回复
热议问题