refactor(core): dedupe SSRF blocked-address check (#3722)

CloudFrontServingController (via CloudFrontOriginHttpClient) and
VerifiedPermissionsOidcSignatureVerifier each independently
implemented the same SSRF-protection check rejecting private/
link-local/reserved IPv4 and IPv6 ranges before a server-side fetch,
found via CPD as an 18-line block (#3715). The two files actually
duplicated three methods end to end, differently named
(isBlockedOriginAddress/isBlockedPublicAddress,
isIpv4MappedAddress/ipv4Mapped, isBlockedIpv4Address/blockedIpv4), not
just the one CPD's threshold happened to flag.

Extracted the full check into SsrfProtection.isBlockedAddress in
core.common, and pointed both call sites (plus their tests) at it,
removing all three duplicated methods from both files rather than
leaving a thin wrapper behind.

No behavior change: the blocked ranges and precedence are identical
to both copies before this, only their location changed.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-16 00:13:32 -05:00
1 parent 03ead297a3
commit a1730e3227
6 files changed
+85 -119

No files matched your search

@@ -0,0 +1,69 @@
package io.github.hectorvent.floci.core.common;
import java.net.InetAddress;
/**
* SSRF protection for any code that resolves a caller-supplied hostname and fetches from the
* resolved address server-side: rejects private, link-local, loopback, multicast, and other
* reserved IPv4/IPv6 ranges. {@code CloudFrontServingController} (via
* {@code CloudFrontOriginHttpClient}) and {@code VerifiedPermissionsOidcSignatureVerifier} each
* implemented this independently before this class existed.
*/
public final class SsrfProtection {
private SsrfProtection() {
}
public static boolean isBlockedAddress(InetAddress address) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress()
|| address.isLinkLocalAddress() || address.isSiteLocalAddress()
|| address.isMulticastAddress()) {
return true;
}
byte[] bytes = address.getAddress();
if (bytes.length == 4) {
return isBlockedIpv4Address(bytes, 0);
}
if (bytes.length == 16) {
if (isIpv4MappedAddress(bytes)) {
return isBlockedIpv4Address(bytes, 12);
}
int first = Byte.toUnsignedInt(bytes[0]);
boolean uniqueLocal = (first & 0xfe) == 0xfc;
boolean documentation = first == 0x20
&& Byte.toUnsignedInt(bytes[1]) == 0x01
&& Byte.toUnsignedInt(bytes[2]) == 0x0d
&& Byte.toUnsignedInt(bytes[3]) == 0xb8;
return uniqueLocal || documentation;
}
return true;
}
private static boolean isIpv4MappedAddress(byte[] bytes) {
for (int i = 0; i < 10; i++) {
if (bytes[i] != 0) {
return false;
}
}
return bytes[10] == (byte) 0xff && bytes[11] == (byte) 0xff;
}
private static boolean isBlockedIpv4Address(byte[] bytes, int offset) {
int first = Byte.toUnsignedInt(bytes[offset]);
int second = Byte.toUnsignedInt(bytes[offset + 1]);
int third = Byte.toUnsignedInt(bytes[offset + 2]);
return first == 0
|| first == 10
|| first == 127
|| (first == 100 && second >= 64 && second <= 127)
|| (first == 169 && second == 254)
|| (first == 172 && second >= 16 && second <= 31)
|| (first == 192 && second == 168)
|| (first == 192 && second == 0 && third == 0)
|| (first == 192 && second == 0 && third == 2)
|| (first == 198 && (second == 18 || second == 19))
|| (first == 198 && second == 51 && third == 100)
|| (first == 203 && second == 0 && third == 113)
|| first >= 224;
}
}
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.cloudfront;
import io.github.hectorvent.floci.core.common.SsrfProtection;
import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
import org.apache.hc.client5.http.config.ConnectionConfig;
@@ -205,7 +206,7 @@ final class CloudFrontOriginHttpClient implements AutoCloseable {
}
if (!allowedHosts.contains(normalizedHost)) {
for (InetAddress address : addresses) {
if (CloudFrontServingController.isBlockedOriginAddress(address)) {
if (SsrfProtection.isBlockedAddress(address)) {
throw new UnknownHostException(
"CloudFront origin host resolves to a blocked address: " + normalizedHost);
}
@@ -24,7 +24,6 @@ import jakarta.ws.rs.core.UriInfo;
import org.jboss.logging.Logger;
import java.net.IDN;
import java.net.InetAddress;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@@ -559,59 +558,6 @@ public class CloudFrontServingController {
return normalized.substring(prefix.length());
}
static boolean isBlockedOriginAddress(InetAddress address) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress()
|| address.isLinkLocalAddress() || address.isSiteLocalAddress()
|| address.isMulticastAddress()) {
return true;
}
byte[] bytes = address.getAddress();
if (bytes.length == 4) {
return isBlockedIpv4Address(bytes, 0);
}
if (bytes.length == 16) {
if (isIpv4MappedAddress(bytes)) {
return isBlockedIpv4Address(bytes, 12);
}
int first = Byte.toUnsignedInt(bytes[0]);
boolean uniqueLocal = (first & 0xfe) == 0xfc;
boolean documentation = first == 0x20
&& Byte.toUnsignedInt(bytes[1]) == 0x01
&& Byte.toUnsignedInt(bytes[2]) == 0x0d
&& Byte.toUnsignedInt(bytes[3]) == 0xb8;
return uniqueLocal || documentation;
}
return true;
}
private static boolean isIpv4MappedAddress(byte[] bytes) {
for (int i = 0; i < 10; i++) {
if (bytes[i] != 0) {
return false;
}
}
return bytes[10] == (byte) 0xff && bytes[11] == (byte) 0xff;
}
private static boolean isBlockedIpv4Address(byte[] bytes, int offset) {
int first = Byte.toUnsignedInt(bytes[offset]);
int second = Byte.toUnsignedInt(bytes[offset + 1]);
int third = Byte.toUnsignedInt(bytes[offset + 2]);
return first == 0
|| first == 10
|| first == 127
|| (first == 100 && second >= 64 && second <= 127)
|| (first == 169 && second == 254)
|| (first == 172 && second >= 16 && second <= 31)
|| (first == 192 && second == 168)
|| (first == 192 && second == 0 && third == 0)
|| (first == 192 && second == 0 && third == 2)
|| (first == 198 && (second == 18 || second == 19))
|| (first == 198 && second == 51 && third == 100)
|| (first == 203 && second == 0 && third == 113)
|| first >= 224;
}
static URI buildCustomOriginUri(String protocol, String domainName, int port,
String forwardUri, String rawQuery) {
if (port < 1 || port > 65535) {
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.verifiedpermissions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.SsrfProtection;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -226,7 +227,7 @@ public class VerifiedPermissionsOidcSignatureVerifier implements AutoCloseable {
}
String host = uri.getHost();
boolean literalAddress = host.indexOf(':') >= 0 || host.matches("[0-9]{1,3}(?:\\.[0-9]{1,3}){3}");
if (literalAddress && isBlockedPublicAddress(InetAddress.ofLiteral(host))) {
if (literalAddress && SsrfProtection.isBlockedAddress(InetAddress.ofLiteral(host))) {
throw new VerificationException(description + " resolves to a non-public address");
}
return uri;
@@ -293,7 +294,7 @@ public class VerifiedPermissionsOidcSignatureVerifier implements AutoCloseable {
throw new UnknownHostException("OIDC host has no addresses: " + host);
}
for (InetAddress address : addresses) {
if (isBlockedPublicAddress(address)) {
if (SsrfProtection.isBlockedAddress(address)) {
throw new UnknownHostException("OIDC host resolves to a non-public address: " + host);
}
}
@@ -306,59 +307,6 @@ public class VerifiedPermissionsOidcSignatureVerifier implements AutoCloseable {
}
}
static boolean isBlockedPublicAddress(InetAddress address) {
if (address.isAnyLocalAddress() || address.isLoopbackAddress()
|| address.isLinkLocalAddress() || address.isSiteLocalAddress()
|| address.isMulticastAddress()) {
return true;
}
byte[] bytes = address.getAddress();
if (bytes.length == 4) {
return blockedIpv4(bytes, 0);
}
if (bytes.length == 16) {
if (ipv4Mapped(bytes)) {
return blockedIpv4(bytes, 12);
}
int first = Byte.toUnsignedInt(bytes[0]);
boolean uniqueLocal = (first & 0xfe) == 0xfc;
boolean documentation = first == 0x20
&& Byte.toUnsignedInt(bytes[1]) == 0x01
&& Byte.toUnsignedInt(bytes[2]) == 0x0d
&& Byte.toUnsignedInt(bytes[3]) == 0xb8;
return uniqueLocal || documentation;
}
return true;
}
private static boolean ipv4Mapped(byte[] bytes) {
for (int i = 0; i < 10; i++) {
if (bytes[i] != 0) {
return false;
}
}
return bytes[10] == (byte) 0xff && bytes[11] == (byte) 0xff;
}
private static boolean blockedIpv4(byte[] bytes, int offset) {
int first = Byte.toUnsignedInt(bytes[offset]);
int second = Byte.toUnsignedInt(bytes[offset + 1]);
int third = Byte.toUnsignedInt(bytes[offset + 2]);
return first == 0
|| first == 10
|| first == 127
|| (first == 100 && second >= 64 && second <= 127)
|| (first == 169 && second == 254)
|| (first == 172 && second >= 16 && second <= 31)
|| (first == 192 && second == 168)
|| (first == 192 && second == 0 && third == 0)
|| (first == 192 && second == 0 && third == 2)
|| (first == 198 && (second == 18 || second == 19))
|| (first == 198 && second == 51 && third == 100)
|| (first == 203 && second == 0 && third == 113)
|| first >= 224;
}
public static class VerificationException extends RuntimeException {
public VerificationException(String message) {
super(message);