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();