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

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