diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..30e263d4 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -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`. diff --git a/R/aFIPC.R b/R/aFIPC.R index 62546519..918e19b1 100644 --- a/R/aFIPC.R +++ b/R/aFIPC.R @@ -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)) } } @@ -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)) } } @@ -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)) } } diff --git a/tests/testthat/test-sentinel-validation.R b/tests/testthat/test-sentinel-validation.R index 900f0ee3..6a027f09 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -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" + ) +})