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

@@ -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"));
}
}