diff --git a/.Rbuildignore b/.Rbuildignore index 232504f..a08052b 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -22,3 +22,5 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^\.semgrepignore$ +^test_validation\.R$ diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a4..033b492 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-30 - Integer Overflow Coercion Vulnerability with as.integer() +**Vulnerability:** Interactive `readline()` prompts validated input using unbounded regex `^[0-9]+$` before coercion with `as.integer()`. +**Learning:** This introduces a potential integer overflow coercion vulnerability (DoS). Excessively large numeric strings (e.g. `9999999999999999999999`) pass the regex check but overflow the max integer limit when evaluated by `as.integer()`, resulting in `NA` and causing downstream type errors or process crashes. +**Prevention:** Strictly match against exact expected values using bounded regular expressions (e.g. `^[12]$` instead of `^[0-9]+$`) when validating input choices prior to integer coercion. diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1..c90753c 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 6254651..918e19b 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/test_dummy.R b/test_dummy.R deleted file mode 100644 index e6f7019..0000000 --- a/test_dummy.R +++ /dev/null @@ -1,2 +0,0 @@ -source("R/aFIPC.R") -source("R/surveyFA.R") diff --git a/tests/testthat/test-sentinel-validation.R b/tests/testthat/test-sentinel-validation.R index 900f0ee..111be18 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -35,3 +35,108 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp "Security Error: confirmCommonItems must be a single non-NA logical value or NULL" ) }) + +test_that("interactive prompt readline validates inputs strictly", { + # Mock interactive to return TRUE and mock readline input + # We test that an invalid input (e.g. '3' or '9999999999') will cause the retry loop to fail + + mock_interactive <- mockery::mock(TRUE, cycle=TRUE) + mock_readline_fail <- mockery::mock("3", "999", "a", cycle=TRUE) + + mockery::stub(aFIPC::autoFIPC, "interactive", mock_interactive) + mockery::stub(aFIPC::autoFIPC, "readline", mock_readline_fail) + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=c(1,0,1)), + oldformYData = data.frame(A=c(1,1,0)), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A') + ), + "Too many invalid common item confirmation attempts" + ) +}) + +test_that("interactive prompt readline validates inputs strictly for BILOG priors", { + + mock_interactive <- mockery::mock(TRUE, cycle=TRUE) + mock_readline_fail <- mockery::mock("3", "999", "a", cycle=TRUE) + + mockery::stub(aFIPC::autoFIPC, "interactive", mock_interactive) + mockery::stub(aFIPC::autoFIPC, "readline", mock_readline_fail) + + # For oldformBILOGprior + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=c(1,0,1)), + oldformYData = data.frame(A=c(1,1,0)), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = TRUE, + itemtype = '3PL' + ), + "Too many invalid oldform BILOG prior attempts" + ) +}) + +test_that("interactive prompt readline validates inputs strictly for newform BILOG priors", { + + mock_interactive <- mockery::mock(TRUE, cycle=TRUE) + mock_readline_fail <- mockery::mock("3", "999", "a", cycle=TRUE) + + mockery::stub(aFIPC::autoFIPC, "interactive", mock_interactive) + mockery::stub(aFIPC::autoFIPC, "readline", mock_readline_fail) + + # To reach newformBILOGprior we need oldformBILOGprior to pass, so we pass it explicitly + # We also need enough data to not fail the mirt 3PL estimation + set.seed(123) + N <- 100 + new_data <- data.frame( + A = rbinom(N, 1, 0.5), + B = rbinom(N, 1, 0.5), + C = rbinom(N, 1, 0.5), + D = rbinom(N, 1, 0.5), + E = rbinom(N, 1, 0.5) + ) + old_data <- data.frame( + A = rbinom(N, 1, 0.5), + B = rbinom(N, 1, 0.5), + C = rbinom(N, 1, 0.5), + D = rbinom(N, 1, 0.5), + F = rbinom(N, 1, 0.5) + ) + expect_error( + aFIPC::autoFIPC( + newformXData = new_data, + oldformYData = old_data, + newformCommonItemNames = c('A', 'B', 'C', 'D'), + oldformCommonItemNames = c('A', 'B', 'C', 'D'), + confirmCommonItems = TRUE, + itemtype = '3PL', + oldformBILOGprior = FALSE + ), + "Too many invalid newform BILOG prior attempts" + ) +}) + +test_that("integer overflow via interactive readline coercion is prevented", { + + mock_interactive <- mockery::mock(TRUE, cycle=TRUE) + # This uses a value that would cause integer overflow if not strictly matched + mock_readline_overflow <- mockery::mock("99999999999999999999", cycle=TRUE) + + mockery::stub(aFIPC::autoFIPC, "interactive", mock_interactive) + mockery::stub(aFIPC::autoFIPC, "readline", mock_readline_overflow) + + # It should fail validation with "Too many invalid common item confirmation attempts" + # instead of crashing with coercion/type errors + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=c(1,0,1)), + oldformYData = data.frame(A=c(1,1,0)), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A') + ), + "Too many invalid common item confirmation attempts" + ) +})