mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(elbv2): block link-local and metadata targets (#4031)
Reject link-local and cloud instance-metadata addresses in RegisterTargets with InvalidTarget, and resolve target hosts once in the data plane and health checker, connecting to the checked address. Loopback and private ranges stay reachable so a load balancer can front a neighbouring container.
This commit is contained in:
1 parent
9c23757e4f
commit
9dab36197a
8 files changed
+133
-2
No files matched your search
@@ -21,6 +21,7 @@ import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpClientOptions;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.http.RequestOptions;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
@@ -29,6 +30,7 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
@@ -596,8 +598,29 @@ public class ElbV2DataPlane {
|
||||
private void proxyRequest(io.vertx.core.http.HttpServerRequest req, String host, int port,
|
||||
boolean preserveHostHeader) {
|
||||
req.pause();
|
||||
if (ElbV2TargetResolver.isIpLiteral(host)) {
|
||||
try {
|
||||
proxyRequestTo(req, ElbV2TargetResolver.resolveCheckedAddress(host), host, port, preserveHostHeader);
|
||||
} catch (IOException e) {
|
||||
rejectTarget(req, host, e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
vertx.<String>executeBlocking(() -> ElbV2TargetResolver.resolveCheckedAddress(host))
|
||||
.onSuccess(address -> proxyRequestTo(req, address, host, port, preserveHostHeader))
|
||||
.onFailure(err -> rejectTarget(req, host, err));
|
||||
}
|
||||
|
||||
private void rejectTarget(HttpServerRequest req, String host, Throwable err) {
|
||||
LOG.warnv("Refusing to proxy to target {0}: {1}", host, err.getMessage());
|
||||
req.resume();
|
||||
req.response().setStatusCode(503).end("Service unavailable");
|
||||
}
|
||||
|
||||
private void proxyRequestTo(HttpServerRequest req, String address, String host, int port,
|
||||
boolean preserveHostHeader) {
|
||||
RequestOptions opts = new RequestOptions()
|
||||
.setHost(host)
|
||||
.setHost(address)
|
||||
.setPort(port)
|
||||
.setURI(req.uri())
|
||||
.setMethod(req.method());
|
||||
|
||||
@@ -195,7 +195,7 @@ public class ElbV2HealthChecker implements Resettable {
|
||||
}
|
||||
|
||||
private int probe(String host, int port, String path, int timeoutSeconds) throws IOException {
|
||||
URL url = new URL("http", host, port, path);
|
||||
URL url = new URL("http", ElbV2TargetResolver.resolveCheckedAddress(host), port, path);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setConnectTimeout(timeoutSeconds * 1000);
|
||||
conn.setReadTimeout(timeoutSeconds * 1000);
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.SsrfProtection;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -15,6 +16,8 @@ import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -776,6 +779,11 @@ public class ElbV2Service implements ResourceProvider {
|
||||
|
||||
public void registerTargets(String region, String tgArn, List<TargetDescription> targets) {
|
||||
TargetGroup tg = requireTargetGroup(region, tgArn);
|
||||
if (!"lambda".equals(tg.getTargetType())) {
|
||||
for (TargetDescription t : targets) {
|
||||
requireNotMetadataAddress(t.getId());
|
||||
}
|
||||
}
|
||||
List<TargetDescription> existing = tg.getTargets();
|
||||
for (TargetDescription t : targets) {
|
||||
// replace if same id+port already registered
|
||||
@@ -786,6 +794,18 @@ public class ElbV2Service implements ResourceProvider {
|
||||
healthChecker.addTargets(tgArn, targets, tg);
|
||||
}
|
||||
|
||||
private static void requireNotMetadataAddress(String targetId) {
|
||||
if (!ElbV2TargetResolver.isIpLiteral(targetId)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(targetId), targetId);
|
||||
} catch (IOException e) {
|
||||
throw new AwsException("InvalidTarget",
|
||||
"The IP address '" + targetId + "' is not a valid target", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public void deregisterTargets(String region, String tgArn, List<TargetDescription> targets) {
|
||||
TargetGroup tg = requireTargetGroup(region, tgArn);
|
||||
for (TargetDescription t : targets) {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.elbv2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.SsrfProtection;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
final class ElbV2TargetResolver {
|
||||
|
||||
private ElbV2TargetResolver() {}
|
||||
@@ -32,4 +36,14 @@ final class ElbV2TargetResolver {
|
||||
}
|
||||
return targetId;
|
||||
}
|
||||
|
||||
/** Resolves once, rejects metadata addresses, and returns the address to connect to instead of the name. */
|
||||
static String resolveCheckedAddress(String host) throws IOException {
|
||||
InetAddress[] addresses = SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(host), host);
|
||||
return addresses[0].getHostAddress();
|
||||
}
|
||||
|
||||
static boolean isIpLiteral(String host) {
|
||||
return host != null && (host.contains(":") || host.matches("[0-9.]+"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user