mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user