mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix: Support S3 auth enforcement (#1689)
This commit is contained in:
1 parent
8590ffec34
commit
a3e8bf5a8b
18 files changed
+2377
-41
No files matched your search
@@ -0,0 +1,115 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3AclPolicyTest {
|
||||
|
||||
@Test
|
||||
void parsesGrantFromAccessControlList() {
|
||||
S3AclPolicy policy = parse(acl(groupGrant(S3AclPolicy.ALL_USERS_GROUP_URI, "READ")));
|
||||
|
||||
assertEquals(1, policy.grants().size());
|
||||
S3AclPolicy.Grant grant = policy.grants().getFirst();
|
||||
assertEquals(S3AclPolicy.ALL_USERS_GROUP_URI, grant.grantee().uri());
|
||||
assertEquals(S3AclPolicy.Permission.READ, grant.permission());
|
||||
assertTrue(grant.allowsPublicRead());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesNamespacedAcl() {
|
||||
String grant = groupGrant(S3AclPolicy.ALL_USERS_GROUP_URI, "FULL_CONTROL");
|
||||
S3AclPolicy policy = parse("""
|
||||
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<AccessControlList>
|
||||
%s
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(grant));
|
||||
|
||||
assertEquals(1, policy.grants().size());
|
||||
assertEquals(S3AclPolicy.Permission.FULL_CONTROL, policy.grants().getFirst().permission());
|
||||
assertTrue(policy.grants().getFirst().allowsPublicRead());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresGrantOutsideAccessControlList() {
|
||||
String grant = groupGrant(S3AclPolicy.ALL_USERS_GROUP_URI, "READ");
|
||||
S3AclPolicy policy = parse("""
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList/>
|
||||
<Metadata>
|
||||
%s
|
||||
</Metadata>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(grant));
|
||||
|
||||
assertTrue(policy.grants().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsEmptyPolicyForUnsupportedShape() {
|
||||
assertTrue(parse("<NotAnAcl/>").grants().isEmpty());
|
||||
assertTrue(parse("<AccessControlPolicy/>").grants().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapsUnknownPermission() {
|
||||
S3AclPolicy policy = parse(acl(groupGrant(S3AclPolicy.ALL_USERS_GROUP_URI, "DELETE")));
|
||||
|
||||
assertEquals(S3AclPolicy.Permission.UNKNOWN, policy.grants().getFirst().permission());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedAcl() {
|
||||
assertInvalidAcl("<AccessControlPolicy>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsDoctype() {
|
||||
assertInvalidAcl("""
|
||||
<!DOCTYPE AccessControlPolicy [
|
||||
<!ENTITY external SYSTEM "file:///etc/passwd">
|
||||
]>
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList/>
|
||||
</AccessControlPolicy>
|
||||
""");
|
||||
}
|
||||
|
||||
private static void assertInvalidAcl(String acl) {
|
||||
assertThrows(S3AclPolicy.AclParseException.class, () -> S3AclPolicy.parse(acl));
|
||||
}
|
||||
|
||||
private static S3AclPolicy parse(String acl) {
|
||||
try {
|
||||
return S3AclPolicy.parse(acl);
|
||||
} catch (S3AclPolicy.AclParseException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String acl(String... grants) {
|
||||
return """
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList>
|
||||
%s
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(String.join("\n", grants));
|
||||
}
|
||||
|
||||
private static String groupGrant(String groupUri, String permission) {
|
||||
return """
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
|
||||
<URI>%s</URI>
|
||||
</Grantee>
|
||||
<Permission>%s</Permission>
|
||||
</Grant>
|
||||
""".formatted(groupUri, permission);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3AclPublicAccessEvaluatorTest {
|
||||
|
||||
private static final String AUTHENTICATED_USERS_GROUP_URI =
|
||||
"http://acs.amazonaws.com/groups/global/AuthenticatedUsers";
|
||||
|
||||
@Test
|
||||
void allUsersReadAclAllowsPublicRead() {
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "READ"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allUsersFullControlAclAllowsPublicRead() {
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "FULL_CONTROL"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allUsersWriteAclDoesNotAllowPublicRead() {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "WRITE"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticatedUsersReadAclDoesNotAllowPublicRead() {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl(
|
||||
groupGrant(AUTHENTICATED_USERS_GROUP_URI, "READ"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantsDoNotCombineAcrossAclEntries() {
|
||||
String acl = acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "WRITE"),
|
||||
groupGrant(AUTHENTICATED_USERS_GROUP_URI, "READ"));
|
||||
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl));
|
||||
}
|
||||
|
||||
@Test
|
||||
void grantsOutsideAccessControlListDoNotAllowPublicRead() {
|
||||
String acl = """
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList>
|
||||
</AccessControlList>
|
||||
<Metadata>
|
||||
%s
|
||||
</Metadata>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "READ"));
|
||||
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl));
|
||||
}
|
||||
|
||||
@Test
|
||||
void namespaceAttributesDoNotBlockPublicReadAcl() {
|
||||
String acl = """
|
||||
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
|
||||
<URI>%s</URI>
|
||||
</Grantee>
|
||||
<Permission>READ</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI);
|
||||
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclAllowsPublicRead(acl));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidAclDoesNotAllowPublicRead() {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(null));
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead(""));
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead("<AccessControlPolicy>"));
|
||||
}
|
||||
|
||||
private static String acl(String... grants) {
|
||||
return """
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList>
|
||||
%s
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""".formatted(String.join("\n", grants));
|
||||
}
|
||||
|
||||
private static String groupGrant(String groupUri, String permission) {
|
||||
return """
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
|
||||
<URI>%s</URI>
|
||||
</Grantee>
|
||||
<Permission>%s</Permission>
|
||||
</Grant>
|
||||
""".formatted(groupUri, permission);
|
||||
}
|
||||
}
|
||||
+666
@@ -0,0 +1,666 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3AuthEnforcementIntegrationTest.S3AuthProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3AuthEnforcementIntegrationTest {
|
||||
|
||||
private static final String PUBLIC_BUCKET = "auth-public-bucket";
|
||||
private static final String PRIVATE_BUCKET = "auth-private-bucket";
|
||||
private static final String WEBSITE_BUCKET = "auth-website-bucket";
|
||||
private static final String WEBSITE_ERROR_BUCKET = "auth-website-error-bucket";
|
||||
private static final String BUCKET_ACL_BUCKET = "auth-bucket-acl-bucket";
|
||||
private static final String DENY_BUCKET = "auth-deny-bucket";
|
||||
private static final String GET_ONLY_BUCKET = "auth-get-only-bucket";
|
||||
private static final String VERSION_BUCKET = "auth-version-bucket";
|
||||
private static final String PUBLIC_KEY = "public.txt";
|
||||
private static final String PRIVATE_KEY = "private.txt";
|
||||
private static final String ERROR_KEY = "error.html";
|
||||
private static final String ACL_KEY = "acl-list.txt";
|
||||
private static final String DENY_KEY = "deny.txt";
|
||||
private static final String VERSION_KEY = "versioned.txt";
|
||||
private static final String SIGNING_TIMESTAMP = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'")
|
||||
.withZone(ZoneOffset.UTC)
|
||||
.format(Instant.now());
|
||||
private static final String SIGNING_DATE = SIGNING_TIMESTAMP.substring(0, 8);
|
||||
private static final String LOCAL_AUTH_HEADER = authorizationHeader("test");
|
||||
private static final String BAD_AUTH_HEADER = authorizationHeader("bad-key");
|
||||
private static final String ACCOUNT_SHAPED_AUTH_HEADER = authorizationHeader("123456789012");
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createBucketsAndObjects() {
|
||||
given().when().put("/" + PUBLIC_BUCKET).then().statusCode(200);
|
||||
given().when().put("/" + PRIVATE_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.body("public body")
|
||||
.when()
|
||||
.put("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.body("private body")
|
||||
.when()
|
||||
.put("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicReadPolicy(PUBLIC_BUCKET))
|
||||
.when()
|
||||
.put("/" + PUBLIC_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void unsignedRequestCanReadPublicObject() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("public body"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.head("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void unsignedRequestCanListPublicBucket() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "?list-type=2")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>" + PUBLIC_KEY + "</Key>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void unsignedRequestCannotReadPrivateObject() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.head("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void unsignedRequestCannotListPrivateBucket() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "?list-type=2")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void signedRequestWithBadAccessKeyCannotUsePublicAccess() {
|
||||
given()
|
||||
.header("Authorization", BAD_AUTH_HEADER)
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void signedRequestWithAccountShapedAccessKeyCannotReadPrivateObject() {
|
||||
given()
|
||||
.header("Authorization", ACCOUNT_SHAPED_AUTH_HEADER)
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void presignedRequestWithBadAccessKeyCannotUsePublicAccess() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("bad-key"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "test")
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void malformedPresignedRequestCannotUsePublicAccess() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Error.Code", equalTo("AuthorizationQueryParametersError"))
|
||||
.body(containsString("Query-string authentication version 4 requires the X-Amz-Algorithm"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void signedRequestWithLocalAccessKeyCanReadPrivateObject() {
|
||||
given()
|
||||
.header("Authorization", LOCAL_AUTH_HEADER)
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("private body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void presignedRequestWithLocalAccessKeyCanReadPrivateObject() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "test")
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("private body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void websiteRootAuthorizesIndexObjectReadNotBucketList() {
|
||||
given().when().put("/" + WEBSITE_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("text/html")
|
||||
.body("<html>index</html>")
|
||||
.when()
|
||||
.put("/" + WEBSITE_BUCKET + "/index.html")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(websiteConfiguration())
|
||||
.when()
|
||||
.put("/" + WEBSITE_BUCKET + "?website")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicGetObjectPolicy(WEBSITE_BUCKET))
|
||||
.when()
|
||||
.put("/" + WEBSITE_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Host", WEBSITE_BUCKET + ".s3-website-us-east-1.localhost:"
|
||||
+ io.restassured.RestAssured.port)
|
||||
.when()
|
||||
.get("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("<html>index</html>"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + WEBSITE_BUCKET + "?list-type=2")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void websiteRootUsesErrorDocumentForDeniedIndexObject() {
|
||||
given().when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("text/html")
|
||||
.body("<html>private index</html>")
|
||||
.when()
|
||||
.put("/" + WEBSITE_ERROR_BUCKET + "/index.html")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("text/html")
|
||||
.body("<html>denied</html>")
|
||||
.when()
|
||||
.put("/" + WEBSITE_ERROR_BUCKET + "/" + ERROR_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(websiteConfiguration(ERROR_KEY))
|
||||
.when()
|
||||
.put("/" + WEBSITE_ERROR_BUCKET + "?website")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicGetObjectPolicy(WEBSITE_ERROR_BUCKET, ERROR_KEY))
|
||||
.when()
|
||||
.put("/" + WEBSITE_ERROR_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("Host", WEBSITE_ERROR_BUCKET + ".s3-website-us-east-1.localhost:"
|
||||
+ io.restassured.RestAssured.port)
|
||||
.when()
|
||||
.get("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.header("x-amz-error-code", "AccessDenied")
|
||||
.body(equalTo("<html>denied</html>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void bucketAclPublicReadAllowsUnsignedList() {
|
||||
given().when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.body("listed")
|
||||
.when()
|
||||
.put("/" + BUCKET_ACL_BUCKET + "/" + ACL_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(publicReadAcl())
|
||||
.when()
|
||||
.put("/" + BUCKET_ACL_BUCKET + "?acl")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET_ACL_BUCKET + "?list-type=2")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>" + ACL_KEY + "</Key>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void explicitBucketPolicyDenyOverridesPublicObjectAcl() {
|
||||
given().when().put("/" + DENY_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.header("x-amz-acl", "public-read")
|
||||
.body("denied")
|
||||
.when()
|
||||
.put("/" + DENY_BUCKET + "/" + DENY_KEY)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(denyGetObjectPolicy(DENY_BUCKET))
|
||||
.when()
|
||||
.put("/" + DENY_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + DENY_BUCKET + "/" + DENY_KEY)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void unsignedRequestCannotReadPrivateBucketSubresources() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "?acl")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "?versions")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
void publicGetObjectPolicyDoesNotAuthorizeObjectSubresources() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY + "?acl")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY + "?tagging")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void headBucketHonorsAuthEnforcement() {
|
||||
given()
|
||||
.when()
|
||||
.head("/" + PRIVATE_BUCKET)
|
||||
.then()
|
||||
.statusCode(403);
|
||||
|
||||
given()
|
||||
.header("Authorization", BAD_AUTH_HEADER)
|
||||
.when()
|
||||
.head("/" + PUBLIC_BUCKET)
|
||||
.then()
|
||||
.statusCode(403);
|
||||
|
||||
given()
|
||||
.header("Authorization", LOCAL_AUTH_HEADER)
|
||||
.when()
|
||||
.head("/" + PRIVATE_BUCKET)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void selectObjectContentHonorsReadAuthorization() {
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body(selectRequest())
|
||||
.when()
|
||||
.post("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY + "?select&select-type=2")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.header("Authorization", BAD_AUTH_HEADER)
|
||||
.contentType("application/xml")
|
||||
.body(selectRequest())
|
||||
.when()
|
||||
.post("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY + "?select&select-type=2")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void missingObjectRequiresListBucketToReturnNoSuchKey() {
|
||||
given().when().put("/" + GET_ONLY_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicGetObjectPolicy(GET_ONLY_BUCKET))
|
||||
.when()
|
||||
.put("/" + GET_ONLY_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + GET_ONLY_BUCKET + "/missing.txt")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/missing.txt")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("Error.Code", equalTo("NoSuchKey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void versionedObjectReadRequiresGetObjectVersion() {
|
||||
given().when().put("/" + VERSION_BUCKET).then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
|
||||
.when()
|
||||
.put("/" + VERSION_BUCKET + "?versioning")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String versionId = given()
|
||||
.body("versioned body")
|
||||
.when()
|
||||
.put("/" + VERSION_BUCKET + "/" + VERSION_KEY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().header("x-amz-version-id");
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObject"))
|
||||
.when()
|
||||
.put("/" + VERSION_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + VERSION_BUCKET + "/" + VERSION_KEY + "?versionId=" + versionId)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObjectVersion"))
|
||||
.when()
|
||||
.put("/" + VERSION_BUCKET + "?policy")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + VERSION_BUCKET + "/" + VERSION_KEY + "?versionId=" + versionId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("versioned body"));
|
||||
}
|
||||
|
||||
private static String publicReadPolicy(String bucket) {
|
||||
return """
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Principal": "*",
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Principal": "*",
|
||||
"Action": ["s3:ListBucket"],
|
||||
"Resource": ["arn:aws:s3:::%s"]
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(bucket, bucket);
|
||||
}
|
||||
|
||||
private static String authorizationHeader(String accessKeyId) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + credential(accessKeyId)
|
||||
+ ", SignedHeaders=host;x-amz-date, Signature=test";
|
||||
}
|
||||
|
||||
private static String credential(String accessKeyId) {
|
||||
return accessKeyId + "/" + SIGNING_DATE + "/us-east-1/s3/aws4_request";
|
||||
}
|
||||
|
||||
private static String websiteConfiguration() {
|
||||
return websiteConfiguration(null);
|
||||
}
|
||||
|
||||
private static String websiteConfiguration(String errorDocument) {
|
||||
if (errorDocument == null) {
|
||||
return """
|
||||
<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<IndexDocument>
|
||||
<Suffix>index.html</Suffix>
|
||||
</IndexDocument>
|
||||
</WebsiteConfiguration>
|
||||
""";
|
||||
}
|
||||
return """
|
||||
<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<IndexDocument>
|
||||
<Suffix>index.html</Suffix>
|
||||
</IndexDocument>
|
||||
<ErrorDocument>
|
||||
<Key>%s</Key>
|
||||
</ErrorDocument>
|
||||
</WebsiteConfiguration>
|
||||
""".formatted(errorDocument);
|
||||
}
|
||||
|
||||
private static String publicGetObjectPolicy(String bucket) {
|
||||
return publicGetObjectPolicy(bucket, "*");
|
||||
}
|
||||
|
||||
private static String publicGetObjectPolicy(String bucket, String key) {
|
||||
return publicObjectActionPolicy(bucket, key, "s3:GetObject");
|
||||
}
|
||||
|
||||
private static String publicObjectActionPolicy(String bucket, String action) {
|
||||
return publicObjectActionPolicy(bucket, "*", action);
|
||||
}
|
||||
|
||||
private static String publicObjectActionPolicy(String bucket, String key, String action) {
|
||||
return """
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": {
|
||||
"Effect": "Allow",
|
||||
"Principal": "*",
|
||||
"Action": "%s",
|
||||
"Resource": "arn:aws:s3:::%s/%s"
|
||||
}
|
||||
}
|
||||
""".formatted(action, bucket, key);
|
||||
}
|
||||
|
||||
private static String denyGetObjectPolicy(String bucket) {
|
||||
return """
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": {
|
||||
"Effect": "Deny",
|
||||
"Principal": "*",
|
||||
"Action": "s3:GetObject",
|
||||
"Resource": "arn:aws:s3:::%s/*"
|
||||
}
|
||||
}
|
||||
""".formatted(bucket);
|
||||
}
|
||||
|
||||
private static String publicReadAcl() {
|
||||
return """
|
||||
<AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Owner>
|
||||
<ID>owner</ID>
|
||||
</Owner>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
|
||||
<URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
|
||||
</Grantee>
|
||||
<Permission>READ</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""";
|
||||
}
|
||||
|
||||
private static String selectRequest() {
|
||||
return """
|
||||
<SelectObjectContentRequest>
|
||||
<Expression>SELECT * FROM S3Object</Expression>
|
||||
<InputSerialization>
|
||||
<CSV>
|
||||
<FileHeaderInfo>NONE</FileHeaderInfo>
|
||||
</CSV>
|
||||
</InputSerialization>
|
||||
<OutputSerialization>
|
||||
<CSV />
|
||||
</OutputSerialization>
|
||||
</SelectObjectContentRequest>
|
||||
""";
|
||||
}
|
||||
|
||||
public static final class S3AuthProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.s3.enforce-auth", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1855,6 +1855,35 @@ class S3IntegrationTest {
|
||||
.body(containsString("NoSuchPublicAccessBlockConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(104)
|
||||
void malformedAuthorizationHeaderIsIgnoredWhenAuthEnforcementDisabled() {
|
||||
String bucket = "auth-disabled-bucket";
|
||||
String key = "greeting.txt";
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
given().body("Hello World from S3!").when().put("/" + bucket + "/" + key).then().statusCode(200);
|
||||
try {
|
||||
given()
|
||||
.header("Authorization", "not-a-valid-signature")
|
||||
.when()
|
||||
.get("/" + bucket + "/" + key)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("Hello World from S3!"));
|
||||
|
||||
given()
|
||||
.header("Authorization", "not-a-valid-signature")
|
||||
.when()
|
||||
.get("/" + bucket + "?list-type=2")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Key>" + key + "</Key>"));
|
||||
} finally {
|
||||
given().when().delete("/" + bucket + "/" + key);
|
||||
given().when().delete("/" + bucket);
|
||||
}
|
||||
}
|
||||
|
||||
// --- ListObjectsV2 pagination ---
|
||||
|
||||
@Test
|
||||
|
||||
+313
@@ -0,0 +1,313 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.github.hectorvent.floci.services.s3.S3PublicAccessEvaluator.PublicAccessDecision.DENY;
|
||||
import static io.github.hectorvent.floci.services.s3.S3PublicAccessEvaluator.PublicAccessDecision.NEUTRAL;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3PublicAccessEvaluatorTest {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private static final String BUCKET = "public-bucket";
|
||||
private static final String BUCKET_ARN = "arn:aws:s3:::" + BUCKET;
|
||||
private static final String OBJECT_ARN = BUCKET_ARN + "/folder/object.txt";
|
||||
|
||||
@Test
|
||||
void blankPolicyDoesNotAllow() {
|
||||
assertFalse(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, "", "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidJsonDoesNotAllow() {
|
||||
assertFalse(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, "{not-json", "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicStringPrincipalAllowsMatchingObjectAction() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicAwsPrincipalAllowsMatchingBucketAction() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":{"AWS":"*"},
|
||||
"Action":["s3:ListBucket"],
|
||||
"Resource":["arn:aws:s3:::public-bucket"]
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:ListBucket", BUCKET_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicPrincipalArrayAllowsMatchingAction() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":["arn:aws:iam::123456789012:root","*"],
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonPublicPrincipalDoesNotAllow() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":{"AWS":"arn:aws:iam::123456789012:root"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void conditionStatementDoesNotAllowAnonymousRead() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"StringEquals":{"aws:PrincipalAccount":"123456789012"}}
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingEffectDoesNotAllowAnonymousRead() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitDenyOverridesAllow() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
},
|
||||
{
|
||||
"Effect":"Deny",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/folder/object.txt"
|
||||
}
|
||||
]}""";
|
||||
|
||||
assertEquals(DENY, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void conditionalDenyFailsClosedAndOverridesAllow() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
},
|
||||
{
|
||||
"Effect":"Deny",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*",
|
||||
"Condition":{"StringEquals":{"aws:SourceVpc":"vpc-123"}}
|
||||
}
|
||||
]}""";
|
||||
|
||||
assertEquals(DENY, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardActionAndResourceMatch() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:Get*",
|
||||
"Resource":"arn:aws:s3:::public-bucket/folder/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notActionAllowsWhenRequestedActionIsNotExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"NotAction":"s3:PutObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notActionDoesNotAllowWhenRequestedActionIsExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"NotAction":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notResourceAllowsWhenRequestedResourceIsNotExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"NotResource":"arn:aws:s3:::other-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notResourceDoesNotAllowWhenRequestedResourceIsExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"NotResource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notPrincipalAllowsAnonymousWhenOnlySpecificPrincipalIsExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"NotPrincipal":{"AWS":"arn:aws:iam::123456789012:root"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertTrue(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notPrincipalWildcardDoesNotAllowAnonymous() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"NotPrincipal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void denyWithNotPrincipalOverridesAllowWhenAnonymousIsNotExcluded() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
},
|
||||
{
|
||||
"Effect":"Deny",
|
||||
"NotPrincipal":{"AWS":"arn:aws:iam::123456789012:root"},
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}
|
||||
]}""";
|
||||
|
||||
assertEquals(DENY, S3PublicAccessEvaluator.publicPolicyDecision(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void actionMismatchDoesNotAllow() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:PutObject",
|
||||
"Resource":"arn:aws:s3:::public-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertFalse(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceMismatchDoesNotAllow() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":{
|
||||
"Effect":"Allow",
|
||||
"Principal":"*",
|
||||
"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::other-bucket/*"
|
||||
}}""";
|
||||
|
||||
assertFalse(S3PublicAccessEvaluator.publicPolicyAllows(
|
||||
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
|
||||
}
|
||||
|
||||
@Test
|
||||
void arnHelpersBuildBucketAndObjectArns() {
|
||||
assertEquals(BUCKET_ARN, S3PublicAccessEvaluator.bucketArn(BUCKET));
|
||||
assertEquals(OBJECT_ARN, S3PublicAccessEvaluator.objectArn(BUCKET, "folder/object.txt"));
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3RequestAuthorizationParserTest {
|
||||
|
||||
@Test
|
||||
void returnsUnsignedAuthorizationWithoutAuthMaterial() {
|
||||
S3Service.RequestAuthorization authorization = parse(null, query());
|
||||
|
||||
assertFalse(authorization.signed());
|
||||
assertNull(authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesAuthorizationHeaderAccessKey() {
|
||||
S3Service.RequestAuthorization authorization = parse(authorizationHeader("test"), query());
|
||||
|
||||
assertTrue(authorization.signed());
|
||||
assertEquals("test", authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesPresignedQueryAccessKey() {
|
||||
S3Service.RequestAuthorization authorization = parse(null, presignedQuery("bad-key"));
|
||||
|
||||
assertTrue(authorization.signed());
|
||||
assertEquals("bad-key", authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizationHeaderTakesPrecedenceOverPresignedQuery() {
|
||||
String authorizationHeader = authorizationHeader("header-key");
|
||||
MultivaluedMap<String, String> query = presignedQuery("query-key");
|
||||
|
||||
S3Service.RequestAuthorization authorization = parse(authorizationHeader, query);
|
||||
|
||||
assertTrue(authorization.signed());
|
||||
assertEquals("header-key", authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedAuthorizationHeader() {
|
||||
String authorization = "AWS4-HMAC-SHA256 Credential=test/20260702/us-east-1/s3/aws4_request, SignedHeaders=host";
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> parse(authorization, query()));
|
||||
|
||||
assertEquals("AuthorizationHeaderMalformed", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedPresignedQuery() {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
query.add("X-Amz-Credential", "test/20260702/us-east-1/s3/aws4_request");
|
||||
query.add("X-Amz-Date", "20260702T120000Z");
|
||||
query.add("X-Amz-Expires", "3600");
|
||||
query.add("X-Amz-SignedHeaders", "host");
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> parse(null, query));
|
||||
|
||||
assertEquals("AuthorizationQueryParametersError", e.getErrorCode());
|
||||
assertEquals("Query-string authentication version 4 requires the X-Amz-Algorithm, "
|
||||
+ "X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, "
|
||||
+ "and X-Amz-Expires parameters.", e.getMessage());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresNonPresignedQueryParameters() {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("versionId", "123");
|
||||
|
||||
S3Service.RequestAuthorization authorization = parse(null, query);
|
||||
|
||||
assertFalse(authorization.signed());
|
||||
assertNull(authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresStrayXAmzQueryParametersWithoutAlgorithm() {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("X-Amz-Meta-Test", "ignored");
|
||||
|
||||
S3Service.RequestAuthorization authorization = parse(null, query);
|
||||
|
||||
assertFalse(authorization.signed());
|
||||
assertNull(authorization.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledEnforcementDoesNotValidateMalformedAuthorizationHeader() {
|
||||
String authorization = "not-a-valid-signature";
|
||||
|
||||
S3Service.RequestAuthorization parsed = S3RequestAuthorizationParser.parseIfRequired(
|
||||
false, authorization, query());
|
||||
|
||||
assertFalse(parsed.signed());
|
||||
assertNull(parsed.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledEnforcementDoesNotValidateMalformedPresignedQuery() {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
|
||||
S3Service.RequestAuthorization parsed = S3RequestAuthorizationParser.parseIfRequired(
|
||||
false, null, query);
|
||||
|
||||
assertFalse(parsed.signed());
|
||||
assertNull(parsed.accessKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void signedDetectionDoesNotValidateMalformedAuthMaterial() {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
|
||||
assertTrue(S3RequestAuthorizationParser.isSigned("not-a-valid-signature", query()));
|
||||
assertTrue(S3RequestAuthorizationParser.isSigned(null, query));
|
||||
}
|
||||
|
||||
private static S3Service.RequestAuthorization parse(String authorization, MultivaluedMap<String, String> query) {
|
||||
return S3RequestAuthorizationParser.parse(authorization, query);
|
||||
}
|
||||
|
||||
private static String authorizationHeader(String accessKeyId) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + credential(accessKeyId)
|
||||
+ ", SignedHeaders=host;x-amz-date, Signature=test";
|
||||
}
|
||||
|
||||
private static MultivaluedMap<String, String> presignedQuery(String accessKeyId) {
|
||||
MultivaluedMap<String, String> query = query();
|
||||
query.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
query.add("X-Amz-Credential", credential(accessKeyId));
|
||||
query.add("X-Amz-Date", "20260702T120000Z");
|
||||
query.add("X-Amz-Expires", "3600");
|
||||
query.add("X-Amz-SignedHeaders", "host");
|
||||
query.add("X-Amz-Signature", "test");
|
||||
return query;
|
||||
}
|
||||
|
||||
private static String credential(String accessKeyId) {
|
||||
return accessKeyId + "/20260702/us-east-1/s3/aws4_request";
|
||||
}
|
||||
|
||||
private static MultivaluedMap<String, String> query() {
|
||||
return new MultivaluedHashMap<>();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user