mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): verify SigV4 signature on presigned URLs when enforce-auth is enabled (#1842)
* fix(s3): verify SigV4 signature on presigned URLs when enforce-auth is enabled * test(s3): use real SigV4 signatures in presigned URL auth enforcement tests * test(s3): assert tampered presigned signature is rejected * fix(s3): construct presigned canonical query per SigV4 encoding rules * test(s3): cover presigned canonical query edge cases Extract canonical query construction into a package-private buildCanonicalQueryString method and add unit tests for the SigV4 encoding, ordering, empty-value, duplicate-name, and signature-exclusion branches. * test(s3): add SDK-signed presigned URL regression tests Add integration tests that generate presigned URLs with the AWS SDK S3Presigner (an independent signer) under enforce-auth: both original URLs succeed, transplanting object A's path onto object B's query is rejected with 403 SignatureDoesNotMatch, reordered and special-character queries still verify, expired URLs and malformed credentials are rejected, and an IAM-issued access key verifies. Adds the aws-sdk s3 test dependency. * fix(s3): collapse sequential spaces in signed header values per SigV4 Canonical values for non-host signed headers were only trimmed. SigV4 also collapses sequential spaces to a single space, so a valid presigned request carrying such a header could be rejected. Extract canonicalizeHeaderValue and add unit coverage. * style(s3): log caught exception in presigned SigV4 verification The catch block silently returned false. Log at debug per repo style so a swallowed exception can be diagnosed. * test(s3): move SDK presigned URL tests to compatibility-tests Move S3PresignedUrlSdkIntegrationTest from the main project to compatibility-tests/sdk-test-java and remove the aws-sdk s3 test dependency from the main pom.xml per CONTRIBUTING.md guidelines. * fix(s3): read X-Amz-Content-Sha256 instead of hardcoding UNSIGNED-PAYLOAD * fix(s3): reject presigned URLs with X-Amz-Expires outside 1-604800 * fix(s3): reject presigned URLs with unsupported X-Amz-Algorithm
This commit is contained in:
1 parent
e4010ec12f
commit
3fe836aa16
4 files changed
+761
-7
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class PreSignedUrlFilterTest {
|
||||
|
||||
private static MultivaluedMap<String, String> params() {
|
||||
return new MultivaluedHashMap<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortsByEncodedNameRegardlessOfInputOrder() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("X-Amz-Date", "20260101T000000Z");
|
||||
params.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
params.add("X-Amz-Expires", "300");
|
||||
|
||||
assertEquals(
|
||||
"X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20260101T000000Z&X-Amz-Expires=300",
|
||||
PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void encodesReservedCharactersAndSpacesPerSigV4() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("p", "a b+c/d%e");
|
||||
|
||||
// space -> %20 (not +), '+' -> %2B, '/' -> %2F, '%' -> %25, uppercase hex
|
||||
assertEquals("p=a%20b%2Bc%2Fd%25e", PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void encodesParameterNameAsWellAsValue() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("a b", "c/d");
|
||||
|
||||
assertEquals("a%20b=c%2Fd", PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void encodesNonAsciiValueByteByByte() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("p", "é€"); // é (C3 A9), € (E2 82 AC) in UTF-8
|
||||
|
||||
assertEquals("p=%C3%A9%E2%82%AC", PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void representsEmptyValueAsNameEquals() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("acl", "");
|
||||
|
||||
assertEquals("acl=", PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortsDuplicateNamesByEncodedValue() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("k", "b");
|
||||
params.add("k", "a");
|
||||
params.add("k", "10");
|
||||
|
||||
// code-point order on encoded values: "10" < "a" < "b" ('1' < 'a' < 'b')
|
||||
assertEquals("k=10&k=a&k=b", PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
|
||||
@Test
|
||||
void canonicalHeaderValueCollapsesSequentialSpaces() {
|
||||
assertEquals("a b", PreSignedUrlFilter.canonicalizeHeaderValue("a b"));
|
||||
assertEquals("a b", PreSignedUrlFilter.canonicalizeHeaderValue("a b"));
|
||||
assertEquals("a b", PreSignedUrlFilter.canonicalizeHeaderValue(" a b "));
|
||||
assertEquals("", PreSignedUrlFilter.canonicalizeHeaderValue(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void excludesSignatureButKeepsEveryOtherParameter() {
|
||||
MultivaluedMap<String, String> params = params();
|
||||
params.add("X-Amz-Signature", "deadbeef");
|
||||
params.add("X-Amz-Date", "20260101T000000Z");
|
||||
params.add("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
|
||||
assertEquals(
|
||||
"X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20260101T000000Z",
|
||||
PreSignedUrlFilter.buildCanonicalQueryString(params));
|
||||
}
|
||||
}
|
||||
+200
-4
@@ -8,10 +8,17 @@ import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -156,15 +163,17 @@ class S3AuthEnforcementIntegrationTest {
|
||||
@Test
|
||||
@Order(8)
|
||||
void presignedRequestWithBadAccessKeyCannotUsePublicAccess() {
|
||||
String path = "/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY;
|
||||
String sig = presignedSignature("GET", path, "bad-key", "bad-key", "3600");
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("bad-key"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "test")
|
||||
.queryParam("X-Amz-Signature", sig)
|
||||
.when()
|
||||
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("InvalidAccessKeyId"));
|
||||
@@ -198,15 +207,17 @@ class S3AuthEnforcementIntegrationTest {
|
||||
@Test
|
||||
@Order(11)
|
||||
void presignedRequestWithLocalAccessKeyCanReadPrivateObject() {
|
||||
String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
|
||||
String sig = presignedSignature("GET", path, "test", "test", "3600");
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "test")
|
||||
.queryParam("X-Amz-Signature", sig)
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("private body"));
|
||||
@@ -526,6 +537,121 @@ class S3AuthEnforcementIntegrationTest {
|
||||
.body(equalTo("versioned body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void presignedRequestWithExplicitContentSha256IsAccepted() throws Exception {
|
||||
String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
|
||||
String contentHash = sha256Hex("private body");
|
||||
String sig = presignedSignature("GET", path, "test", "test", "3600", contentHash);
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Content-Sha256", contentHash)
|
||||
.queryParam("X-Amz-Signature", sig)
|
||||
.when()
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("private body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void presignedRequestWithMismatchedContentSha256IsRejected() throws Exception {
|
||||
String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
|
||||
String contentHash = sha256Hex("private body");
|
||||
String wrongHash = sha256Hex("wrong body");
|
||||
String sig = presignedSignature("GET", path, "test", "test", "3600", contentHash);
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Content-Sha256", wrongHash)
|
||||
.queryParam("X-Amz-Signature", sig)
|
||||
.when()
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("SignatureDoesNotMatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(24)
|
||||
void presignedRequestWithTamperedSignatureIsRejected() {
|
||||
String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
|
||||
String signature = presignedSignature("GET", path, "test", "test", "3600");
|
||||
String tampered = signature.substring(0, signature.length() - 1)
|
||||
+ (signature.endsWith("0") ? "1" : "0");
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", tampered)
|
||||
.when()
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("SignatureDoesNotMatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(25)
|
||||
void presignedRequestWithExpiresExceedingMaxIsRejected() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "604801")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "dummy")
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("AuthorizationQueryParametersError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(26)
|
||||
void presignedRequestWithExpiresZeroIsRejected() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "0")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "dummy")
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("AuthorizationQueryParametersError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(27)
|
||||
void presignedRequestWithUnsupportedAlgorithmIsRejected() {
|
||||
given()
|
||||
.queryParam("X-Amz-Algorithm", "BOGUS-ALGORITHM")
|
||||
.queryParam("X-Amz-Credential", credential("test"))
|
||||
.queryParam("X-Amz-Date", SIGNING_TIMESTAMP)
|
||||
.queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host")
|
||||
.queryParam("X-Amz-Signature", "dummy")
|
||||
.when()
|
||||
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("AuthorizationQueryParametersError"));
|
||||
}
|
||||
|
||||
private static String publicReadPolicy(String bucket) {
|
||||
return """
|
||||
{
|
||||
@@ -557,6 +683,76 @@ class S3AuthEnforcementIntegrationTest {
|
||||
return accessKeyId + "/" + SIGNING_DATE + "/us-east-1/s3/aws4_request";
|
||||
}
|
||||
|
||||
private static String presignedSignature(String method, String path,
|
||||
String accessKeyId, String secretKey, String expires) {
|
||||
return presignedSignature(method, path, accessKeyId, secretKey, expires, "UNSIGNED-PAYLOAD");
|
||||
}
|
||||
|
||||
private static String presignedSignature(String method, String path,
|
||||
String accessKeyId, String secretKey,
|
||||
String expires, String payloadHash) {
|
||||
try {
|
||||
String credentialScope = SIGNING_DATE + "/us-east-1/s3/aws4_request";
|
||||
String encodedCredential = URLEncoder.encode(
|
||||
accessKeyId + "/" + credentialScope, StandardCharsets.UTF_8);
|
||||
|
||||
// Build query string in sorted order (excluding Signature)
|
||||
String contentSha256Param = "UNSIGNED-PAYLOAD".equals(payloadHash)
|
||||
? "" : "&X-Amz-Content-Sha256=" + payloadHash;
|
||||
String canonicalQueryString = "X-Amz-Algorithm=AWS4-HMAC-SHA256"
|
||||
+ contentSha256Param
|
||||
+ "&X-Amz-Credential=" + encodedCredential
|
||||
+ "&X-Amz-Date=" + SIGNING_TIMESTAMP
|
||||
+ "&X-Amz-Expires=" + expires
|
||||
+ "&X-Amz-SignedHeaders=host";
|
||||
|
||||
String canonicalRequest = method + "\n"
|
||||
+ path + "\n"
|
||||
+ canonicalQueryString + "\n"
|
||||
+ "host:localhost:" + io.restassured.RestAssured.port + "\n\n"
|
||||
+ "host\n"
|
||||
+ payloadHash;
|
||||
|
||||
String stringToSign = "AWS4-HMAC-SHA256\n"
|
||||
+ SIGNING_TIMESTAMP + "\n"
|
||||
+ credentialScope + "\n"
|
||||
+ sha256Hex(canonicalRequest);
|
||||
|
||||
byte[] signingKey = deriveSigningKey(secretKey, SIGNING_DATE, "us-east-1", "s3");
|
||||
return hexEncode(hmacSha256(signingKey, stringToSign));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] deriveSigningKey(String secretKey, String date, String region,
|
||||
String service) throws Exception {
|
||||
byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);
|
||||
byte[] kDate = hmacSha256(kSecret, date);
|
||||
byte[] kRegion = hmacSha256(kDate, region);
|
||||
byte[] kService = hmacSha256(kRegion, service);
|
||||
return hmacSha256(kService, "aws4_request");
|
||||
}
|
||||
|
||||
private static byte[] hmacSha256(byte[] key, String data) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(key, "HmacSHA256"));
|
||||
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String sha256Hex(String input) throws Exception {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
private static String hexEncode(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String websiteConfiguration() {
|
||||
return websiteConfiguration(null);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user