diff --git a/.Rbuildignore b/.Rbuildignore index 232504f..28b2d85 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -22,3 +22,6 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^\.semgrepignore$ +^test_dummy\.R$ +^test_validation\.R$ diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a4..1108d62 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -1,4 +1,21 @@ +# Sentinel Journal + ## 2024-07-12 - Fix missing parameter validations -**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. + +**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-13 - Fix integer overflow coercion vulnerability in readline input validation + +**Vulnerability:** Weak regex `^[0-9]+$` allows excessive numeric strings that +coerce to `NA` inside `as.integer()`, which can lead to unhandled process crashes. +**Learning:** `readline()` validation should strictly match exact expected bounds +for interactive prompts instead of open-ended digit validation to prevent +integer overflow coercion. +**Prevention:** Strictly match against exact expected values (e.g., `^[12]$`) +instead of unbounded digit classes when validating user inputs mapped to integers. 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-autoFIPC.R b/tests/testthat/test-autoFIPC.R index 13cecd9..c1be0b3 100644 --- a/tests/testthat/test-autoFIPC.R +++ b/tests/testthat/test-autoFIPC.R @@ -89,3 +89,24 @@ test_that("autoFIPC validates input types securely", { "Security Error: tryEM must be a single non-NA logical value" ) }) + +test_that("binary prompt choices reject overflow and malformed values", { + expect_identical(aFIPC:::parseBinaryChoice("1"), 1L) + expect_identical(aFIPC:::parseBinaryChoice("2"), 2L) + + invalid_values <- list( + "0", + "3", + "01", + "2147483648", + paste0(strrep("9", 10000L)), + "1\n2", + "", + NA_character_, + c("1", "2"), + 1 + ) + for (value in invalid_values) { + expect_identical(aFIPC:::parseBinaryChoice(value), NA_integer_) + } +})