Skip to content
Open
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
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
**Vulnerability:** Unvalidated inputs passed to `if()` statements can cause process crashes (`condition has length > 1`) or unexpected coercion vulnerabilities.
**Learning:** In R, optional boolean parameters that default to `NULL` should be validated using explicit runtime type validation (e.g., `if (!is.null(flag) && (!is.logical(flag) || length(flag) != 1 || is.na(flag)))`).
**Prevention:** Always implement explicit runtime type validation for optional boolean parameters.
## 2024-09-14 - Fix weak regex validation for readline inputs
**Vulnerability:** Weak regex `^[0-9]+$` on interactive `readline` inputs allows passing arbitrarily large numbers that exceed the 32-bit integer limit, resulting in coercion to `NA` when passed to `as.integer()` and breaking downstream logic.
**Learning:** When validating `readline()` inputs intended for `as.integer()` coercion, using unbounded regex like `^[0-9]+$` is a security vulnerability because inputs exceeding the 32-bit integer limit coerce to `NA`.
**Prevention:** Always use strictly bounded exact-match regex (e.g., `^[12]$`) when parsing integer choices to prevent coercion to `NA`.
6 changes: 3 additions & 3 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ autoFIPC <-
}
for (attempt in seq_len(3)) {
n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ")
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ autoFIPC <-
readline(
prompt = "Do you want to use default BILOG-MG priors for oldform Data? (1: Yes 2: No) : "
)
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ autoFIPC <-
readline(
prompt = "Do you want to use default BILOG-MG priors for newform Data? (1: Yes 2: No) : "
)
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down
55 changes: 55 additions & 0 deletions tests/testthat/test-sentinel-validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,58 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp
"Security Error: confirmCommonItems must be a single non-NA logical value or NULL"
)
})

test_that("autoFIPC strictly validates readline inputs using exact bounds", {
dummy_mod <- function(...) {
mod <- new("SingleGroupClass")
mod@OptimInfo$converged <- TRUE
mod@OptimInfo$secondordertest <- TRUE
mod@Data$data <- data.frame(A=1, B=2)
return(mod)
}

mockery::stub(autoFIPC, "mirt::mirt", dummy_mod)
mockery::stub(autoFIPC, "interactive", function() TRUE)
mockery::stub(autoFIPC, "readline", mockery::mock("33333333333", "33333333", "2"))

expect_error(
expect_message(
autoFIPC(
newformXData = data.frame(A=1, B=2),
oldformYData = data.frame(A=1, B=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
confirmCommonItems = NULL
),
"Checking correspond common item names"
),
"Please write down pairs correctly"
)
})

test_that("autoFIPC strictly validates oldformBILOGprior and newformBILOGprior using exact bounds", {
dummy_mod <- function(...) {
mod <- new("SingleGroupClass")
mod@OptimInfo$converged <- TRUE
mod@OptimInfo$secondordertest <- TRUE
mod@Data$data <- data.frame(A=1, B=2)
return(mod)
}

mockery::stub(autoFIPC, "mirt::mirt", dummy_mod)
mockery::stub(autoFIPC, "interactive", function() TRUE)
mockery::stub(autoFIPC, "readline", mockery::mock("1", "3333333", "333333", "3333333", "33333333"))

expect_error(
autoFIPC(
newformXData = data.frame(A=1, B=2),
oldformYData = data.frame(A=1, B=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
oldformBILOGprior = NULL,
newformBILOGprior = NULL,
confirmCommonItems = NULL
),
"Too many invalid oldform BILOG prior attempts"
)
})
Loading