In a situation where you wanted to allow users to define search filters, would it be possible to take a string, dynamically compile that, and execute it in a sandbox which wouldn't allow it access to anything outside of the sandbox.
Consider that my app has a list of objects. I want to allow a power user to write C# in the interface (so, literally in a textarea inside of a browser) and receive that as a string (as a POST).
Something like this:
var foo = "bar";
return objects.Where(o => o.Property == foo);
Again, I would get this as a string (this is not compiled out of Visual Studio). Then I could dynamically compile that into a temporary helper class (this is the easy part -- I've done this).
What I haven't done is execute that in such a way that I know it's not going negatively affect resources outside of itself. Because, let's face it, they could write this:
File.Delete(Path.Combine(AppDomain.Current.BaseDirectory), "web.config"));
That would clearly be bad, but since they're writing C# as a string and having it dynamically compiled, I'm not sure there's much I can do about it.
What I want to do it compile and execute this string in such a way that it only has accesses to the resources I give it. It will be compiled to something like this:
public static class Temp : IDynamicFilter
{
public static List<MyClass> Filter(List<MyClass> objects)
{
[their string goes here]
}
}
Then Filter
will be called (from IDynamicFilter
).
I realize that this clearly might be a bad idea. And some of this is hypothetical -- I'm quite interested in the theory of running sandboxed code-within-code, beyond the specifics of this example.