Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

security(s3): harden path traversal defenses in S3Service and S3Controller (#508)

iam-ssrivastav65063144c90f
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
index 6833330745..c57c5274ad 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java
@@ -262,6 +262,7 @@ public class S3Controller {
@QueryParam("encoding-type") String encodingType,
@QueryParam("key-marker") String keyMarker,
@Context UriInfo uriInfo) {
+ validateRawUri();
try {
if (hasQueryParam(uriInfo, "uploads")) {
return handleListMultipartUploads(bucket);
@@ -1714,6 +1715,7 @@ public class S3Controller {
throw new AwsException("InvalidArgument",
"Bucket POST must contain a field named 'key'.", 400);
}
+ validateKeyNoTraversal(key);
if (fileData == null) {
throw new AwsException("InvalidArgument",
@@ -1988,7 +1990,35 @@ public class S3Controller {
return uriInfo.getPathParameters().getFirst("key");
}
String rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());
- return URLDecoder.decode(rawKey, StandardCharsets.UTF_8);
+ String key = URLDecoder.decode(rawKey, StandardCharsets.UTF_8);
+ validateKeyNoTraversal(key);
+ return key;
+ }
+
+ private void validateKeyNoTraversal(String key) {
+ if (key == null) return;
+ try {
+ // Use a dummy root to mirror the service pattern, allowing leading slashes but keeping them in sandbox
+ java.nio.file.Path dummyRoot = java.nio.file.Path.of("/s3-sandbox");
+ String safeKey = key;
+ while (safeKey.startsWith("/")) {
+ safeKey = safeKey.substring(1);
+ }
+ java.nio.file.Path resolved = dummyRoot.resolve(safeKey).normalize();
+ if (!resolved.startsWith(dummyRoot)) {
+ throw new AwsException("InvalidKey", "The specified key is invalid.", 400);
+ }
+ } catch (java.nio.file.InvalidPathException e) {
+ throw new AwsException("InvalidKey", "The specified key contains invalid characters.", 400);
+ }
+ }
+
+ private void validateRawUri() {
+ String rawUri = currentVertxRequest.getCurrent().request().uri();
+ String lower = rawUri.toLowerCase();
+ if (lower.contains("/..") || lower.contains("../") || lower.contains("%2e%2e") || lower.contains("%2e.") || lower.contains(".%2e")) {
+ throw new AwsException("InvalidKey", "The specified key is invalid.", 400);
+ }
}
private Response handleGetBucketWebsite(String bucket) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
index 09163e9f81..d68c6fd222 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
@@ -1811,11 +1811,33 @@ public class S3Service {
private static final String DATA_SUFFIX = ".s3data";
private Path resolveObjectPath(String bucketName, String key) {
- return dataRoot.resolve(bucketName).resolve(key + DATA_SUFFIX);
+ Path bucketDir = dataRoot.resolve(bucketName).normalize();
+
+ String safeKey = key;
+ while (safeKey.startsWith("/")) {
+ safeKey = safeKey.substring(1);
+ }
+
+ Path resolved = bucketDir.resolve(safeKey + DATA_SUFFIX).normalize();
+ if (!resolved.startsWith(bucketDir)) {
+ throw new AwsException("InvalidKey", "The specified key is invalid.", 400);
+ }
+ return resolved;
}
private Path resolveVersionedPath(String bucketName, String key, String versionId) {
- return dataRoot.resolve(".versions").resolve(bucketName).resolve(key).resolve(versionId + DATA_SUFFIX);
+ Path baseDir = dataRoot.resolve(".versions").resolve(bucketName).normalize();
+
+ String safeKey = key;
+ while (safeKey.startsWith("/")) {
+ safeKey = safeKey.substring(1);
+ }
+
+ Path resolved = baseDir.resolve(safeKey).resolve(versionId + DATA_SUFFIX).normalize();
+ if (!resolved.startsWith(baseDir)) {
+ throw new AwsException("InvalidKey", "The specified key is invalid.", 400);
+ }
+ return resolved;
}
private void writeVersionedFile(String bucketName, String key, String versionId, byte[] data) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
index 02bbbd148a..cad1ef961b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
@@ -146,6 +146,26 @@ class S3IntegrationTest {
.body(containsString("data/config.json"));
}
+ @Test
+ @Order(10)
+ void pathTraversalInUrlIsNormalizedByFramework() {
+ // Vertx normalizes raw `..` in URL paths before the application layer,
+ // so /test-bucket/../../secret.txt becomes /secret.txt at the framework level
+ // and routes to a bucket-level handler (not S3Service.putObject for test-bucket).
+ //
+ // The actual service-layer traversal guard (resolveObjectPath) is tested
+ // in S3ServiceTest.resolvePathWithTraversalThrows.
+ //
+ // Verify that the normalized path does NOT result in a 500 error.
+ given()
+ .contentType("text/plain")
+ .body("safe-data")
+ .when()
+ .put("/test-bucket/../../secret.txt")
+ .then()
+ .statusCode(not(equalTo(500)));
+ }
+
@Test
@Order(11)
void listObjectsWithPrefix() {
@@ -1418,4 +1438,39 @@ class S3IntegrationTest {
given().when().delete("/" + bucket);
}
+ @Test
+ void pathTraversalAttemptsReturn400() {
+ // 1. URL-encoded dots survival through Vertx but decoded by our extractObjectKey
+ given()
+ .urlEncodingEnabled(false)
+ .pathParam("bucket", "test-bucket")
+ .when()
+ .get("/{bucket}/%2e%2e/%2e%2e/secret.txt")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidKey"));
+
+ // 2. Null byte (survives URL-decoding but fails java.nio.file.Path validation)
+ given()
+ .urlEncodingEnabled(false)
+ .pathParam("bucket", "test-bucket")
+ .when()
+ .get("/{bucket}/%00.txt")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidKey"));
+
+ // 3. Mixed-case percent-encoded traversal (%2E instead of %2e)
+ // Absolute paths like //etc/passwd are normalized by the HTTP server
+ // before reaching the controller, so they can't be tested via HTTP.
+ // They are caught at the service layer by the startsWith(bucketDir) guard.
+ given()
+ .urlEncodingEnabled(false)
+ .pathParam("bucket", "test-bucket")
+ .when()
+ .get("/{bucket}/%2E%2E/%2E%2E/secret.txt")
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidKey"));
+ }
}
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.java
index 3b56092400..7b8da2e67f 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
@@ -478,4 +478,32 @@ class S3ServiceTest {
assertFalse(page2.isTruncated());
assertEquals("c.txt", page2.objects().get(0).getKey());
}
+
+ @Test
+ void resolvePathWithTraversalThrows() {
+ s3Service.createBucket("test-bucket", "us-east-1");
+
+ // Blocked: going above the bucket root
+ AwsException ex = assertThrows(AwsException.class, () ->
+ s3Service.putObject("test-bucket", "../outside.txt", "data".getBytes(), null, null));
+ assertEquals("InvalidKey", ex.getErrorCode());
+
+ // Blocked: deeper traversal
+ assertThrows(AwsException.class, () ->
+ s3Service.getObject("test-bucket", "dir/../../../etc/passwd"));
+ }
+
+ @Test
+ void putObjectWithInternalTraversalStaysWithinBucket() {
+ s3Service.createBucket("test-bucket", "us-east-1");
+ byte[] data = "safe-content".getBytes();
+
+ // Allowed: traversal that normalizes to a path still inside the bucket
+ assertDoesNotThrow(() ->
+ s3Service.putObject("test-bucket", "docs/../file.txt", data, null, null));
+
+ // Retrieve using the same literal key (S3 keys are opaque strings)
+ S3Object got = s3Service.getObject("test-bucket", "docs/../file.txt");
+ assertArrayEquals(data, got.getData());
+ }
}