Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(apigatewayv2): block link-local and metadata targets in HTTP_PROXY integrations (#3962)

Priamo Germosen5f4aea20d52c
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SsrfProtection.java b/src/main/java/io/github/hectorvent/floci/core/common/SsrfProtection.java
index 7208a1ce5e..da82428250 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/SsrfProtection.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SsrfProtection.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.core.common;
import java.net.InetAddress;
+import java.util.Arrays;
/**
* SSRF protection for any code that resolves a caller-supplied hostname and fetches from the
@@ -14,6 +15,31 @@ public final class SsrfProtection {
private SsrfProtection() {
}
+ /**
+ * Narrower than {@link #isBlockedAddress}: only the addresses cloud instance-metadata
+ * services listen on, i.e. IPv4 and IPv6 link-local ({@code 169.254.0.0/16}, {@code fe80::/10})
+ * and the AWS IPv6 metadata address {@code fd00:ec2::254}. For callers that must still reach
+ * loopback and private networks, such as a local emulator proxying to a neighbouring container.
+ */
+ public static boolean isMetadataAddress(InetAddress address) {
+ if (address.isLinkLocalAddress()) {
+ return true;
+ }
+ byte[] bytes = address.getAddress();
+ if (bytes.length == 16) {
+ if (isIpv4MappedAddress(bytes)) {
+ return Byte.toUnsignedInt(bytes[12]) == 169 && Byte.toUnsignedInt(bytes[13]) == 254;
+ }
+ return isAwsIpv6Metadata(bytes);
+ }
+ return false;
+ }
+
+ private static boolean isAwsIpv6Metadata(byte[] bytes) {
+ byte[] metadata = {(byte) 0xfd, 0x00, 0x0e, (byte) 0xc2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02, 0x54};
+ return Arrays.equals(bytes, metadata);
+ }
+
public static boolean isBlockedAddress(InetAddress address) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress()
|| address.isLinkLocalAddress() || address.isSiteLocalAddress()
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java
index ff1b5774fb..048c3bdb38 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.apigatewayv2.proxy;
+import io.github.hectorvent.floci.core.common.SsrfProtection;
import io.github.hectorvent.floci.services.apigatewayv2.model.Integration;
import org.jboss.logging.Logger;
@@ -12,6 +13,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
@@ -346,6 +348,7 @@ public class HttpProxyInvoker {
}
try {
+ resolveNonMetadataTarget(hrb.build().uri().getHost());
HttpResponse<byte[]> resp =
clientFor(options).send(hrb.build(), HttpResponse.BodyHandlers.ofByteArray());
Map<String, List<String>> respHeaders = new LinkedHashMap<>();
@@ -360,6 +363,19 @@ public class HttpProxyInvoker {
}
}
+ private static InetAddress[] resolveNonMetadataTarget(String host) throws IOException {
+ if (host == null || host.isBlank()) {
+ throw new IOException("integration URI has no host");
+ }
+ InetAddress[] addresses = InetAddress.getAllByName(host);
+ for (InetAddress address : addresses) {
+ if (SsrfProtection.isMetadataAddress(address)) {
+ throw new IOException("integration URI resolves to a link-local or metadata address: " + host);
+ }
+ }
+ return addresses;
+ }
+
private static boolean hasHeader(ProxyRequestBuilder builder, String headerName) {
return builder.headers().keySet().stream().anyMatch(headerName::equalsIgnoreCase);
}
@@ -386,8 +402,9 @@ public class HttpProxyInvoker {
path += "?" + uri.getRawQuery();
}
+ InetAddress[] targets = resolveNonMetadataTarget(uri.getHost());
try (Socket socket = new Socket()) {
- socket.connect(new InetSocketAddress(uri.getHost(), port), 10_000);
+ socket.connect(new InetSocketAddress(targets[0], port), 10_000);
socket.setSoTimeout((int) Math.min(timeout.toMillis(), Integer.MAX_VALUE));
OutputStream out = socket.getOutputStream();
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SsrfProtectionTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SsrfProtectionTest.java
new file mode 100644
index 0000000000..0f9cc89c2f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/SsrfProtectionTest.java
@@ -0,0 +1,39 @@
+package io.github.hectorvent.floci.core.common;
+
+import org.junit.jupiter.api.Test;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class SsrfProtectionTest {
+
+ @Test
+ void metadataAddressCoversIpv4LinkLocal() throws UnknownHostException {
+ assertTrue(SsrfProtection.isMetadataAddress(InetAddress.getByName("169.254.169.254")));
+ assertTrue(SsrfProtection.isMetadataAddress(InetAddress.getByName("169.254.0.1")));
+ }
+
+ @Test
+ void metadataAddressCoversIpv6LinkLocalAndAwsMetadata() throws UnknownHostException {
+ assertTrue(SsrfProtection.isMetadataAddress(InetAddress.getByName("fe80::1")));
+ assertTrue(SsrfProtection.isMetadataAddress(InetAddress.getByName("fd00:ec2::254")));
+ }
+
+ @Test
+ void metadataAddressCoversIpv4MappedLinkLocal() throws UnknownHostException {
+ assertTrue(SsrfProtection.isMetadataAddress(InetAddress.getByName("::ffff:169.254.169.254")));
+ }
+
+ @Test
+ void metadataAddressLeavesLoopbackAndPrivateRangesReachable() throws UnknownHostException {
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("127.0.0.1")));
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("::1")));
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("10.0.0.5")));
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("172.17.0.2")));
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("192.168.1.10")));
+ assertFalse(SsrfProtection.isMetadataAddress(InetAddress.getByName("fd00:ec2::255")));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java
index b941f6c7a0..719151946f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java
@@ -246,4 +246,52 @@ class HttpProxyInvokerTest {
assertTrue(q.contains("page=2"));
assertTrue(q.contains("user=u-42"));
}
+
+ @Test
+ void rejectsLinkLocalMetadataTarget() {
+ Integration integration = httpProxyIntegration("http://169.254.169.254/latest/meta-data/", null);
+ RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
+
+ ProxyResult result = new HttpProxyInvoker().invoke(integration, ctx);
+
+ assertEquals(502, result.statusCode());
+ assertTrue(new String(result.body(), StandardCharsets.UTF_8).contains("link-local or metadata address"),
+ "the target must be rejected before any connection is attempted");
+ }
+
+ @Test
+ void rejectsLinkLocalMetadataTargetWhenHostHeaderIsOverridden() {
+ Integration integration = httpProxyIntegration("http://169.254.169.254/latest/meta-data/",
+ Map.of("overwrite:header.Host", "lb.localhost.test"));
+ RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
+
+ ProxyResult result = new HttpProxyInvoker().invoke(integration, ctx);
+
+ assertEquals(502, result.statusCode());
+ assertTrue(new String(result.body(), StandardCharsets.UTF_8).contains("link-local or metadata address"),
+ "the target must be rejected before any connection is attempted");
+ }
+
+ @Test
+ void rejectsAwsIpv6MetadataTarget() {
+ Integration integration = httpProxyIntegration("http://[fd00:ec2::254]/latest/meta-data/", null);
+ RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
+
+ ProxyResult result = new HttpProxyInvoker().invoke(integration, ctx);
+
+ assertEquals(502, result.statusCode());
+ assertTrue(new String(result.body(), StandardCharsets.UTF_8).contains("link-local or metadata address"),
+ "the target must be rejected before any connection is attempted");
+ }
+
+ @Test
+ void stillReachesLoopbackBackend() {
+ Integration integration = httpProxyIntegration("http://127.0.0.1:" + backendPort + "/ok", null);
+ RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
+
+ ProxyResult result = new HttpProxyInvoker().invoke(integration, ctx);
+
+ assertEquals(200, result.statusCode());
+ assertNotNull(received.get(), "loopback backends must stay reachable");
+ }
}