简体   繁体   English

Assembly.CreateInstance和安全性

[英]Assembly.CreateInstance and security

I'm toying around with the idea of using C#'s ability to compile code on-demand as the basis of a scripting language. 我正在考虑使用C#的能力来按需编译代码作为脚本语言的基础。 I was wondering, how can I sandbox the scripts that I am executing so that they cannot access the file system, network, etc. Basically, I want restricted permissions on the script being run. 我想知道,我如何沙箱我正在执行的脚本,以便他们无法访问文件系统,网络等。基本上,我希望对正在运行的脚本的权限受到限制。

Steps that I take: 我采取的步骤:

CompilerResults r = CSharpCodeProvider.CompileAssemblyFromSource(source);

Assembly a = r.CompiledAssembly;

IScript s = a.CreateInstance(...);

s.EntryPoint(...);

The recommended approach for this is to execute the suspect code in a sandboxed appdomain . 建议的方法是在沙盒应用程序域中执行可疑代码。 Several reasons are given at http://blogs.msdn.com/b/shawnfa/archive/2006/04/19/579066.aspx , and an even more important one is that most of the other potential approaches are deprecated as of .NET 4.0. http://blogs.msdn.com/b/shawnfa/archive/2006/04/19/579066.aspx给出了几个原因,更重要的一个原因是大多数其他潜在的方法都被弃用了。 NET 4.0。

Edit: disregard that, it's not safe! 编辑:无视,这不安全!

Check out System.Security.Permissions.SecurityPermission (and subclasses like FileIOPermission ) and this tutorial . 查看System.Security.Permissions.SecurityPermission (以及类似FileIOPermission的子类)和本教程 If you want to temporary deny code from doing unsafe actions, you can use statements like this: 如果要暂时拒绝代码执行不安全的操作,可以使用如下语句:

NamedPermissionSet ps = new NamedPermissionSet("Nothing");
ps.Deny();
CallYourScriptHere();
CodeAccessPermission.RevertAll();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM