1

I am currently developing shiny applications that aim to teach R via interactive courses. For that purpose, I have already worked with multiple-choice questions and free-text questions. Now I want to tackle questions where the users of the app (students) can enter their own R code in a text field and run it.

My current implementation basically uses eval inside an observer.

## evaluate the users expression and store the results.
observeEvent(input$evluate, {
  reactives$result <- eval(parse(text = input$console_in))
})

This implementation has serious drawbacks when it comes to security since users can insert and run arbitrary codes on the server.

  • What are the best practices to make the console safer?
  • How should the working directory be specified during the evaluation?

It is planned to release an open-source version of this software at some point. Therefore, I would prefer a solution which is not platform dependent and which doesn't complicate the deployment of the application.

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43

1 Answers1

1

For evaluating arbitrary code, I like the whitelisting approach where you put all known and safe functions in an empty environment to be evaluated in. I think that's the simple and easy solution vs. blacklisting functions or trying to sandbox outside of R. Here's a much better answer with examples: Safely evaluating arithmetic expressions in R?

Alternatively, here's a POC package that takes a blacklisting approach: https://github.com/Rapporter/sandboxR

All other sandboxing methods I can think of are Linux specific. There's https://github.com/jeroen/RAppArmor which uses AppArmor to sandbox at the OS level. And then using Docker or Linux containers to run sandboxed code.

greg L
  • 4,034
  • 1
  • 19
  • 18
  • Thank you very much. For some reason I was just thinking about blacklisting and sandboxing but the whitelist approach seems best for my usecase. Since the apps are pedagogic, I don't mind limiting my users to a hand-selected set of functions and this security-layer seems really hard to get around. – Gregor de Cillia Jan 02 '19 at 17:29