Skip to content

Commit

Permalink
Merge pull request #42618 from phillip-kruger/dev-ui-allowed-host-regex
Browse files Browse the repository at this point in the history
DevUI: Allow regex for defined allowed hosts
  • Loading branch information
cescoffier authored Aug 20, 2024
2 parents 2ac0a6d + 58fef84 commit 34ca0e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,9 +23,11 @@ public class LocalHostOnlyFilter implements Handler<RoutingContext> {
private static final String LOCAL_HOST_IP = "127.0.0.1";

private final List<String> hosts;
private final List<Pattern> hostsPatterns;

public LocalHostOnlyFilter(List<String> hosts) {
this.hosts = hosts;
this.hostsPatterns = detectPatterns();
}

@Override
Expand All @@ -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<Pattern> detectPatterns() {
if (this.hosts != null && !this.hosts.isEmpty()) {
List<Pattern> 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;
}
}
}

0 comments on commit 34ca0e0

Please sign in to comment.