fix(s3): act on the object version a tagging request names (#5775)

PutObjectTagging and DeleteObjectTagging ignored versionId and changed the
current version. GetObjectTagging dropped it in the controller, so it read
the current version too, and the service took versionId=null as the
current version even on a key that has no null version.

All three now act on the version named. "null" names only the null
version, the object stored without a version id; on a key that has none
the request answers NoSuchVersion. Each returns the version it acted on in
x-amz-version-id. A request on a delete marker, named or current, answers
405 MethodNotAllowed with Method and ResourceType, and with
x-amz-delete-marker: true, as S3 does. A put or delete runs under the
bucket monitor that writes, deletes and the lifecycle sweep hold, so a
version deleted between the lookup and the write is not written back.

Since they act on the version named, a request naming one is now checked
as s3:GetObjectVersionTagging, s3:PutObjectVersionTagging or
s3:DeleteObjectVersionTagging, carrying s3:versionid, in place of the base
tagging action. Part 1 kept these requests on the base actions because the
operations changed the current version. The s3:ExistingObjectTag lookup
already read the version named, so the tags a condition sees are now those
of the version the request changes.

The IAM condition context still reads s3:ExistingObjectTag for
versionId=null from the current version, which GetObject still serves for
it, so a deny on those tags keeps applying.

Refs #5716
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-16 07:55:28 -05:00
1 parent 608a38bff3
commit 7e98592761
14 files changed
+372 -75

No files matched your search

@@ -244,15 +244,16 @@ public class IamConditionContextResolver {
// 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:GetObject", "s3:GetObjectVersion", "s3:GetObjectTagging",
"s3:GetObjectVersionTagging", "s3:GetObjectAcl", "s3:GetObjectVersionAcl",
"s3:PutObjectAcl", "s3:PutObjectVersionAcl", "s3:DeleteObjectTagging",
"s3:DeleteObjectVersionTagging", "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));
case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> merge(
s3ExistingObjectTagConditionContext(ctx), s3PutObjectTaggingBodyConditionContext(ctx));
default -> null;
};
}
@@ -1157,9 +1157,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
case "s3:ListAllMyBuckets" -> "ListBuckets";
case "s3:GetObjectAcl", "s3:GetObjectVersionAcl" -> "GetObjectAcl";
case "s3:PutObjectAcl", "s3:PutObjectVersionAcl" -> "PutObjectAcl";
case "s3:GetObjectTagging" -> "GetObjectTagging";
case "s3:PutObjectTagging" -> "PutObjectTagging";
case "s3:DeleteObjectTagging" -> "DeleteObjectTagging";
case "s3:GetObjectTagging", "s3:GetObjectVersionTagging" -> "GetObjectTagging";
case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> "PutObjectTagging";
case "s3:DeleteObjectTagging", "s3:DeleteObjectVersionTagging" -> "DeleteObjectTagging";
default -> null;
};
}
@@ -487,11 +487,8 @@ public class IamActionRegistry {
* 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.
* (see {@link S3ObjectActions#forVersion}). 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) {
String versionId = params.getFirst("versionId");
@@ -501,7 +498,7 @@ public class IamActionRegistry {
yield "s3:ListMultipartUploadParts";
}
if (params.containsKey("tagging")) {
yield "s3:GetObjectTagging";
yield S3ObjectActions.forVersion("s3:GetObjectTagging", versionId);
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion(annotationReadAction(params), versionId);
@@ -525,7 +522,7 @@ public class IamActionRegistry {
}
case "PUT" -> {
if (params.containsKey("tagging")) {
yield "s3:PutObjectTagging";
yield S3ObjectActions.forVersion("s3:PutObjectTagging", versionId);
}
if (params.containsKey("retention")) {
yield "s3:PutObjectRetention";
@@ -544,7 +541,7 @@ public class IamActionRegistry {
}
case "DELETE" -> {
if (params.containsKey("tagging")) {
yield "s3:DeleteObjectTagging";
yield S3ObjectActions.forVersion("s3:DeleteObjectTagging", versionId);
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion("s3:DeleteObjectAnnotation", versionId);
@@ -811,7 +811,8 @@ public class S3Controller {
if (hasQueryParam(uriInfo, "tagging")) {
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectTagging", authorization);
return handlePutObjectTagging(bucket, key, readWholeBody(body));
return handlePutObjectTagging(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"),
readWholeBody(body));
}
if (hasQueryParam(uriInfo, "retention")) {
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectRetention", authorization);
@@ -981,7 +982,7 @@ public class S3Controller {
if (hasQueryParam(uriInfo, "tagging")) {
s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectTagging", authorization);
return handleGetObjectTagging(bucket, key);
return handleGetObjectTagging(bucket, key, versionId);
}
if (hasQueryParam(uriInfo, "annotation")) {
String annotationNameParam = uriInfo.getQueryParameters().getFirst("annotationName");
@@ -1484,8 +1485,8 @@ public class S3Controller {
if (hasQueryParam(uriInfo, "tagging")) {
s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObjectTagging", authorization);
s3Service.deleteObjectTagging(bucket, key);
return Response.noContent().build();
return withVersionId(Response.noContent(), s3Service.deleteObjectTagging(bucket, key, versionId))
.build();
}
if (hasQueryParam(uriInfo, "annotation")) {
boolean bypass = "true".equalsIgnoreCase(
@@ -2615,16 +2616,21 @@ public class S3Controller {
// --- Object Tagging ---
private Response handlePutObjectTagging(String bucket, String key, byte[] body) {
private Response handlePutObjectTagging(String bucket, String key, String versionId, byte[] body) {
String xml = new String(body, StandardCharsets.UTF_8);
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
s3Service.putObjectTagging(bucket, key, tags);
return Response.ok().build();
return withVersionId(Response.ok(), s3Service.putObjectTagging(bucket, key, versionId, tags)).build();
}
private Response handleGetObjectTagging(String bucket, String key) {
Map<String, String> tags = s3Service.getObjectTagging(bucket, key);
return Response.ok(buildTaggingXml(tags)).type(MediaType.APPLICATION_XML).build();
private Response handleGetObjectTagging(String bucket, String key, String versionId) {
S3Service.ObjectTaggingResult tagging = s3Service.getObjectTaggingResult(bucket, key, versionId);
return withVersionId(Response.ok(buildTaggingXml(tagging.tags())), tagging.versionId())
.type(MediaType.APPLICATION_XML).build();
}
/** {@code resp} with the id of the version a request acted on, when that version has one. */
private static Response.ResponseBuilder withVersionId(Response.ResponseBuilder resp, String versionId) {
return versionId != null ? resp.header("x-amz-version-id", versionId) : resp;
}
private String buildTaggingXml(Map<String, String> tags) {
@@ -3573,10 +3579,12 @@ public class S3Controller {
}
// S3 InvalidArgument responses carry ArgumentName and ArgumentValue so the SDK can
// surface which input was rejected, and EntityTooSmall and EntityTooLarge the proposed and
// allowed sizes. Multipart size errors also identify the rejected part.
// allowed sizes. Multipart size errors also identify the rejected part, and MethodNotAllowed
// the method and the kind of resource that refused it.
// They travel through AwsException.extendedData.
if (e.getExtendedData() != null) {
for (String field : List.of("ProposedSize", "MinSizeAllowed", "MaxSizeAllowed", "PartNumber", "ETag")) {
for (String field : List.of("Method", "ResourceType", "ProposedSize", "MinSizeAllowed",
"MaxSizeAllowed", "PartNumber", "ETag")) {
Object value = e.getExtendedData().get(field);
if (value != null) {
xmlBuilder.elem(field, value.toString());
@@ -3612,7 +3620,13 @@ public class S3Controller {
.elem("RequestId", UUID.randomUUID().toString())
.end("Error")
.build();
return Response.status(e.getHttpStatus()).entity(xml).type(MediaType.APPLICATION_XML).build();
Response.ResponseBuilder response = Response.status(e.getHttpStatus())
.entity(xml).type(MediaType.APPLICATION_XML);
// A request refused because it reached a delete marker says so in x-amz-delete-marker.
if (e.getExtendedData() != null && "DeleteMarker".equals(e.getExtendedData().get("ResourceType"))) {
response.header("x-amz-delete-marker", "true");
}
return response.build();
}
private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch,
@@ -12,15 +12,16 @@ 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).
* have none, and are checked as they are.
*/
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:GetObjectTagging", "s3:GetObjectVersionTagging"),
Map.entry("s3:PutObjectTagging", "s3:PutObjectVersionTagging"),
Map.entry("s3:DeleteObjectTagging", "s3:DeleteObjectVersionTagging"),
Map.entry("s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation"),
Map.entry("s3:ListObjectAnnotations", "s3:ListObjectVersionAnnotations"),
Map.entry("s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation"),
@@ -2799,24 +2799,108 @@ public class S3Service implements Resettable, ResourceProvider {
// --- Object Tagging ---
/** The tags of one object version, and that version's id: null for an object stored without one. */
public record ObjectTaggingResult(String versionId, Map<String, String> tags) {
}
public void putObjectTagging(String bucketName, String key, Map<String, String> tags) {
setCurrentObjectTags(bucketName, key, tags != null ? tags : new HashMap<>());
LOG.debugv("Put tags on object: {0}/{1}", bucketName, key);
putObjectTagging(bucketName, key, null, tags);
}
/**
* Replaces the tags of the version {@code versionId} names, or of the current version when it is
* null, and returns that version's id.
*/
public String putObjectTagging(String bucketName, String key, String versionId, Map<String, String> tags) {
return updateObjectTagging(bucketName, key, versionId, "PUT", tags != null ? tags : new HashMap<>());
}
public Map<String, String> getObjectTagging(String bucketName, String key) {
return getObjectTagging(bucketName, key, null);
}
/** Tags of one version, or of the current version when {@code versionId} is null or "null". */
/**
* Tags of one version, or of the current version when {@code versionId} is null or "null". The
* IAM condition context reads {@code s3:ExistingObjectTag} here, so these must be the tags of the
* object a request reaches, and GetObject, HeadObject and the annotation operations still serve
* the current version for "null". GetObjectTagging reads {@link #getObjectTaggingResult}.
*/
public Map<String, String> getObjectTagging(String bucketName, String key, String versionId) {
S3Object obj = getStoredObject(bucketName, key, "null".equals(versionId) ? null : versionId);
return obj.getTags() != null ? obj.getTags() : Map.of();
}
/**
* The tags of the version {@code versionId} names, or of the current version when it is null,
* with that version's id. "null" names the null version only, and a delete marker is refused.
*/
public ObjectTaggingResult getObjectTaggingResult(String bucketName, String key, String versionId) {
S3Object obj = getTaggingTargetEntry(bucketName, key, versionId, "GET").value();
return new ObjectTaggingResult(obj.getVersionId(), obj.getTags() != null ? obj.getTags() : Map.of());
}
public void deleteObjectTagging(String bucketName, String key) {
setCurrentObjectTags(bucketName, key, new HashMap<>());
LOG.debugv("Deleted tags from object: {0}/{1}", bucketName, key);
deleteObjectTagging(bucketName, key, null);
}
/**
* Removes the tags of the version {@code versionId} names, or of the current version when it is
* null, and returns that version's id.
*/
public String deleteObjectTagging(String bucketName, String key, String versionId) {
return updateObjectTagging(bucketName, key, versionId, "DELETE", new HashMap<>());
}
/**
* Gives the version a tagging request names the tags {@code tags}. It runs under the bucket
* monitor that writes, deletes and the lifecycle sweep hold, so the version is resolved and
* rewritten in one step: a version deleted between the lookup and the write is not written back,
* and the sweep never sees the tags it matched a rule against change before it acts.
*/
private String updateObjectTagging(String bucketName, String key, String versionId, String method,
Map<String, String> tags) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
synchronized (bucket) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getTaggingTargetEntry(bucketName, key, versionId, method);
S3Object obj = ownedObject.value();
obj.setTags(tags);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);
LOG.debugv("{0} tags of object: {1}/{2} version {3}", method, bucketName, key, obj.getVersionId());
return obj.getVersionId();
}
}
/**
* The version a tagging request acts on: the one {@code versionId} names, or the current version
* when it is null. The literal {@code "null"} names the null version, resolved as
* deleteObjectLocked resolves it: the object at the plain key, when that object has no version
* id. A current version with an id is not it, and floci keeps no null version beside one, so the
* request names a version that does not exist. A delete marker, named or current, has no tags:
* S3 refuses the request with MethodNotAllowed, naming the method and the marker.
*/
private AccountAwareStorageBackend.OwnedEntry<S3Object> getTaggingTargetEntry(
String bucketName, String key, String versionId, String method) {
AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket = resolveBucketEntry(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
boolean nullVersion = "null".equals(versionId);
String storeKey = versionId == null || nullVersion
? objectKey(bucketName, key)
: versionedKey(bucketName, key, versionId);
S3Object object = resolveObjectForAccount(ownedBucket.account(), storeKey)
.filter(existing -> !nullVersion || existing.getVersionId() == null)
.orElseThrow(() -> versionId != null
? new AwsException("NoSuchVersion", "The specified version does not exist.", 404)
: new AwsException("NoSuchKey", "The specified key does not exist.", 404));
if (object.isDeleteMarker()) {
throw new AwsException("MethodNotAllowed",
"The specified method is not allowed against this resource.", 405,
Map.of("Method", method, "ResourceType", "DeleteMarker"));
}
return new AccountAwareStorageBackend.OwnedEntry<>(ownedBucket.account(), object);
}
// --- Object Annotations ---
@@ -6575,23 +6659,6 @@ public class S3Service implements Resettable, ResourceProvider {
* still refers to this object, checked under the bucket lock that puts and deletes hold, so a
* concurrent overwrite or delete marker is never replaced by the object it superseded.
*/
/**
* Replaces the current version's tags under the bucket monitor that writes, deletes and the
* lifecycle sweep hold, so the object is resolved and rewritten in one step: the sweep never
* sees the tags it matched a rule against change before it acts.
*/
private void setCurrentObjectTags(String bucketName, String key, Map<String, String> tags) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
synchronized (bucket) {
AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =
getStoredObjectEntry(bucketName, key, null);
ownedObject.value().setTags(tags);
putObjectMetadataForAccount(ownedObject.account(), bucketName, key, ownedObject.value());
}
}
private void putObjectMetadataForAccount(String accountId, String bucketName, String key, S3Object object) {
Bucket bucket = resolveBucket(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",