diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..8cf8ac09 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -2,3 +2,8 @@ **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-19 - Fix Integer Overflow in readline validation +**Vulnerability:** Weak numeric regex `^[0-9]+$` for `readline()` inputs intended for `as.integer()` coercion causes integer overflows. Inputs exceeding the 32-bit integer limit coerce to `NA`, potentially breaking downstream logic or causing crashes. +**Learning:** Always use strictly bounded exact-match regex (e.g., `^[12]$`) when expecting specific small integer values from user input. +**Prevention:** Use strictly bounded regular expressions when parsing numeric options from user interactive sessions. diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..c90753c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ Description: Automates fixed item parameter linking for test linking under the item response theory paradigm using mirt package estimates. License: GPL-3 | file LICENSE Imports: mirt, methods -Suggests: testthat (>= 3.0.0) +Suggests: testthat (>= 3.0.0), mockery Encoding: UTF-8 Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 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..d8fcb50b 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -1,7 +1,7 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGprior, and confirmCommonItems", { # newformBILOGprior expect_error( - aFIPC::autoFIPC( + autoFIPC( newformXData = data.frame(A=1), oldformYData = data.frame(A=2), newformCommonItemNames = c('A'), @@ -13,7 +13,7 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp # oldformBILOGprior expect_error( - aFIPC::autoFIPC( + autoFIPC( newformXData = data.frame(A=1), oldformYData = data.frame(A=2), newformCommonItemNames = c('A'), @@ -25,7 +25,7 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp # confirmCommonItems expect_error( - aFIPC::autoFIPC( + autoFIPC( newformXData = data.frame(A=1), oldformYData = data.frame(A=2), newformCommonItemNames = c('A'), @@ -35,3 +35,19 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp "Security Error: confirmCommonItems must be a single non-NA logical value or NULL" ) }) + +test_that("readline validates exact 1 or 2 to avoid integer overflows", { + mockery::stub(autoFIPC, 'readline', mockery::mock("3", "9999999999", "abc", cycle = TRUE)) + mockery::stub(autoFIPC, 'interactive', function() TRUE) + + expect_error( + autoFIPC( + newformXData = data.frame(A=1), + oldformYData = data.frame(A=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = NULL + ), + "Too many invalid common item confirmation attempts" + ) +})