refactor(auth): dedupe SigV4 header-verification helpers (#3706)

ExecuteApiSigV4Authorizer (API Gateway) and S3HeaderSignatureFilter
(S3) each carried their own copy of containsHeader/isSha256Hex, a
20-line block found duplicated verbatim via CPD (#3700). Moved both
into SigV4RequestValidator in core.common.auth, alongside the other
shared SigV4 primitives (sha256Hex, hmacSha256, deriveSigningKey)
that both callers already use from there, and pointed both call sites
at the shared version. Added direct unit coverage for the two moved
methods, matching this class's existing pattern of pinning shared
primitives independently of any one caller's request flow.

No behavior change: the moved logic is identical, only its location
and visibility changed.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-15 21:56:52 -05:00
1 parent 3641a074dc
commit 8a074fcddd
4 files changed
+45 -47

No files matched your search

@@ -189,6 +189,29 @@ public final class SigV4RequestValidator {
return value == null ? "" : value.trim().replaceAll("\\s+", " ");
}
/** Whether {@code name} appears among the semicolon-separated headers of {@code SignedHeaders}. */
public static boolean containsHeader(String signedHeaders, String name) {
for (String header : signedHeaders.split(";")) {
if (name.equals(header)) {
return true;
}
}
return false;
}
/** Whether {@code value} looks like a SHA-256 hex digest: 64 hex characters. */
public static boolean isSha256Hex(String value) {
if (value.length() != 64) {
return false;
}
for (int index = 0; index < value.length(); index++) {
if (Character.digit(value.charAt(index), 16) < 0) {
return false;
}
}
return true;
}
/**
* Public so other SigV4 verifiers with a different canonical-request shape (e.g. S3's real
* REST request signing in {@code PreSignedUrlFilter}) can reuse the crypto primitives below
@@ -168,7 +168,7 @@ public class ExecuteApiSigV4Authorizer {
// signed is /{stage}/{route}, identical across every API on this emulator. Without host in
// the canonical request, a signature minted for one API replays against any other API that
// happens to expose the same stage and route.
if (!containsHeader(signed.signedHeaders(), "host")) {
if (!SigV4RequestValidator.containsHeader(signed.signedHeaders(), "host")) {
return Result.rejected(Failure.MALFORMED, "SignedHeaders does not cover host");
}
@@ -497,8 +497,8 @@ public class ExecuteApiSigV4Authorizer {
String signedHeaders) throws Exception {
String declared = headers == null ? null : headers.getHeaderString("x-amz-content-sha256");
if (declared != null && !declared.isBlank()
&& containsHeader(signedHeaders, "x-amz-content-sha256")
&& !isSha256Hex(declared.trim())) {
&& SigV4RequestValidator.containsHeader(signedHeaders, "x-amz-content-sha256")
&& !SigV4RequestValidator.isSha256Hex(declared.trim())) {
return declared.trim();
}
if (presigned) {
@@ -507,27 +507,6 @@ public class ExecuteApiSigV4Authorizer {
return SigV4RequestValidator.sha256Hex(body == null ? new byte[0] : body);
}
private static boolean containsHeader(String signedHeaders, String name) {
for (String header : signedHeaders.split(";")) {
if (name.equals(header)) {
return true;
}
}
return false;
}
private static boolean isSha256Hex(String value) {
if (value.length() != 64) {
return false;
}
for (int index = 0; index < value.length(); index++) {
if (Character.digit(value.charAt(index), 16) < 0) {
return false;
}
}
return true;
}
// ──────────────────────────── Crypto and encoding helpers ────────────────────────────
/** RFC 3986 percent-encoding as SigV4 defines it: {@code /} is escaped, {@code -._~} are not. */
@@ -157,7 +157,7 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
boolean matches;
try {
matches = containsHeader(signedHeaders, "host") && signatureMatches(
matches = SigV4RequestValidator.containsHeader(signedHeaders, "host") && signatureMatches(
ctx, secretKey.get(), scopeDate, region, amzDate, signedHeaders, declaredHash, signature);
} catch (Exception e) {
LOG.debugv(e, "S3 header SigV4 verification failed to complete for accessKey={0}", accessKeyId);
@@ -169,7 +169,7 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
return;
}
if (isSha256Hex(declaredHash) && !bodyMatches(ctx, declaredHash)) {
if (SigV4RequestValidator.isSha256Hex(declaredHash) && !bodyMatches(ctx, declaredHash)) {
abort(ctx, 400, "XAmzContentSHA256Mismatch",
"The provided 'x-amz-content-sha256' header does not match what was computed.");
}
@@ -266,27 +266,6 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
return null;
}
private static boolean containsHeader(String signedHeaders, String name) {
for (String header : signedHeaders.split(";")) {
if (name.equals(header)) {
return true;
}
}
return false;
}
private static boolean isSha256Hex(String value) {
if (value.length() != 64) {
return false;
}
for (int index = 0; index < value.length(); index++) {
if (Character.digit(value.charAt(index), 16) < 0) {
return false;
}
}
return true;
}
private static void abort(ContainerRequestContext ctx, int status, String code, String message) {
ctx.abortWith(PreSignedUrlFilter.errorResponse(status, code, message));
}
@@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Pins the shared SigV4 crypto primitives against AWS's own published test vector, independent
@@ -51,4 +53,19 @@ class SigV4RequestValidatorTest {
void hexEncodeLowercasesAndZeroPadsEachByte() {
assertEquals("00ff0a", SigV4RequestValidator.hexEncode(new byte[]{0x00, (byte) 0xFF, 0x0A}));
}
@Test
void containsHeaderMatchesAnyOfTheSemicolonSeparatedNames() {
assertTrue(SigV4RequestValidator.containsHeader("host;x-amz-date;x-amz-content-sha256", "host"));
assertTrue(SigV4RequestValidator.containsHeader("host;x-amz-date", "x-amz-date"));
assertFalse(SigV4RequestValidator.containsHeader("host;x-amz-date", "authorization"));
}
@Test
void isSha256HexRequiresExactlySixtyFourHexCharacters() {
assertTrue(SigV4RequestValidator.isSha256Hex(
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
assertFalse(SigV4RequestValidator.isSha256Hex("UNSIGNED-PAYLOAD"));
assertFalse(SigV4RequestValidator.isSha256Hex("e3b0c442"));
}
}