fix(s3): keep a bucket's data inside its own account directory (#4244)

A bucket name went into the storage path unvalidated, so a bucket named .. resolved to the account storage root itself and the containment check then passed for any account's objects. Reject a name that is not a single directory at CreateBucket, and resolve every bucket path through a helper that asserts the directory sits directly under its account, which also covers a bucket persisted before the name was refused.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-23 18:07:48 -05:00
1 parent 34f4f72e82
commit 2e21c013c5
4 files changed
+154 -13

No files matched your search

@@ -0,0 +1,70 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
/**
* No spelling of a traversing bucket name reaches another account's objects. The status codes vary
* by spelling: JAX-RS answers a literal {@code ..} or {@code .} path itself, while the encoded
* forms reach the service undecoded and name an ordinary directory. {@code S3ServiceTest} pins the
* service guard directly, for the callers that never come through this router.
*/
@QuarkusTest
class S3BucketNameTraversalIntegrationTest {
private static final String VICTIM =
"AWS4-HMAC-SHA256 Credential=000000000011/20260215/us-east-1/s3/aws4_request, SignedHeaders=host, Signature=abc";
private static final String ATTACKER =
"AWS4-HMAC-SHA256 Credential=000000000012/20260215/us-east-1/s3/aws4_request, SignedHeaders=host, Signature=abc";
@Test
void createBucketRefusesTheAccountStorageRoot() {
given()
.header("Authorization", ATTACKER)
.when()
.put("/.accounts")
.then()
.statusCode(400)
.body(containsString("InvalidBucketName"));
}
@ParameterizedTest
@ValueSource(strings = {"..", ".", "%2E%2E", "..%2Fvictim"})
void noSpellingOfATraversalBucketReachesAnotherAccountsObject(String bucketName) {
given()
.header("Authorization", VICTIM)
.when()
.put("/traversal-victim-bucket")
.then()
.statusCode(anyOf(is(200), is(409)));
given()
.header("Authorization", VICTIM)
.body("victim-secret")
.when()
.put("/traversal-victim-bucket/secret.txt")
.then()
.statusCode(200);
given()
.header("Authorization", ATTACKER)
.when()
.put("/" + bucketName);
given()
.header("Authorization", ATTACKER)
.when()
.get("/" + bucketName + "/000000000011/traversal-victim-bucket/secret.txt")
.then()
.statusCode(not(200))
.body(not(containsString("victim-secret")));
}
}
@@ -17,6 +17,8 @@ import io.github.hectorvent.floci.services.s3.model.WebsiteConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -202,6 +204,39 @@ class S3ServiceTest {
assertArrayEquals(data, assertDoesNotThrow(() -> Files.readAllBytes(filePath)));
}
@ParameterizedTest
@ValueSource(strings = {"..", ".", "../", "a/b", "../victim", ".accounts", "a\\b", " "})
void createBucketRejectsANameThatWouldNotStayInsideTheAccountDirectory(String bucketName) {
AwsException ex = assertThrows(AwsException.class,
() -> s3Service.createBucket(bucketName, "us-east-1"), bucketName);
assertEquals("InvalidBucketName", ex.getErrorCode());
}
@Test
void objectPathsRefuseABucketNameThatEscapesTheAccountDirectory() {
InMemoryStorage<String, Bucket> bucketStore = new InMemoryStorage<>();
InMemoryStorage<String, S3Object> objectStore = new InMemoryStorage<>();
Path dataRoot = tempDir.resolve("escape-s3");
S3Service service = new S3Service(bucketStore, objectStore, dataRoot, false);
// Straight into the store, as a bucket persisted before the name was refused would be.
bucketStore.put("..", new Bucket(".."));
AwsException put = assertThrows(AwsException.class, () -> service.putObject(
"..", "000000000002/victim-bucket/secret.txt", "stolen".getBytes(StandardCharsets.UTF_8),
"text/plain", null));
assertEquals("InvalidBucketName", put.getErrorCode());
}
@Test
void aBucketNamedLikeADotDirectoryStillResolvesInsideTheAccount() {
// ".hidden" normalises to a real child, unlike "..".
s3Service.createBucket(".hidden", "us-east-1");
s3Service.putObject(".hidden", "k.txt", "v".getBytes(StandardCharsets.UTF_8), "text/plain", null);
assertTrue(Files.exists(tempDir.resolve("s3/.accounts/000000000000/.hidden/k.txt.s3data")));
}
@Test
void putObjectDoesNotRetainBytesInObjectStore() {
InMemoryStorage<String, Bucket> bucketStore = new InMemoryStorage<>();