mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sns): refuse http endpoints on link-local and metadata addresses (#4400)
Subscribe checked only that an http/https endpoint carried a matching scheme, then Floci posted the SubscriptionConfirmation and every notification to it. An unauthenticated Subscribe therefore turned the emulator into a blind request forwarder against addresses only it can reach. Resolve and screen the endpoint at Subscribe, which is where a name can be judged. At delivery, screen only an endpoint that names an address outright, which reaches a subscription stored before Subscribe began refusing them and costs no lookup. Deliberately no second resolution there: HttpClient resolves the name again when it connects and cannot be given the result of a check, so a lookup could never decide where the request goes. Loopback and private ranges stay allowed, since delivering to a neighbouring container is the normal case, and a host that does not resolve yet is accepted as it is on AWS.
This commit is contained in:
1 parent
2df052181d
commit
b18ddc2c24
3 files changed
+185
-8
No files matched your search
@@ -93,6 +93,24 @@ Supported subscription protocols:
|
||||
For `FilterPolicyScope=MessageBody`, nested policy objects descend into JSON objects and arrays.
|
||||
An object inside an array matches when one array element satisfies the complete nested policy.
|
||||
|
||||
## HTTP and HTTPS endpoint addresses
|
||||
|
||||
Floci posts the `SubscriptionConfirmation` and every notification to a subscribed `http`/`https`
|
||||
endpoint itself, so it screens where that request may go.
|
||||
|
||||
`Subscribe` resolves the endpoint's host and refuses it with `InvalidParameter` when it resolves to
|
||||
a link-local or cloud instance-metadata address (`169.254.0.0/16`, `fe80::/10`, `fd00:ec2::254`).
|
||||
This is the check that judges a hostname. Loopback and private addresses stay allowed, since
|
||||
delivering to a neighbouring container is the normal case here. An endpoint whose host does not
|
||||
resolve yet is accepted, as it is on AWS.
|
||||
|
||||
Delivery screens only an endpoint that names an address outright, an IPv4 or bracketed IPv6
|
||||
literal, which covers a subscription stored before `Subscribe` began refusing them. **It does not
|
||||
resolve a hostname again**, so a hostname that passed `Subscribe` and later resolves to a metadata
|
||||
address is not screened at delivery: Floci's HTTP client resolves the name itself when it connects
|
||||
and cannot be given the result of an earlier check, so a lookup there could not decide where the
|
||||
request goes. Treat the `Subscribe` check as the boundary, not the delivery one.
|
||||
|
||||
## Message size
|
||||
|
||||
`MaximumMessageSize` is the per-topic limit, in bytes, on a published payload. It accepts `1024`
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.common.SsrfProtection;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
|
||||
@@ -32,8 +33,11 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
@@ -441,6 +445,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
throw new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Endpoint scheme does not match protocol '" + protocol + "'.", 400);
|
||||
}
|
||||
if ("http".equals(protocol) || "https".equals(protocol)) {
|
||||
requireDeliverableEndpoint(endpoint);
|
||||
}
|
||||
if ("firehose".equals(protocol)) {
|
||||
requireFirehoseRoleArn(attributes == null ? null : attributes.get(SUBSCRIPTION_ROLE_ARN));
|
||||
}
|
||||
@@ -1734,7 +1741,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
LOG.debugv("Delivered SNS message to Lambda: {0}", sub.getEndpoint());
|
||||
}
|
||||
case "http", "https" -> {
|
||||
if (httpClient == null) break;
|
||||
if (httpClient == null) {
|
||||
break;
|
||||
}
|
||||
boolean rawDelivery = "true".equalsIgnoreCase(sub.getAttributes().get("RawMessageDelivery"));
|
||||
String body = rawDelivery
|
||||
? protocolMessage
|
||||
@@ -1754,9 +1763,9 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
String endpoint = sub.getEndpoint();
|
||||
httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
|
||||
.thenAccept(response -> logHttpResult("Delivered SNS notification", endpoint, response.statusCode()))
|
||||
.exceptionally(ex -> { LOG.warnv("Failed to deliver SNS message to {0}: {1}", endpoint, ex.getMessage()); return null; });
|
||||
// Screened off this thread: Publish fans out to every subscriber here, and a
|
||||
// slow DNS answer for one endpoint must not hold up the others or the caller.
|
||||
postScreenedAsync(request, endpoint, "Delivered SNS notification");
|
||||
}
|
||||
case "application" -> {
|
||||
String region = extractRegionFromArn(sub.getEndpoint());
|
||||
@@ -2004,15 +2013,95 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
.header("x-amz-sns-subscription-arn", "PendingConfirmation")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
String endpoint = subscription.getEndpoint();
|
||||
httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
|
||||
.thenAccept(response -> logHttpResult("Sent SubscriptionConfirmation", endpoint, response.statusCode()))
|
||||
.exceptionally(ex -> { LOG.warnv("Failed to send SubscriptionConfirmation to {0}: {1}", endpoint, ex.getMessage()); return null; });
|
||||
postScreenedAsync(request, subscription.getEndpoint(), "Sent SubscriptionConfirmation");
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to send SubscriptionConfirmation to {0}: {1}", subscription.getEndpoint(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects an endpoint that resolves to a link-local or cloud instance-metadata address. Floci
|
||||
* posts to a subscribed endpoint itself, so without this an unauthenticated Subscribe turns the
|
||||
* emulator into a blind request forwarder against addresses only it can reach. Loopback and
|
||||
* private ranges stay allowed: a local topic delivering to a neighbouring container is the
|
||||
* normal case here.
|
||||
*/
|
||||
private static void requireDeliverableEndpoint(String endpoint) {
|
||||
if (endpoint == null) {
|
||||
return;
|
||||
}
|
||||
String host;
|
||||
try {
|
||||
host = URI.create(endpoint).getHost();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: Endpoint is not a valid URL.", 400);
|
||||
}
|
||||
if (host == null || host.isBlank()) {
|
||||
throw new AwsException("InvalidParameter", "Invalid parameter: Endpoint is not a valid URL.", 400);
|
||||
}
|
||||
try {
|
||||
SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(host), host);
|
||||
} catch (UnknownHostException e) {
|
||||
// Unresolvable now does not mean unresolvable at delivery time, and AWS accepts an
|
||||
// endpoint whose DNS is not yet live, so this is not the place to refuse it.
|
||||
LOG.debugv("SNS endpoint host {0} did not resolve at subscribe time: {1}", host, e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new AwsException("InvalidParameter",
|
||||
"Invalid parameter: Endpoint " + endpoint + " is not a permitted address.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
/** Screens the endpoint, then posts. Nothing here resolves a name, so nothing here blocks. */
|
||||
private void postScreenedAsync(HttpRequest request, String endpoint, String what) {
|
||||
if (!deliverable(endpoint)) {
|
||||
return;
|
||||
}
|
||||
httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
|
||||
.thenAccept(response -> logHttpResult(what, endpoint, response.statusCode()))
|
||||
.exceptionally(ex -> {
|
||||
LOG.warnv("Failed to reach SNS endpoint {0}: {1}", endpoint, ex.getMessage());
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this endpoint may be posted to, judged without resolving anything. Subscribe is where
|
||||
* a name gets resolved and screened; this catches the endpoint that names an address outright,
|
||||
* including a subscription stored before Subscribe began refusing them.
|
||||
*
|
||||
* <p>Deliberately not a second DNS lookup. {@link HttpClient} resolves the name again when it
|
||||
* connects and cannot be handed the result of a check, so a lookup here could never be the thing
|
||||
* that decides where the request goes: it would cost a resolution per delivery and still leave
|
||||
* the same rebinding residual.
|
||||
*/
|
||||
static boolean deliverable(String endpoint) {
|
||||
String host;
|
||||
try {
|
||||
host = URI.create(endpoint).getHost();
|
||||
} catch (IllegalArgumentException e) {
|
||||
LOG.warnv("Refusing to deliver to SNS endpoint {0}: not a valid URL", endpoint);
|
||||
return false;
|
||||
}
|
||||
if (host == null || !isIpLiteral(host)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(host), host);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
LOG.warnv("Refusing to deliver to SNS endpoint {0}: {1}", endpoint, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** True for a bracketed IPv6 literal or a dotted IPv4 one, neither of which needs resolving. */
|
||||
private static boolean isIpLiteral(String host) {
|
||||
String bare = host.startsWith("[") && host.endsWith("]")
|
||||
? host.substring(1, host.length() - 1)
|
||||
: host;
|
||||
return bare.indexOf(':') >= 0 || bare.matches("[0-9.]+");
|
||||
}
|
||||
|
||||
private String sqsArnToUrl(String arn) {
|
||||
if (arn == null) return null;
|
||||
if (arn.startsWith("http")) return arn;
|
||||
|
||||
@@ -14,6 +14,8 @@ import io.github.hectorvent.floci.services.sns.model.Topic;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -149,6 +151,74 @@ class SnsServiceTest {
|
||||
assertTrue(snsService.listSubscriptions(REGION).isEmpty());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"http://169.254.169.254/latest/meta-data/",
|
||||
"http://169.254.170.2/v2/credentials",
|
||||
"https://[fe80::1]/",
|
||||
"http://[fd00:ec2::254]/latest/meta-data/"
|
||||
})
|
||||
void subscribe_rejectsAnEndpointOnALinkLocalOrMetadataAddress(String endpoint) {
|
||||
Topic topic = snsService.createTopic("ssrf-topic", null, null, REGION);
|
||||
String protocol = endpoint.startsWith("https://") ? "https" : "http";
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> snsService.subscribe(
|
||||
topic.getTopicArn(), protocol, endpoint, REGION, Map.of()), endpoint);
|
||||
|
||||
assertEquals("InvalidParameter", ex.getErrorCode());
|
||||
assertTrue(snsService.listSubscriptions(REGION).isEmpty(), endpoint);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"http://127.0.0.1:8080/hook",
|
||||
"http://172.17.0.2:9000/hook",
|
||||
"http://10.0.0.5/hook",
|
||||
"https://example.test/hook"
|
||||
})
|
||||
void subscribe_stillAcceptsLoopbackPrivateAndOrdinaryEndpoints(String endpoint) {
|
||||
Topic topic = snsService.createTopic("ok-topic", null, null, REGION);
|
||||
String protocol = endpoint.startsWith("https://") ? "https" : "http";
|
||||
|
||||
Subscription sub = snsService.subscribe(topic.getTopicArn(), protocol, endpoint, REGION, Map.of());
|
||||
|
||||
assertEquals(endpoint, sub.getEndpoint());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"http://169.254.169.254/latest/meta-data/",
|
||||
"http://[fd00:ec2::254]/",
|
||||
"http://169.254.170.2/v2/credentials"
|
||||
})
|
||||
void delivery_refusesAnEndpointThatNamesTheAddressOutright(String endpoint) {
|
||||
// Reaches a subscription stored before Subscribe began refusing these, and costs no lookup.
|
||||
assertFalse(SnsService.deliverable(endpoint), endpoint);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"http://127.0.0.1:8080/hook", "http://10.0.0.5/hook", "https://example.test/x"})
|
||||
void delivery_stillAllowsLoopbackPrivateAndOrdinaryEndpoints(String endpoint) {
|
||||
assertTrue(SnsService.deliverable(endpoint), endpoint);
|
||||
}
|
||||
|
||||
@Test
|
||||
void delivery_doesNotResolveANameAndSoAllowsOne() {
|
||||
// Names are screened at Subscribe. Resolving again here could not decide anything, because
|
||||
// HttpClient resolves once more when it connects, so this deliberately does not try.
|
||||
assertTrue(SnsService.deliverable("http://sns-endpoint-that-does-not-resolve.invalid/hook"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscribe_rejectsAnEndpointWithNoHost() {
|
||||
Topic topic = snsService.createTopic("nohost-topic", null, null, REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> snsService.subscribe(
|
||||
topic.getTopicArn(), "http", "http:///no-host", REGION, Map.of()));
|
||||
|
||||
assertEquals("InvalidParameter", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void subscribe_firehoseRoleArnIsReturnedAndCanBeUpdated() {
|
||||
Topic topic = snsService.createTopic("firehose-topic", null, null, REGION);
|
||||
|
||||
Reference in new issue
Block a user