From a12ce737976fd0cc061246e4a305c5ed1f96456e Mon Sep 17 00:00:00 2001 From: Tadgh Date: Tue, 21 Sep 2021 15:31:36 -0400 Subject: [PATCH] Add more tests --- .../java/ca/uhn/fhir/util/FhirTerser.java | 5 ++- .../server/interceptor/auth/RuleImplOp.java | 3 +- .../auth/AuthorizationInterceptorR4Test.java | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java index 34d5db49c5c..aff6e95cf26 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java @@ -48,6 +48,7 @@ import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; @@ -738,7 +739,9 @@ public class FhirTerser { //If passed an additional set of searchparameter names, add them for comparison purposes. if (theAdditionalCompartmentParamNames != null) { - List additionalParams = theAdditionalCompartmentParamNames.stream().map(paramName -> sourceDef.getSearchParam(paramName)).collect(Collectors.toList()); + List additionalParams = theAdditionalCompartmentParamNames.stream().map(sourceDef::getSearchParam) + .filter(Objects::nonNull) + .collect(Collectors.toList()); if (params == null || params.isEmpty()) { params = additionalParams; } else { diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleImplOp.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleImplOp.java index 697a48c75c9..fa5841be869 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleImplOp.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/auth/RuleImplOp.java @@ -30,6 +30,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -382,7 +383,7 @@ class RuleImplOp extends BaseRule /* implements IAuthRule */ { List params = sourceDef.getSearchParamsForCompartmentName(compartmentOwnerResourceType); Set additionalParamNames = myAdditionalCompartmentSearchParamMap.getSearchParamNamesForResourceType(sourceDef.getName()); - List additionalParams = additionalParamNames.stream().map(sourceDef::getSearchParam).collect(Collectors.toList()); + List additionalParams = additionalParamNames.stream().map(sourceDef::getSearchParam).filter(Objects::nonNull).collect(Collectors.toList()); if (params == null || params.isEmpty()) { params = additionalParams; } else { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/AuthorizationInterceptorR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/AuthorizationInterceptorR4Test.java index 2b79752822f..4ae0084f50b 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/AuthorizationInterceptorR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/auth/AuthorizationInterceptorR4Test.java @@ -460,6 +460,47 @@ public class AuthorizationInterceptorR4Test { assertEquals(403, status.getStatusLine().getStatusCode()); } + + @Test + public void testNonsenseParametersThrowAtRuntime() throws Exception { + //Given + ourServlet.registerInterceptor(new AuthorizationInterceptor(PolicyEnum.DENY) { + @Override + public List buildRuleList(RequestDetails theRequestDetails) { + AdditionalCompartmentSearchParameters additionalCompartmentSearchParameters = new AdditionalCompartmentSearchParameters(); + additionalCompartmentSearchParameters.addSearchParameters("device:garbage"); + List relatedIds = new ArrayList<>(); + relatedIds.add(new IdType("Patient/123")); + return new RuleBuilder() + .allow().read().allResources() + .inCompartmentWithAdditionalSearchParams("Patient", relatedIds, additionalCompartmentSearchParameters) + .andThen().denyAll() + .build(); + } + }); + + HttpGet httpGet; + HttpResponse status; + + Patient patient; + patient = new Patient(); + patient.setId("Patient/123"); + Device d = new Device(); + d.getPatient().setResource(patient); + + ourHitMethod = false; + ourReturn = Collections.singletonList(d); + + //When + httpGet = new HttpGet("http://localhost:" + ourPort + "/Device/124456"); + status = ourClient.execute(httpGet); + extractResponseAndClose(status); + + //then + assertFalse(ourHitMethod); + assertEquals(403, status.getStatusLine().getStatusCode()); + } + @Test public void testRuleBuilderAdditionalSearchParamsInvalidValues() { //Too many colons