From 16adb54b55b0ceb5945b645e4f81a8f72401a6f6 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:26:21 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITIC?= =?UTF-8?q?AL]=20Fix=20integer=20coercion=20vulnerability=20from=20weak=20?= =?UTF-8?q?regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `readline()` 입력값을 `as.integer()`로 형변환할 때, 기존의 약한 정규식 `^[0-9]+$`를 사용할 경우 범위 밖의 매우 큰 숫자가 입력되면 `NA`로 강제 변환되어 이후 조건문에서 크래시(condition has length > 1)가 발생할 수 있습니다. 이를 정확히 `^[12]$`로 제한하여 프로그램 충돌을 방지하고 보안성을 높였습니다. 해당 문제를 검증하기 위한 테스트 또한 추가하였습니다. --- .jules/sentinel.md | 5 ++ R/aFIPC.R | 6 +- tests/testthat/test-sentinel-validation.R | 69 +++++++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a4..b75474a 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-07-28 - Fix integer coercion vulnerability from weak regex +**Vulnerability:** Weak regex ^[0-9]+$ allows large integer inputs in readline that coerce to NA, crashing the process in subsequent if conditions. +**Learning:** Coercing large string numbers via as.integer() results in NA. We must strictly bound inputs for interactive prompts to valid options only. +**Prevention:** Use exactly-bounded regex like ^[12]$ when validating prompt options before coercion. 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/tests/testthat/test-sentinel-validation.R b/tests/testthat/test-sentinel-validation.R index 900f0ee..18fe199 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -35,3 +35,72 @@ 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 integer validation bounds work for interactive prompts", { + library(mockery) + + # Mock interactive() to return TRUE so we enter the readline loop + mock_interactive <- mock(TRUE, cycle = TRUE) + stub(aFIPC::autoFIPC, 'interactive', mock_interactive) + + # Stub checkCorrect readline with '3' which is invalid, it should fail + mock_readline <- mock("3", "3", "3", cycle = TRUE) + stub(aFIPC::autoFIPC, 'readline', mock_readline) + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=c(1,0,1)), + oldformYData = data.frame(A=c(0,1,0)), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = NULL # explicit NULL ensures we hit checkCorrect prompt + ), + "Too many invalid common item confirmation attempts" + ) +}) + +test_that("autoFIPC integer validation bounds work for oldform and newform BILOG prior prompts", { + library(mockery) + + mock_interactive <- mock(TRUE, cycle = TRUE) + stub(aFIPC::autoFIPC, 'interactive', mock_interactive) + + mock_readline <- mock("3", "3", "3", cycle = TRUE) + stub(aFIPC::autoFIPC, 'readline', mock_readline) + + # Need at least 4 items for mirt to fit the model initially without crashing before hitting newform prompt + df <- data.frame(A=c(1,0,1,0,1), B=c(0,1,0,1,0), C=c(1,1,0,0,1), D=c(0,0,1,1,0), E=c(1,0,1,0,1)) + + expect_error( + aFIPC::autoFIPC( + newformXData = df, + oldformYData = df, + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = TRUE, + itemtype = '3PL', + oldformBILOGprior = NULL, + newformBILOGprior = TRUE, + checkIPD = FALSE + ), + "Too many invalid oldform BILOG prior attempts" + ) + + # Stub oldform model directly so it bypasses fitting and goes to newform BILOG prompt + mock_oldformModel <- mirt::mirt(df, 1, itemtype='2PL', verbose=FALSE) + + expect_error( + aFIPC::autoFIPC( + newformXData = df, + oldformYData = mock_oldformModel, + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = TRUE, + itemtype = '3PL', + oldformBILOGprior = TRUE, + newformBILOGprior = NULL, + checkIPD = FALSE + ), + "Too many invalid newform BILOG prior attempts" + ) +}) From 7b16a1e1b9a7e454cf01ba6c1f90145768d8838f Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:32:54 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITIC?= =?UTF-8?q?AL]=20Fix=20integer=20coercion=20vulnerability=20from=20weak=20?= =?UTF-8?q?regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `readline()` 입력값을 `as.integer()`로 형변환할 때, 기존의 약한 정규식 `^[0-9]+$`를 사용할 경우 범위 밖의 매우 큰 숫자가 입력되면 `NA`로 강제 변환되어 이후 조건문에서 크래시(condition has length > 1)가 발생할 수 있습니다. 이를 정확히 `^[12]$`로 제한하여 프로그램 충돌을 방지하고 보안성을 높였습니다. 해당 문제를 검증하기 위한 테스트 또한 추가하였습니다. From 4eed7092a5aba4f4f07b1a969bd47a008b0e6dbb Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:45:20 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITIC?= =?UTF-8?q?AL]=20Fix=20integer=20coercion=20vulnerability=20from=20weak=20?= =?UTF-8?q?regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `readline()` 입력값을 `as.integer()`로 형변환할 때, 기존의 약한 정규식 `^[0-9]+$`를 사용할 경우 범위 밖의 매우 큰 숫자가 입력되면 `NA`로 강제 변환되어 이후 조건문에서 크래시(condition has length > 1)가 발생할 수 있습니다. 이를 정확히 `^[12]$`로 제한하여 프로그램 충돌을 방지하고 보안성을 높였습니다. 해당 문제를 검증하기 위한 테스트 또한 추가하였습니다. --- .Rbuildignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.Rbuildignore b/.Rbuildignore index 232504f..388f1c6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -22,3 +22,4 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^\.semgrepignore$ From 50d9f1787fc800d556b72f51beae924148cac83b Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:58:53 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITIC?= =?UTF-8?q?AL]=20Fix=20integer=20coercion=20vulnerability=20from=20weak=20?= =?UTF-8?q?regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `readline()` 입력값을 `as.integer()`로 형변환할 때, 기존의 약한 정규식 `^[0-9]+$`를 사용할 경우 범위 밖의 매우 큰 숫자가 입력되면 `NA`로 강제 변환되어 이후 조건문에서 크래시(condition has length > 1)가 발생할 수 있습니다. 이를 정확히 `^[12]$`로 제한하여 프로그램 충돌을 방지하고 보안성을 높였습니다. 해당 문제를 검증하기 위한 테스트 또한 추가하였습니다. --- .Rbuildignore | 2 ++ DESCRIPTION | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.Rbuildignore b/.Rbuildignore index 388f1c6..28b2d85 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -23,3 +23,5 @@ ^\.trivyignore\.yaml$ ^trivy\.yaml$ ^\.semgrepignore$ +^test_dummy\.R$ +^test_validation\.R$ 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