Sandboxed ironpython in C # SecurityException

Ok that's what i have

private static AppDomain CreateSandbox()
{
  var permissions = new PermissionSet(PermissionState.None);
  permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

  permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read| FileIOPermissionAccess.PathDiscovery, AppDomain.CurrentDomain.BaseDirectory));
  var appinfo = new AppDomainSetup();
  appinfo.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

  return AppDomain.CreateDomain("Scripting sandbox", null, appinfo, permissions, fullTrustAssembly);
}

And when I try to load some broken python code

try
{
    var src = engine.CreateScriptSourceFromString(s.Python, SourceCodeKind.Statements);
    src.Execute(ActionsScope);
}
catch (Exception e)
{
    ExceptionOperations eo = engine.GetService<ExceptionOperations>();
    string error = eo.FormatException(e);
    Debug.WriteLine(error);
}

This gives me SecurityExceptioninstead of letting me see the actual exception from the code. If I install PermissionSet(PermissionState.Unrestricted), it works fine.

Any ideas on what permissions I need to catch these blasted bugs?

System.Security.SecurityException was caught
  Message=Request failed.
  Source=Microsoft.Scripting
  GrantedSet=<PermissionSet class="System.Security.PermissionSet"
version="1">
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Read="F:\Programming\OCTGN\octgnFX\Octgn\bin\ReleaseMode with Debug\"
PathDiscovery="F:\Programming\OCTGN\octgnFX\Octgn\bin\ReleaseMode with Debug\"/>
<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
Flags="Execution"/>
</PermissionSet>

  PermissionState=<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>

  RefusedSet=""
  Url=file:///F:/Programming/OCTGN/octgnFX/Octgn/bin/ReleaseMode with Debug/Microsoft.Scripting.DLL
  StackTrace:
       at Microsoft.Scripting.SyntaxErrorException.GetObjectData(SerializationInfo info, StreamingContext context)
       at System.Runtime.Serialization.ObjectCloneHelper.GetObjectData(Object serObj, String& typeName, String& assemName, String[]& fieldNames, Object[]& fieldValues)
       at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
       at Octgn.Scripting.Engine.LoadScripts() in F:\Programming\OCTGN\octgnFX\Octgn\Scripting\Engine.cs:line 58
  InnerException: 
+5
source share
2 answers

Based on the stack trace, there is a syntax error in your code, so Python's runtime is causing it SyntaxErrorException. This exception is not caught and therefore it must be serialized at the application boundary.

, , GetObjectData() of SyntaxErrorException [SecurityCritical], , .

+1

ObjectHandle ExecuteAndWrap ( ScriptScope, ObjectHandle). , , , FormatException. , .

ExecuteAndWrap, - ExceptionOperations.FormatException.

+1

All Articles