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

Resolve the integration target before connecting and reject link-local and cloud instance-metadata addresses on both the HttpClient path and the Host-override socket path. Loopback and private ranges stay reachable so local backends keep working.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-19 19:53:20 -05:00
1 parent 5a11e7f6fb
commit 5f4aea20d5
4 files changed
+131 -1

No files matched your search

@@ -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()
@@ -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();
@@ -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")));
}
}
@@ -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");
}
}