c# - Code Access Security exception in restricted AppDomain -
goal: need run code in appdomain limited permissions - should have no access @ fancy or unsafe, except few helper methods have defined elsewhere.
what i've done: i'm creating sandbox appdomain required basic permissions, , creating proxy object, runs code:
static appdomain createsandbox() { var e = new evidence(); e.addhostevidence(new zone(securityzone.internet)); var ps = securitymanager.getstandardsandbox(e); var security = new securitypermission(securitypermissionflag.execution); ps.addpermission(security); var setup = new appdomainsetup { applicationbase = path.getdirectoryname(assembly.getexecutingassembly().location) }; return appdomain.createdomain("sandbox" + datetime.now, null, setup, ps); } public class proxy : marshalbyrefobject { public proxy() { } public dostuff() { // perform custom operation requiring permission helperassembly.helpermethods.method1(); // other stuff low permission level ... ... ... } }
i've put helper methods in dedicated strong-named assembly, , marked them , container class [securitysafecritical]:
// helperassembly.dll namespace helperassembly { [securitysafecritical] public class helpermethods { [securitysafecritical] public static void method1() { new securitypermission(securitypermissionflag.unmanagedcode) .assert(); try { // logic requiring unmanaged code ... } { codeaccesssecurity.revertall(); } } } }
then, load helper assembly in sandbox appdomain , run proxy.dostuff(), expecting execute helper method , on way:
var appdomain = createsandbox(); appdomain.load(typeof(helperassembly.helpermethods).assembly.fullname); var proxy = (proxy)sandbox.createinstance( typeof(proxy).assembly.fullname, typeof(proxy).fullname).unwrap(); proxy.dostuff();
however, running code causes exception on assert() line in helper method:
unhandled exception: system.invalidoperationexception: cannot perform cas asserts in security transparent methods
what reason behavior , how can achieve i'm trying do? understanding, code in untrusted appdomain security transparent, while code in helper assembly security safe-critical, meaning should able request permissions assert().
i'm missing piece of puzzle, it's better understanding of code access security explain going wrong. appreciated.
your "trusted" assembly needs have allowpartiallytrustedcallers
attribute securitysafecritical
callable across assembly boundary. must added fulltrustassemblies
in call createdomain
.
Comments
Post a Comment