perf(core): read the SigV4 credential scope without regular expressions (#7658)

* perf(core): read the SigV4 credential scope without regular expressions

Every request ran its Authorization header through regular expressions
five to eight times to read the access key, the region and the signing
service. The account id check String.matches("\\d{12}") also compiled a
new pattern twice per request.

SigV4CredentialScope, RegionResolver and AccountResolver now read the
common header shape with indexOf. Any other shape still goes through the
same regular expressions, so every result stays the same. The account id
check is a plain loop.

* perf(core): read the access key by hand and skip the regexes without a credential
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-06 08:56:43 -04:00
1 parent 7f43385349
commit cf4b3f0e82
8 files changed
+365 -10

No files matched your search

@@ -296,7 +296,7 @@ public class AccountContextFilter implements ContainerRequestFilter {
*/
private String resolveAccount(String akid, String resolvedDefault) {
if (akid != null) {
if (akid.matches("\\d{12}")) {
if (AccountResolver.isAccountId(akid)) {
return akid;
}
Optional<String> credentialAccount = sessionAccountLookup.resolveAccountId(akid);
@@ -10,7 +10,6 @@ import java.util.regex.Pattern;
@ApplicationScoped
public class AccountResolver {
private static final Pattern AKID_PATTERN = Pattern.compile("Credential=([^/]+)/");
private static final Pattern S3_SIGV2_AKID_PATTERN = Pattern.compile("AWS ([^\\s:]+):[^\\s:]+");
public static final String SIGV4_SCHEME = "AWS4-HMAC-SHA256";
private static final String SIGV4_PREFIX = SIGV4_SCHEME + " ";
@@ -27,6 +26,19 @@ public class AccountResolver {
this.defaultAccountId = defaultAccountId;
}
static boolean isAccountId(String value) {
if (value == null || value.length() != 12) {
return false;
}
for (int i = 0; i < 12; i++) {
char c = value.charAt(i);
if (c < '0' || '9' < c) {
return false;
}
}
return true;
}
/**
* Returns the account ID for the given Authorization header.
* When the access key ID is exactly 12 digits it is used directly as the account ID,
@@ -35,7 +47,7 @@ public class AccountResolver {
*/
public String resolve(String authorizationHeader) {
String akid = extractAccessKeyId(authorizationHeader);
if (akid != null && akid.matches("\\d{12}")) {
if (isAccountId(akid)) {
return akid;
}
return defaultAccountId;
@@ -53,8 +65,18 @@ public class AccountResolver {
if (!trimmed.startsWith(SIGV4_PREFIX) && !trimmed.startsWith(SIGV4A_PREFIX)) {
return null;
}
Matcher m = AKID_PATTERN.matcher(trimmed);
return m.find() ? m.group(1) : null;
for (int credential = trimmed.indexOf(SigV4CredentialScope.CREDENTIAL); credential >= 0;
credential = trimmed.indexOf(SigV4CredentialScope.CREDENTIAL, credential + 1)) {
int start = credential + SigV4CredentialScope.CREDENTIAL.length();
int slash = trimmed.indexOf('/', start);
if (slash < 0) {
return null;
}
if (start < slash) {
return trimmed.substring(start, slash);
}
}
return null;
}
/** Extracts S3 routing metadata without adding SigV2 authentication support. */
@@ -81,7 +103,7 @@ public class AccountResolver {
return defaultAccountId;
}
String akid = extractPresignedAccessKeyId(credentialValue);
if (akid != null && akid.matches("\\d{12}")) {
if (isAccountId(akid)) {
return akid;
}
return defaultAccountId;
@@ -1617,7 +1617,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
* default account when neither resolves.
*/
private String resolveCredentialAccountId(String akid, String authorizationHeader) {
if (akid != null && !akid.matches("\\d{12}")) {
if (akid != null && !AccountResolver.isAccountId(akid)) {
Optional<String> credentialAccount = sessionAccountLookup.resolveAccountId(akid);
if (credentialAccount.isPresent()) {
return credentialAccount.get();
@@ -104,6 +104,13 @@ public class RegionResolver {
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return defaultRegion;
}
String region = SigV4CredentialScope.segment(authorizationHeader, SigV4CredentialScope.Segment.THIRD);
if (region != null) {
return withoutPseudoRegion(region);
}
if (!SigV4CredentialScope.hasCredential(authorizationHeader)) {
return defaultRegion;
}
Matcher matcher = CREDENTIAL_REGION_PATTERN.matcher(authorizationHeader);
return matcher.find() ? withoutPseudoRegion(matcher.group(1)) : defaultRegion;
}
@@ -190,6 +197,13 @@ public class RegionResolver {
if (authorizationHeader == null || authorizationHeader.isEmpty()) {
return null;
}
String region = SigV4CredentialScope.segment(authorizationHeader, SigV4CredentialScope.Segment.THIRD);
if (region != null) {
return withoutPseudoRegion(region);
}
if (!SigV4CredentialScope.hasCredential(authorizationHeader)) {
return null;
}
Matcher matcher = CREDENTIAL_REGION_PATTERN.matcher(authorizationHeader);
return matcher.find() ? withoutPseudoRegion(matcher.group(1)) : null;
}
@@ -17,15 +17,99 @@ public final class SigV4CredentialScope {
// SigV4A drops the region segment: Credential=<key>/<date>/<service>/aws4_request
private static final Pattern SIGV4A_SERVICE_PATTERN =
Pattern.compile("Credential=[^,\\s]+/\\d{8}/([^/,\\s]+)/aws4_request");
private static final Pattern ACCESS_KEY_PATTERN = Pattern.compile("Credential=([^/\\s,]+)/");
static final String CREDENTIAL = "Credential=";
private static final String TERMINATOR = "aws4_request";
private SigV4CredentialScope() {
}
/** A credential scope segment that {@link #segment} reads. */
enum Segment {
/** The region, or the service in the SigV4A form, which has no region. */
THIRD,
SERVICE
}
/**
* Reads one segment of the common header shape without a regular expression. Returns null for
* any other shape, and the caller then runs its regex, so both give the same answer for every input.
*/
static String segment(String header, Segment segment) {
int credential = header.indexOf(CREDENTIAL);
if (credential < 0 || header.indexOf(CREDENTIAL, credential + 1) >= 0) {
return null;
}
int start = credential + CREDENTIAL.length();
int end = start;
int length = header.length();
while (end < length) {
char c = header.charAt(end);
if (c == ',' || isRegexWhitespace(c)) {
break;
}
end++;
}
if (header.indexOf('/', end) >= 0) {
return null;
}
int slash1 = header.indexOf('/', start);
int slash2 = slash1 < 0 ? -1 : header.indexOf('/', slash1 + 1);
int slash3 = slash2 < 0 ? -1 : header.indexOf('/', slash2 + 1);
if (slash3 < 0) {
return null;
}
int slash4 = header.indexOf('/', slash3 + 1);
int terminator = slash4 < 0 ? slash3 : slash4;
if (end - terminator - 1 != TERMINATOR.length() || !header.startsWith(TERMINATOR, terminator + 1)) {
return null;
}
if (slash1 == start || slash2 == slash1 + 1 || slash3 == slash2 + 1
|| !isEightDigits(header, slash1 + 1, slash2)) {
return null;
}
if (slash4 >= 0 && (slash4 == slash3 + 1 || isEightDigits(header, slash2 + 1, slash3))) {
return null;
}
return switch (segment) {
case THIRD -> header.substring(slash2 + 1, slash3);
case SERVICE -> slash4 < 0 ? header.substring(slash2 + 1, slash3) : header.substring(slash3 + 1, slash4);
};
}
/** Every credential scope pattern starts with {@link #CREDENTIAL}, so a header without it matches none. */
static boolean hasCredential(String header) {
return header.contains(CREDENTIAL);
}
private static boolean isRegexWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\u000B' || c == '\f' || c == '\r';
}
private static boolean isEightDigits(String value, int from, int to) {
if (to - from != 8) {
return false;
}
for (int i = from; i < to; i++) {
char c = value.charAt(i);
if (c < '0' || '9' < c) {
return false;
}
}
return true;
}
public static Optional<String> serviceName(String authorization) {
if (authorization == null) {
return Optional.empty();
}
String service = segment(authorization, Segment.SERVICE);
if (service != null) {
return Optional.of(service.toLowerCase(Locale.ROOT));
}
if (!hasCredential(authorization)) {
return Optional.empty();
}
Matcher matcher = SERVICE_PATTERN.matcher(authorization);
if (matcher.find()) {
return Optional.of(matcher.group(1).toLowerCase(Locale.ROOT));
@@ -43,8 +127,22 @@ public final class SigV4CredentialScope {
if (authorization == null) {
return Optional.empty();
}
Matcher matcher = ACCESS_KEY_PATTERN.matcher(authorization);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
for (int credential = authorization.indexOf(CREDENTIAL); credential >= 0;
credential = authorization.indexOf(CREDENTIAL, credential + 1)) {
int start = credential + CREDENTIAL.length();
int end = start;
while (end < authorization.length()) {
char c = authorization.charAt(end);
if (c == '/' || c == ',' || isRegexWhitespace(c)) {
break;
}
end++;
}
if (start < end && end < authorization.length() && authorization.charAt(end) == '/') {
return Optional.of(authorization.substring(start, end));
}
}
return Optional.empty();
}
public static Optional<String> serviceNameFromCredential(String credential) {