Skip to content

armgit / floci

Public Imported from GitHub
0

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

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

Priamo Germosena1730e3227ae
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
new file mode 100644
index 0000000000..7208a1ce5e
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SsrfProtection.java
@@ -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;
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginHttpClient.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginHttpClient.java
index 018b5fb264..993d218ff5 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginHttpClient.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginHttpClient.java
@@ -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);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
index d6bf7f2b7a..99eaec12c2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
@@ -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) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifier.java b/src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifier.java
index 7c1fb02052..cd426ab304 100644
--- a/src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifier.java
+++ b/src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifier.java
@@ -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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginSecurityTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginSecurityTest.java
index 88c7b048f8..a92ee028de 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginSecurityTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontOriginSecurityTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.cloudfront;
+import io.github.hectorvent.floci.core.common.SsrfProtection;
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
import org.junit.jupiter.api.Test;
@@ -23,17 +24,17 @@ class CloudFrontOriginSecurityTest {
"169.254.169.254", "100.64.0.1", "224.0.0.1", "240.0.0.1",
"::1", "fe80::1", "fc00::1", "2001:db8::1"
}) {
- assertTrue(CloudFrontServingController.isBlockedOriginAddress(InetAddress.getByName(address)),
+ assertTrue(SsrfProtection.isBlockedAddress(InetAddress.getByName(address)),
address + " must not be reachable as a default custom origin");
}
}
@Test
void permitsPublicAddresses() throws Exception {
- assertFalse(CloudFrontServingController.isBlockedOriginAddress(InetAddress.getByName("8.8.8.8")));
- assertFalse(CloudFrontServingController.isBlockedOriginAddress(
+ assertFalse(SsrfProtection.isBlockedAddress(InetAddress.getByName("8.8.8.8")));
+ assertFalse(SsrfProtection.isBlockedAddress(
InetAddress.getByName("2606:4700:4700::1111")));
- assertFalse(CloudFrontServingController.isBlockedOriginAddress(ipv4MappedAddress("8.8.8.8")));
+ assertFalse(SsrfProtection.isBlockedAddress(ipv4MappedAddress("8.8.8.8")));
}
@Test
@@ -43,7 +44,7 @@ class CloudFrontOriginSecurityTest {
"169.254.169.254", "100.64.0.1", "192.0.0.1", "192.0.2.1", "198.18.0.1",
"198.51.100.1", "203.0.113.1", "224.0.0.1", "240.0.0.1"
}) {
- assertTrue(CloudFrontServingController.isBlockedOriginAddress(ipv4MappedAddress(address)),
+ assertTrue(SsrfProtection.isBlockedAddress(ipv4MappedAddress(address)),
"IPv4-mapped " + address + " must not be reachable as a default custom origin");
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifierTest.java b/src/test/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifierTest.java
index 6eefa4d7d3..a2f0d65e90 100644
--- a/src/test/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifierTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsOidcSignatureVerifierTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.verifiedpermissions;
import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.SsrfProtection;
import org.apache.hc.client5.http.SystemDefaultDnsResolver;
import org.junit.jupiter.api.Test;
@@ -32,13 +33,13 @@ class VerifiedPermissionsOidcSignatureVerifierTest {
() -> VerifiedPermissionsOidcSignatureVerifier.validatePublicHttpsUri(
"https://[::1]", "OIDC issuer"));
- assertTrue(VerifiedPermissionsOidcSignatureVerifier.isBlockedPublicAddress(
+ assertTrue(SsrfProtection.isBlockedAddress(
InetAddress.getByName("127.0.0.1")));
- assertTrue(VerifiedPermissionsOidcSignatureVerifier.isBlockedPublicAddress(
+ assertTrue(SsrfProtection.isBlockedAddress(
InetAddress.getByName("169.254.169.254")));
- assertTrue(VerifiedPermissionsOidcSignatureVerifier.isBlockedPublicAddress(
+ assertTrue(SsrfProtection.isBlockedAddress(
InetAddress.getByName("10.0.0.1")));
- assertTrue(VerifiedPermissionsOidcSignatureVerifier.isBlockedPublicAddress(
+ assertTrue(SsrfProtection.isBlockedAddress(
InetAddress.getByName("192.168.1.1")));
}