Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-09-10 - [LOW] Strict interactive input validation
**Vulnerability:** (Correctness/Input Validation) Weak regex `^[0-9]+$` on interactive inputs allowed arbitrary integers, bypassing menu constraints and potentially causing unexpected downstream behavior.
**Learning:** Always use strictly bounded exact-match regex (e.g., `^[12]$`) when checking `readline()` inputs for constrained menu options.
**Prevention:** Use stricter regex validations for interactive prompts rather than general numeric validations.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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))
}
}
Expand Down
48 changes: 45 additions & 3 deletions tests/testthat/test-sentinel-validation.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGprior, and confirmCommonItems", {
# newformBILOGprior
expect_error(
aFIPC::autoFIPC(
autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
Expand All @@ -13,7 +13,7 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp

# oldformBILOGprior
expect_error(
aFIPC::autoFIPC(
autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
Expand All @@ -25,7 +25,7 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp

# confirmCommonItems
expect_error(
aFIPC::autoFIPC(
autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
Expand All @@ -35,3 +35,45 @@ 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 strictly validates interactive readline confirmation inputs", {
# Mock valid "1" responses to pass the three interactive prompts
mockery::stub(autoFIPC, 'readline', mockery::mock('1', '1', '1'))
mockery::stub(autoFIPC, 'interactive', function() TRUE)

# The test should proceed past the interactive prompts and fail downstream,
# indicating that the valid exact-match response '1' was accepted.
err <- tryCatch({
autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A')
)
"No error"
}, error = function(e) e$message)

expect_false(grepl("Too many invalid common item confirmation attempts", err))
})

test_that("autoFIPC correctly rejects invalid numeric inputs and hits the retry limit", {
# Mock invalid inputs: '0', '3', '9999999999' (out of range), 'abc' (non-numeric), '' (whitespace/empty)
# The loop tries 3 times. We just need to fail the first prompt (checkCorrect) 3 times.
mockery::stub(autoFIPC, 'readline', mockery::mock('0', '3', '9999999999'))
mockery::stub(autoFIPC, 'interactive', function() TRUE)

err <- tryCatch({
autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A')
)
"No error"
}, error = function(e) e$message)

expect_true(grepl("Too many invalid common item confirmation attempts", err))
})
Loading