mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(auth): reject ambiguous SigV4 authorization headers (#8332)
* fix(auth): reject ambiguous SigV4 authorization headers Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
5df26c5135
commit
e182cdad82
3 files changed
+242
-1
No files matched your search
@@ -22,6 +22,8 @@ public record SigV4AuthorizationHeader(String credential, String signedHeaders,
|
||||
* algorithm. A parameter missing from an otherwise well-formed header comes back null on the
|
||||
* record instead, left for the caller to reject alongside whatever else it requires (e.g. an
|
||||
* {@code X-Amz-Date} it reads from a different source than this header).
|
||||
* Duplicate parameters and conflicting or incomplete repeated headers return a record with
|
||||
* null parameters. Equivalent complete repeated headers resolve to the same parameters.
|
||||
*/
|
||||
public static SigV4AuthorizationHeader parse(String authorization) {
|
||||
return parse(authorization, ALGORITHM);
|
||||
@@ -37,16 +39,48 @@ public record SigV4AuthorizationHeader(String credential, String signedHeaders,
|
||||
return null;
|
||||
}
|
||||
Map<String, String> parameters = new LinkedHashMap<>();
|
||||
Map<String, String> firstHeader = null;
|
||||
boolean missingEqualSign = false;
|
||||
for (String part : trimmed.substring(algorithm.length()).split(",")) {
|
||||
part = part.trim();
|
||||
if (startsHeader(part, algorithm)) {
|
||||
if (missingEqualSign || !complete(parameters)
|
||||
|| firstHeader != null && !firstHeader.equals(parameters)) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
firstHeader = parameters;
|
||||
parameters = new LinkedHashMap<>();
|
||||
part = part.substring(algorithm.length()).trim();
|
||||
}
|
||||
int equals = part.indexOf('=');
|
||||
if (equals > 0) {
|
||||
parameters.put(part.substring(0, equals).trim(), part.substring(equals + 1).trim());
|
||||
String name = part.substring(0, equals).trim();
|
||||
if (parameters.putIfAbsent(name, part.substring(equals + 1).trim()) != null) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
} else if (equals < 0 && !part.isBlank()) {
|
||||
missingEqualSign = true;
|
||||
}
|
||||
}
|
||||
if (firstHeader != null && (missingEqualSign || !complete(parameters) || !firstHeader.equals(parameters))) {
|
||||
return incomplete(missingEqualSign);
|
||||
}
|
||||
return new SigV4AuthorizationHeader(parameters.get("Credential"), parameters.get("SignedHeaders"),
|
||||
parameters.get("Signature"), missingEqualSign);
|
||||
}
|
||||
|
||||
private static boolean startsHeader(String part, String algorithm) {
|
||||
return part.regionMatches(true, 0, algorithm, 0, algorithm.length())
|
||||
&& (part.length() == algorithm.length() || Character.isWhitespace(part.charAt(algorithm.length())));
|
||||
}
|
||||
|
||||
private static boolean complete(Map<String, String> parameters) {
|
||||
return !parameters.getOrDefault("Credential", "").isBlank()
|
||||
&& !parameters.getOrDefault("SignedHeaders", "").isBlank()
|
||||
&& !parameters.getOrDefault("Signature", "").isBlank();
|
||||
}
|
||||
|
||||
private static SigV4AuthorizationHeader incomplete(boolean missingEqualSign) {
|
||||
return new SigV4AuthorizationHeader(null, null, null, missingEqualSign);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import io.floci.aws.testing.ValidateSignaturesProfile;
|
||||
import io.floci.aws.testutil.AwsRequestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ValidateSignaturesProfile.class)
|
||||
class SigV4HeaderAmbiguityIntegrationTest {
|
||||
private static final byte[] BODY = "{}".getBytes(StandardCharsets.UTF_8);
|
||||
private static final String TARGET = "AmazonSQS.ListQueues";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@Test
|
||||
void aSingleSignedHeaderRemainsAuthenticated() throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
assertEquals(200, send(signed, List.of(signed.get("Authorization"))).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void identicalPhysicalAuthorizationHeadersRemainAuthenticated() throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
String authorization = signed.get("Authorization");
|
||||
assertEquals(200, send(signed, List.of(authorization, authorization)).statusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void identicalCombinedAuthorizationHeadersRemainAuthenticated() throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
String authorization = signed.get("Authorization");
|
||||
assertEquals(200, send(signed, List.of(authorization + ", " + authorization)).statusCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Credential", "SignedHeaders", "Signature"})
|
||||
void duplicateParametersAreRejectedEvenWhenTheLastValueWouldVerify(String parameter) throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
String authorization = signed.get("Authorization");
|
||||
int start = authorization.indexOf(parameter + "=") + parameter.length() + 1;
|
||||
int end = authorization.indexOf(',', start);
|
||||
String value = authorization.substring(start, end < 0 ? authorization.length() : end).trim();
|
||||
assertIncomplete(send(signed, List.of(authorization + ", " + parameter + "=" + value)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aValidLaterHeaderCannotHideAnEarlierInvalidSignature() throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
String authorization = signed.get("Authorization");
|
||||
String first = authorization.substring(0, authorization.indexOf("Signature="))
|
||||
+ "Signature=" + "0".repeat(64);
|
||||
assertIncomplete(send(signed, List.of(first, authorization)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCompleteHeaderCannotFillTheMissingFieldsOfAnotherHeader() throws Exception {
|
||||
Map<String, String> signed = signed();
|
||||
String authorization = signed.get("Authorization");
|
||||
String incomplete = authorization.substring(0, authorization.indexOf(','));
|
||||
assertIncomplete(send(signed, List.of(incomplete, authorization)));
|
||||
}
|
||||
|
||||
private Map<String, String> signed() throws Exception {
|
||||
return AwsRequestSigner.signedAs("test", "test", "sqs")
|
||||
.headersFor("POST", endpoint, Map.of("x-amz-target", TARGET), BODY);
|
||||
}
|
||||
|
||||
private HttpResponse<String> send(Map<String, String> signed, List<String> authorizations) throws Exception {
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(endpoint)
|
||||
.timeout(Duration.ofSeconds(15)).version(HttpClient.Version.HTTP_1_1)
|
||||
.header("Content-Type", "application/x-amz-json-1.0")
|
||||
.header("X-Amz-Target", TARGET).header("X-Amz-Date", signed.get("X-Amz-Date"))
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(BODY));
|
||||
authorizations.forEach(value -> request.header("Authorization", value));
|
||||
try (HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build()) {
|
||||
return client.send(request.build(), HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertIncomplete(HttpResponse<String> response) {
|
||||
assertEquals(400, response.statusCode(), response.body());
|
||||
assertTrue(response.body().contains("IncompleteSignatureException"), response.body());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package io.floci.aws.core.common.auth;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SigV4AuthorizationHeaderServiceTest {
|
||||
private static final String ALGORITHM = "AWS4-HMAC-SHA256";
|
||||
private static final String CREDENTIAL = "FIRST/20261113/us-east-1/sqs/aws4_request";
|
||||
private static final String HEADERS = "host;x-amz-date";
|
||||
private static final String SIGNATURE = "a".repeat(64);
|
||||
private static final String AUTHORIZATION = ALGORITHM + " Credential=" + CREDENTIAL
|
||||
+ ", SignedHeaders=" + HEADERS + ", Signature=" + SIGNATURE;
|
||||
|
||||
@Test
|
||||
void singleCompleteHeaderKeepsItsParameters() {
|
||||
assertEquals(new SigV4AuthorizationHeader(CREDENTIAL, HEADERS, SIGNATURE, false),
|
||||
SigV4AuthorizationHeader.parse(AUTHORIZATION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void identicalCompleteHeaderRepetitionsKeepOneIdentity() {
|
||||
assertEquals(SigV4AuthorizationHeader.parse(AUTHORIZATION),
|
||||
SigV4AuthorizationHeader.parse(AUTHORIZATION + ", " + AUTHORIZATION + ", " + AUTHORIZATION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equivalentCompleteHeadersMayOrderTheirParametersDifferently() {
|
||||
String reordered = ALGORITHM + " Signature=" + SIGNATURE
|
||||
+ ", Credential=" + CREDENTIAL + ", SignedHeaders=" + HEADERS;
|
||||
assertEquals(SigV4AuthorizationHeader.parse(AUTHORIZATION),
|
||||
SigV4AuthorizationHeader.parse(AUTHORIZATION + ", " + reordered));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"Credential, FIRST/20261113/us-east-1/sqs/aws4_request",
|
||||
"Credential, SECOND/20261113/us-east-1/sqs/aws4_request",
|
||||
"SignedHeaders, host;x-amz-date",
|
||||
"SignedHeaders, host",
|
||||
"Signature, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"Signature, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
})
|
||||
void duplicateParametersCannotReplaceEarlierValues(String name, String value) {
|
||||
assertIncomplete(AUTHORIZATION + ", " + name + "=" + value, ALGORITHM);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Credential", "SignedHeaders", "Signature"})
|
||||
void differentCompleteHeaderCannotSupplyAnotherIdentityOrSignature(String parameter) {
|
||||
String second = switch (parameter) {
|
||||
case "Credential" -> AUTHORIZATION.replace("FIRST/", "SECOND/");
|
||||
case "SignedHeaders" -> AUTHORIZATION.replace(HEADERS, "host");
|
||||
default -> AUTHORIZATION.replace(SIGNATURE, "b".repeat(64));
|
||||
};
|
||||
assertIncomplete(AUTHORIZATION + ", " + second, ALGORITHM);
|
||||
assertIncomplete(second + ", " + AUTHORIZATION, ALGORITHM);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"Credential=FIRST/20261113/us-east-1/sqs/aws4_request",
|
||||
"SignedHeaders=host;x-amz-date",
|
||||
"Signature=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
""
|
||||
})
|
||||
void partialHeadersCannotBorrowParametersFromAnotherHeader(String parameters) {
|
||||
String partial = ALGORITHM + " " + parameters;
|
||||
assertIncomplete(partial + ", " + AUTHORIZATION, ALGORITHM);
|
||||
assertIncomplete(AUTHORIZATION + ", " + partial, ALGORITHM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchingOuterHeadersCannotHideAConflictingMiddleHeader() {
|
||||
String conflicting = AUTHORIZATION.replace("FIRST/", "SECOND/");
|
||||
assertIncomplete(AUTHORIZATION + ", " + conflicting + ", " + AUTHORIZATION, ALGORITHM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sigV4aUsesTheSameDuplicateParameterBoundary() {
|
||||
String algorithm = "AWS4-ECDSA-P256-SHA256";
|
||||
String value = AUTHORIZATION.replace(ALGORITHM, algorithm).replace("/us-east-1/", "/");
|
||||
assertEquals(SigV4AuthorizationHeader.parse(value, algorithm),
|
||||
SigV4AuthorizationHeader.parse(value + ", " + value, algorithm));
|
||||
assertIncomplete(value + ", Signature=" + SIGNATURE, algorithm);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnrecognizedAuthenticationSchemeRemainsUnrecognized() {
|
||||
assertNull(SigV4AuthorizationHeader.parse(null));
|
||||
assertNull(SigV4AuthorizationHeader.parse("Bearer example"));
|
||||
}
|
||||
|
||||
private static void assertIncomplete(String value, String algorithm) {
|
||||
SigV4AuthorizationHeader parsed = SigV4AuthorizationHeader.parse(value, algorithm);
|
||||
// A recognized malformed scheme must reach rejection, not an unsigned fallback.
|
||||
assertNotNull(parsed);
|
||||
assertNull(parsed.credential());
|
||||
assertNull(parsed.signedHeaders());
|
||||
assertNull(parsed.signature());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user