diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..0cccc72f 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-08-27 - Fix unhandled NA coercion crash in interactive prompts +**Vulnerability:** Weak regex `^[0-9]+$` on interactive integer inputs allows large numbers which coerce to `NA` via `as.integer()`, breaking `if` conditions and causing unhandled exceptions. +**Learning:** Using overly permissive regex for bounded integer choices exposes the application to coercion crashes. +**Prevention:** Use strictly bounded exact-match regex like `^[12]$` when reading integer choices via `readline()` in R to prevent coercion crashes. diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..f5216ca5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,8 @@ 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-readline-coercion.R b/tests/testthat/test-readline-coercion.R new file mode 100644 index 00000000..c8486e17 --- /dev/null +++ b/tests/testthat/test-readline-coercion.R @@ -0,0 +1,16 @@ +test_that("interactive prompt handles large numbers correctly without crashing", { + # We mock readline to return a huge number that coerces to NA + my_readline <- function(...) "9999999999999999999999999" + mockery::stub(aFIPC::autoFIPC, 'readline', my_readline) + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=1), + oldformYData = data.frame(A=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A') + ), + "Too many invalid common item confirmation attempts" + ) +})