mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(core): treat a request no protocol claims as the call its route runs (#6200)
With floci.protocols.strict-claiming off, the default, a request that no wire protocol claims is logged and served by whatever route it matches. The checks after routing read the claim, so they saw a request of no protocol: - IAM enforcement served it unsigned. The MissingAuthenticationToken refusal applies only to a JSON, CBOR or Query claim. An unsigned SQS ListQueues over JSON or Query, or a Kinesis ListStreams over CBOR, was served when it carried a Smithy-Protocol header Floci does not serve that path by: an unknown value, or rpc-v2-cbor outside the rpcv2 path. An X-Amz-Target call with no Content-Type reached the CBOR endpoint the same way. - IAM enforcement checked a signed REST request as the action its X-Amz-Target header named, not its route's. A user allowed only s3:GetObject deleted an object by sending the DELETE with X-Amz-Target: AmazonS3.GetObject and any Smithy-Protocol header. A signed JSON call it refused got the REST status, 403, rather than JSON's 400 AccessDeniedException. - A call to a disabled service was served when only its claim would have named the service, as for an unsigned JSON or rpcv2 call. - A REST JSON service request that fell through to S3's wildcard routes was not rejected, and a REST request signed for a service Floci lacks skipped the guard that refuses it before routing, so S3's routes answered it with a misleading error. The new AwsRouteClaimFilter runs after route matching and before every filter that reads the claim, AccountContextFilter's unknown-Region refusal and the signature check among them. It claims such a request by the route it reached, through ProtocolClaimer.routeClaim: - the JSON 1.0, JSON 1.1 and CBOR endpoints by X-Amz-Target; - the rpcv2 route by its path; - the Query endpoint's POST by the form; - every other route as REST. Those routes serve the request the same way whatever Smithy-Protocol header it carries, so the claim names the call that runs. A request claimed as REST also gets the unknown-service-scope check a claimed one gets before routing, which AwsProtocolClaimFilter now exposes for it. A claimed request is left as it was, and strict claiming still rejects an unclaimed one before routing. An X-Amz-Target post with a content type no protocol uses, the third kind of request the strict-claiming docs list, matches no route at all, and got a 500 rather than its 415, as any root POST in a content type no endpoint consumes did. Several resource methods share POST /, so RESTEasy Reactive routes it to a placeholder with no method of its own that picks one by content type, and answers 415 from there when none consumes it. The ResourceInfo producer then returns the placeholder's missing method, and CDI refuses the null with IllegalProductException. AwsRestJsonErrorHeaderFilter and S3CorsFilter read ResourceInfo on every response, so each failed on it. Both now read it through the new RoutedResource, which gives null when routing chose no method. docs/services/iam.md and the strict-claiming setting's docs say how such a request is handled. IamEnforcementFilter builds its rpcv2 route claim through the same ProtocolClaimer helper.
This commit is contained in:
1 parent
64718c4034
commit
2f57441b84
19 files changed
+644
-42
No files matched your search
+18
@@ -1,7 +1,9 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import jakarta.enterprise.inject.IllegalProductException;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerResponseContext;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.InlineMocks.inlineMock;
|
||||
@@ -31,4 +33,20 @@ class AwsRestJsonErrorHeaderFilterTest {
|
||||
verify(response, never()).getEntity();
|
||||
verify(response, never()).getHeaders();
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesTheErrorOfARequestRoutingTurnedAwayAlone() {
|
||||
// A 415 from RESTEasy's media-type dispatch leaves a ResourceInfo whose producer CDI refuses.
|
||||
ResourceInfo noMethodChosen = mock(ResourceInfo.class);
|
||||
when(noMethodChosen.getResourceClass()).thenThrow(new IllegalProductException("no method chosen"));
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
ContainerResponseContext response = mock(ContainerResponseContext.class);
|
||||
when(response.getStatus()).thenReturn(415);
|
||||
|
||||
AwsRestJsonErrorHeaderFilter filter = new AwsRestJsonErrorHeaderFilter(mock(ResolvedServiceCatalog.class));
|
||||
filter.resourceInfo = noMethodChosen;
|
||||
filter.filter(request, response);
|
||||
|
||||
verify(response, never()).getHeaders();
|
||||
}
|
||||
}
|
||||
+28
@@ -1,19 +1,26 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* A SigV4 scope region that no partition publishes or admits by its region pattern is refused,
|
||||
@@ -62,6 +69,27 @@ class PartitionUnknownRegionIntegrationTest {
|
||||
}
|
||||
|
||||
/** A presigned POST's credential sits in the form body, out of the ingress filter's sight. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void anUnclaimedCborRequestSignedForAnUnknownRegionGetsTheCborSignatureError(boolean rpcV2) throws Exception {
|
||||
// An unrecognised Smithy-Protocol leaves it to its route to claim, which has to happen before
|
||||
// the Region check picks the error format.
|
||||
ObjectMapper cbor = new ObjectMapper(new CBORFactory());
|
||||
String mediaType = rpcV2 ? "application/cbor" : "application/x-amz-cbor-1.1";
|
||||
RequestSpecification request = given().contentType(mediaType).accept(mediaType)
|
||||
.header("Authorization", PartitionMatrix.sigV4Auth(UNKNOWN, "monitoring"))
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body(cbor.writeValueAsBytes(Map.of()));
|
||||
if (!rpcV2) {
|
||||
request.header("X-Amz-Target", "GraniteServiceVersion20100801.ListMetrics");
|
||||
}
|
||||
byte[] response = request.when().post(rpcV2
|
||||
? "/service/GraniteServiceVersion20100801/operation/ListMetrics" : "/").then().statusCode(400)
|
||||
.contentType(mediaType).header("x-amzn-query-error", equalTo("InvalidSignatureException;Sender"))
|
||||
.extract().asByteArray();
|
||||
assertEquals("InvalidSignatureException", cbor.readTree(response).path("__type").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPresignedPostSignedForAnUnknownRegionGetsS3sMalformedHeaderError() {
|
||||
given()
|
||||
|
||||
+52
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.S3Controller;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
@@ -221,4 +222,55 @@ class ProtocolClaimerIntegrationTest {
|
||||
|
||||
assertEquals(WireProtocol.REST, claim.protocol());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnclaimedRequestIsClaimedAsTheEndpointItReachedServesIt() {
|
||||
ClaimSignals signals = new ClaimSignals("POST", "/", "application/x-amz-json-1.0", "unrecognised",
|
||||
"AmazonSQS.ListQueues", SQS_AUTH, null, new MultivaluedHashMap<>());
|
||||
MultivaluedHashMap<String, String> noPathParameters = new MultivaluedHashMap<>();
|
||||
|
||||
ProtocolClaim json = claimer.routeClaim(AwsJsonController.class, signals, noPathParameters);
|
||||
assertEquals(WireProtocol.AWS_JSON_1_0, json.protocol());
|
||||
assertEquals("sqs", json.service().externalKey());
|
||||
assertEquals("ListQueues", json.operation());
|
||||
|
||||
assertEquals(WireProtocol.AWS_JSON_1_1,
|
||||
claimer.routeClaim(AwsJson11Controller.class, signals, noPathParameters).protocol());
|
||||
assertEquals(WireProtocol.AWS_CBOR_TARGET,
|
||||
claimer.routeClaim(AwsJsonCborController.class, signals, noPathParameters).protocol());
|
||||
|
||||
ProtocolClaim query = claimer.routeClaim(AwsQueryController.class, signals, noPathParameters);
|
||||
assertEquals(WireProtocol.AWS_QUERY, query.protocol());
|
||||
assertEquals("sqs", query.service().externalKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnclaimedRequestOnTheRpcV2RouteIsClaimedByThePathItDispatches() {
|
||||
MultivaluedHashMap<String, String> pathParameters = new MultivaluedHashMap<>();
|
||||
pathParameters.putSingle(AwsJsonCborController.RPC_V2_SERVICE_PARAMETER, "DynamoDB_20120810");
|
||||
pathParameters.putSingle(AwsJsonCborController.RPC_V2_OPERATION_PARAMETER, "ListTables");
|
||||
|
||||
ProtocolClaim claim = claimer.routeClaim(AwsJsonCborController.class,
|
||||
new ClaimSignals("POST", "/service/DynamoDB_20120810/operation/ListTables", "application/cbor",
|
||||
"unrecognised", "AmazonSQS.ListQueues", null, null, new MultivaluedHashMap<>()),
|
||||
pathParameters);
|
||||
|
||||
assertEquals(WireProtocol.RPCV2_CBOR, claim.protocol());
|
||||
assertEquals("dynamodb", claim.service().externalKey());
|
||||
assertEquals("ListTables", claim.operation());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnclaimedRequestOnAnyOtherRouteIsRest() {
|
||||
MultivaluedHashMap<String, String> noPathParameters = new MultivaluedHashMap<>();
|
||||
ClaimSignals delete = new ClaimSignals("DELETE", "/bucket/key", null, "unrecognised",
|
||||
"AmazonS3.GetObject", null, null, new MultivaluedHashMap<>());
|
||||
assertEquals(WireProtocol.REST, claimer.routeClaim(S3Controller.class, delete, noPathParameters).protocol());
|
||||
|
||||
// The Query endpoint's GET route is reached as a Query call only through the Query GET
|
||||
// rewrite, which only a claimed request gets.
|
||||
ClaimSignals get = new ClaimSignals("GET", AwsQueryController.QUERY_GET_PATH, null, "unrecognised",
|
||||
null, null, null, new MultivaluedHashMap<>());
|
||||
assertEquals(WireProtocol.REST, claimer.routeClaim(AwsQueryController.class, get, noPathParameters).protocol());
|
||||
}
|
||||
}
|
||||
+38
@@ -43,6 +43,23 @@ class ServiceEnablementIntegrationTest {
|
||||
.body("message", equalTo("Service acm is not enabled."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTargetedRequestWithAnUnrecognisedSmithyProtocolIsStillRejected() {
|
||||
// No protocol claims it, so it is served as its route is: as a call to acm, which is disabled.
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "CertificateManager.ListCertificates")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.contentType("application/json")
|
||||
.body("__type", equalTo("ServiceNotAvailableException"))
|
||||
.body("message", equalTo("Service acm is not enabled."));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ecsTargetedRequestsAreRejected() {
|
||||
given()
|
||||
@@ -154,6 +171,27 @@ class ServiceEnablementIntegrationTest {
|
||||
assertEquals("Service dynamodb is not enabled.", body.get("message").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anRpcV2CallWithAnUnrecognisedSmithyProtocolIsStillRejected() throws Exception {
|
||||
// Unsigned, so only the path it is routed by names the service.
|
||||
JsonNode body = cborBody(
|
||||
given()
|
||||
.contentType("application/cbor")
|
||||
.accept("application/cbor")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body(CBOR_MAPPER.writeValueAsBytes(Map.of()))
|
||||
.when()
|
||||
.post("/service/DynamoDB/operation/ListTables")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.contentType("application/cbor")
|
||||
.extract().asByteArray()
|
||||
);
|
||||
|
||||
assertEquals("ServiceNotAvailableException", body.get("__type").asText());
|
||||
assertEquals("Service dynamodb is not enabled.", body.get("message").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void signedLambdaGetRequestsReturnJsonWhenServiceDisabled() {
|
||||
given()
|
||||
|
||||
+13
@@ -40,6 +40,19 @@ class UnknownServiceScopeGuardDisabledIntegrationTest {
|
||||
.body(containsString("<Code>NoSuchBucket</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedScopeWithAnUnrecognisedSmithyProtocolFallsThroughWhenRejectionDisabled() {
|
||||
given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260707/us-east-1/account"
|
||||
+ "/aws4_request, SignedHeaders=host;x-amz-date, Signature=deadbeef")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.when()
|
||||
.get("/guard-disabled-no-such-bucket")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("<Code>NoSuchBucket</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void knownRestJsonScopeFallsThroughWhenRejectionDisabled() {
|
||||
given()
|
||||
|
||||
+33
@@ -45,6 +45,23 @@ class UnknownServiceScopeGuardIntegrationTest {
|
||||
.body("__type", equalTo("UnknownOperationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedRestServiceScopeWithAnUnrecognisedSmithyProtocolGetsUnknownOperation() {
|
||||
// No protocol claims it before routing; the REST route it reaches claims it, and the guard
|
||||
// still applies.
|
||||
given()
|
||||
.header("Authorization", authorization("support"))
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/listRegions")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("UnknownOperationException"))
|
||||
.body("message", equalTo("Unknown operation: POST /listRegions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void knownRestJsonScopeCannotFallThroughToS3Wildcard() {
|
||||
given()
|
||||
@@ -61,6 +78,22 @@ class UnknownServiceScopeGuardIntegrationTest {
|
||||
.body("message", equalTo("Unknown operation: POST /prompts"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKnownRestJsonScopeWithAnUnrecognisedSmithyProtocolCannotFallThroughToS3Wildcard() {
|
||||
// No protocol claims it, so it is judged as the S3 route it reached, which bedrock is not.
|
||||
given()
|
||||
.header("Authorization", authorization("bedrock"))
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.contentType("application/x-amz-json-1.1")
|
||||
.body("{\"name\":\"probe\"}")
|
||||
.when()
|
||||
.post("/prompts")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("UnknownOperationException"))
|
||||
.body("message", equalTo("Unknown operation: POST /prompts"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bedrockScopeStillReachesBedrockRuntimeRoutes() {
|
||||
given()
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
/**
|
||||
* A request no resource method consumes is answered with routing's own status. The response filters
|
||||
* also run for it, and reading the routed resource there used to fail with a 500.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class UnroutableRequestIntegrationTest {
|
||||
|
||||
@Test
|
||||
void aRootPostNoEndpointConsumesGetsUnsupportedMediaType() {
|
||||
given()
|
||||
.contentType("text/plain")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(415);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTargetedCallInAContentTypeNoEndpointConsumesGetsUnsupportedMediaType() {
|
||||
// No protocol claims it either: an X-Amz-Target post with a content type no protocol uses.
|
||||
given()
|
||||
.contentType("text/plain")
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(415);
|
||||
}
|
||||
}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* A request no wire protocol claims, such as one carrying a {@code Smithy-Protocol} value Floci does
|
||||
* not serve it by, is still served while strict claiming is off, the default. It is checked as the
|
||||
* call its route runs: unsigned, a call to the JSON, CBOR or Query endpoint is refused, and a REST
|
||||
* request is checked as its route's action, whatever an {@code X-Amz-Target} header on it names.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class UnclaimedRequestEnforcementIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final byte[] EMPTY_CBOR_MAP = {(byte) 0xa0};
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsignedJsonCallWithAnUnrecognisedSmithyProtocolIsRefused() {
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("__type", equalTo("MissingAuthenticationTokenException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsignedJsonCallCarryingTheRpcV2HeaderOffItsPathIsRefused() {
|
||||
// The header names a protocol Floci serves, but only on the rpcv2 path; at the root the JSON
|
||||
// endpoint serves the call.
|
||||
given()
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.header("smithy-protocol", "rpc-v2-cbor")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("__type", equalTo("MissingAuthenticationTokenException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsignedQueryCallWithAnUnrecognisedSmithyProtocolIsRefused() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.formParam("Action", "ListQueues")
|
||||
.formParam("Version", "2012-11-05")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("<Code>MissingAuthenticationToken</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsignedCborCallWithAnUnrecognisedSmithyProtocolIsRefused() {
|
||||
byte[] body = given()
|
||||
.contentType("application/x-amz-cbor-1.1")
|
||||
.header("X-Amz-Target", "Kinesis_20131202.ListStreams")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body(EMPTY_CBOR_MAP)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.extract().asByteArray();
|
||||
|
||||
assertTrue(new String(body, StandardCharsets.ISO_8859_1).contains("MissingAuthenticationToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsignedTargetedCallWithNoContentTypeIsRefused() throws IOException, InterruptedException {
|
||||
// No protocol claims an X-Amz-Target call without a content type, yet a route serves it. Rest
|
||||
// Assured always sends a content type with a body, so this one goes out by hand.
|
||||
HttpResponse<String> response = HttpClient.newHttpClient().send(
|
||||
HttpRequest.newBuilder(baseUri)
|
||||
.header("X-Amz-Target", "Kinesis_20131202.ListStreams")
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(EMPTY_CBOR_MAP))
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofString(StandardCharsets.ISO_8859_1));
|
||||
|
||||
assertEquals(403, response.statusCode());
|
||||
assertTrue(response.body().contains("MissingAuthenticationToken"), response.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSignedJsonCallWithAnUnrecognisedSmithyProtocolIsCheckedAsItsTarget() {
|
||||
String lister = userWithPolicy(allow("sqs:ListQueues", "*"));
|
||||
String other = userWithPolicy(allow("sns:ListTopics", "*"));
|
||||
|
||||
listQueues(lister).then().statusCode(200);
|
||||
listQueues(other).then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("AccessDeniedException"))
|
||||
.body("message", containsString("sqs:ListQueues"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRestRequestIsCheckedAsItsRouteWhateverTargetItCarries() {
|
||||
String bucket = "unclaimed-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().header("Authorization", auth(ACCOUNT_ID, "s3")).when().put("/" + bucket)
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", auth(ACCOUNT_ID, "s3")).body("kept").when().put("/" + bucket + "/doc")
|
||||
.then().statusCode(200);
|
||||
String reader = userWithPolicy(allow("s3:GetObject", "arn:aws:s3:::" + bucket + "/*"));
|
||||
|
||||
// Unclaimed, the request was checked as the s3:GetObject its target named, and the delete ran.
|
||||
given()
|
||||
.header("Authorization", auth(reader, "s3"))
|
||||
.header("X-Amz-Target", "AmazonS3.GetObject")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.when()
|
||||
.delete("/" + bucket + "/doc")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("s3:DeleteObject"));
|
||||
|
||||
given().header("Authorization", auth(reader, "s3")).when().get("/" + bucket + "/doc")
|
||||
.then().statusCode(200).body(equalTo("kept"));
|
||||
}
|
||||
|
||||
private static Response listQueues(String accessKeyId) {
|
||||
return given()
|
||||
.header("Authorization", auth(accessKeyId, "sqs"))
|
||||
.contentType("application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.header("smithy-protocol", "unrecognised")
|
||||
.body("{}")
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
private static String allow(String action, String resource) {
|
||||
return """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"%s","Resource":"%s"}]}"""
|
||||
.formatted(action, resource);
|
||||
}
|
||||
|
||||
private String userWithPolicy(String document) {
|
||||
String userName = "unclaimed-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
|
||||
auth(ACCOUNT_ID, "iam"), userName).accessKeyId();
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ACCOUNT_ID, "iam"), userName, "only", document);
|
||||
return accessKeyId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.enterprise.inject.IllegalProductException;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerResponseContext;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class S3CorsFilterTest {
|
||||
|
||||
@Test
|
||||
void leavesTheResponseToARequestRoutingTurnedAwayAlone() {
|
||||
// A 415 from RESTEasy's media-type dispatch leaves a ResourceInfo whose producer CDI refuses.
|
||||
ResourceInfo noMethodChosen = mock(ResourceInfo.class);
|
||||
when(noMethodChosen.getResourceClass()).thenThrow(new IllegalProductException("no method chosen"));
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getHeaderString("Origin")).thenReturn("https://example.com");
|
||||
ContainerResponseContext response = mock(ContainerResponseContext.class);
|
||||
|
||||
new S3CorsFilter(s3Service, noMethodChosen).filter(request, response);
|
||||
|
||||
verifyNoInteractions(s3Service);
|
||||
verify(response, never()).getHeaders();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user