.NET / Security: Limiting loaded assembly assemblies from accessing specific APIs

In a shell application, I need to be able to load and execute other .NET assemblies at runtime, but without giving them full trust. In fact, I want to limit them (loaded assemblies) to touching any system resources (streams, networks, etc.), the only exception is isolated storage. However, assemblies that are from "me" must be run with full confidence.

I am considering security of access to the code, but I'm not quite sure what I should use.

How would you do that?

+4
source share
1 answer

CAS is pretty much what you need here. More specifically, you want to load the assembly into your own application domain:

var myEvidence = new Evidence(new object[] {SecurityZone.Internet}); var newDomain = AppDomain.CreateDomain("InternetDomain"); myDomain.Load("MyUntrustedAssembly.dll", myEvidence); myDomain.CreateInstanceAndUnwrap("MyUntrustedAssembly","MyUntrustedObjectType"); //do your work with the untrusted assembly/type AppDomain.Unload(myDomain); 

Read about the application domains, different zones, and the default permission sets assigned to them. The Internet is the most restrictive for system zones / permission sets available for assembly, which can actually be performed (there is also a limited zone, assemblies falling into this zone cannot be executed). You can use the .NET Configuration tool to create permission sets and determine the conditions (evidence) that code must satisfy in order to provide a permission set.

+8
source

All Articles