Skip to content
Prev 36761 / 63424 Next

R in sandbox/jail (long question)

How about some "computing on the language", something like this:
  
exprs <- parse("SCRIPT.R")
invalids <- c(".Internal", ".Primitive")
if( any( invalids %in% all.names(exprs) ) )
   stop("sandbox check failed")


I believe this would prevent evaluating any direct calls to '.Primitive'
and '.Internal'. Of course, you could extend the 'invalids' vector to
include any names. If you want to consider arguments to calls (i.e.
argument to 'file' or 'library') or something more sophisticated, check
out the functions in the codetools package, something like this:


library(codetools)

walkerCall <- function(e, w) {
  for( ee in as.list(e)) {
    if(!missing(ee)) {
      if(is.call(ee)) {

        #stop .Internal calls
        if(ee[1] == call(".Internal"))
          stop("invalid \'.Internal()\' call")

        #restrict file to STDIN
        if(ee[1] == call("file")) {
          mc <- match.call(file, ee)
          if(mc[[2]] != "stdin")
            stop("\'file()\' only valid with \'description=\"stdin\"\'")
        }

      }
      walkCode(ee, w)
    }
  }
}

walker <- makeCodeWalker(call=walkerCall, leaf=function(e,w){})
exprs <- parse("SCRIPT.R")
for( expr in exprs ) 
    walkCode(expr,walker)

I'm a little surprised this there isn't a 'sandbox' package or something
similar to this. A reverse depends check on the codetools package
indicates there is not. However, I believe there is some demand for it.

Matt Shotwell
http://biostatmatt.com
On Tue, 2010-05-18 at 22:38 -0400, Assaf Gordon wrote: