source_and_eval_safe: Wrapper to source a file and safely evaluate an expression

Description Usage Arguments Details Value Examples

View source: R/source_eval.R

Description

We assume all files surrounding the to be evaluated files have different user ids and file modes not allowing the supplied user id to read them. One way to do that is to just set all files within the evaluation directories to root:root removing group and others the rights to read (or write or execute). We therefore also chmod the supplied file back to mode “0644” ensuring that the file can be read so that the expression can be evaluated—but nothing else should be in reach.

Usage

1
2
3
4
5

Arguments

file

A filename with an R file to be source, typically containing the student code to be evaluated safely.

expr

An expression to be evaluate by eval_safe, typically the name of the sane of the function containing the student code plus the argument supplied from the test runner.

uid

Optional numeric or character user id identifying the user id with (presumably lower) privileges as which the code is running; the numeric uid is obtained via user_info is a character is supplied. Note that using this argument requires being the ‘root’ user.

path

Optional path to a file that should be hidden before evaluation happens. It is then unhidden on exit.

Details

The source_and_eval_safe_with_hiding variant can hide a given file, for example containing a reference answer, but assigning it to a unique temporary name so that it cannot be sourced.

The eval_safe_as convenience function fetches the (numeric) user id before calling unix::eval_safe; it is equivalent to source_and_eval_safe but does not involve a file.

Note that you must run these functions as the ‘root’ user in order to set the uid.

Value

A value of the expr sourced from file and evaluated by uid, or NULL in case of error.

Examples

1
2
3
4
5
## Not run: 
n <- sample(3:20, 1)         # random payload
res <- source_and_eval_safe("code/fib.R", fib(n), "ag")

## End(Not run)

stat447/plr documentation built on Dec. 23, 2021, 5:24 a.m.