
Statically verify the AST safety of a compiled R script
Source:R/security.R
verify_compiled_r_ast.RdParses an R script into an Abstract Syntax Tree (AST) without evaluating any
code and verifies that every function call belongs strictly to a closed
allowlist of safe statistical/base functions (or non-shadowing local
functions defined earlier in lexical order). Rejects all :::, <<-,
dynamic/computed callees, arbitrary S4 slots, arbitrary package loading,
extra-argument injections in library() / requireNamespace(), and
dangerous or unwhitelisted primitives—even when passed as higher-order
function arguments (e.g., to lapply, vapply, or tryCatch).
Value
An S3 object of class spec_ast_verification with elements:
safe: logical scalar (TRUEif zero violations).violations: data.frame with columnscode,symbol,message.called_functions: unique character vector of all called symbols.n_expressions: integer number of top-level parsed expressions.
Examples
spec <- new_spec_ir()
code <- compile_r_script(spec)
verify_compiled_r_ast(code)
#> ✔ AST safety verification PASSED (6 expressions, 62 unique whitelisted calls).
# Unsafe scripts are caught statically without ever being executed:
verify_compiled_r_ast("x <- 1; system('id')")$safe
#> [1] FALSE