mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 15:49:33 -05:00
fix(cloudfront): serve custom origins that name Floci itself in-process (#6951)
* fix(cloudfront): serve custom origins that name Floci itself in-process * fix(cloudfront): keep self-origin requests inside the origin path and accept the TLS AWS port Resolve the viewer's dot segments before prepending the origin path, as CloudFront does, so a path like /x/../../other-bucket/key can no longer reach another bucket. Recognize the extra listener the TLS proxy binds on floci.tls.aws-https-port as Floci's own port. * docs(cloudfront): explain why self-origin dot segments are resolved first CloudFront forwards the raw path; the guard is needed because Floci's listener resolves dot segments. Give the ignored exception in selfHost its reason.
This commit is contained in:
1 parent
29e44332f2
commit
43a01e2688
4 files changed
+278
-7
No files matched your search
+9
-2
@@ -674,6 +674,14 @@ final class CloudFrontOriginHttpClient implements AutoCloseable {
|
||||
client.close();
|
||||
}
|
||||
|
||||
/** The origin host resolves to an address the private-address guard refuses. */
|
||||
static final class BlockedOriginHostException extends UnknownHostException {
|
||||
|
||||
BlockedOriginHostException(String host) {
|
||||
super("CloudFront origin host resolves to a blocked address: " + host);
|
||||
}
|
||||
}
|
||||
|
||||
private record ValidatingDnsResolver(DnsResolver delegate, Set<String> allowedHosts)
|
||||
implements DnsResolver {
|
||||
|
||||
@@ -687,8 +695,7 @@ final class CloudFrontOriginHttpClient implements AutoCloseable {
|
||||
if (!allowedHosts.contains(normalizedHost)) {
|
||||
for (InetAddress address : addresses) {
|
||||
if (SsrfProtection.isBlockedAddress(address)) {
|
||||
throw new UnknownHostException(
|
||||
"CloudFront origin host resolves to a blocked address: " + normalizedHost);
|
||||
throw new BlockedOriginHostException(normalizedHost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+56
-5
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cloudfront;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.TlsProxyServer;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.apigateway.ApiGatewayExecuteApiHostFilter;
|
||||
@@ -118,9 +119,11 @@ public class CloudFrontServingController {
|
||||
private final ApiGatewayV2Service apiGatewayV2Service;
|
||||
private final ApiGatewayExecuteApiHostFilter executeApiHostFilter;
|
||||
private final String flociHostname;
|
||||
private final int flociPort;
|
||||
private final LambdaWebIamAuth lambdaWebIamAuth;
|
||||
private final CurrentVertxRequest currentVertxRequest;
|
||||
private final HttpServer httpServer;
|
||||
private final TlsProxyServer tlsProxyServer;
|
||||
private final CloudFrontOriginHttpClient httpClient;
|
||||
private final CloudFrontOriginHttpClient flociClient = CloudFrontOriginHttpClient.toFloci();
|
||||
|
||||
@@ -130,7 +133,7 @@ public class CloudFrontServingController {
|
||||
ApiGatewayService apiGatewayService, ApiGatewayV2Service apiGatewayV2Service,
|
||||
ApiGatewayExecuteApiHostFilter executeApiHostFilter,
|
||||
EmulatorConfig emulatorConfig, CurrentVertxRequest currentVertxRequest,
|
||||
HttpServer httpServer) {
|
||||
HttpServer httpServer, TlsProxyServer tlsProxyServer) {
|
||||
this.service = service;
|
||||
this.s3Service = s3Service;
|
||||
this.lambdaWebService = lambdaWebService;
|
||||
@@ -138,9 +141,11 @@ public class CloudFrontServingController {
|
||||
this.apiGatewayV2Service = apiGatewayV2Service;
|
||||
this.executeApiHostFilter = executeApiHostFilter;
|
||||
this.flociHostname = emulatorConfig.hostname().orElse("localhost");
|
||||
this.flociPort = emulatorConfig.port();
|
||||
this.lambdaWebIamAuth = lambdaWebIamAuth;
|
||||
this.currentVertxRequest = currentVertxRequest;
|
||||
this.httpServer = httpServer;
|
||||
this.tlsProxyServer = tlsProxyServer;
|
||||
this.httpClient = new CloudFrontOriginHttpClient(
|
||||
emulatorConfig.services().cloudfront().allowedPrivateOriginHosts().orElse(List.of()));
|
||||
}
|
||||
@@ -787,16 +792,18 @@ public class CloudFrontServingController {
|
||||
// Floci serves its own Web Function and execute-api endpoints, whatever port and protocol the origin names.
|
||||
String webEndpoint = webFunctionEndpoint(origin);
|
||||
String executeApiHost = webEndpoint == null ? executeApiHost(origin) : null;
|
||||
boolean inProcess = webEndpoint != null || executeApiHost != null;
|
||||
String selfHost = webEndpoint == null && executeApiHost == null ? selfHost(origin, viewerScheme) : null;
|
||||
boolean inProcess = webEndpoint != null || executeApiHost != null || selfHost != null;
|
||||
if (inProcess && originRequest.headers().stream()
|
||||
.anyMatch(header -> "Host".equalsIgnoreCase(header.name()))) {
|
||||
// CloudFront checks the endpoint's certificate against the forwarded viewer Host, which it never names.
|
||||
// CloudFront checks the endpoint's certificate against the forwarded viewer Host, which it never
|
||||
// names, and a self origin would route the distribution's own Host back to the distribution.
|
||||
LOG.debugv("CloudFront forwards the viewer Host to in-process origin {0}", origin.getId());
|
||||
return OriginResponse.error(502, "Bad Gateway.");
|
||||
}
|
||||
// Dot segments would be resolved after the host filter's check and escape the API's stage.
|
||||
String originPath = executeApiHost != null
|
||||
? CloudFrontRequestRouter.normalizePath(rawPath(forwardUri)) : forwardUri;
|
||||
String originPath = executeApiHost != null ? CloudFrontRequestRouter.normalizePath(rawPath(forwardUri))
|
||||
: selfHost != null ? selfOriginPath(origin, rawPath(forwardUri)) : forwardUri;
|
||||
if (executeApiHost != null && !executeApiHostFilter.routes(executeApiHost, originPath)) {
|
||||
byte[] notFound = ApiGatewayExecuteApiHostFilter.NOT_FOUND_BODY.getBytes(StandardCharsets.UTF_8);
|
||||
return new OriginResponse(404, MediaType.APPLICATION_JSON, notFound, notFound.length, Map.of());
|
||||
@@ -823,6 +830,9 @@ public class CloudFrontServingController {
|
||||
if (executeApiHost != null) {
|
||||
originHeaders.put("Host", executeApiHost);
|
||||
}
|
||||
if (selfHost != null) {
|
||||
originHeaders.put("Host", selfHost);
|
||||
}
|
||||
if (webEndpoint != null) {
|
||||
originHeaders.put("Host", webEndpoint);
|
||||
if (alwaysAmzAuth(origin)) {
|
||||
@@ -852,6 +862,11 @@ public class CloudFrontServingController {
|
||||
} catch (CloudFrontOriginTimeoutException e) {
|
||||
LOG.warnv("CloudFront custom origin timed out for host {0}", safeOriginName(origin));
|
||||
return OriginResponse.error(504, ORIGIN_TIMEOUT_MESSAGE);
|
||||
} catch (CloudFrontOriginHttpClient.BlockedOriginHostException e) {
|
||||
LOG.warnv("CloudFront custom origin host {0} resolves to a private or reserved address;"
|
||||
+ " list it in floci.services.cloudfront.allowed-private-origin-hosts to allow it",
|
||||
safeOriginName(origin));
|
||||
return OriginResponse.error(502, "Bad Gateway.");
|
||||
} catch (Exception e) {
|
||||
// Do not log the target URI: forwarded query strings can contain credentials or tokens.
|
||||
LOG.warnv("CloudFront custom origin fetch failed for host {0}: {1}",
|
||||
@@ -875,6 +890,42 @@ public class CloudFrontServingController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The origin's host when it names this Floci: its hostname, {@code localhost} or a loopback
|
||||
* address, on the port Floci listens on for the protocol the request would use. Such an origin,
|
||||
* typically a path-style S3 bucket, is served in-process, so the private-address guard that
|
||||
* protects the other custom origins does not refuse it.
|
||||
*/
|
||||
private String selfHost(Origin origin, String viewerScheme) {
|
||||
String host;
|
||||
try {
|
||||
host = normalizeOriginHost(origin.getDomainName());
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// An invalid domain cannot name Floci; the normal origin fetch rejects it.
|
||||
return null;
|
||||
}
|
||||
boolean flociHost = host.equalsIgnoreCase(flociHostname) || host.equalsIgnoreCase("localhost")
|
||||
|| host.equals("127.0.0.1") || host.equals("::1");
|
||||
if (!flociHost) {
|
||||
return null;
|
||||
}
|
||||
int port = buildCustomOriginUri(origin, viewerScheme, "/", null).getPort();
|
||||
return port == flociPort || port == httpServer.getPort() || tlsProxyServer.reservesPort(port)
|
||||
? origin.getDomainName().trim() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The path a self origin is asked for. CloudFront sends the viewer's path to the origin as it
|
||||
* came, but Floci's listener resolves dot segments, so they would climb out of the bucket the
|
||||
* origin path names. Resolving them within the viewer's part first keeps the request inside it.
|
||||
*/
|
||||
private static String selfOriginPath(Origin origin, String forwardPath) {
|
||||
String prefix = CloudFrontRequestRouter.joinPath(origin.getOriginPath(), "");
|
||||
prefix = prefix.substring(0, prefix.length() - 1);
|
||||
String viewerPath = forwardPath.substring(prefix.length());
|
||||
return CloudFrontRequestRouter.normalizePath(prefix + CloudFrontRequestRouter.normalizePath(viewerPath));
|
||||
}
|
||||
|
||||
/**
|
||||
* The origin's host when it is an execute-api endpoint of an HTTP or REST API that exists in this
|
||||
* Floci, which the execute-api host filter then routes, and null otherwise.
|
||||
|
||||
Reference in new issue
Block a user