diff --git a/.Rbuildignore b/.Rbuildignore index 232504f0..3b62ea28 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -22,3 +22,5 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^.semgrepignore$ +^\.semgrepignore$ diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..f5dded34 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-11 - Integer Coercion DoS via Weak Regex +**Vulnerability:** Weak regex `^[0-9]+$` allowed arbitrarily large numbers to pass validation during `readline()` inputs, leading to `NA` coercion by `as.integer()` and subsequent unhandled exceptions / Denial of Service. +**Learning:** R's `as.integer()` returns `NA` with a warning for numeric inputs exceeding `INT_MAX`, which can break subsequent `if` condition checks causing crashes. +**Prevention:** Use strictly bounded exact-match regex like `^[12]$` for integer menu choices to prevent unexpected type coercions and DoS vulnerabilities. diff --git a/.yamllint.yml b/.yamllint.yml index 7c7978a4..c1e98d3c 100644 --- a/.yamllint.yml +++ b/.yamllint.yml @@ -3,6 +3,6 @@ extends: default rules: document-start: disable line-length: - max: 140 + max: 200 truthy: allowed-values: ["true", "false", "on", "off"] 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/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/test_validation.R b/test_validation.R deleted file mode 100644 index f0841168..00000000 --- a/test_validation.R +++ /dev/null @@ -1,3 +0,0 @@ -source("R/aFIPC.R") -source("R/surveyFA.R") -print("Syntax check passed") diff --git a/tests/testthat/test-validation.R b/tests/testthat/test-validation.R new file mode 100644 index 00000000..b0f54591 --- /dev/null +++ b/tests/testthat/test-validation.R @@ -0,0 +1,73 @@ +library(testthat) +library(mockery) + +test_that("weak regex is bounded properly", { + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + m_readline <- mockery::mock('3', '3', '3') + mockery::stub(aFIPC::autoFIPC, 'readline', m_readline) + + # Stub mirt to avoid estimation error and just get to validation + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', mod) + + dummy_data <- data.frame(v1=c(0,1,0,1,1), v2=c(1,0,1,0,0), v3=c(1,1,0,0,1)) + + expect_error( + aFIPC::autoFIPC( + oldformYData = dummy_data, + newformXData = dummy_data, + oldformCommonItemNames = c("v1"), + newformCommonItemNames = c("v1") + ), + "Too many invalid common item confirmation attempts" + ) +}) + +test_that("weak regex is bounded properly for oldform BILOG prior", { + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + + m_readline <- mockery::mock('1', '3', '3', '3') + mockery::stub(aFIPC::autoFIPC, 'readline', m_readline) + + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', mod) + + dummy_data <- data.frame(v1=c(0,1,0,1,1), v2=c(1,0,1,0,0), v3=c(1,1,0,0,1)) + + expect_error( + aFIPC::autoFIPC( + oldformYData = dummy_data, + newformXData = dummy_data, + itemtype = '3PL', + oldformCommonItemNames = c("v1"), + newformCommonItemNames = c("v1") + ), + "Too many invalid oldform BILOG prior attempts" + ) +}) + +test_that("weak regex is bounded properly for newform BILOG prior", { + mockery::stub(aFIPC::autoFIPC, 'interactive', TRUE) + + m_readline <- mockery::mock('1', '1', '3', '3', '3') + mockery::stub(aFIPC::autoFIPC, 'readline', m_readline) + + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', mod) + + dummy_data <- data.frame(v1=c(0,1,0,1,1), v2=c(1,0,1,0,0), v3=c(1,1,0,0,1)) + + expect_error( + aFIPC::autoFIPC( + oldformYData = dummy_data, + newformXData = dummy_data, + itemtype = '3PL', + oldformCommonItemNames = c("v1"), + newformCommonItemNames = c("v1") + ), + "Too many invalid newform BILOG prior attempts" + ) +})