Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions R/DSLiteServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ DSLiteServer <- R6::R6Class(
#' @param sid The session ID.
#' @param expr The R expression to evaluate.
aggregate = function(sid, expr) {
exprr <- private$.as.language(sid, expr, private$.config$AggregateMethods)
exprr <- private$.as.language(sid, expr, private$.config$AggregateMethods, aggregate = TRUE)
origwd <- setwd(private$.get.wd(sid))
on.exit(setwd(origwd))
tryCatch(eval(exprr, envir = private$.session(sid)))
Expand Down Expand Up @@ -482,7 +482,7 @@ DSLiteServer <- R6::R6Class(
private$.sessions[[sid]]
},
# apply configuration to function calls in the expression
.as.language = function(sid, expr, methods) {
.as.language = function(sid, expr, methods, aggregate = FALSE) {
if (is.null(expr)) {
stop("Invalid expression type: 'NULL'. Expected a call or character vector.",
call. = FALSE)
Expand Down Expand Up @@ -545,13 +545,13 @@ DSLiteServer <- R6::R6Class(
}
}

ast <- private$.parse(exprStr)
if (!is.null(methods)) {

# develop function calls according to configured methods
ast <- private$.parse(exprStr)
replaceFunctionNode(ast)
exprStr <- ast$to_string()
}
validateNode(ast, aggregate)
if (getOption("dslite.verbose", FALSE)) {
message(paste0("Expression to evaluate: ", exprStr))
}
Expand Down
47 changes: 43 additions & 4 deletions R/lex-yacc.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ Parser <- R6::R6Class(
c('right', '^')
),

p_expression_named = function(doc='expression : NAME "=" expression', p) {
p_expression_named = function(doc='expression : NAME "=" expression
| STRING "=" expression
| DBLSTRING "=" expression', p) {
op <- ParameterNode$new(p$get(3))
op$add_child(SymbolNode$new(p$get(2)))
name <- p$get(2)
op$add_child(if (grepl("^['\"]", name)) StringNode$new(name) else SymbolNode$new(name))
op$add_child(p$get(4))
p$set(1, op)
},
Expand Down Expand Up @@ -90,8 +93,7 @@ Parser <- R6::R6Class(
}
},

p_expression_uminus = function(doc="expression : '-' expression %prec UMINUS
| '+' expression %prec UMINUS", p) {
p_expression_uminus = function(doc="expression : '-' expression %prec UMINUS", p) {
op <- UnaryOpNode$new(p$get(2))
op$add_child(p$get(3))
p$set(1, op)
Expand Down Expand Up @@ -158,3 +160,40 @@ Parser <- R6::R6Class(
}
)
)

# Checks applied on the parsed expression, aligned with datashield4j's DSRScriptValidator and NoAssignmentValidator.
validateNode <- function(node, aggregate = FALSE, parent = NULL, inFormula = FALSE) {
if (aggregate && is.null(parent)) {
# parentheses are transparent, as in datashield4j's grammar
root <- node
while (inherits(root, "GroupNode")) root <- root$children[[1]]
if (!inherits(root, "FunctionNode"))
stop("Aggregate expression must invoke a function.", call. = FALSE)
}
if (inherits(node, "ParameterNode")) {
if (inherits(parent, "FunctionNode")) {
# R only accepts a name or a string there: a NAME token can also hold a column (D$A)
if (!inherits(node$children[[1]], "StringNode") && !grepl("^[a-zA-Z][a-zA-Z0-9_.]*$", node$children[[1]]$name))
stop("Invalid argument name.", call. = FALSE)
} else if (aggregate) {
# anywhere else than in a named argument, = assigns in the R session
stop("Assignments are not allowed.", call. = FALSE)
}
}
# ranges are limited to numbers, as R's ':' operator would also accept symbols and function calls
# (in a formula, ':' is an interaction term)
if (inherits(node, "RangeNode") && !inFormula && !all(vapply(node$children, isNumericNode, logical(1))))
stop("Range operands must be numbers.", call. = FALSE)
inFormula <- inFormula || inherits(node, "FormulaNode")
for (child in node$children)
if (!is.null(child)) validateNode(child, aggregate, node, inFormula)
invisible(node)
}

isNumericNode <- function(node) {
if (inherits(node, "NumericNode")) return(TRUE)
ok <- inherits(node, c("RangeNode", "GroupNode")) ||
(inherits(node, "UnaryOpNode") && node$name == "-") ||
(inherits(node, "BinaryOpNode") && node$name %in% c("+", "-", "*", "/", "^", "%*%"))
ok && all(vapply(node$children, isNumericNode, logical(1)))
}
27 changes: 24 additions & 3 deletions tests/testthat/test-parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ test_that("A number with floating point, exponent and negative signs", {
})

test_that("A number with floating point, exponent and positive signs", {
ast <- DSLite::testParse("+3.1e+4")
expect_true(inherits(ast, "UnaryOpNode"))
expect_equal(ast$to_string(), "+3.1e+4")
ast <- DSLite::testParse("3.1e+4")
expect_true(inherits(ast, "NumericNode"))
expect_equal(ast$to_string(), "3.1e+4")
# no unary plus, as in datashield4j
expect_error(DSLite::testParse("+3.1e+4"))
})

test_that("An integer", {
Expand Down Expand Up @@ -377,3 +379,22 @@ test_that("A function with parameters", {
expect_true(inherits(ast, "FunctionNode"))
expect_equal(ast$to_string(), "some_func(B, C(), D, E(F(G / H)), A + B * C())")
})

test_that("Validation is aligned with datashield4j", {
valid <- function(expr, aggregate = FALSE) DSLite:::validateNode(DSLite::testParse(expr), aggregate)
for (expr in c("1:10", "c(-1:3)", "f(1:(2*3))", "f(2^1:3)", "f(y ~ a:b)", "f(x = 1)", "f(x.y_1 = 1)", "x = f(1)"))
expect_silent(valid(expr))
for (expr in c("f(1:n)", "f(D$A:3)", "f(1:g(3))", "f('a':3)"))
expect_error(valid(expr), "Range operands must be numbers")
expect_error(valid("f(D$A = 1)"), "Invalid argument name")
expect_error(valid("f(_a = 1)"), "Invalid argument name")
expect_error(valid("f((x = 1))", aggregate = TRUE), "Assignments are not allowed")
expect_silent(valid("f(x = 1)", aggregate = TRUE))
expect_silent(valid("f('x' = 1, \"y\" = 2)"))
expect_equal(DSLite::testParse("f('x' = 1)")$to_string(), "f('x' = 1)")
expect_error(DSLite::testParse("+1"))
for (expr in c("1 + f(1)", "-f(1)", "x", "'a'", "x = f(1)"))
expect_error(valid(expr, aggregate = TRUE), "must invoke a function|Assignments")
expect_silent(valid("((f(1)))", aggregate = TRUE))
expect_error(valid("1 + f(1)", aggregate = TRUE), "must invoke a function")
})
18 changes: 18 additions & 0 deletions tests/testthat/test-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ test_that("methods operations work", {
testServ$assignMethod("foo", NULL)
expect_equal(testServ$aggregateMethod("foo"), NULL)
})

test_that("aggregate rejects assignments", {
methods <- data.frame(name = "length", value = "base::length", class = "function", stringsAsFactors = FALSE)
serv <- DSLite::DSLiteServer$new(config = list(AggregateMethods = methods), strict = TRUE)
sid <- serv$newSession()
expect_error(serv$aggregate(sid, "length((x = 1))"), "Assignments are not allowed")
expect_false(exists("x", envir = serv$getSession(sid), inherits = FALSE))
serv$closeSession(sid)
})

test_that("expressions are validated without configured methods", {
serv <- DSLite::DSLiteServer$new(config = list(), strict = TRUE)
sid <- serv$newSession()
expect_error(serv$aggregate(sid, "length((x = 1))"), "Assignments are not allowed")
expect_error(serv$aggregate(sid, "1 + 1"), "must invoke a function")
expect_equal(serv$aggregate(sid, "length(1:3)"), 3)
serv$closeSession(sid)
})
Loading