fix(iam): authorize S3 object operations as the actions S3 documents (#5756)

Under IAM enforcement, CreateMultipartUpload, CompleteMultipartUpload, RestoreObject and
SelectObjectContent were never checked against the caller's identity policies. They are POST
requests, the registry had no S3 POST rule, and an action no rule maps is allowed. A user with no S3
permission could start and complete multipart uploads, restore objects and read them through
Select.

Many other S3 requests were checked as the wrong action:
- ListParts and AbortMultipartUpload as s3:GetObject and s3:DeleteObject.
- Object retention and legal hold as s3:GetObject and s3:PutObject, so a user allowed only to write
  objects could set a retention period or a legal hold.
- Object annotations as the plain object actions.
- A request naming a versionId as the current-version action. A deny on s3:DeleteObjectVersion, the
  usual guard against permanent deletes, did not stop one, and reading a version needed s3:GetObject
  rather than s3:GetObjectVersion.
- Analytics, inventory and Intelligent-Tiering configurations as s3:ListBucket, s3:CreateBucket or
  s3:DeleteBucket.
- DeleteBucketTagging as s3:DeleteBucketTagging, which is not an IAM action. A policy naming it
  granted the call, and one granting s3:PutBucketTagging, the action AWS checks, was refused.
- A request naming two sub-resources as the first one the registry looked for. The controller runs
  a different one: GET ?acl&tagging returns the tags while needing only s3:GetObjectAcl.

The object sub-resources are now resolved per method, in the order S3Controller dispatches them. A
versionId selects the version action AWS checks for a read, head, delete, ACL or annotation
request, and for Select, which S3Controller runs on the version named; the bucket configurations
follow the controller's bucket chains. Tagging keeps the tagging actions: the tagging operations
still act on the current version whatever versionId names, so a version tagging action would
authorize one version and let the request change another.

The actions come from the S3 user guide's "Required permissions for S3 API operations" and
multipart permissions tables, the API reference and the Service Reference:
- s3:PutObject for creating and completing an upload;
- s3:RestoreObject, and s3:GetObject for Select;
- s3:AbortMultipartUpload and s3:ListMultipartUploadParts;
- the retention, legal-hold, version and configuration actions;
- s3:PutBucketTagging for DeleteBucketTagging, and each configuration's Put action for its delete.

A write carrying If-Match already needed s3:GetObject as well. The conditional-writes guide lists
PutObject, CopyObject and CompleteMultipartUpload as the operations that take the header, so now
that completing an upload is checked as s3:PutObject it gets that check too, while
CreateMultipartUpload and UploadPart, which ignore the header, no longer do.

A conditional delete with an If-Match ETag already needed s3:GetObject. A delete naming a version is
now s3:DeleteObjectVersion, so that check covers it too, still as s3:GetObject: the
conditional-deletes guide evaluates the condition against the current version only.

Condition keys, per the Service Reference:
- s3:versionid is given to the version actions.
- s3:ExistingObjectTag is given to the version actions and the annotation actions, read from the
  version the request names.
- DeleteBucketTagging, checked as s3:PutBucketTagging, keeps the bucket's tags in its context.

CloudTrail denials name the version actions' operations, and give no name to a POST operation
rather than a wrong one.

The actions an operation needs besides its own, S3 behaviour behind these checks and S3's own
bucket-policy check follow separately (#5716).

Refs #5716
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-15 22:14:39 -05:00
1 parent c23641457a
commit 21184df828
11 files changed
+967 -51

No files matched your search

@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
import io.github.hectorvent.floci.services.s3.S3Service;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
@@ -67,6 +68,7 @@ public class IamConditionContextResolver {
public static final String EXISTING_OBJECT_TAG_PREFIX = "s3:ExistingObjectTag/";
public static final String REQUEST_OBJECT_TAG_PREFIX = "s3:RequestObjectTag/";
public static final String REQUEST_OBJECT_TAG_KEYS = "s3:RequestObjectTagKeys";
public static final String VERSION_ID = "s3:versionid";
private final Instance<DynamoDbFacade> dynamoDbFacade;
private final Instance<Ec2Service> ec2Service;
@@ -214,22 +216,40 @@ public class IamConditionContextResolver {
// ── S3 ──────────────────────────────────────────────────────────────────────
private Map<String, List<String>> s3ConditionContext(String action, ContainerRequestContext ctx) {
Map<String, List<String>> conditions = s3ActionConditionContext(action, ctx);
// A version action is checked only for a request naming a version, and carries its id.
if (S3ObjectActions.isVersionAction(action) && ctx.getUriInfo() != null) {
String versionId = ctx.getUriInfo().getQueryParameters().getFirst("versionId");
if (versionId != null) {
conditions = merge(conditions, Map.of(VERSION_ID, List.of(versionId)));
}
}
return conditions;
}
private Map<String, List<String>> s3ActionConditionContext(String action, ContainerRequestContext ctx) {
return switch (action) {
case "s3:ListBucket" -> s3BucketListConditionContext(ctx.getUriInfo().getQueryParameters());
case "s3:PutBucketTagging" -> s3PutBucketTaggingConditionContext(ctx);
case "s3:GetBucketTagging", "s3:DeleteBucketTagging", "s3:DeleteBucket" ->
s3BucketResourceTagConditionContext(ctx);
// DeleteBucketTagging is checked as s3:PutBucketTagging, and sends no tags of its own:
// what it has is the bucket's current tags, as GetBucketTagging does.
case "s3:PutBucketTagging" -> "DELETE".equalsIgnoreCase(ctx.getMethod())
? s3BucketResourceTagConditionContext(ctx)
: s3PutBucketTaggingConditionContext(ctx);
case "s3:GetBucketTagging", "s3:DeleteBucket" -> s3BucketResourceTagConditionContext(ctx);
// Object tags. Which action is given which key is what real AWS was measured to do,
// not what reads naturally: s3:DeleteObject is given neither key, and s3:PutObject is
// given the tags of the REQUEST only, never those of an object it is about to
// overwrite. See S3ObjectTagConditionEnforcementIntegrationTest.
//
// s3:GetObjectVersion is deliberately absent. IamActionRegistry authorizes a GET
// carrying ?versionId= as s3:GetObject, and IamEnforcementFilter is the only caller
// of resolve, so an arm for it would never be reached. The versionId is read from the
// request instead, below.
case "s3:GetObject", "s3:GetObjectTagging", "s3:GetObjectAcl",
"s3:PutObjectAcl", "s3:DeleteObjectTagging" -> s3ExistingObjectTagConditionContext(ctx);
// The version actions IamActionRegistry checks for a request naming a versionId carry
// the key as their base actions do, per the Service Reference, as do the annotation
// actions, and the versionId picks whose tags are read.
case "s3:GetObject", "s3:GetObjectVersion", "s3:GetObjectTagging", "s3:GetObjectAcl",
"s3:GetObjectVersionAcl", "s3:PutObjectAcl", "s3:PutObjectVersionAcl", "s3:DeleteObjectTagging",
"s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation", "s3:ListObjectAnnotations",
"s3:ListObjectVersionAnnotations", "s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation",
"s3:DeleteObjectAnnotation", "s3:DeleteObjectVersionAnnotation" ->
s3ExistingObjectTagConditionContext(ctx);
case "s3:PutObject" -> s3RequestObjectTagConditionContext(ctx);
case "s3:PutObjectTagging" -> merge(s3ExistingObjectTagConditionContext(ctx),
s3PutObjectTaggingBodyConditionContext(ctx));
@@ -446,7 +446,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
"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.
// credential is narrower: it may authorize only its literal object and action. 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.
if (!scope.action().equals(action) || resources.isEmpty()
|| resources.stream().anyMatch(resource -> !scope.resourceArn().equals(resource))) {
ctx.abortWith(accessDeniedForRequest(action, credentialScope, ctx));
@@ -520,15 +523,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
// against real AWS: under a GetObject allow conditioned on s3:ExistingObjectTag the
// conditional write is AccessDenied, under a GetObject allow scoped by prefix alone it
// succeeds, and with no GetObject at all it is denied. If-None-Match needs no such
// permission.
if ("s3:PutObject".equals(action) && ctx.getHeaderString("If-Match") != null) {
// permission. The user guide's conditional writes page lists PutObject, CopyObject and
// CompleteMultipartUpload as the operations that take If-Match; CreateMultipartUpload and
// UploadPart, also checked as s3:PutObject, ignore it, so it adds nothing to them.
if ("s3:PutObject".equals(action) && evaluatesIfMatch(ctx) && ctx.getHeaderString("If-Match") != null) {
abortIfDenied(ctx, caller, principal, "s3:GetObject", credentialScope, resources,
withoutObjectTags(targetContexts), region, accountId, akid);
}
// A DeleteObject conditioned on an ETag reveals whether the object still has it, and the
// conditional-deletes guide requires s3:GetObject for it; If-Match: * (existence only)
// needs just s3:DeleteObject. The read is checked the way the PutObject case above is.
if ("s3:DeleteObject".equals(action) && isPlainDeleteObject(ctx)
// needs just the delete. A delete naming a version, checked as s3:DeleteObjectVersion, is
// checked the same way: the guide says the condition is evaluated against the current
// version only, so the read is of that one, s3:GetObject. The read is checked the way the
// PutObject case above is.
if (("s3:DeleteObject".equals(action) || "s3:DeleteObjectVersion".equals(action))
&& isPlainDeleteObject(ctx)
&& isETagCondition(ctx.getHeaderString("If-Match"))) {
abortIfDenied(ctx, caller, principal, "s3:GetObject", credentialScope, resources,
withoutObjectTags(targetContexts), region, accountId, akid);
@@ -536,10 +545,22 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
// The DELETE subresources S3Controller.deleteObject routes away from DeleteObject (?uploadId= is
// AbortMultipartUpload, which the action registry also files under s3:DeleteObject). Everything
// else, including the x-id and X-Amz-* parameters SDKs and presigned URLs add, is a DeleteObject.
// AbortMultipartUpload). Everything else, including the x-id and X-Amz-* parameters SDKs and
// presigned URLs add, is a DeleteObject.
private static final Set<String> NON_DELETE_OBJECT_SUBRESOURCES = Set.of("uploadId", "tagging", "annotation");
// PutObject and CopyObject are any PUT S3Controller does not run as UploadPart or UploadPartCopy,
// which name both an uploadId and a partNumber; CompleteMultipartUpload is a POST naming its
// upload, without ?uploads.
private static boolean evaluatesIfMatch(ContainerRequestContext ctx) {
MultivaluedMap<String, String> params = ctx.getUriInfo().getQueryParameters();
boolean namesUpload = params.containsKey("uploadId");
if ("PUT".equalsIgnoreCase(ctx.getMethod())) {
return !(namesUpload && params.containsKey("partNumber"));
}
return "POST".equalsIgnoreCase(ctx.getMethod()) && namesUpload && !params.containsKey("uploads");
}
private static boolean isPlainDeleteObject(ContainerRequestContext ctx) {
return "DELETE".equalsIgnoreCase(ctx.getMethod())
&& ctx.getUriInfo().getQueryParameters().keySet().stream()
@@ -1125,15 +1146,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if (action == null) return null;
// Action set sourced from IamActionRegistry — see that file for any
// additions. HEAD on an object is bucketed under s3:GetObject by the
// registry, so we distinguish via httpMethod.
// registry, so we distinguish via httpMethod. The POST operations checked as
// s3:PutObject or s3:GetObject (multipart create and complete, Select) cannot
// be told apart from the action, so they get no name rather than a wrong one.
if ("POST".equalsIgnoreCase(httpMethod)) {
return null;
}
return switch (action) {
case "s3:GetObject" -> "HEAD".equalsIgnoreCase(httpMethod) ? "HeadObject" : "GetObject";
case "s3:GetObject", "s3:GetObjectVersion" ->
"HEAD".equalsIgnoreCase(httpMethod) ? "HeadObject" : "GetObject";
case "s3:PutObject" -> "PutObject";
case "s3:DeleteObject" -> "DeleteObject";
case "s3:DeleteObject", "s3:DeleteObjectVersion" -> "DeleteObject";
case "s3:ListBucket" -> "ListObjects";
case "s3:ListAllMyBuckets" -> "ListBuckets";
case "s3:GetObjectAcl" -> "GetObjectAcl";
case "s3:PutObjectAcl" -> "PutObjectAcl";
case "s3:GetObjectAcl", "s3:GetObjectVersionAcl" -> "GetObjectAcl";
case "s3:PutObjectAcl", "s3:PutObjectVersionAcl" -> "PutObjectAcl";
case "s3:GetObjectTagging" -> "GetObjectTagging";
case "s3:PutObjectTagging" -> "PutObjectTagging";
case "s3:DeleteObjectTagging" -> "DeleteObjectTagging";
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsProtocolClaimFilter;
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
import io.github.hectorvent.floci.core.common.ProtocolClaim;
import io.github.hectorvent.floci.core.common.WireProtocol;
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MediaType;
@@ -411,7 +412,10 @@ public class IamActionRegistry {
sub("requestPayment", "s3:GetBucketRequestPayment"),
sub("accelerate", "s3:GetAccelerateConfiguration"),
sub("replication", "s3:GetReplicationConfiguration"),
sub("metrics", "s3:GetMetricsConfiguration"));
sub("metrics", "s3:GetMetricsConfiguration"),
sub("intelligent-tiering", "s3:GetIntelligentTieringConfiguration"),
sub("analytics", "s3:GetAnalyticsConfiguration"),
sub("inventory", "s3:GetInventoryConfiguration"));
private static final List<SubResourceAction> PUT_BUCKET_SUBRESOURCES = List.of(
sub("notification", "s3:PutBucketNotification"),
@@ -430,13 +434,16 @@ public class IamActionRegistry {
sub("requestPayment", "s3:PutBucketRequestPayment"),
sub("accelerate", "s3:PutAccelerateConfiguration"),
sub("replication", "s3:PutReplicationConfiguration"),
sub("metrics", "s3:PutMetricsConfiguration"));
sub("metrics", "s3:PutMetricsConfiguration"),
sub("intelligent-tiering", "s3:PutIntelligentTieringConfiguration"),
sub("analytics", "s3:PutAnalyticsConfiguration"),
sub("inventory", "s3:PutInventoryConfiguration"));
// AWS gives only DeleteBucketPolicy and DeleteBucketWebsite their own action; removing any other
// sub-resource is authorised by the same Put* action that sets it. ?accelerate is absent because
// S3Controller rejects DELETE on it with 405, so no mapping should claim the request.
private static final List<SubResourceAction> DELETE_BUCKET_SUBRESOURCES = List.of(
sub("tagging", "s3:DeleteBucketTagging"),
sub("tagging", "s3:PutBucketTagging"),
sub("website", "s3:DeleteBucketWebsite"),
sub("policy", "s3:DeleteBucketPolicy"),
sub("cors", "s3:PutBucketCORS"),
@@ -445,7 +452,10 @@ public class IamActionRegistry {
sub("publicAccessBlock", "s3:PutBucketPublicAccessBlock"),
sub("ownershipControls", "s3:PutBucketOwnershipControls"),
sub("replication", "s3:PutReplicationConfiguration"),
sub("metrics", "s3:PutMetricsConfiguration"));
sub("metrics", "s3:PutMetricsConfiguration"),
sub("intelligent-tiering", "s3:PutIntelligentTieringConfiguration"),
sub("analytics", "s3:PutAnalyticsConfiguration"),
sub("inventory", "s3:PutInventoryConfiguration"));
private static String resolveS3SubResourceAction(String method, ContainerRequestContext ctx) {
MultivaluedMap<String, String> params = ctx.getUriInfo().getQueryParameters();
@@ -473,23 +483,98 @@ public class IamActionRegistry {
return null;
}
/**
* The operation an object sub-resource selects, checked in the order S3Controller dispatches
* them: a request naming several runs the first, so it is checked as that one. A
* {@code versionId} selects the version action AWS checks in place of an action that has one
* (see {@link S3ObjectActions#forVersion}). Tagging keeps its own actions: the tagging
* operations act on the current version whatever {@code versionId} names, so a version tagging
* action would authorize one version and let the request change another. Null where the
* controller falls through to the plain object operation on the current version, which the
* rules check.
*/
private static String objectSubResourceAction(String method, MultivaluedMap<String, String> params) {
if (params.containsKey("acl")) {
return switch (method) {
case "GET" -> "s3:GetObjectAcl";
case "PUT" -> "s3:PutObjectAcl";
default -> null;
};
}
if (params.containsKey("tagging")) {
return switch (method) {
case "GET" -> "s3:GetObjectTagging";
case "PUT" -> "s3:PutObjectTagging";
case "DELETE" -> "s3:DeleteObjectTagging";
default -> null;
};
}
return null;
String versionId = params.getFirst("versionId");
return switch (method) {
case "GET" -> {
if (params.containsKey("uploadId")) {
yield "s3:ListMultipartUploadParts";
}
if (params.containsKey("tagging")) {
yield "s3:GetObjectTagging";
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion(annotationReadAction(params), versionId);
}
if (params.containsKey("retention")) {
yield "s3:GetObjectRetention";
}
if (params.containsKey("legal-hold")) {
yield "s3:GetObjectLegalHold";
}
if (params.containsKey("acl")) {
yield S3ObjectActions.forVersion("s3:GetObjectAcl", versionId);
}
yield versionId != null ? "s3:GetObjectVersion" : null;
}
case "HEAD" -> {
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion(annotationReadAction(params), versionId);
}
yield versionId != null ? "s3:GetObjectVersion" : null;
}
case "PUT" -> {
if (params.containsKey("tagging")) {
yield "s3:PutObjectTagging";
}
if (params.containsKey("retention")) {
yield "s3:PutObjectRetention";
}
if (params.containsKey("legal-hold")) {
yield "s3:PutObjectLegalHold";
}
if (params.containsKey("acl")) {
yield S3ObjectActions.forVersion("s3:PutObjectAcl", versionId);
}
// A PUT naming an uploadId and a partNumber is UploadPart, which the rules check as
// s3:PutObject, as they do PutObject and CopyObject.
yield params.containsKey("annotation")
? S3ObjectActions.forVersion("s3:PutObjectAnnotation", versionId)
: null;
}
case "DELETE" -> {
if (params.containsKey("tagging")) {
yield "s3:DeleteObjectTagging";
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion("s3:DeleteObjectAnnotation", versionId);
}
if (params.containsKey("uploadId")) {
yield "s3:AbortMultipartUpload";
}
yield versionId != null ? "s3:DeleteObjectVersion" : null;
}
case "POST" -> {
if (params.containsKey("uploads")) {
yield "s3:PutObject";
}
if (params.containsKey("restore")) {
yield "s3:RestoreObject";
}
if (params.containsKey("select")) {
// S3Controller selects from the version a versionId names.
yield S3ObjectActions.forVersion("s3:GetObject", versionId);
}
yield params.containsKey("uploadId") ? "s3:PutObject" : null;
}
default -> null;
};
}
/** A named annotation is GetObjectAnnotation; an empty or absent name lists them, as S3Controller does. */
private static String annotationReadAction(MultivaluedMap<String, String> params) {
String name = params.getFirst("annotationName");
return name != null && !name.isEmpty() ? "s3:GetObjectAnnotation" : "s3:ListObjectAnnotations";
}
}
@@ -0,0 +1,41 @@
package io.github.hectorvent.floci.services.s3;
import java.util.Map;
/**
* The version actions AWS checks for an S3 object request that names a {@code versionId}, in place of
* the action the request is otherwise checked as. The source is the Service Authorization Reference
* for S3, which gives each its own action.
*/
public final class S3ObjectActions {
/**
* The object actions that have a version counterpart, which AWS checks in their place when the
* request names a {@code versionId}. Retention, legal hold, restore and the multipart actions
* have none, and are checked as they are. The tagging actions are left out although AWS gives
* them version counterparts: the tagging operations act on the current version whatever
* {@code versionId} names (see IamActionRegistry).
*/
private static final Map<String, String> VERSION_ACTIONS = Map.ofEntries(
Map.entry("s3:GetObject", "s3:GetObjectVersion"),
Map.entry("s3:DeleteObject", "s3:DeleteObjectVersion"),
Map.entry("s3:GetObjectAcl", "s3:GetObjectVersionAcl"),
Map.entry("s3:PutObjectAcl", "s3:PutObjectVersionAcl"),
Map.entry("s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation"),
Map.entry("s3:ListObjectAnnotations", "s3:ListObjectVersionAnnotations"),
Map.entry("s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation"),
Map.entry("s3:DeleteObjectAnnotation", "s3:DeleteObjectVersionAnnotation"));
private S3ObjectActions() {
}
/** What a request naming {@code versionId} is checked as: {@code action}'s version counterpart, if it has one. */
public static String forVersion(String action, String versionId) {
return versionId == null ? action : VERSION_ACTIONS.getOrDefault(action, action);
}
/** True for an action AWS checks only for a request naming a version, which carries {@code s3:versionid}. */
public static boolean isVersionAction(String action) {
return VERSION_ACTIONS.containsValue(action);
}
}