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

fix(uri-validation): Validate URI ranges for discovery plugins and related targets #746

Merged
merged 2 commits into from
Dec 19, 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
17 changes: 16 additions & 1 deletion src/main/java/io/cryostat/discovery/Discovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,15 @@ public PluginRegistration register(@Context RoutingContext ctx, JsonObject body)
callbackUri));
}

// TODO apply URI range validation to the remote address
InetAddress remoteAddress = getRemoteAddress(ctx);
URI remoteURI = new URI(remoteAddress.getHostAddress());
if (!uriUtil.validateUri(remoteURI)) {
throw new BadRequestException(
String.format(
"Remote Address of \"%s\" is unacceptable with the"
+ " current URI range settings",
remoteURI));
}
URI location;
DiscoveryPlugin plugin;
if (StringUtils.isNotBlank(pluginId) && StringUtils.isNotBlank(priorToken)) {
Expand Down Expand Up @@ -312,6 +319,14 @@ public void publish(
plugin.realm.children.addAll(body);
for (var b : body) {
if (b.target != null) {
// URI range validation
if (!uriUtil.validateUri(b.target.connectUrl)) {
throw new BadRequestException(
String.format(
"Connect URL of \"%s\" is unacceptable with the"
+ " current URI range settings",
b.target.connectUrl));
}
b.target.discoveryNode = b;
b.target.discoveryNode.parent = plugin.realm;
b.parent = plugin.realm;
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/io/cryostat/targets/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.cryostat.targets;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -32,6 +33,7 @@

import io.cryostat.discovery.DiscoveryNode;
import io.cryostat.recordings.ActiveRecording;
import io.cryostat.util.URIUtil;
import io.cryostat.ws.MessagingServer;
import io.cryostat.ws.Notification;

Expand Down Expand Up @@ -278,6 +280,7 @@ public record TargetDiscovery(EventKind kind, Target serviceRef, String jvmId) {
@ApplicationScoped
static class Listener {

@Inject URIUtil uriUtil;
@Inject Logger logger;
@Inject EventBus bus;

Expand All @@ -290,7 +293,17 @@ void prePersist(Target target) {
if (!Objects.equals(encodedAlias, target.alias)) {
target.alias = encodedAlias;
}

try {
if (!uriUtil.validateUri(target.connectUrl)) {
throw new IllegalArgumentException(
String.format(
"Connect URL of \"%s\" is unacceptable with the"
+ " current URI range settings",
target.connectUrl));
}
} catch (MalformedURLException me) {
throw new IllegalArgumentException(me);
}
if (target.labels == null) {
target.labels = new HashMap<>();
}
Expand Down
Loading