Skip to content
Closed
16 changes: 16 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@
**Vulnerability:** Raw `stop()` and `warning()` calls without `call. = FALSE` in `llcont.R` and `vuongtest.R` exposed execution stack/call details when raised.
**Learning:** While some instances of `stop()` inside `tryCatch()` were previously fixed to hide the call stack, other standalone exceptions and warnings still leaked call context. Security must be consistently applied across the entire codebase.
**Prevention:** Always set `call. = FALSE` when using `stop()` or `warning()` to enforce a secure-by-default boundary and prevent internal execution paths from being disclosed to the end user.
## 2024-08-11 - Enforce secure error boundaries by validating input types early

**Vulnerability:**
Exported functions like `vuongtest` and `icci` expect certain arguments like `nested` to be logical scalars, and `adj` to be string scalars from a set of choices. When arrays or other types are passed (e.g. `nested=c(TRUE, FALSE)` or `adj=c("aic", "bic")`), the condition expressions (e.g., `if (nested)` or `if (adj == "aic")`) inside internal logic result in `length > 1` warnings/errors, or other raw R execution stack faults. These expose the internal execution flow to the user and violate the "fail securely" principle. They bypass the top-level `stop(..., call. = FALSE)` safeguards.

**Learning:**
In R, unvalidated arguments can bypass top-level input safeguards and trigger raw R errors deep inside internal logic, leaking internal execution contexts via error stack traces (e.g., `<simpleError in if (adj == "aic") ...>`). Relying on internal control flow `if` statements to implicitly validate inputs is insecure.

**Prevention:**
Always explicitly validate the type, length, and bounds of user inputs at the very beginning of exported functions (using assertions or functions like `match.arg()`) and use `stop(..., call. = FALSE)` to fail securely. For example:
```R
if (length(nested) > 1 || !is.logical(nested)) {
stop("Argument 'nested' must be a single logical value.", call. = FALSE)
}
adj <- match.arg(adj, c("none", "aic", "bic"))
```
4 changes: 4 additions & 0 deletions R/icci.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
#' @export
icci <- function(object1, object2, conf.level=.95, ll1=llcont, ll2=llcont) {

if (length(conf.level) > 1 || !is.numeric(conf.level) || conf.level <= 0 || conf.level >= 1) {
stop("Argument 'conf.level' must be a single numeric value between 0 and 1.", call. = FALSE)
}

## check objects, issue warnings/errors, get classes/calls
obinfo <- check.obj(object1, object2)
callA <- obinfo$callA; classA <- obinfo$classA
Expand Down
5 changes: 5 additions & 0 deletions R/vuongtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
#' @export
vuongtest <- function(object1, object2, nested=FALSE, adj="none", ll1=llcont, ll2=llcont, score1=NULL, score2=NULL, vc1=vcov, vc2=vcov) {

if (length(nested) > 1 || !is.logical(nested)) {
stop("Argument 'nested' must be a single logical value.", call. = FALSE)
}
adj <- match.arg(adj, c("none", "aic", "bic"))

## check objects, issue warnings/errors, get classes/calls
obinfo <- check.obj(object1, object2)
callA <- obinfo$callA; classA <- obinfo$classA
Expand Down
58 changes: 58 additions & 0 deletions tests/testthat/test-input-validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
capture_input_error <- function(expression) {
tryCatch(force(expression), error = identity)
}

expect_fail_closed_input <- function(error, expected_message) {
expect_s3_class(error, "error")
expect_identical(conditionMessage(error), expected_message)
expect_null(conditionCall(error))
}

test_that("vuongtest rejects invalid nested values without exposing calls", {
invalid_values <- list(logical(), c(TRUE, FALSE), NA, "yes")

for (value in invalid_values) {
error <- capture_input_error(vuongtest(NULL, NULL, nested = value))
expect_fail_closed_input(
error,
"Argument 'nested' must be a single non-missing logical value."
)
}
})

test_that("vuongtest rejects invalid adjustment values without exposing calls", {
invalid_values <- list(character(), c("aic", "bic"), NA_character_, "other", 1)

for (value in invalid_values) {
error <- capture_input_error(vuongtest(NULL, NULL, adj = value))
expect_fail_closed_input(
error,
"Argument 'adj' must be one of \"none\", \"aic\", or \"bic\"."
)
}
})

test_that("icci rejects invalid confidence levels without exposing calls", {
invalid_values <- list(
numeric(),
c(0.90, 0.95),
NA_real_,
NaN,
Inf,
-Inf,
0,
1,
"0.95"
)

for (value in invalid_values) {
error <- capture_input_error(icci(NULL, NULL, conf.level = value))
expect_fail_closed_input(
error,
paste(
"Argument 'conf.level' must be a single finite numeric value",
"strictly between 0 and 1."
)
)
}
})
Loading