Skip to content

Commit 7cb57fc

Browse files
committed
add disclosure check for matrix assign function
1 parent 12d90f6 commit 7cb57fc

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎R/matrixDetDS1.R‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
matrixDetDS1 <- function(M1.name=NULL,logarithm){
1818

19+
dsBase::checkPermissivePrivacyControlLevel(c('permissive', 'avocado', 'banana'))
20+
21+
thr <- dsBase::listDisclosureSettingsDS()
22+
nfilter.subset <- as.numeric(thr$nfilter.subset)
23+
1924
M1 <- .loadServersideObject(M1.name)
2025
.checkClass(obj = M1, obj_name = M1.name, permitted_classes = c("matrix", "data.frame"))
2126

@@ -33,6 +38,12 @@ if(ncol(M1)!=nrow(M1))
3338
stop(error.message, call. = FALSE)
3439
}
3540

41+
#Check matrix large enough to reduce disclosure risk
42+
if(nrow(M1)<nfilter.subset)
43+
{
44+
error.message<-"FAILED: matrix is too small (nrows < nfilter.subset), please respecify"
45+
stop(error.message, call. = FALSE)
46+
}
3647

3748
output<-determinant(M1,logarithm=logarithm)
3849

‎tests/testthat/test-smk-matrixDetDS1.R‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ set.standard.disclosure.settings()
1111
# Tests
1212
#
1313

14-
test_that("simple matrixDetDS1", {
15-
M1 <- matrix(c(1, 2, 3, 4), 2, 2)
16-
14+
test_that("simple matrixDetDS1 passes with matrix of sufficient dimensions", {
15+
M1 <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 10), 3, 3)
1716
res <- matrixDetDS1("M1", logarithm=FALSE)
18-
19-
expect_true(is.list(res))
2017
expect_equal(res$matrix.determinant, determinant(M1, logarithm=FALSE))
2118
})
2219

@@ -28,3 +25,18 @@ test_that("matrixDetDS1 errors when input is wrong type", {
2825
bad_input <- c("a", "b", "c")
2926
expect_error(matrixDetDS1("bad_input", logarithm=FALSE), regexp = "must be of type")
3027
})
28+
29+
test_that("matrixDetDS1 errors when matrix is too small (disclosure guard)", {
30+
M1x1 <- matrix(7, 1, 1)
31+
M2x2 <- matrix(c(1, 2, 3, 4), 2, 2)
32+
33+
expect_error(matrixDetDS1("M1x1", logarithm=FALSE), regexp = "too small")
34+
expect_error(matrixDetDS1("M2x2", logarithm=FALSE), regexp = "too small")
35+
})
36+
37+
test_that("matrixDetDS1 is blocked in non-permissive mode", {
38+
options(datashield.privacyControlLevel = "non-permissive")
39+
on.exit(options(datashield.privacyControlLevel = NULL), add = TRUE)
40+
M1 <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 10), 3, 3)
41+
expect_error(matrixDetDS1("M1", logarithm=FALSE), regexp = "non-permissive")
42+
})

0 commit comments

Comments
 (0)