CS-Script 是否提供开箱即用的沙盒?

Does CS-Script provide sandboxing out of box?

我正在研究 cs-script,我想知道它开箱即用的安全性如何。我知道脚本是动态加载和卸载的,但该脚本能否逃脱其正在执行的程序集?即它可以使用反射从同一进程中的其他程序集访问和实例化 类 吗?

所以我的问题是 cs-script 是默认带有内置安全性还是不麻烦?

如果你的意思是 CS-Script http://www.csscript.net/,那么它可以引用和调用其他程序集,使用正常语法:

using MyOtherAssembly;

CS-Script 使用隐式 加载来尝试根据 using 语句中的命名空间计算出要加载的程序集。这并不能保证在所有情况下都有效,在这种情况下您将需要使用显式加载,例如,您可以为 CS-Script 提供一条指令以显式加载所需的程序集:

//css_ref "..\MyOtherAssembly.dll"
using MyOtherAssembly;

//css_ref是一个特殊的注释,被CS-Script处理为加载程序集的指令,在这种情况下引用的程序集在脚本的父目录。这与普通程序集的项目文件中的程序集引用类似。您还可以使用 CS-Script 命令行显式加载程序集。

我还发现,如果在调用脚本之前引用的程序集已经加载到 AppDomain 中,则不需要使用隐式或显式加载。我假设 CS-Script 检测到命名空间已经存在于 AppDomain 中,因此不会为隐式加载而烦恼。

此处提供了有关程序集加载的更多信息:http://www.csscript.net/help/using_.net_assemblies.html

Edit1:您不能禁用隐式加载,但您可以将脚本放入没有任何其他程序集的目录中,这将阻止隐式加载找到任何其他程序集。该脚本仍然能够调用已加载的命名空间。但这只是"security by obscurity";托管代码的本质意味着坚定的人总是可以通过反射访问您的代码。

简而言之:不,CS-script 不提供任何开箱即用的安全功能。

在这里回答:

The immediate attractive solution is to use .NET Sandbox. It is designed exactly for this sort of scenarios.The CLR standard sandboxing is available for the host application running scripts with CS-Script. The idea is that you initialise CAS before loading the suspicious script and the rest is a CLR responsibility. And if you need to configure directories/files permissions you do it with CAS tools. This way scripting is a "transportation" for the routine provided by your user. And CS-Script is a convenient mechanism for implementing such transport but the actual security concerns are addressed by .NET Sendoxing, which has comprehensive set of functionality to cover practically all possible security scenarios. With CS-script downloadables you can find the Sendboxing sample (\Samples\Sandboxing) which demonstrates how to prevent script from file I/O operations (e.g. create file).

使用 .Net Security Credentials 和 cs-script 的示例位于:http://www.csscript.net/Samples.html (sandbox.zip)

为了安全地执行不受信任的 cs 脚本(尽可能隔离),在加载脚本(到新的应用程序域)之前创建一个具有安全限制的新 AppDomain。然后可以在主域和脚本域之间编组数据。 参见 https://msdn.microsoft.com/en-us/library/bb763046%28v=vs.110%29.aspx