fix(s3): make versionId=null name only the null version (#5887)

A request naming versionId=null asks for the null version, the one an
object written while versioning was off or suspended has. GetObject,
HeadObject, GetObjectAttributes, a copy source and the annotation
operations served the current version for it instead, even on a key
with no null version, so a request authorized for the null version was
served another. Retention, legal hold and the object ACL looked for a
version stored under the id "null", which never exists, so they never
found the null version even when there was one.

Every lookup now resolves the id the way tagging and DeleteObject
already did: the null version is the object at the plain key when it
has no version id, and a key without one answers as for any missing
version, NoSuchVersion, or NoSuchKey for the annotation operations as
for their other missing versions. A read of the null version goes
through the same optimistic snapshot as a read of the current version,
so a write landing mid-read cannot pair one version's metadata with
another's bytes. The s3:ExistingObjectTag lookup reads the null
version's tags the same way, and under s3.enforce-auth an anonymous
read naming the null version is checked against that version's ACL.

Refs #5716
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-17 22:31:26 -05:00
1 parent fb6a5da282
commit 63cf09a396
7 files changed
+280 -83

No files matched your search

@@ -150,18 +150,20 @@ class S3ObjectTagConditionEnforcementIntegrationTest {
}
@Test
void aReadNamingTheNullVersionIsAuthorizedAgainstTheVersionItServes() {
void aReadNamingTheNullVersionIsAuthorizedAgainstThatVersion() {
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.
// Seeded before versioning was enabled, a/tagged-a and a/tagged-b are null versions, and a
// read naming one is checked against its tags. a/secret, written with versioning on, has no
// null version: a read naming one is answered NoSuchVersion and serves nothing.
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.getVersion("a/tagged-b", "null"), "the null version carries the denied tag");
assertEquals(200, f.getVersion("a/tagged-a", "null"), "the null version carries an allowed tag");
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");
assertEquals(404, f.getVersion("a/secret", "null"), "a key without a null version serves none");
}
private static String allow(String actions, String resource, String condition) {
@@ -2464,6 +2464,33 @@ class S3AuthEnforcementIntegrationTest {
given().when().get("/" + bucket + "?list-type=2").then().statusCode(200);
}
@Test
@Order(59)
void anAnonymousReadNamingTheNullVersionIsCheckedAgainstThatVersionsAcl() {
String bucket = "auth-null-version-acl-bucket";
given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
enableLegacyAcls(bucket);
given().filter(LOCAL_SIGNER).body("old").when().put("/" + bucket + "/old").then().statusCode(200);
given().filter(LOCAL_SIGNER)
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning")
.then().statusCode(200);
given().filter(LOCAL_SIGNER).header("x-amz-acl", "public-read").body("new")
.when().put("/" + bucket + "/new")
.then().statusCode(200);
// "old", written before versioning, is the null version, and its own ACL makes it public.
given().filter(LOCAL_SIGNER).queryParam("versionId", "null").header("x-amz-acl", "public-read")
.when().put("/" + bucket + "/old?acl")
.then().statusCode(200);
given().queryParam("versionId", "null").when().get("/" + bucket + "/old")
.then().statusCode(200).body(equalTo("old"));
// "new" has no null version: its public current version does not make one readable.
given().when().get("/" + bucket + "/new").then().statusCode(200).body(equalTo("new"));
given().queryParam("versionId", "null").when().get("/" + bucket + "/new").then().statusCode(403);
}
private static String publicObjectActionPolicy(String bucket, String action) {
return publicObjectActionPolicy(bucket, "*", action);
}
@@ -0,0 +1,67 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* A request naming {@code versionId=null} in disk-storage mode, where the null version's bytes are
* the object file at the plain key: GetObject streams it and CopyObject pins it, and a key without
* a null version has neither.
*/
class S3NullVersionDiskTest {
@TempDir
Path tempDir;
private S3Service s3Service;
@BeforeEach
void setUp() {
s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir.resolve("s3"), false);
s3Service.createBucket("bucket", "us-east-1");
s3Service.putObject("bucket", "old.txt", bytes("null version"), "text/plain", Map.of());
s3Service.putBucketVersioning("bucket", "Enabled");
s3Service.putObject("bucket", "new.txt", bytes("current version"), "text/plain", Map.of());
}
@Test
void theNullVersionIsStreamedAndCopiedFromTheObjectFile() throws IOException {
try (S3Service.ObjectRead read = s3Service.openObject("bucket", "old.txt", "null")) {
assertArrayEquals(bytes("null version"), read.body().readAllBytes());
}
s3Service.copyObject("bucket", "old.txt", "bucket", "copy.txt", "null", new CopyObjectOptions());
assertArrayEquals(bytes("null version"), s3Service.getObject("bucket", "copy.txt").getData());
}
@Test
void aKeyWithoutANullVersionIsNeitherStreamedNorCopied() {
for (Executable request : List.<Executable>of(
() -> s3Service.openObject("bucket", "new.txt", "null").close(),
() -> s3Service.copyObject("bucket", "new.txt", "bucket", "copy.txt", "null",
new CopyObjectOptions()))) {
AwsException e = assertThrows(AwsException.class, request);
assertEquals("NoSuchVersion", e.getErrorCode());
}
}
private static byte[] bytes(String text) {
return text.getBytes(StandardCharsets.UTF_8);
}
}
@@ -583,6 +583,95 @@ class S3VersioningIntegrationTest {
}
}
@Test
@Order(24)
void versionIdNullNamesOnlyTheNullVersionOnEveryObjectOperation() {
String bucket = "versioned-null-version-test";
given().when().put("/" + bucket).then().statusCode(200);
// ACLs enabled, so the ACL requests below reach the version they name.
given().body("<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>")
.when().put("/" + bucket + "?ownershipControls").then().statusCode(200);
given().body("old").when().put("/" + bucket + "/old").then().statusCode(200);
given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning").then().statusCode(200);
given().body("new").when().put("/" + bucket + "/new").then().statusCode(200);
// "old", written before versioning, is the null version.
String old = "/" + bucket + "/old";
given().queryParam("versionId", "null").when().get(old).then().statusCode(200).body(equalTo("old"));
given().queryParam("versionId", "null").when().head(old).then().statusCode(200);
given().queryParam("versionId", "null").header("x-amz-object-attributes", "ObjectSize")
.when().get(old + "?attributes").then().statusCode(200)
.body(containsString("<ObjectSize>3</ObjectSize>"));
given().queryParam("versionId", "null").when().get(old + "?acl").then().statusCode(200);
given().queryParam("versionId", "null").header("x-amz-acl", "private")
.when().put(old + "?acl").then().statusCode(200);
given().queryParam("versionId", "null").queryParam("annotationName", "labels").body("x")
.when().put(old + "?annotation").then().statusCode(200);
given().queryParam("versionId", "null").queryParam("annotationName", "labels")
.when().get(old + "?annotation").then().statusCode(200);
given().header("x-amz-copy-source", "/" + bucket + "/old?versionId=null")
.when().put("/" + bucket + "/copy").then().statusCode(200);
given().when().get("/" + bucket + "/copy").then().statusCode(200).body(equalTo("old"));
// "new", written with versioning on, has no null version, and its current version is not
// one: each request naming the null version fails as for any missing version.
String added = "/" + bucket + "/new";
Matcher<String> noSuchVersion = containsString("<Code>NoSuchVersion</Code>");
given().queryParam("versionId", "null").when().get(added).then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").when().head(added).then().statusCode(404);
given().queryParam("versionId", "null").header("x-amz-object-attributes", "ObjectSize")
.when().get(added + "?attributes").then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").when().get(added + "?acl").then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").header("x-amz-acl", "private")
.when().put(added + "?acl").then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").queryParam("annotationName", "labels").body("x")
.when().put(added + "?annotation").then().statusCode(404)
.body(containsString("<Code>NoSuchKey</Code>"));
given().header("x-amz-copy-source", "/" + bucket + "/new?versionId=null")
.when().put("/" + bucket + "/copy-of-new").then().statusCode(404).body(noSuchVersion);
given().when().get(added).then().statusCode(200).body(equalTo("new"));
}
@Test
@Order(25)
void retentionAndLegalHoldFindTheNullVersion() {
// Object Lock enabled on a bucket that already holds a null version, which keeps it.
String bucket = "versioned-null-version-lock-test";
given().when().put("/" + bucket).then().statusCode(200);
given().body("old").when().put("/" + bucket + "/old").then().statusCode(200);
given().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning").then().statusCode(200);
given().body("<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled></ObjectLockConfiguration>")
.when().put("/" + bucket + "?object-lock").then().statusCode(200);
given().body("new").when().put("/" + bucket + "/new").then().statusCode(200);
String retention = "<Retention><Mode>GOVERNANCE</Mode>"
+ "<RetainUntilDate>2031-01-01T00:00:00Z</RetainUntilDate></Retention>";
String legalHold = "<LegalHold><Status>OFF</Status></LegalHold>";
String old = "/" + bucket + "/old";
given().queryParam("versionId", "null").body(retention).when().put(old + "?retention").then().statusCode(200);
given().queryParam("versionId", "null").when().get(old + "?retention")
.then().statusCode(200).body(containsString("<Mode>GOVERNANCE</Mode>"));
given().queryParam("versionId", "null").body(legalHold).when().put(old + "?legal-hold").then().statusCode(200);
given().queryParam("versionId", "null").when().get(old + "?legal-hold")
.then().statusCode(200).body(containsString("<Status>OFF</Status>"));
String added = "/" + bucket + "/new";
Matcher<String> noSuchVersion = containsString("<Code>NoSuchVersion</Code>");
given().queryParam("versionId", "null").body(retention).when().put(added + "?retention")
.then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").when().get(added + "?retention")
.then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").body(legalHold).when().put(added + "?legal-hold")
.then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").when().get(added + "?legal-hold")
.then().statusCode(404).body(noSuchVersion);
given().queryParam("versionId", "null").header("x-amz-bypass-governance-retention", "true")
.when().delete(old).then().statusCode(204);
}
private static String tagging(String value) {
return "<Tagging><TagSet><Tag><Key>version</Key><Value>" + value + "</Value></Tag></TagSet></Tagging>";
}
@@ -382,14 +382,12 @@ class S3VersioningServiceTest {
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"))) {
() -> s3Service.deleteObjectTagging("versioned-bucket", "new", "null"),
() -> s3Service.getObjectTagging("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.