Cache regex patterns in frequently used production code

Closes #32428

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2024-08-27 16:27:40 +02:00 committed by Pedro Igor
parent f8393124cd
commit 5bd3da657b
2 changed files with 6 additions and 2 deletions

View file

@ -1,9 +1,12 @@
package org.keycloak.cookie; package org.keycloak.cookie;
import java.net.URI; import java.net.URI;
import java.util.regex.Pattern;
class SecureContextResolver { class SecureContextResolver {
private static final Pattern LOCALHOST_IPV4 = Pattern.compile("127.\\d{1,3}.\\d{1,3}.\\d{1,3}");
/** /**
* Determines if a URI is potentially trustworthy, meaning a user agent can generally trust it to deliver data securely. * Determines if a URI is potentially trustworthy, meaning a user agent can generally trust it to deliver data securely.
* *
@ -28,7 +31,7 @@ class SecureContextResolver {
} }
// The host matches a CIDR notation of 127.0.0.0/8 // The host matches a CIDR notation of 127.0.0.0/8
if (host.matches("127.\\d{1,3}.\\d{1,3}.\\d{1,3}")) { if (LOCALHOST_IPV4.matcher(host).matches()) {
return true; return true;
} }

View file

@ -61,6 +61,7 @@ public class UPConfigUtils {
public static final String ROLE_ADMIN = UserProfileConstants.ROLE_ADMIN; public static final String ROLE_ADMIN = UserProfileConstants.ROLE_ADMIN;
private static final Set<String> PSEUDOROLES = new HashSet<>(); private static final Set<String> PSEUDOROLES = new HashSet<>();
public static final Pattern ATTRIBUTE_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9\\._\\-]+");
static { static {
PSEUDOROLES.add(ROLE_ADMIN); PSEUDOROLES.add(ROLE_ADMIN);
@ -239,7 +240,7 @@ public class UPConfigUtils {
* @return * @return
*/ */
public static boolean isValidAttributeName(String attributeName) { public static boolean isValidAttributeName(String attributeName) {
return Pattern.matches("[a-zA-Z0-9\\._\\-]+", attributeName); return ATTRIBUTE_NAME_PATTERN.matcher(attributeName).matches();
} }
/** /**