From 58fef849d62b0381f1434aa5e1750ff590a0930a Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Mon, 19 Aug 2024 19:07:32 +1000 Subject: [PATCH] DevUI: Allow regex for defined allowed hosts Signed-off-by: Phillip Kruger --- .../quarkus/devui/deployment/DevUIConfig.java | 1 + .../devui/runtime/LocalHostOnlyFilter.java | 42 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/devui/deployment/DevUIConfig.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/devui/deployment/DevUIConfig.java index e5a2c5ab3acc3..c1dbfd9fc50b5 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/devui/deployment/DevUIConfig.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/devui/deployment/DevUIConfig.java @@ -26,6 +26,7 @@ public class DevUIConfig { * More hosts allowed for Dev UI * * Comma separated list of valid URLs, e.g.: www.quarkus.io, myhost.com + * (This can also be a regex) * By default localhost and 127.0.0.1 will always be allowed */ @ConfigItem diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/devui/runtime/LocalHostOnlyFilter.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/devui/runtime/LocalHostOnlyFilter.java index b5d0c9d50903b..a34c70222f271 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/devui/runtime/LocalHostOnlyFilter.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/devui/runtime/LocalHostOnlyFilter.java @@ -4,7 +4,11 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import org.jboss.logging.Logger; @@ -19,9 +23,11 @@ public class LocalHostOnlyFilter implements Handler { private static final String LOCAL_HOST_IP = "127.0.0.1"; private final List hosts; + private final List hostsPatterns; public LocalHostOnlyFilter(List hosts) { this.hosts = hosts; + this.hostsPatterns = detectPatterns(); } @Override @@ -45,11 +51,45 @@ private boolean hostIsValid(RoutingContext event) { if (host.equals(LOCAL_HOST) || host.equals(LOCAL_HOST_IP)) { return true; + } else if (this.hosts != null && this.hosts.contains(host)) { + return true; + } else if (this.hostsPatterns != null && !this.hostsPatterns.isEmpty()) { + // Regex + for (Pattern pat : this.hostsPatterns) { + Matcher matcher = pat.matcher(host); + if (matcher.matches()) { + return true; + } + } } - return this.hosts != null && this.hosts.contains(host); + return false; } catch (MalformedURLException | URISyntaxException e) { LOG.error("Error while checking if Dev UI is localhost", e); } return false; } + + private List detectPatterns() { + if (this.hosts != null && !this.hosts.isEmpty()) { + List pat = new ArrayList<>(); + for (String h : this.hosts) { + Pattern p = toPattern(h); + if (p != null) { + pat.add(p); + } + } + if (!pat.isEmpty()) { + return pat; + } + } + return null; + } + + private Pattern toPattern(String regex) { + try { + return Pattern.compile(regex); + } catch (PatternSyntaxException e) { + return null; + } + } }