Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevUI: Allow regex for defined allowed hosts #42618

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
phillip-kruger marked this conversation as resolved.
Show resolved Hide resolved
* 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;
}
}
}
Loading