diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..157188aa 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-07-25 - Fix weak regex validation causing integer overflow DoS +**Vulnerability:** Weak regex `^[0-9]+$` allows arbitrarily large numeric strings that coerce to `NA` when cast to integer, causing crash/DoS when passed to `if()`. +**Learning:** When reading bounded numeric options via `readline()` in R, avoid weak numeric regex as large inputs will overflow native integer coercion, breaking unhandled boolean contexts. +**Prevention:** Use strictly bounded exact-match regex like `^[12]$` when validating constrained terminal inputs to prevent unexpected `NA` coercions. 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-readline.R b/tests/testthat/test-readline.R new file mode 100644 index 00000000..720ee514 --- /dev/null +++ b/tests/testthat/test-readline.R @@ -0,0 +1,30 @@ +library(testthat) + +test_that("autoFIPC handles invalid readline inputs securely", { + # We test the actual aFIPC::autoFIPC function using mockery to stub readline + # We use mockery::stub on the internal functions that call readline + + # Dummy input data + new_model <- data.frame(matrix(rnorm(20), nrow=10)) + old_model <- data.frame(matrix(rnorm(20), nrow=10)) + + # Stub interactive to return TRUE so we enter the readline branch + mockery::stub(autoFIPC, 'interactive', TRUE) + + # Mock readline to return a large number that caused the NA DoS previously + # We use forced failure to simulate the 3 failed attempts + mock_readline <- mockery::mock("1000000000000", "1000000000000", "1000000000000") + mockery::stub(autoFIPC, 'readline', mock_readline) + + # When confirmCommonItems is NULL, it prompts. If it fails 3 times, it stops. + expect_error( + autoFIPC( + newformXData = new_model, + oldformYData = old_model, + newformCommonItemNames = "X1", + oldformCommonItemNames = "X1", + confirmCommonItems = NULL + ), + "Too many invalid common item confirmation attempts" + ) +})