From 1026f0ee125d1661bf7d8e17e8e0335577a77baf Mon Sep 17 00:00:00 2001 From: Yannick Marcon Date: Wed, 11 Nov 2026 20:55:55 +0100 Subject: [PATCH] Align expression validation with datashield4j 3.0.0 Validate parsed expressions as datashield4j does: aggregate expressions must invoke a function and cannot assign, argument names must be names or strings, and range operands must be numbers. Accept quoted argument names, drop unary plus, and validate even when no methods are configured. --- R/DSLiteServer.R | 8 +++--- R/lex-yacc.R | 47 +++++++++++++++++++++++++++++++++--- tests/testthat/test-parser.R | 27 ++++++++++++++++++--- tests/testthat/test-server.R | 18 ++++++++++++++ 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/R/DSLiteServer.R b/R/DSLiteServer.R index d80d90d..699efe5 100644 --- a/R/DSLiteServer.R +++ b/R/DSLiteServer.R @@ -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))) @@ -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) @@ -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)) } diff --git a/R/lex-yacc.R b/R/lex-yacc.R index 8f88ae1..700e095 100644 --- a/R/lex-yacc.R +++ b/R/lex-yacc.R @@ -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) }, @@ -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) @@ -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))) +} diff --git a/tests/testthat/test-parser.R b/tests/testthat/test-parser.R index 866af7a..2fa028e 100644 --- a/tests/testthat/test-parser.R +++ b/tests/testthat/test-parser.R @@ -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", { @@ -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") +}) diff --git a/tests/testthat/test-server.R b/tests/testthat/test-server.R index 54d852c..a2c076c 100644 --- a/tests/testthat/test-server.R +++ b/tests/testthat/test-server.R @@ -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) +})