Running a .Net application in a sandbox

前端 未结 1 1759
萌比男神i
萌比男神i 2021-01-21 00:19

Over the months, I\'ve developed a personal tool that I\'m using to compile C# 3.5 Xaml projects online. Basically, I\'m compiling with the CodeDom compiler. I\'m thinking about

1条回答
  •  梦毁少年i
    2021-01-21 00:46

    Spin up an AppDomain, load assemblies in it, look for an interface you control, Activate up the implementing type, call your method. Just don't let any instances cross that AppDomain barrier (including exceptions!) that you don't 100% control.

    Controlling the security policies for your external-code AppDomain is a bit much for a single answer, but you can check this link on MSDN or just search for "code access security msdn" to get details about how to secure this domain.

    Edit: There are exceptions you cannot stop, so it is important to watch for them and record in some manner the assemblies that caused the exception so you will not load them again.

    Also, it is always better to inject into this second AppDomain a type that you will then use to do all loading and execution. That way you are ensured that no type (that won't bring down your entire application) will cross any AppDomain boundary. I've found it is useful to define a type that extends MarshalByRefObject that you call methods on that executes insecure code in the second AppDomain. It should never return an unsealed type that isn't marked Serializable across the boundary, either as a method parameter or as a return type. As long as you can accomplish this you are 90% of the way there.

    0 讨论(0)
提交回复
热议问题