Skip to contents

gptr_risk() is the advisory static classifier behind gptr's permission prompts. It reads code without evaluating it and labels each effect it can see with a level from 0 (known read-only) to 4 (critical) and a category (read, object_write, file_write, file_delete, network, process, install, dynamic, session, secret, interactive, critical, control, plus unlisted for functions the risk tables do not list). Level 0 is only what the tables know to be read-only; code whose function is computed at run time is level 3. It cannot see S4 methods, compiled code, package load hooks or the files source() reads: it decides when gptr asks you, it is not a security boundary.

Usage

gptr_risk(code, envir = NULL, root = NULL)

Arguments

code

R code: a character vector, a call or an expression.

envir

NULL or the environment the code would run in. When given, assignments to existing bindings (of envir itself for a plain assignment evaluated in envir; none for one in the code of local() without envir, which binds in its own environment; for <<-, assign(), replacement calls and an assignment inside local(envir =), with() or within() also of its enclosing environments up to the global environment) are reported as overwrites with their size (the workspace's size rule in a leaf: each part once, without code or shared environments; promises and active bindings are never forced) and user-defined functions called by the code are classified through their bodies.

root

The project root used for path classes; NULL means the current project root.

Value

A gptr_risk object: a list with level (integer 0-4), label, categories, flagged (a data frame with columns call, fn, level, category, path, path_class), paths, secret, secret_guard, assigned, dynamic, and the additive fields sizes (bytes of overwritten objects, by name), secrets (guarded secret names), kind, parse_error, calls (every function the code calls, including through the functions it reads in envir) and targets (files: the files the code writes, deletes or downloads to, as permission rules see paths).

Details

Paths are classified relative to root (workspace, temp, outside, protected, control, instructions, critical, url, wildcard, unknown). Calls of peter$sh(), system(), system2() (its env as the start of the command line), processx::run() (also as callr::run()) and sys::exec_wait(), sys::exec_internal() and sys::exec_background() with literal commands are classified with the command table (inst/extdata/risk-commands.csv), peter$sql() by its statements and peter$py() by a token scan. A command the code computes, or a shell.exec() file it computes, is a dynamic row at level 3. A path, command or variable name built with c(), list(), paste0(), file.path() or another helper of base R counts as one the code computes when the code binds that helper's bare name with =, <- or <<-, as a function's argument or as a for variable, or when the helper found from envir (the global environment when NULL) is not base R's; a helper the code binds otherwise (with assign(), delayedAssign(), makeActiveBinding(), list2env() or attach(), in a list given to with() or within(), or into an environment, e$paste0 = f or .GlobalEnv[["paste0"]] = f) is read as base R's. Plugins and users extend both tables with risk_rule records.

Examples

gptr_risk("unlink('data', recursive = TRUE)")$level
#> [1] 3
gptr_risk("summary(mtcars)")
#> risk 0 (read-only)
e = new.env()
e$df = data.frame(a = 1:3)
gptr_risk("df = head(df, 2)", envir = e)$flagged
#>                                  call   fn level     category path path_class
#> 1 overwrites `df` <data.frame, 576 B> <NA>     2 object_write <NA>       <NA>