Sandbox Code.eval_string?

148 views
Skip to first unread message

Frank Liu

unread,
Apr 12, 2015, 9:03:29 PM4/12/15
to elixir-l...@googlegroups.com
Hi All,

I'd like to create something that allows users to input some elixir code and have it executed in the server elixir environment. However, we don't want to execute File.rm! obviously. So is there a way to sandbox the process that is running Code.eval_string? For example, in IRC, beamie seems to blacklist some functions:

```elixir

<gniquil> ~~ FIle.read!("../.mix")

[17:46:15] <beamie> ** (UndefinedFunctionError) undefined function: FIle.read!/1 (module FIle is not available)

[17:46:15] <beamie>     FIle.read!("../.mix")

[17:46:15] <beamie>     (stdlib) erl_eval.erl:657: :erl_eval.do_apply/6

[17:46:15] <beamie> Output truncated: https://gist.github.com/63bd16a36f3635e42ff6

```

How was this done? But if this was done, how come this works?

```elixir

~~ File.ls("..")

<beamie> {:ok,

[17:45:12] <beamie> ["home", ".dockerenv", ".dockerinit", "etc", "dev", "tmp", ".mix", "usr",

[17:45:12] <beamie>   "lib", "var", "sbin", "sys", "root", "srv", "bin", "mnt", "boot", "media",

[17:45:12] <beamie> Output truncated: https://gist.github.com/6463c9187cd74919c944

```

Frank

Eric Meadows-Jönsson

unread,
Apr 12, 2015, 9:47:52 PM4/12/15
to elixir-l...@googlegroups.com

beamie does not blacklist calls, you misspelled File as FIle and you cannot call File.read! on a directory. Instead beamie performs sandboxing by executing every command in a new docker container.

You can perform sandboxing by traversing the AST from Code.string_to_quoted and only accept a subset of “safe” expressions which you pass to Code.eval_quoted. This is very hard to do though and it’s easy to miss edge cases, it would be much safer to use an approach similar to beamie’s.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/fbda5eec-7ae2-48f8-a4fd-25fd842e1f3c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson
Reply all
Reply to author
Forward
0 new messages