mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
security(s3): harden path traversal defenses in S3Service and S3Controller (#508)
- Add normalize() + startsWith(bucketDir) guards to resolveObjectPath and resolveVersionedPath in S3Service (primary defense) - Add controller-level validateKeyNoTraversal mirroring the service pattern with try/catch for InvalidPathException (null bytes → 400) - Add validateRawUri to catch percent-encoded traversal (%2e%2e) before Vert.x normalization strips it - Strip leading slashes from keys before filesystem resolution to allow legitimate S3 keys like /file.txt without escaping the bucket - Add integration tests for %2e%2e, %00, and %2E%2E vectors asserting 400 InvalidKey - Add unit tests for service-layer traversal rejection
This commit is contained in:
1 parent
247c62cd19
commit
65063144c9
4 files changed
+138
-3
No files matched your search
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user