# Create pseudo sandbox for hosted IronRuby script

**URL:** <https://www.ruby-forum.com/t/create-pseudo-sandbox-for-hosted-ironruby-script/183105>\
**Category:** IronRuby\
**Created:** [February 8, 2010, 9:41am UTC](https://www.ruby-forum.com/t/create-pseudo-sandbox-for-hosted-ironruby-script/183105 "2010-02-08T09:41:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 8, 2010, 9:41am UTC](https://www.ruby-forum.com/t/create-pseudo-sandbox-for-hosted-ironruby-script/183105/1 "2010-02-08T09:41:01Z")

</div>

Hi,

I have an multi-user VoIP application that allows users to execute  
IronRuby scripts for their call dial plans. I’m looking to restrict what  
the IronRuby scripts are allowed to do to protect the server in case a  
nasty user should decide to try and cause some damage. I’ve restricted  
the process executing the scripts as much as I can but would like to go  
further. For example my users don’t need to be able to access the file  
system so I’d like to disable the File and Dir Ruby standard library  
classes.

The approach I’ve looked into and that seems to work is to comment out  
the modules I don’t want in the IronRuby.Libraries assembly and the  
BuiltinsLibraryInitializer.LoadModules class. Is that a reasonable  
approach?

Thanks,

Aaron

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 8, 2010, 9:56am UTC](https://www.ruby-forum.com/t/create-pseudo-sandbox-for-hosted-ironruby-script/183105/2 "2010-02-08T09:56:04Z")

</div>

> I have an multi-user VoIP application that allows users to execute  
> IronRuby scripts for their call dial plans. I’m looking to restrict what  
> the IronRuby scripts are allowed to do to protect the server in case a  
> nasty user should decide to try and cause some damage.

This kind of “blank-slate” approach would be useful to me too.

Is that something that can be achieved using isolated scopes ?

– Thibaut

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [February 9, 2010, 1:11pm UTC](https://www.ruby-forum.com/t/create-pseudo-sandbox-for-hosted-ironruby-script/183105/3 "2010-02-09T13:11:35Z")

</div>

You should start without IronRuby, and get a basic C# test dll sandboxed  
first. One less variable to worry about. The links below have some  
useful information.

> **[How to Safely Host Untrusted Add-Ins with the .NET Framework 2.0](https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/november/how-to-safely-host-untrusted-add-ins-with-the-net-framework-2-0)**

> **[How to: Run Partially Trusted Code in a Sandbox](https://learn.microsoft.com/en-us/previous-versions/dotnet/framework/code-access-security/how-to-run-partially-trusted-code-in-a-sandbox?redirectedfrom=MSDN)**
>
> Discover how to run partially trusted code in a sandbox in .NET. The AppDomain class is an effective way of sandboxing managed applications.

* * *

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid] on behalf of Pascal Normandin  
[removed\_email\_address@domain.invalid]  
Sent: Monday, February 08, 2010 6:25 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Create pseudo sandbox for hosted IronRuby  
script

Hello,

Here is what I’ve done to achieve this but I’d really like to know if  
this is the right method. It gets really confusing when it comes to  
TrustLevel and assembly permissions.

> From my basic tests I was unable to access any resources from the computer not even the file system.

Pascal Normandin

```
    protected static ScriptRuntime CreateIronRubyRuntime(bool 

```

runInSandBox)  
{  
// Setup the ruby engine in a Sandbox  
var rubySetup = Ruby.CreateRubySetup();

```
        rubySetup.Options["InterpretedMode"] = true;

        var runtimeSetup = new ScriptRuntimeSetup();
        runtimeSetup.LanguageSetups.Add(rubySetup);
        runtimeSetup.DebugMode = false;

        ScriptRuntime runtime;
        if (runInSandBox)
        {
            // Create AppDomain Info
            AppDomainSetup info = new AppDomainSetup();
            info.ApplicationBase = 

```

AppDomain.CurrentDomain.BaseDirectory + “\bin”;  
info.ApplicationName = “IRPlugin”;

```
            // Set permissions
            PermissionSet ps1 = new 

```

PermissionSet(PermissionState.None);  
SecurityPermissionFlag flag =  
SecurityPermissionFlag.SkipVerification |  
SecurityPermissionFlag.Execution |  
SecurityPermissionFlag.ControlAppDomain;  
ps1.AddPermission(new SecurityPermission(flag));

```
            // Create the AppDomain
            AppDomain newDomain = 

```

AppDomain.CreateDomain(“IRPluginDomain”, null, info, ps1);

```
            runtime = ScriptRuntime.CreateRemote(newDomain, 

```

runtimeSetup);  
}  
else  
{  
runtime = Ruby.CreateRuntime(runtimeSetup);  
}

```
        return runtime;
    }

```

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Thibaut  
BarrÃ¨re  
Sent: February-08-10 3:46 AM  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Ironruby-core] Create pseudo sandbox for hosted IronRuby  
script

> I have an multi-user VoIP application that allows users to execute  
> IronRuby scripts for their call dial plans. I’m looking to restrict what  
> the IronRuby scripts are allowed to do to protect the server in case a  
> nasty user should decide to try and cause some damage.

This kind of “blank-slate” approach would be useful to me too.

Is that something that can be achieved using isolated scopes ?

– Thibaut
