diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..bee7232d 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. + +## 2026-09-05 - Fix unsafe regex validation for readline inputs +**Vulnerability:** Weak regex `^[0-9]+$` for `readline()` inputs intended for `as.integer()` coercion. Inputs exceeding the 32-bit integer limit coerce to `NA`, breaking downstream logic. +**Learning:** Using unbounded numeric regex for bounded integer choices is a security vulnerability because it allows large numbers that overflow R's 32-bit integer limit. +**Prevention:** Always use strictly bounded exact-match regex (e.g., `^[12]$`) for integer choices. diff --git a/AGENTS.md b/AGENTS.md index 63f46ef4..438c75fe 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -110,3 +110,5 @@ Applies to every agent (Claude, Codex, Cursor, opencode, ...) working in this re psychometrics literature. Commit paper PDFs only when redistribution is permitted; otherwise cite, link, and summarize. + +Strix uses the zero-cost `orchestrator/free` diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..02539efd 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) Encoding: UTF-8 Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 +Suggests: testthat (>= 3.0.0), mockery 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/test_dummy.R b/test_dummy.R deleted file mode 100644 index e6f7019b..00000000 --- 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 900f0ee3..93d2842e 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -35,3 +35,78 @@ 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 strict readline validation bounds integer overflow inputs", { + # Avoid real execution overhead: use a dummy S4 object to represent models + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mod@OptimInfo$secondordertest <- TRUE + + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + + # Mock 'surveyFA' to throw an error so we short-circuit the execution immediately after checking our logic + mockery::stub(aFIPC::autoFIPC, 'surveyFA', function(...) stop('forced failure')) + + # Mock 'mirt::mirt' to always return our dummy model to bypass actual fitting + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', function(...) mod) + + # Provide valid dataframes to pass initial checks + new_df <- data.frame(A=c(1,0,1)) + old_df <- data.frame(A=c(1,1,0)) + + # Test checkCorrect (confirmCommonItems = NULL) + + # Should reject invalid inputs and fail after 3 attempts + mockery::stub(aFIPC::autoFIPC, 'readline', mockery::mock('0', '3', ' 1', 'a', '9999999999')) + expect_error( + aFIPC::autoFIPC(newformXData = new_df, oldformYData = old_df, newformCommonItemNames = 'A', oldformCommonItemNames = 'A', confirmCommonItems = NULL), + "Too many invalid common item confirmation attempts" + ) + + # Accepts exactly '1' or '2' + mockery::stub(aFIPC::autoFIPC, 'readline', mockery::mock('0', '2')) + expect_error( + aFIPC::autoFIPC(newformXData = new_df, oldformYData = old_df, newformCommonItemNames = 'A', oldformCommonItemNames = 'A', confirmCommonItems = NULL), + "Please write down pairs correctly" + ) +}) + +test_that("autoFIPC strict readline validation bounds integer overflow inputs for BILOG priors", { + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mod@OptimInfo$secondordertest <- TRUE + + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + mockery::stub(aFIPC::autoFIPC, 'surveyFA', function(...) stop('forced failure')) + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', function(...) mod) + + new_df <- data.frame(A=c(1,0,1)) + old_df <- data.frame(A=c(1,1,0)) + + # Should reject invalid inputs and fail after 3 attempts + mockery::stub(aFIPC::autoFIPC, 'readline', mockery::mock('1', '0', '3', ' 1', 'a', '9999999999')) + expect_error( + aFIPC::autoFIPC(newformXData = new_df, oldformYData = old_df, newformCommonItemNames = 'A', oldformCommonItemNames = 'A', confirmCommonItems = NULL, itemtype = '3PL', oldformBILOGprior = NULL), + "Too many invalid oldform BILOG prior attempts" + ) +}) + +test_that("autoFIPC strict readline validation bounds integer overflow inputs for newform BILOG priors", { + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mod@OptimInfo$secondordertest <- TRUE + + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + mockery::stub(aFIPC::autoFIPC, 'surveyFA', function(...) stop('forced failure')) + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', function(...) mod) + + new_df <- data.frame(A=c(1,0,1)) + old_df <- data.frame(A=c(1,1,0)) + + # Should reject invalid inputs and fail after 3 attempts + mockery::stub(aFIPC::autoFIPC, 'readline', mockery::mock('1', '0', '3', ' 1', 'a', '9999999999')) + expect_error( + aFIPC::autoFIPC(newformXData = new_df, oldformYData = old_df, newformCommonItemNames = 'A', oldformCommonItemNames = 'A', confirmCommonItems = NULL, itemtype = '3PL', newformBILOGprior = NULL, oldformBILOGprior = TRUE), + "Too many invalid newform BILOG prior attempts" + ) +})