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

@@ -288,6 +288,23 @@ class IamConditionContextResolverTest {
assertFalse(resolver.resolve("s3", "s3:GetObject", request).containsKey("s3:versionid"));
}
@Test
void versionTaggingActionsCarryTheVersionAndItsTags() {
ContainerRequestContext request = s3Request("/bucket/object",
"<Tagging><TagSet><Tag><Key>Team</Key><Value>payments</Value></Tag></TagSet></Tagging>");
request.getUriInfo().getQueryParameters().putSingle("versionId", "v1");
when(s3Service.getObjectTagging("bucket", "object", "v1")).thenReturn(Map.of("env", "a"));
for (String action : List.of("s3:GetObjectVersionTagging", "s3:DeleteObjectVersionTagging")) {
Map<String, List<String>> conditions = resolver.resolve("s3", action, request);
assertEquals(List.of("v1"), conditions.get("s3:versionid"), action);
assertEquals(List.of("a"), conditions.get("s3:ExistingObjectTag/env"), action);
}
Map<String, List<String>> put = resolver.resolve("s3", "s3:PutObjectVersionTagging", request);
assertEquals(List.of("v1"), put.get("s3:versionid"));
assertEquals(List.of("a"), put.get("s3:ExistingObjectTag/env"));
assertEquals(List.of("payments"), put.get("s3:RequestObjectTag/Team"));
}
@Test
void annotationActionsCarryTheObjectsTags() {
when(s3Service.getObjectTagging("bucket", "object", null)).thenReturn(Map.of("env", "a"));
@@ -85,6 +85,12 @@ class IamEnforcementFilterUnitTest {
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAcl", "GET"));
assertEquals("PutObjectAcl",
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionAcl", "PUT"));
assertEquals("GetObjectTagging",
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionTagging", "GET"));
assertEquals("PutObjectTagging",
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionTagging", "PUT"));
assertEquals("DeleteObjectTagging",
IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersionTagging", "DELETE"));
}
@Test
@@ -420,10 +420,12 @@ class IamActionRegistryTest {
assertEquals("s3:DeleteObjectVersion", s3Action("DELETE", "/bucket/key", "versionId=v1"));
assertEquals("s3:GetObjectVersionAcl", s3Action("GET", "/bucket/key", "acl", "versionId=v1"));
assertEquals("s3:PutObjectVersionAcl", s3Action("PUT", "/bucket/key", "acl", "versionId=v1"));
// Tagging keeps its own actions: the tagging operations act on the current version.
assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:PutObjectTagging", s3Action("PUT", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:DeleteObjectTagging", s3Action("DELETE", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:PutObjectVersionTagging", s3Action("PUT", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:DeleteObjectVersionTagging",
s3Action("DELETE", "/bucket/key", "tagging", "versionId=v1"));
// "null" names the null version, which is a version as any other.
assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=null"));
assertEquals("s3:GetObjectVersionAnnotation",
s3Action("GET", "/bucket/key", "annotation", "annotationName=n", "versionId=v1"));
assertEquals("s3:GetObjectVersionAnnotation",
@@ -243,22 +243,48 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {
}
@Test
void theTagsOfAVersionAreCheckedAsTheTaggingActions() {
// The tagging operations act on the current version whatever versionId names, so a version
// tagging action alone must not let a caller read or change the current version's tags.
void theTagsOfAVersionNeedTheVersionTaggingActions() {
Fixture f = new Fixture("version-tags", true);
String versionId = f.seedVersion("a/tagged");
f.policy(f.allow("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging"));
f.policy(f.allow("s3:GetObjectTagging", "s3:PutObjectTagging", "s3:DeleteObjectTagging"));
denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().get(f.path("a/tagged")).then(), "s3:GetObjectTagging");
.when().get(f.path("a/tagged")).then(), "s3:GetObjectVersionTagging");
denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.body("<Tagging><TagSet/></Tagging>").when().put(f.path("a/tagged")).then(),
"s3:PutObjectTagging");
"s3:PutObjectVersionTagging");
denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().delete(f.path("a/tagged")).then(), "s3:DeleteObjectVersionTagging");
f.policy(f.allow("s3:GetObjectTagging"));
f.policy(f.allow("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging",
"s3:DeleteObjectVersionTagging"));
f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.body("<Tagging><TagSet/></Tagging>").when().put(f.path("a/tagged")).then().statusCode(200);
f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().get(f.path("a/tagged")).then().statusCode(200);
f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().delete(f.path("a/tagged")).then().statusCode(204);
}
@Test
void theTagsAVersionTaggingConditionReadsAreThoseOfTheVersionItChanges() {
Fixture f = new Fixture("version-tag-condition", true);
String older = f.seedVersion("a/doc", "env=a");
String current = f.seedVersion("a/doc", "env=b");
f.policy(f.allowWhen("{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"a\"}}",
"s3:PutObjectVersionTagging"));
String tagging = "<Tagging><TagSet><Tag><Key>env</Key><Value>a</Value></Tag>"
+ "<Tag><Key>seen</Key><Value>yes</Value></Tag></TagSet></Tagging>";
f.user().queryParam("tagging", "").queryParam("versionId", older).body(tagging)
.when().put(f.path("a/doc")).then().statusCode(200).header("x-amz-version-id", older);
denied(f.user().queryParam("tagging", "").queryParam("versionId", current).body(tagging)
.when().put(f.path("a/doc")).then(), "s3:PutObjectVersionTagging");
f.admin().queryParam("tagging", "").queryParam("versionId", older).when().get(f.path("a/doc"))
.then().statusCode(200).body(containsString("<Key>seen</Key>"));
f.admin().queryParam("tagging", "").when().get(f.path("a/doc"))
.then().statusCode(200).body(not(containsString("<Key>seen</Key>")));
}
@Test
@@ -149,6 +149,21 @@ class S3ObjectTagConditionEnforcementIntegrationTest {
assertEquals(403, f.put("a/bad", "%zz=a", null), "an undecodable key");
}
@Test
void aReadNamingTheNullVersionIsAuthorizedAgainstTheVersionItServes() {
Fixture f = new Fixture("nullversion", true);
// Written with versioning on, the key has no null version. S3 answers a read naming one with
// NoSuchVersion, but floci serves it the current version, so that version's tags must decide
// or the deny on them would be skipped.
f.seedVersion("a/secret", "env=b");
f.policy(allow("\"s3:GetObject\",\"s3:GetObjectVersion\"", f.objects(), null) + ","
+ "{\"Effect\":\"Deny\",\"Action\":[\"s3:GetObject\",\"s3:GetObjectVersion\"],"
+ "\"Resource\":\"" + f.objects() + "\","
+ "\"Condition\":{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"b\"}}}");
assertEquals(403, f.get("a/secret"), "the current version carries the denied tag");
assertEquals(403, f.getVersion("a/secret", "null"), "the same version, reached through versionId=null");
}
private static String allow(String actions, String resource, String condition) {
String action = actions.startsWith("\"") ? "[" + actions + "]" : "\"" + actions + "\"";
return "{\"Effect\":\"Allow\",\"Action\":" + action + ",\"Resource\":\"" + resource + "\""
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@@ -517,4 +518,78 @@ class S3VersioningIntegrationTest {
given().when().delete("/" + bucket + "/key?versionId=" + versionId).then().statusCode(204);
given().when().delete("/" + bucket).then().statusCode(204);
}
@Test
@Order(22)
void taggingActsOnTheVersionNamed() {
String bucket = "versioned-tagging-test";
given().when().put("/" + bucket).then().statusCode(200);
// Written before versioning, "old" is the null version, which has no version id to report.
given().body("v0").when().put("/" + bucket + "/old").then().statusCode(200);
given().body(tagging("unversioned")).when().put("/" + bucket + "/old?tagging")
.then().statusCode(200).header("x-amz-version-id", nullValue());
given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning").then().statusCode(200);
String v1 = given().body("v1").when().put("/" + bucket + "/key")
.then().statusCode(200).extract().header("x-amz-version-id");
String v2 = given().body("v2").when().put("/" + bucket + "/key")
.then().statusCode(200).extract().header("x-amz-version-id");
given().body(tagging("first")).when().put("/" + bucket + "/key?tagging&versionId=" + v1)
.then().statusCode(200).header("x-amz-version-id", v1);
given().when().get("/" + bucket + "/key?tagging&versionId=" + v1)
.then().statusCode(200).header("x-amz-version-id", v1)
.body(containsString("<Value>first</Value>"));
given().when().get("/" + bucket + "/key?tagging")
.then().statusCode(200).header("x-amz-version-id", v2)
.body(not(containsString("<Tag>")));
given().when().delete("/" + bucket + "/key?tagging&versionId=" + v1)
.then().statusCode(204).header("x-amz-version-id", v1);
given().when().get("/" + bucket + "/key?tagging&versionId=" + v1)
.then().statusCode(200).body(not(containsString("<Tag>")));
// versionId=null names the null version only: "key" was written with versioning on.
given().when().get("/" + bucket + "/old?tagging&versionId=null")
.then().statusCode(200).body(containsString("<Value>unversioned</Value>"));
given().body(tagging("other")).when().put("/" + bucket + "/key?tagging&versionId=null")
.then().statusCode(404).body(containsString("<Code>NoSuchVersion</Code>"));
given().when().get("/" + bucket + "/key?tagging")
.then().statusCode(200).body(not(containsString("<Value>other</Value>")));
}
@Test
@Order(23)
void taggingADeleteMarkerIsNotAllowed() {
String bucket = "versioned-tagging-marker-test";
given().when().put("/" + bucket).then().statusCode(200);
given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning").then().statusCode(200);
given().body("data").when().put("/" + bucket + "/key").then().statusCode(200);
String markerId = given().when().delete("/" + bucket + "/key")
.then().statusCode(204).extract().header("x-amz-version-id");
// Refused whether the request names the marker or the marker is the current version.
String key = "/" + bucket + "/key?tagging";
for (String path : List.of(key, key + "&versionId=" + markerId)) {
given().body(tagging("v")).when().put(path)
.then().statusCode(405).header("x-amz-delete-marker", "true")
.body(deleteMarkerRefused("PUT"));
given().when().get(path)
.then().statusCode(405).header("x-amz-delete-marker", "true")
.body(deleteMarkerRefused("GET"));
given().when().delete(path)
.then().statusCode(405).header("x-amz-delete-marker", "true")
.body(deleteMarkerRefused("DELETE"));
}
}
private static String tagging(String value) {
return "<Tagging><TagSet><Tag><Key>version</Key><Value>" + value + "</Value></Tag></TagSet></Tagging>";
}
private static Matcher<String> deleteMarkerRefused(String method) {
return allOf(containsString("<Code>MethodNotAllowed</Code>"),
containsString("<Method>" + method + "</Method>"),
containsString("<ResourceType>DeleteMarker</ResourceType>"));
}
}
@@ -13,6 +13,7 @@ import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -343,6 +344,83 @@ class S3VersioningServiceTest {
assertEquals("keep.txt", objects.get(0).getKey());
}
@Test
void taggingAVersionChangesOnlyThatVersion() {
s3Service.putBucketVersioning("versioned-bucket", "Enabled");
S3Object v1 = s3Service.putObject("versioned-bucket", "key",
"v1".getBytes(StandardCharsets.UTF_8), "text/plain", null);
S3Object v2 = s3Service.putObject("versioned-bucket", "key",
"v2".getBytes(StandardCharsets.UTF_8), "text/plain", null);
assertEquals(v2.getVersionId(),
s3Service.putObjectTagging("versioned-bucket", "key", null, Map.of("version", "current")));
assertEquals(v1.getVersionId(),
s3Service.putObjectTagging("versioned-bucket", "key", v1.getVersionId(), Map.of("version", "first")));
assertEquals(new S3Service.ObjectTaggingResult(v1.getVersionId(), Map.of("version", "first")),
s3Service.getObjectTaggingResult("versioned-bucket", "key", v1.getVersionId()));
assertEquals(new S3Service.ObjectTaggingResult(v2.getVersionId(), Map.of("version", "current")),
s3Service.getObjectTaggingResult("versioned-bucket", "key", null));
assertEquals(v1.getVersionId(), s3Service.deleteObjectTagging("versioned-bucket", "key", v1.getVersionId()));
assertEquals(Map.of(), s3Service.getObjectTaggingResult("versioned-bucket", "key", v1.getVersionId()).tags());
assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "key"));
}
@Test
void versionIdNullNamesTheNullVersionAndNoOther() {
// An object written before versioning is the null version.
s3Service.putObject("versioned-bucket", "old", "v0".getBytes(StandardCharsets.UTF_8), "text/plain", null);
s3Service.putBucketVersioning("versioned-bucket", "Enabled");
assertNull(s3Service.putObjectTagging("versioned-bucket", "old", "null", Map.of("version", "null")));
assertEquals(new S3Service.ObjectTaggingResult(null, Map.of("version", "null")),
s3Service.getObjectTaggingResult("versioned-bucket", "old", "null"));
// A key written only with versioning on has no null version, and its current version is
// not one: a request naming the null version fails and leaves the current one alone.
s3Service.putObject("versioned-bucket", "new", "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null);
s3Service.putObjectTagging("versioned-bucket", "new", Map.of("version", "current"));
for (Runnable request : List.<Runnable>of(
() -> s3Service.getObjectTaggingResult("versioned-bucket", "new", "null"),
() -> s3Service.putObjectTagging("versioned-bucket", "new", "null", Map.of("version", "other")),
() -> s3Service.deleteObjectTagging("versioned-bucket", "new", "null"))) {
AwsException e = assertThrows(AwsException.class, request::run);
assertEquals("NoSuchVersion", e.getErrorCode());
}
assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "new"));
// The condition context's lookup still takes "null" as the current version, which is what
// GetObject serves for it.
assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "new", "null"));
// An object written while versioning is suspended is the null version too, above the
// versions written before it.
s3Service.putBucketVersioning("versioned-bucket", "Suspended");
s3Service.putObject("versioned-bucket", "new", "v2".getBytes(StandardCharsets.UTF_8), "text/plain", null);
assertNull(s3Service.deleteObjectTagging("versioned-bucket", "new", "null"));
s3Service.putObjectTagging("versioned-bucket", "new", "null", Map.of("version", "suspended"));
assertEquals(Map.of("version", "suspended"), s3Service.getObjectTagging("versioned-bucket", "new"));
}
@Test
void taggingADeleteMarkerIsNotAllowed() {
s3Service.putBucketVersioning("versioned-bucket", "Enabled");
s3Service.putObject("versioned-bucket", "key", "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null);
String markerId = s3Service.deleteObject("versioned-bucket", "key").getVersionId();
// The marker is refused whether the request names it or it is the current version.
for (String versionId : Arrays.asList(null, markerId)) {
Map<String, Runnable> requests = Map.of(
"GET", () -> s3Service.getObjectTaggingResult("versioned-bucket", "key", versionId),
"PUT", () -> s3Service.putObjectTagging("versioned-bucket", "key", versionId, Map.of("k", "v")),
"DELETE", () -> s3Service.deleteObjectTagging("versioned-bucket", "key", versionId));
requests.forEach((method, request) -> {
AwsException e = assertThrows(AwsException.class, request::run);
assertEquals("MethodNotAllowed", e.getErrorCode());
assertEquals(405, e.getHttpStatus());
assertEquals(Map.of("Method", method, "ResourceType", "DeleteMarker"), e.getExtendedData());
});
}
}
@Test
void copyObjectPreservesSourceTagsByDefault() {
s3Service.putBucketVersioning("versioned-bucket", "Enabled");