diff --git a/src/main/java/io/floci/aws/core/common/IamEnforcementFilter.java b/src/main/java/io/floci/aws/core/common/IamEnforcementFilter.java index af5b5ea633..25afec2824 100644 --- a/src/main/java/io/floci/aws/core/common/IamEnforcementFilter.java +++ b/src/main/java/io/floci/aws/core/common/IamEnforcementFilter.java @@ -27,8 +27,6 @@ import io.floci.aws.services.s3.S3ObjectActions; import io.floci.aws.services.s3.S3ScratchSpace; import io.floci.aws.services.s3.S3Service; import io.quarkus.vertx.http.runtime.CurrentVertxRequest; -import io.vertx.core.http.HttpServerRequest; -import io.vertx.ext.web.RoutingContext; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.Instance; import jakarta.inject.Inject; @@ -42,8 +40,6 @@ import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.Provider; import org.jboss.logging.Logger; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -528,30 +524,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter { Optional presignedScope = iamService.presignedScope(akid); if (presignedScope.isPresent()) { PresignedScope scope = presignedScope.get(); - if ("s3".equals(credentialScope)) { - // UriInfo has normalized consecutive slashes. S3 and the SigV4 verifier use - // the wire path, where /bucket//key names the distinct object /key. - RoutingContext routingContext = currentVertxRequest.getCurrent(); - HttpServerRequest request = routingContext == null ? null : routingContext.request(); - // Use the wire path that S3's SigV4 verifier signs. uri() may instead be an - // absolute-form request target forwarded by a proxy. - String rawPath = request == null ? null : request.path(); - if (rawPath == null || !rawPath.startsWith("/")) { - ctx.abortWith(accessDeniedForRequest(action, credentialScope, ctx)); - return; - } - String decodedPath; - try { - decodedPath = URLDecoder.decode(rawPath.replace("+", "%2B"), StandardCharsets.UTF_8); - } catch (IllegalArgumentException invalidEncoding) { - ctx.abortWith(accessDeniedForRequest(action, credentialScope, ctx)); - return; - } - resources = List.of(AwsArnUtils.Arn.global(AwsRegions.partitionFor(region), - "s3", "", decodedPath.substring(1)).toString()); - } // IAM's resource glob treats * and ? in object keys as patterns. An internal URL - // credential is narrower: it may authorize only its literal object and action. That + // credential is narrower: it may authorize only its literal object and action, which the + // request's resources name as its service reads it (an S3 key keeps the slashes it was + // sent with, as S3's own reading of it gives them). That // action is never a version action: a generated URL signs only its X-Amz-* parameters, // so it names no versionId, and PreSignedUrlFilter refuses one appended to it as a // signature mismatch before this check. diff --git a/src/test/java/io/floci/aws/core/common/IamEnforcementFilterTest.java b/src/test/java/io/floci/aws/core/common/IamEnforcementFilterTest.java index f5522ede80..3849b97ea1 100644 --- a/src/test/java/io/floci/aws/core/common/IamEnforcementFilterTest.java +++ b/src/test/java/io/floci/aws/core/common/IamEnforcementFilterTest.java @@ -21,8 +21,6 @@ import io.floci.aws.services.s3.S3ScratchSpace; import io.floci.aws.services.s3.S3Service; import io.floci.aws.services.scheduler.SchedulerController; import io.quarkus.vertx.http.runtime.CurrentVertxRequest; -import io.vertx.core.http.HttpServerRequest; -import io.vertx.ext.web.RoutingContext; import jakarta.enterprise.inject.Instance; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ResourceInfo; @@ -1094,22 +1092,35 @@ class IamEnforcementFilterTest { verify(containerRequest).abortWith(any()); } + /** A scoped presigned request whose resources are not its URL's own object is refused. */ @Test - void scopedPresignedRequestWithoutRoutingContextIsDenied() { - assertScopedPresignedRequestWithoutHttpRequestIsDenied(null); - } - - @Test - void scopedPresignedRequestWithoutHttpRequestIsDenied() { - assertScopedPresignedRequestWithoutHttpRequestIsDenied(mock(RoutingContext.class)); - } - - @Test - void scopedPresignedRequestUsesWirePathForAbsoluteFormProxyTarget() { + void aScopedPresignedRequestOnAnotherResourceIsDenied() { + ContainerRequestContext containerRequest = mock(ContainerRequestContext.class); + String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, " + + "SignedHeaders=host, Signature=abc"; + when(containerRequest.getHeaderString("Authorization")).thenReturn(auth); + when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIASCOPE"); + when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:GetObject"); + when(iamService.resolveCallerContext(eq("ASIASCOPE"), any())) + .thenReturn(CallerContext.of(List.of("{\"Version\":\"2012-10-17\",\"Statement\":[]}"))); + when(iamService.presignedScope("ASIASCOPE")) + .thenReturn(Optional.of(new IamService.PresignedScope( + "s3:GetObject", "arn:aws:s3:::bucket/key"))); + + newFilter().filter(containerRequest); + + ArgumentCaptor response = ArgumentCaptor.forClass(Response.class); + verify(containerRequest).abortWith(response.capture()); + assertEquals(403, response.getValue().getStatus()); + } + + /** + * A scoped presigned request is checked on the resources its request names, as its service reads them: an S3 key + * keeps the slashes it was sent with, so {@code /bucket//key} is the object {@code /key}. + */ + @Test + void aScopedPresignedRequestIsCheckedOnTheResourcesItsRequestNames() { ContainerRequestContext containerRequest = mock(ContainerRequestContext.class); - CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class); - RoutingContext routingContext = mock(RoutingContext.class); - HttpServerRequest httpRequest = mock(HttpServerRequest.class); String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, " + "SignedHeaders=host, Signature=abc"; requestContext.setAccountId("000000000000"); @@ -1121,16 +1132,14 @@ class IamEnforcementFilterTest { when(iamService.presignedScope("ASIASCOPE")) .thenReturn(Optional.of(new IamService.PresignedScope( "s3:GetObject", "arn:aws:s3:::bucket//key"))); - when(currentVertxRequest.getCurrent()).thenReturn(routingContext); - when(routingContext.request()).thenReturn(httpRequest); - when(httpRequest.uri()).thenReturn("https://proxy.example/bucket//key?X-Amz-Algorithm=AWS4-HMAC-SHA256"); - when(httpRequest.path()).thenReturn("/bucket//key"); + when(arnBuilder.buildResources(eq("s3"), any(), eq(containerRequest), any(), any(), any())) + .thenReturn(List.of("arn:aws:s3:::bucket//key")); when(conditionContextResolver.resolveRemainingTargets("s3", "s3:GetObject", containerRequest)) .thenReturn(List.of()); when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), any(), any(), any())) .thenReturn(IamPolicyEvaluator.Decision.ALLOW); - newFilter(currentVertxRequest).filter(containerRequest); + newFilter().filter(containerRequest); verify(containerRequest, never()).abortWith(any()); verify(evaluator).evaluateResolvedResourcePolicy(any(), any(), any(), @@ -1143,9 +1152,6 @@ class IamEnforcementFilterTest { // the extra actions of an object creation on its own: the policies here allow everything, // so only the guard can deny the legal hold. ContainerRequestContext containerRequest = mock(ContainerRequestContext.class); - CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class); - RoutingContext routingContext = mock(RoutingContext.class); - HttpServerRequest httpRequest = mock(HttpServerRequest.class); UriInfo uriInfo = mock(UriInfo.class); when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>()); when(containerRequest.getUriInfo()).thenReturn(uriInfo); @@ -1162,13 +1168,12 @@ class IamEnforcementFilterTest { when(iamService.presignedScope("ASIASCOPE")) .thenReturn(Optional.of(new IamService.PresignedScope( "s3:PutObject", "arn:aws:s3:::bucket/key"))); - when(currentVertxRequest.getCurrent()).thenReturn(routingContext); - when(routingContext.request()).thenReturn(httpRequest); - when(httpRequest.path()).thenReturn("/bucket/key"); + when(arnBuilder.buildResources(eq("s3"), any(), eq(containerRequest), any(), any(), any())) + .thenReturn(List.of("arn:aws:s3:::bucket/key")); when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), any(), any(), any())) .thenReturn(IamPolicyEvaluator.Decision.ALLOW); - newFilter(currentVertxRequest).filter(containerRequest); + newFilter().filter(containerRequest); ArgumentCaptor denied = ArgumentCaptor.captor(); verify(containerRequest).abortWith(denied.capture()); @@ -1176,30 +1181,6 @@ class IamEnforcementFilterTest { assertTrue(entityString(denied.getValue()).contains("s3:PutObjectLegalHold")); } - private void assertScopedPresignedRequestWithoutHttpRequestIsDenied(RoutingContext routingContext) { - ContainerRequestContext containerRequest = mock(ContainerRequestContext.class); - CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class); - String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, " - + "SignedHeaders=host, Signature=abc"; - when(containerRequest.getHeaderString("Authorization")).thenReturn(auth); - when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIASCOPE"); - when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:GetObject"); - when(iamService.resolveCallerContext(eq("ASIASCOPE"), any())) - .thenReturn(CallerContext.of(List.of("{\"Version\":\"2012-10-17\",\"Statement\":[]}"))); - when(iamService.presignedScope("ASIASCOPE")) - .thenReturn(Optional.of(new IamService.PresignedScope( - "s3:GetObject", "arn:aws:s3:::bucket/key"))); - if (routingContext != null) { - when(currentVertxRequest.getCurrent()).thenReturn(routingContext); - } - - newFilter(currentVertxRequest).filter(containerRequest); - - ArgumentCaptor response = ArgumentCaptor.forClass(Response.class); - verify(containerRequest).abortWith(response.capture()); - assertEquals(403, response.getValue().getStatus()); - } - @Test void bareAccountIdKeyWithNoScpCeilingStillPassesThrough() { ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);