Skip to contents

Parses 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).

Usage

verify_compiled_r_ast(script, strict = FALSE)

Arguments

script

Character scalar (or vector of lines) containing R source code.

strict

Logical; if TRUE, aborts with a specshiny_ast_violation error when any violation is detected. Defaults to FALSE.

Value

An S3 object of class spec_ast_verification with elements:

  • safe: logical scalar (TRUE if zero violations).

  • violations: data.frame with columns code, 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