Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): reject reserved storage bucket names (#4289)
XuJianb986b95074ef
diff --git a/docs/services/s3.md b/docs/services/s3.mdindex f1f1e1f9f8..891904aa0b 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -194,9 +194,10 @@ When `FLOCI_SERVICES_ATHENA_MOCK=true` is set, Athena queries are stubbed but flFloci does not hold bucket names to AWS's full DNS naming rules, so a name AWS would refuse isusually accepted here. The exception is a name that would not stay a single directory under its-account's storage root: an empty name, `.`, `..`, `.accounts`, or a name containing a path-separator is rejected with `InvalidBucketName`. On the persistent and hybrid backends such a name-climbs out of the owning account's directory, and that directory is the account boundary.+account's storage root: an empty name, `.`, `..`, a name containing a path separator, or one of+Floci's reserved storage directories (`.accounts`, `.versions`, `.annotations`) is rejected with+`InvalidBucketName`. On the persistent and hybrid backends such a name either climbs out of the+owning account's directory or collides with object version and annotation data.## Global Bucket Namespacediff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.javaindex 3b26eefc2f..050ca261f7 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java@@ -4734,11 +4734,14 @@ public class S3Service implements Resettable, ResourceProvider {// making dataRoot/<accountId>/... indistinguishable from the legacy dataRoot/<bucket>/...// layout. A leading "." keeps this namespace unreachable by any real bucket name.private static final String ACCOUNT_STORAGE_ROOT = ".accounts";+ private static final String VERSION_STORAGE_ROOT = ".versions";+ private static final Set<String> RESERVED_BUCKET_NAMES = Set.of(+ ACCOUNT_STORAGE_ROOT, VERSION_STORAGE_ROOT, ANNOTATION_STORAGE_ROOT);/*** Floci does not hold bucket names to AWS's DNS rules, but a name must still be one directory:- * on the disk-backed stores anything else climbs out of the owning account's directory, and- * that directory is the account boundary.+ * on the disk-backed stores anything else climbs out of the owning account's directory or+ * collides with Floci's reserved storage directories.*/private static void requirePathSafeBucketName(String bucketName) {boolean pathSafe = bucketName != null@@ -4748,7 +4751,7 @@ public class S3Service implements Resettable, ResourceProvider {&& bucketName.indexOf('\0') < 0&& !".".equals(bucketName)&& !"..".equals(bucketName)- && !ACCOUNT_STORAGE_ROOT.equals(bucketName);+ && !RESERVED_BUCKET_NAMES.contains(bucketName);if (!pathSafe) {throw new AwsException("InvalidBucketName", "The specified bucket is not valid.", 400);}@@ -4759,6 +4762,7 @@ public class S3Service implements Resettable, ResourceProvider {* resolved path, not the name, so a bucket persisted before the name was refused is caught too.*/private static Path bucketDirectory(Path parent, String bucketName) {+ requirePathSafeBucketName(bucketName);Path resolved = parent.resolve(bucketName).normalize();if (!parent.normalize().equals(resolved.getParent())) {throw new AwsException("InvalidBucketName", "The specified bucket is not valid.", 400);@@ -4834,7 +4838,7 @@ public class S3Service implements Resettable, ResourceProvider {private Path resolveVersionedPath(String accountId, String bucketName, String key, String versionId) {Path baseDir = bucketDirectory(- dataRoot.resolve(ACCOUNT_STORAGE_ROOT).resolve(accountId).resolve(".versions"), bucketName);+ dataRoot.resolve(ACCOUNT_STORAGE_ROOT).resolve(accountId).resolve(VERSION_STORAGE_ROOT), bucketName);String safeKey = key;while (safeKey.startsWith("/")) {@@ -4853,7 +4857,7 @@ public class S3Service implements Resettable, ResourceProvider {while (safeKey.startsWith("/")) {safeKey = safeKey.substring(1);}- return dataRoot.resolve(".versions").resolve(bucketName).normalize()+ return dataRoot.resolve(VERSION_STORAGE_ROOT).resolve(bucketName).normalize().resolve(safeKey).resolve(versionId + DATA_SUFFIX);}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketNameTraversalIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketNameTraversalIntegrationTest.javaindex 280e62f3b2..76b5528ea8 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketNameTraversalIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketNameTraversalIntegrationTest.java@@ -25,12 +25,13 @@ class S3BucketNameTraversalIntegrationTest {private static final String ATTACKER ="AWS4-HMAC-SHA256 Credential=000000000012/20260215/us-east-1/s3/aws4_request, SignedHeaders=host, Signature=abc";- @Test- void createBucketRefusesTheAccountStorageRoot() {+ @ParameterizedTest+ @ValueSource(strings = {".accounts", ".versions", ".annotations"})+ void createBucketRefusesReservedStorageRoots(String bucketName) {given().header("Authorization", ATTACKER).when()- .put("/.accounts")+ .put("/" + bucketName).then().statusCode(400).body(containsString("InvalidBucketName"));diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.javaindex b22a5ed620..45195af6d1 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ServiceTest.java@@ -205,7 +205,9 @@ class S3ServiceTest {}@ParameterizedTest- @ValueSource(strings = {"..", ".", "../", "a/b", "../victim", ".accounts", "a\\b", " "})+ @ValueSource(strings = {+ "..", ".", "../", "a/b", "../victim", ".accounts", ".versions", ".annotations", "a\\b", " "+ })void createBucketRejectsANameThatWouldNotStayInsideTheAccountDirectory(String bucketName) {AwsException ex = assertThrows(AwsException.class,() -> s3Service.createBucket(bucketName, "us-east-1"), bucketName);@@ -228,6 +230,31 @@ class S3ServiceTest {assertEquals("InvalidBucketName", put.getErrorCode());}+ @Test+ void persistedReservedBucketCannotOverwriteAnotherBucketsVersion() {+ InMemoryStorage<String, Bucket> bucketStore = new InMemoryStorage<>();+ InMemoryStorage<String, S3Object> objectStore = new InMemoryStorage<>();+ Path dataRoot = tempDir.resolve("reserved-s3");+ S3Service service = new S3Service(bucketStore, objectStore, dataRoot, false);+ byte[] original = "original".getBytes(StandardCharsets.UTF_8);++ service.createBucket("victim-bucket", "us-east-1");+ service.putBucketVersioning("victim-bucket", "Enabled");+ S3Object version = service.putObject(+ "victim-bucket", "document.txt", original, "text/plain", null);++ // Simulate a bucket record persisted before reserved names were rejected.+ bucketStore.put(".versions", new Bucket(".versions"));+ String collidingKey = "victim-bucket/document.txt/" + version.getVersionId();++ AwsException put = assertThrows(AwsException.class, () -> service.putObject(+ ".versions", collidingKey, "tampered".getBytes(StandardCharsets.UTF_8), "text/plain", null));++ assertEquals("InvalidBucketName", put.getErrorCode());+ assertArrayEquals(original,+ service.getObject("victim-bucket", "document.txt", version.getVersionId()).getData());+ }+@Testvoid aBucketNamedLikeADotDirectoryStillResolvesInsideTheAccount() {// ".hidden" normalises to a real child, unlike "..".@@ -1278,4 +1305,3 @@ class S3ServiceTest {assertDoesNotThrow(() -> s3Service.authorizeSignedDeleteObject("ASIAFAKEKEY00000001", "sessiontoken", "signed-del-bucket", "some/key"));}}-