2012-12-20 2 views
3

IronPython을 스크립팅 목적으로 C# 응용 프로그램에 통합하려고합니다. 스크립트가 파일 시스템/네트워크/민감한 시스템 리소스에 대한 액세스를 제한하는 일련의 보안 정책 하에서 실행되기를 바랍니다. 조사 결과 가장 많이 사용되는 접근 방법은 CAS를 사용하는 것입니다. 이것은 잘 작동하고 사용자가 소켓을 사용하거나 파일 시스템에 액세스하는 것을 허용하지 않습니다. 그러나 스크립트와 상호 작용하도록 변수를 주입하려고하면 특정 매개 변수에 액세스 할 때마다 보안 예외가 발생합니다. 이는 어셈블리에서 정의한 변수에서만 발생합니다. 내가 표준 .NET 유형 (예 : 사전)을 사용하면 잘 작동합니다.CAS를 사용한 IronPython 샌드 박스

내 코드 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
using System.Security.Policy; 
using System.Security.Permissions; 
using System.Security; 
using System.IO; 
using Microsoft.Scripting.Hosting; 
using IronPython.Hosting; 
using System.Runtime.Remoting; 
using System.Net; 

namespace python_in_appdomain 
{ 
    [Serializable] 
    public class Whatever 
    { 
     public int i; 
     public Whatever(int i) 
     { 
      this.i = i; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Remoting(); 
     } 

     public static void Remoting() 
     { 
      AppDomainSetup setup = new AppDomainSetup(); 
      Assembly thisAssembly = Assembly.GetExecutingAssembly(); 
      setup.ApplicationBase = Path.GetDirectoryName(thisAssembly.Location); 

      AssemblyName name = typeof(Program).Assembly.GetName(); 
      StrongName sn = new StrongName(
       new StrongNamePublicKeyBlob(name.GetPublicKey()), 
       name.Name, 
       name.Version 
      ); 

      PermissionSet pset = new PermissionSet(PermissionState.None); 
      pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); 
      pset.AddPermission(new ReflectionPermission(PermissionState.Unrestricted)); 

      setup.PartialTrustVisibleAssemblies = new[] { sn.Name + ", PublicKey=" + sn.PublicKey.ToString() }; 
      AppDomain domain = AppDomain.CreateDomain("Sandbox", AppDomain.CurrentDomain.Evidence, setup, pset, sn); 

      String script = @" 
d.Add('hello', 1) 
w.i = 5 
"; 

      Whatever w = new Whatever(4); 
      Dictionary<string, int> d = new Dictionary<string,int>(); 

      ScriptRuntime py = Python.CreateRuntime(domain); 
      ScriptEngine engine = py.GetEngine("py"); 
      ScriptScope scope = engine.CreateScope(); 
      scope.SetVariable("w", w); 
      scope.SetVariable("d", d); 

      int result; 
      Console.WriteLine(w.i); 
      d.TryGetValue("hello", out result); 
      Console.WriteLine(result); 
      Console.WriteLine("-----"); 

      try 
      { 
       engine.Execute(script, scope); 
      } 
      catch (Exception exc) 
      { 
       Console.WriteLine(exc.ToString()); 
      } 

      w = scope.GetVariable<Whatever>("w"); 
      d = scope.GetVariable<Dictionary<string,int>>("d"); 

      Console.WriteLine("-----"); 
      Console.WriteLine(w.i); 
      d.TryGetValue("hello", out result); 
      Console.WriteLine(result); 

     } 
    } 
} 

주석 처리는 "w.i = 5"라인 (55)에 제한된 보안 설정에서 일반적으로 실행하는 프로그램을 발생합니다. 47 행에서 PermissionState를 Unrestricted로 설정하면 스크립트의 두 행을 모두 정상적으로 실행할 수 있습니다. 다음과 같은 오류 메시지가 나타납니다.

System.Security.SecurityException: Request failed. 
    at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method) 
    at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target) 
    at System.Dynamic.Utils.TypeExtensions.CreateDelegate(MethodInfo methodInfo, Type delegateType, Object target) 
    at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate() 
    at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator) 
    at System.Linq.Expressions.Expression`1.Compile() 
    at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args) 
    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) 
    at Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) 
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
    at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) 
    at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 
    at IronPython.Compiler.PythonScriptCode.Run(Scope scope) 
    at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) 
    at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) 
    at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink) 
    at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) 
    at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope) 
    at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope) 
    at python_in_appdomain.Program.Remoting() in C:\Users\dave\Documents\Visual Studio 2010\Projects\IronPythonTest\IronPythonTest\Program.cs:line 7 
6 
The action that failed was: 
Demand 
The type of the first permission that failed was: 
System.Security.PermissionSet 
The demand was for: 
<PermissionSet class="System.Security.PermissionSet" 
version="1" 
Unrestricted="true"/> 

The granted set of the failing assembly was: 
<PermissionSet class="System.Security.PermissionSet" 
version="1"> 
<IPermission class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
version="1" 
Unrestricted="true"/> 
<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
version="1" 
Flags="Execution"/> 
</PermissionSet> 

The assembly or AppDomain that failed was: 
Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1 
The method that caused the failure was: 
Int32 Run(Microsoft.Scripting.Interpreter.InterpretedFrame) 
The Zone of the assembly that failed was: 
Internet 
The Url of the assembly that failed was: 
file:///C:/Users/dave/Documents/Visual Studio 2010/Projects/IronPythonTest/IronPythonTest/bin/Debug/Microsoft.Dynamic.DLL 

Unhandled Exception: System.Security.SecurityException: Request failed. 
    at System.Delegate.BindToMethodInfo(Object target, IRuntimeMethodInfo method, RuntimeType methodType, DelegateBindingFlags flags) 
    at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure) 
    at System.Dynamic.Utils.TypeExtensions.CreateDelegate(MethodInfo methodInfo, Type delegateType) 
    at System.Runtime.CompilerServices.CallSite`1.MakeUpdateDelegate() 
    at System.Runtime.CompilerServices.CallSite`1.GetUpdateDelegate(T& addr) 
    at System.Runtime.CompilerServices.CallSite`1.GetUpdateDelegate() 
    at System.Runtime.CompilerServices.CallSite`1.Create(CallSiteBinder binder) 
    at System.Func`2.Invoke(T arg) 
    at Microsoft.Scripting.Runtime.DynamicOperations.GetOrCreateSite[T](CallSiteBinder siteBinder, Func`2 factory) 
    at Microsoft.Scripting.Runtime.DynamicOperations.GetOrCreateSite[T1,TResult](CallSiteBinder siteBinder) 
    at Microsoft.Scripting.Runtime.DynamicOperations.ConvertTo[T](Object obj) 
    at IronPython.Runtime.PythonContext.ScopeGetVariable[T](Scope scope, String name) 
    at Microsoft.Scripting.Hosting.ScriptScope.GetVariable[T](String name) 
    at Microsoft.Scripting.Hosting.ScriptScope.GetVariable[T](String name) 
    at python_in_appdomain.Program.Remoting() in C:\Users\dave\Documents\Visual Studio 2010\Projects\IronPythonTest\IronPythonTest\Program.cs:line 8 
3 
    at python_in_appdomain.Program.Main(String[] args) in C:\Users\dave\Documents\Visual Studio 2010\Projects\IronPythonTest\IronPythonTest\Program. 
cs:line 31 

오류는 매우 간단합니다. 그것은 이런 종류의 행동에 대해 전적으로 제한없이 접근 할 것으로 기대한다고 말합니다. 이 주위에 어떤 방법이 있습니까? 왜 사전은 작동하지만 내 변수는 작동하지 않습니까? IronPython이 사전과 같은 변수를 처리하도록 만드는 방법이 있습니까? 나는 아주 명백한 것을 놓치고 있습니까?

도움 주셔서 대단히 감사합니다.

업데이트 2012년 12월 21일

나는 장난 유지 및 근무 해결책을했다. 이와 같이 (동일한 권한 개체로) AppDomain을 생성하면 모든 것이 작동합니다.

AppDomainSetup setup = new AppDomainSetup(); 
      setup.ApplicationBase = System.Environment.CurrentDirectory; 
AppDomain domain = AppDomain.CreateDomain("IPyEngine", new Evidence(), setup, pset); 

나는 내 조립의 증거를 제공 아니에요도 나는 신뢰 목록에 어떤 어셈블리를 추가하고 있음을 알 수 있지만, 왜 나는 완전히 이해하고 말할 수 없습니다. 나는 내가 기념비적으로 어리 석을 내지 않는 것을 확실히 알기 전까지 이것을 답으로 표시하고 싶지 않습니다.

답변

0

이 의심스러운 : 파일이 영역 식별자가 궁금

The Zone of the assembly that failed was: 
Internet 
The Url of the assembly that failed was: 
file:///C:/Users/dave/Documents/Visual Studio 2010/Projects/IronPythonTest/IronPythonTest/bin/Debug/Microsoft.Dynamic.DLL 

. 파일 속성을 확인할 수 있습니까? Unblock it if necessary?

+0

나는이 사실을 알았지 만이 차단에 대해 몰랐다. 모든 DLL의 차단을 해제하고 문제가 지속됩니다. –

+0

그래도 뭔가 다른 것을 찾았습니다. 2012 년 12 월 21 일부터 업데이트를 확인하십시오. –