Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(cloudfront): serve distribution traffic (data plane, managed policies) (#1931)

Jeff Heller036d6d012a55
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 096cdf8c..d2f97950 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Added
+
+- **CloudFront — serves distribution traffic, plus the AWS-managed policy catalog and monitoring subscriptions** — MiniStack's CloudFront was control-plane only: a distribution's `DomainName` stored nothing a client could reach. A viewer request to `<label>.cloudfront.net` (or the resolvable `<label>.cloudfront.<MINISTACK_HOST>` form) is now actually served — ordered cache-behavior path-pattern matching, `ViewerProtocolPolicy`/`AllowedMethods`/`DefaultRootObject`, cache-policy and origin-request-policy header/cookie/query-string forwarding (plus legacy `ForwardedValues`), `viewer-request`/`viewer-response` CloudFront Functions (`cloudfront-js-1.0`/`-2.0`, on a shared Node worker pool also used by AppSync resolvers), a response-headers policy's custom/security/CORS headers, an S3 or custom origin fetch (a custom origin is dialed at its own `DomainName`/port exactly as configured, trusting the gateway's own TLS cert alongside system roots under `USE_SSL`; a stacked distribution — an origin that is itself a CloudFront distribution — is refused with a 403 instead of recursing), and CloudFront's own added headers. `X-Forwarded-For` always carries the viewer's chain plus the client IP regardless of forwarding policy, a function event's `viewer.ip` is the real TCP peer, and a header with several values is forwarded to the origin as one comma-joined line (RFC 9110 §5.3). `DomainName` is now the real `d` + 13-character shape, independent of the distribution's Id. `ListCachePolicies`/`ListOriginRequestPolicies`/`ListResponseHeadersPolicies` seed the AWS-managed catalog (7 cache, 8 origin-request, 5 response-headers policies with a published configuration), so the Terraform data sources that resolve them by name (e.g. `Managed-CachingDisabled`) now find them. `CreateMonitoringSubscription`/`GetMonitoringSubscription`/`DeleteMonitoringSubscription` store a distribution's real-time-metrics subscription status. Not modelled: caching itself, WAF, logging, geo/IP restrictions, custom error pages, signed URLs/cookies, Lambda@Edge, CORS preflight responses, and origins addressed by an AWS hostname (ALB, Function URL, execute-api, S3 website endpoints) — refused with a 502 before a socket opens. Contributed by @skialpine (#1852).
### Fixed
- **API Gateway v2 (WebSocket API) — `$connect` authorization** — a `CUSTOM` `$connect` route ran no authorizer. It now runs its REQUEST authorizer, refuses the handshake with 401, 403 or 500 and passes `principalId` and the context to `requestContext.authorizer` of the connection's events. `CreateAuthorizer`, `CreateRoute`, `UpdateRoute` and the CloudFormation resources refuse a JWT authorizer, JWT route authorization and authorization on a route other than `$connect` with `BadRequestException`, as AWS does; a JWT `$connect` route kept in saved state refuses the handshake with 500 instead of validating the token. `requestContext.stage` names the stage in the connection URL instead of `$default`.
diff --git a/README.md b/README.md
index 4549312b..9af035e5 100644
--- a/README.md
+++ b/README.md
@@ -442,7 +442,7 @@ subnet = ec2.create_subnet(
| **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateDocumentationPart, GetDocumentationPart, GetDocumentationParts, UpdateDocumentationPart, DeleteDocumentationPart, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags, CreateRequestValidator, GetRequestValidators, GetRequestValidator, UpdateRequestValidator, DeleteRequestValidator | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), non-proxy Lambda (AWS — the handler's output is the response body, mapping templates not modeled), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; routing is on the resource+method pair, so a request the matched resource does not serve falls through to a `{proxy+}` elsewhere in the tree (a methodless intermediate, a CORS-preflight-only node, or any undeclared verb), as it does on AWS; TOKEN and REQUEST Lambda authorizers on the data plane, with the returned IAM policy evaluated against each request's own method ARN and `authorizerResultTtlInSeconds` caching the policy per stage and identity source; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag |
| **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response`, `authenticate-oidc` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |
| **KMS** | CreateKey, ReplicateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateMac, VerifyMac, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, GenerateRandom, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, UpdateKeyDescription, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags, GetParametersForImport, ImportKeyMaterial, DeleteImportedKeyMaterial, CreateGrant, ListGrants, RevokeGrant, RetireGrant | 41 actions; multi-Region keys: `CreateKey` with `MultiRegion` mints an `mrk-` key and `ReplicateKey` creates a same-id replica in another region sharing the key material (ciphertext from the primary decrypts against the replica; a primary with live replicas waits in `PendingReplicaDeletion`); CMK specs: RSA (2048/3072/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), HMAC (`HMAC_224`/`HMAC_256`/`HMAC_384`/`HMAC_512` with `KeyUsage=GENERATE_VERIFY_MAC`), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; real RFC 2104 HMAC via `GenerateMac`/`VerifyMac` (4096-byte message cap, `KMSInvalidMacException` on mismatch, `DryRun` supported); envelope encryption, incl. asymmetric data key pairs (`KeyPairSpec` adds RSA_3072); alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; grants (stored and listed, not evaluated for authorization); full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image |
-| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, **Public keys / key groups**: CreatePublicKey, GetPublicKey, GetPublicKeyConfig, UpdatePublicKey, DeletePublicKey, ListPublicKeys, CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig, UpdateKeyGroup, DeleteKeyGroup, ListKeyGroups, **SaaS Manager**: CreateConnectionGroup, GetConnectionGroup, GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup, DeleteConnectionGroup, ListConnectionGroups, CreateDistributionTenant, GetDistributionTenant, GetDistributionTenantByDomain, UpdateDistributionTenant, DeleteDistributionTenant, ListDistributionTenants, ListDistributionTenantsByCustomization, AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL, CreateInvalidationForDistributionTenant, GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant, VerifyDnsConfiguration, GetManagedCertificateDetails, ListDomainConflicts, UpdateDomainAssociation, ListDistributionsByConnectionMode, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`, no viewer-request JS execution at the edge; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again; **SaaS Manager** multi-tenant distributions: `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig`, tenants require a `tenant-only` distribution, CNAMEs are unique across tenants and distribution aliases, a tenant without an explicit connection group lazily gets the account default group, and MiniStack collapses async workflows (tenants deploy, domains activate, DNS verifies, managed certificates issue immediately); key groups validate `Items` against real public keys, and deleting either while still referenced (`PublicKeyInUse` / `ResourceInUse`) is rejected |
+| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateMonitoringSubscription, GetMonitoringSubscription, DeleteMonitoringSubscription, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, **Public keys / key groups**: CreatePublicKey, GetPublicKey, GetPublicKeyConfig, UpdatePublicKey, DeletePublicKey, ListPublicKeys, CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig, UpdateKeyGroup, DeleteKeyGroup, ListKeyGroups, **SaaS Manager**: CreateConnectionGroup, GetConnectionGroup, GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup, DeleteConnectionGroup, ListConnectionGroups, CreateDistributionTenant, GetDistributionTenant, GetDistributionTenantByDomain, UpdateDistributionTenant, DeleteDistributionTenant, ListDistributionTenants, ListDistributionTenantsByCustomization, AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL, CreateInvalidationForDistributionTenant, GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant, VerifyDnsConfiguration, GetManagedCertificateDetails, ListDomainConflicts, UpdateDomainAssociation, ListDistributionsByConnectionMode, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; `ListCachePolicies`/`ListOriginRequestPolicies`/`ListResponseHeadersPolicies` seed the AWS-managed catalog (7 cache, 8 origin-request, 5 response-headers policies with a published configuration), immutable and unaffected by `/_ministack/reset`; a monitoring subscription is stored on its distribution, `CreateMonitoringSubscription` upserting it; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again; **data plane**: a viewer request to a distribution's `DomainName` (the real `d` + 13-character shape, or the resolvable `<label>.cloudfront.<MINISTACK_HOST>` form) is actually served — ordered cache-behavior path matching, viewer-protocol/allowed-methods/default-root-object enforcement, cache-policy and origin-request-policy forwarding (plus legacy `ForwardedValues`), `viewer-request`/`viewer-response` CloudFront Functions evaluated on Node (`cloudfront-js-1.0`/`-2.0`, the same worker pool AppSync resolvers use), response-headers-policy custom/security/CORS headers, and an S3 or custom origin fetch (a custom origin is dialed at its own DomainName/port exactly as configured; an explicit `:port` in the DomainName, which MiniStack's own endpoints such as an HTTP API's `apiEndpoint` carry, overrides `HTTPPort`/`HTTPSPort`); not modelled: caching itself, WAF, logging, geo/IP restrictions, custom error pages, signed URLs/cookies, Lambda@Edge, CORS preflight responses, and origins addressed by an AWS hostname (ALB, Function URL, execute-api, S3 website endpoints) — refused with a 502 before a socket opens, since MiniStack never dials a real AWS host; **SaaS Manager** multi-tenant distributions: `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig`, tenants require a `tenant-only` distribution, CNAMEs are unique across tenants and distribution aliases, a tenant without an explicit connection group lazily gets the account default group, and MiniStack collapses async workflows (tenants deploy, domains activate, DNS verifies, managed certificates issue immediately); key groups validate `Items` against real public keys, and deleting either while still referenced (`PublicKeyInUse` / `ResourceInUse`) is rejected |
| **CloudFront KeyValueStore (data plane)** | DescribeKeyValueStore, ListKeys, GetKey, PutKey, DeleteKey, UpdateKeys | Separate `cloudfront-keyvaluestore` SDK service (REST/JSON, signing name `cloudfront-keyvaluestore`); ETag-based optimistic concurrency on every mutating op; `UpdateKeys` is atomic (validates whole batch, rejects on first invalid entry); `ListKeys` paginates with opaque `NextToken` capped at 50 results per AWS spec; bridges to the management plane via the shared KVS ARN |
| **CloudTrail** | LookupEvents, CreateTrail, DeleteTrail, GetTrail, DescribeTrails, ListTrails, UpdateTrail, GetTrailStatus, StartLogging, StopLogging, PutEventSelectors, GetEventSelectors, AddTags, ListTags, RemoveTags | In-memory audit log; recording opt-in via `CLOUDTRAIL_RECORDING=1` (or runtime config endpoint); per-account ring buffer (`CLOUDTRAIL_MAX_EVENTS=10000`); `LookupEvents` supports all 8 AWS `LookupAttributes`; `IsLogging` flips on Start/StopLogging; `CreateTrail`/`UpdateTrail` persist `KmsKeyId` (normalized to a key ARN; omitted when unset) so `aws_cloudtrail` converges without drift |
| **Resource Groups** | CreateGroup, GetGroup, DeleteGroup, UpdateGroup, ListGroups, GetGroupQuery, UpdateGroupQuery, GetGroupConfiguration, PutGroupConfiguration, GroupResources, UngroupResources, ListGroupResources, ListGroupingStatuses, SearchResources, Tag, Untag, GetTags, GetAccountSettings, UpdateAccountSettings | 19 of 23 spec ops; tag-sync ops omitted (not exposed by AWS CLI / Terraform); `Group` accepts bare name or full ARN |
diff --git a/ministack/app.py b/ministack/app.py
index a8106d9e..21fe15e2 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -47,6 +47,13 @@ def _version() -> str:
# Matches host headers like "{apiId}.execute-api.<host>" or "{apiId}.execute-api.<host>:4566"
_EXECUTE_API_RE = re.compile(r"^([a-f0-9]{8})\.execute-api\." + re.escape(_MINISTACK_HOST) + r"(?::\d+)?$")
+# A CloudFront distribution's DomainName is always AWS-shaped
+# ("<label>.cloudfront.net"); matched the same way ALB's own
+# ".elb.amazonaws.com" / ".alb.localhost" pair is — the real AWS host
+# unconditionally, plus the resolvable local form built on MINISTACK_HOST.
+_CLOUDFRONT_HOST_RE = re.compile(
+ r"^([a-z0-9]+)\.cloudfront\.(?:net|" + re.escape(_MINISTACK_HOST) + r")(?::\d+)?$"
+)
# Lambda Function URL: {urlId}.lambda-url.{region}.<anything>[:port]. The stored
# FunctionUrl carries AWS's own `.on.aws` suffix, so we match any suffix rather
# than only _MINISTACK_HOST — pointing a proxy or an /etc/hosts entry at the
@@ -154,7 +161,7 @@ def _extract_s3_vhost_bucket(host: str):
_S3_VHOST_EXCLUDE_RE = re.compile(
- r"\.(execute-api|lambda-url|alb|emr|efs|elasticache|s3-control|appsync-api|appsync-realtime-api|iot)\."
+ r"\.(execute-api|lambda-url|alb|emr|efs|elasticache|s3-control|appsync-api|appsync-realtime-api|iot|cloudfront)\."
)
_HEALTH_PATHS = ("/_ministack/health", "/_localstack/health", "/health")
_BODY_METHODS = ("POST", "PUT", "PATCH")
@@ -1180,6 +1187,7 @@ async def _handle_pre_body_request(method: str, path: str, headers: dict, query_
_parse_execute_api_url(host, path) is not None
or _parse_lambda_url(host, path) is not None
or _resolve_custom_domain_request(host, path) is not None
+ or _parse_cloudfront_dataplane_host(host) is not None
)
for response in (
None if owns_cors else _handle_options_request(method, request_id),
@@ -1732,6 +1740,43 @@ def _resolve_custom_domain_request(host: str, path: str):
return apigw_v1.resolve_base_path_mapping(hostname, path)
+def _parse_cloudfront_dataplane_host(host: str) -> str | None:
+ """The distribution label addressed by ``host``, or None.
+
+ Host-based only, like ALB's own ``.elb.amazonaws.com`` / ``.alb.localhost``
+ pair and the S3-vhost check: a request whose first label doesn't match
+ this shape isn't a CloudFront distribution request at all, so nothing
+ existing changes."""
+ m = _CLOUDFRONT_HOST_RE.match(host.split(":")[0].lower())
+ return m.group(1) if m else None
+
+
+async def _handle_cloudfront_dataplane_request(
+ host: str, path: str, raw_path: str, raw_query_string: str, method: str,
+ headers: dict, body: bytes, query_params: dict, client_ip: str,
+):
+ """Serve a CloudFront distribution's viewer traffic (Host-based:
+ ``<label>.cloudfront.net`` or ``<label>.cloudfront.<MINISTACK_HOST>``).
+ Checked before execute-api/lambda-url/S3-vhost/ALB host routing so a
+ distribution's own viewer-request function — not the origin it fronts —
+ sees the request first, exactly as a real CloudFront edge would. See
+ ``cloudfront_dataplane.handle_request`` for what ``raw_path``/
+ ``raw_query_string`` carry and why."""
+ label = _parse_cloudfront_dataplane_host(host)
+ if label is None:
+ return None
+ dist = _get_module("cloudfront").find_distribution_for_label(label)
+ if dist is None:
+ return None
+ try:
+ return await _get_module("cloudfront_dataplane").handle_request(
+ dist, method, path, raw_path, raw_query_string, headers, body, query_params, client_ip,
+ )
+ except Exception as e:
+ logger.exception("Error in CloudFront data-plane dispatch: %s", e)
+ return 500, {"Content-Type": "application/json"}, json.dumps({"message": str(e)}).encode()
+
+
async def _handle_execute_api_request(
host: str, path: str, method: str, headers: dict, body: bytes, query_params: dict,
raw_path: str | None = None,
@@ -2122,6 +2167,8 @@ async def _handle_special_data_plane_request(
query_params: dict,
request_id: str,
raw_path: str | None = None,
+ raw_query_string: str = "",
+ client_ip: str = "127.0.0.1",
):
"""Handle special-case service entrypoints before the generic router."""
# Iceberg REST catalog — /iceberg/* is served by two catalogs that share the
@@ -2156,6 +2203,11 @@ async def _handle_special_data_plane_request(
return _with_data_plane_headers(response, request_id)
host = headers.get("host", "")
+ if response := await _handle_cloudfront_dataplane_request(
+ host, path, raw_path if raw_path is not None else path, raw_query_string, method,
+ headers, body, query_params, client_ip,
+ ):
+ return _with_data_plane_headers(response, request_id, wildcard_cors=False)
if response := await _handle_execute_api_request(
host, path, method, headers, body, query_params, raw_path=raw_path
):
@@ -2727,6 +2779,8 @@ async def app(scope, receive, send):
await _handle_special_data_plane_request(
method, path, headers, body, query_params, request_id,
raw_path=scope["raw_path"].decode("ascii") if scope.get("raw_path") else None,
+ raw_query_string=query_string,
+ client_ip=(scope.get("client") or ("127.0.0.1",))[0],
),
receive,
):
diff --git a/ministack/core/appsync_js.py b/ministack/core/appsync_js.py
index f90ed788..22f39b2b 100644
--- a/ministack/core/appsync_js.py
+++ b/ministack/core/appsync_js.py
@@ -3,18 +3,10 @@
"""APPSYNC_JS resolver evaluation.
AppSync resolvers are ES modules exporting `request(ctx)` and `response(ctx)`.
-Evaluating them needs a JavaScript engine, and the image already ships Node for
-the `nodejs*` Lambda runtimes (`Dockerfile`, `core/lambda_runtime.py`), so this
-reuses it rather than adding a dependency.
-
-A pool of workers handles evaluations over a JSON-line protocol, the same
-shape `lambda_runtime` uses: a request per line on stdin, a reply per line on
-stdout, one evaluation in flight per worker. A free worker is leased per
-evaluation and one is spawned when none is free — never queued behind, which
-would recreate the re-entrancy deadlock `lambda_runtime` documents. Each
-worker caches compiled modules by the hash of their source, evaluations are
-bounded by a timeout that kills and respawns a stuck process, and resolver
-stderr is surfaced through the service logger.
+Evaluation runs on the shared `core.node_pool` worker pool (see that module
+for the process management, JSON-line protocol, leasing, recycling, and
+timeout mechanics); each worker caches compiled modules by the hash of their
+source.
Fidelity note: Node is more permissive than the real APPSYNC_JS sandbox, which
forbids `async`/`await`, `try`/`catch` and most globals. A resolver that runs
@@ -24,14 +16,12 @@ work; `aws appsync evaluate-code` is the check for that.
from __future__ import annotations
-import json
import logging
-import subprocess
-import threading
+
+from ministack.core import node_pool
logger = logging.getLogger("appsync")
-_NODE_BINARY = "node"
# AppSync ends a request after 30 seconds, so no single evaluation may outlive
# that. An infinite loop cannot be interrupted from inside Node (vm timeouts do
# not cover async continuations); on expiry the process is killed and respawned.
@@ -524,192 +514,53 @@ class AppSyncJsTimeout(RuntimeError):
"""An evaluation outlived its deadline; the worker process was killed."""
-class _Worker:
- """One Node process, running one evaluation at a time.
-
- A worker holds no evaluation-independent state other than its compiled-
- module cache, so any free worker can serve any resolver. Single-flight per
- worker matters beyond throughput: the compiled module's mutable holder
- (``state`` in the worker script) is per-process, so two evaluations of the
- same resolver in one process would corrupt each other's appended errors.
- """
-
- def __init__(self):
- self._proc = None
- self._lock = threading.Lock()
- self.in_use = False
- self.evals = 0
-
- def _ensure(self):
- if self._proc is not None and self._proc.poll() is None:
- return self._proc
- self._proc = subprocess.Popen(
- [_NODE_BINARY, f"--max-old-space-size={_MAX_OLD_SPACE_MB}",
- "-e", _WORKER_SCRIPT],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- text=True, bufsize=1,
- )
- # Resolver console.log lands on stderr (stdout carries the protocol);
- # surface it through the service logger rather than dropping it.
- threading.Thread(target=_pump_stderr, args=(self._proc,), daemon=True).start()
- return self._proc
-
- def _take_proc(self):
- proc, self._proc = self._proc, None
- return proc
-
- def evaluate(self, code, fn, ctx, timeout=None):
- """Run `fn` (request/response) from `code` against `ctx`.
-
- Returns (status, value, appended_errors, stash, skip_to) where status is
- "ok", "earlyReturn" or "missing". Raises AppSyncJsError on a resolver
- error, AppSyncJsTimeout when the evaluation outlives `timeout` (the
- worker is killed — an infinite loop cannot be interrupted from inside
- Node — and a fresh process spawns on the next call), and RuntimeError
- when the worker cannot be used at all.
- """
- timeout = _EVAL_TIMEOUT if timeout is None else timeout
- payload = json.dumps({"code": code, "fn": fn, "ctx": ctx}) + "\n"
- with self._lock:
- self.evals += 1
- try:
- proc = self._ensure()
- proc.stdin.write(payload)
- proc.stdin.flush()
- except FileNotFoundError as exc: # no node on PATH
- raise RuntimeError(
- "APPSYNC_JS resolvers need Node, which was not found") from exc
- except (BrokenPipeError, OSError) as exc:
- self._proc = None
- raise RuntimeError(f"APPSYNC_JS worker failed: {exc}") from exc
-
- # readline on a thread so a stuck resolver is bounded by `timeout`
- # rather than holding this worker forever — the same shape
- # core/lambda_runtime.py uses for a handler that never returns.
- box = []
-
- def _read():
- try:
- box.append(proc.stdout.readline())
- except Exception:
- box.append("")
-
- reader = threading.Thread(target=_read, daemon=True)
- reader.start()
- reader.join(timeout)
- if reader.is_alive():
- _terminate(self._take_proc())
- raise AppSyncJsTimeout(
- f"APPSYNC_JS evaluation exceeded {int(timeout)} seconds and was cancelled")
- line = box[0] if box else ""
- if not line:
- self._proc = None
- raise RuntimeError("APPSYNC_JS worker closed unexpectedly")
-
- out = json.loads(line)
- if out["status"] == "error":
- err = out["error"]
- raise AppSyncJsError(err["message"], err.get("errorType"),
- err.get("data"), err.get("errorInfo"))
- return (out["status"], out.get("value"), out.get("appended") or [],
- out.get("stash") or {}, out.get("skipTo"))
-
- def shutdown(self):
- with self._lock:
- _terminate(self._take_proc())
-
-
-def _pump_stderr(proc):
- try:
- for line in proc.stderr:
- line = line.rstrip("\n")
- if line:
- logger.info("[appsync-js] %s", line)
- except Exception:
- pass
-
-
-def _terminate(proc):
- if proc is None:
- return
- try:
- proc.terminate()
- try:
- proc.wait(timeout=2)
- except subprocess.TimeoutExpired:
- proc.kill()
- proc.wait(timeout=2)
- except Exception:
- pass
-
-
-# ---------------------------------------------------------------------------
-# Worker pool
-#
-# Lease a free worker or spawn one; never queue behind a busy worker. Waiting
-# would recreate the re-entrancy deadlock core/lambda_runtime.py documents: a
-# resolver whose data source calls back into ministack needs a second worker
-# while its own request still holds the first. Concurrency is already bounded
-# upstream by the asyncio.to_thread pool the data plane runs on, so unbounded
-# spawn here cannot run away.
-# ---------------------------------------------------------------------------
-
-_pool_lock = threading.Lock()
-_pool: list = []
-
-
-def _acquire():
- with _pool_lock:
- for worker in _pool:
- if not worker.in_use:
- worker.in_use = True
- return worker
- worker = _Worker()
- worker.in_use = True
- _pool.append(worker)
- return worker
-
-
-def _release(worker):
- to_kill = None
- with _pool_lock:
- worker.in_use = False
- if worker.evals >= _RECYCLE_AFTER:
- # Recycling bounds whatever a long-lived Node process accumulates
- # (compiled modules, heap fragmentation). The worker stays pooled
- # and respawns lazily on its next lease.
- worker.evals = 0
- to_kill = worker._take_proc()
- else:
- idle = [w for w in _pool if not w.in_use]
- if len(idle) > _MAX_IDLE and worker is not _pool[0]:
- # A burst leaves surplus processes behind; keep a couple warm
- # beyond the first and fold the rest.
- _pool.remove(worker)
- to_kill = worker._take_proc()
- _terminate(to_kill)
+# Concurrency is bounded upstream by the asyncio.to_thread pool the data
+# plane runs on, so the pool's unbounded spawn-when-busy cannot run away; see
+# core/node_pool.py for the re-entrancy reasoning.
+_pool = node_pool.NodeWorkerPool(
+ _WORKER_SCRIPT, log_prefix="appsync-js", logger=logger, timeout=_EVAL_TIMEOUT,
+ max_old_space_mb=_MAX_OLD_SPACE_MB, recycle_after=_RECYCLE_AFTER, max_idle=_MAX_IDLE,
+)
def evaluate(code, fn, ctx, timeout=None):
- worker = _acquire()
+ """Run `fn` (request/response) from `code` against `ctx`.
+
+ Returns (status, value, appended_errors, stash, skip_to) where status is
+ "ok", "earlyReturn" or "missing". Raises AppSyncJsError on a resolver
+ error, AppSyncJsTimeout when the evaluation outlives `timeout` (the
+ worker is killed — an infinite loop cannot be interrupted from inside
+ Node — and a fresh process spawns on the next call), and RuntimeError
+ when the worker cannot be used at all.
+ """
+ eval_timeout = _EVAL_TIMEOUT if timeout is None else timeout
try:
- return worker.evaluate(code, fn, ctx, timeout=timeout)
- finally:
- _release(worker)
+ out = _pool.call({"code": code, "fn": fn, "ctx": ctx}, timeout=eval_timeout)
+ except node_pool.NodeWorkerTimeout as exc:
+ raise AppSyncJsTimeout(
+ f"APPSYNC_JS evaluation exceeded {int(eval_timeout)} seconds and was cancelled"
+ ) from exc
+ except node_pool.NodeWorkerError as exc:
+ if exc.reason == "missing_node":
+ raise RuntimeError(
+ "APPSYNC_JS resolvers need Node, which was not found") from exc
+ if exc.reason == "closed":
+ raise RuntimeError("APPSYNC_JS worker closed unexpectedly") from exc
+ raise RuntimeError(f"APPSYNC_JS worker failed: {exc.cause_text}") from exc
+
+ if out["status"] == "error":
+ err = out["error"]
+ raise AppSyncJsError(err["message"], err.get("errorType"),
+ err.get("data"), err.get("errorInfo"))
+ return (out["status"], out.get("value"), out.get("appended") or [],
+ out.get("stash") or {}, out.get("skipTo"))
def reset():
"""Drop every worker, so a reset leaves no compiled-module cache behind."""
- with _pool_lock:
- workers, _pool[:] = _pool[:], []
- for worker in workers:
- worker.shutdown()
+ _pool.reset()
def available():
"""Whether APPSYNC_JS can be evaluated at all in this environment."""
- try:
- subprocess.run([_NODE_BINARY, "--version"], capture_output=True, timeout=5)
- return True
- except Exception:
- return False
+ return node_pool.available()
diff --git a/ministack/core/cloudfront_js.py b/ministack/core/cloudfront_js.py
new file mode 100644
index 00000000..4bb1701d
--- /dev/null
+++ b/ministack/core/cloudfront_js.py
@@ -0,0 +1,185 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
+"""CloudFront Functions evaluation.
+
+A CloudFront Function is a top-level ``function handler(event) {...}``
+(runtime `cloudfront-js-1.0`) or an ES-module-style function that may use
+`import`/`async` (`cloudfront-js-2.0`; CloudFront Developer Guide, "Writing
+function code for CloudFront Functions"). Evaluation runs on the shared
+`core.node_pool` worker pool (see that module for the process management,
+JSON-line protocol, leasing, recycling, and timeout mechanics); each worker
+caches compiled functions by the hash of their source, the same pattern
+`core/appsync_js.py` uses for resolvers.
+
+Fidelity note: Node is more permissive than the real CloudFront Functions
+runtime, which is a restricted ECMAScript subset (CloudFront Developer
+Guide, "Writing function code for CloudFront Functions" > "Restrictions and
+limitations", and "JavaScript runtime 2.0 features for CloudFront
+Functions"). Function code that runs here can still be refused by real
+CloudFront at publish/associate time. This module reproduces one documented
+restriction that is cheap to enforce and otherwise easy to get wrong: the
+`require()`/`import` surface is limited to `crypto`, `querystring`, and the
+`Buffer` module, not Node's full module graph. Runtime 2.0 also documents a
+fourth module, `cloudfront` (the KeyValueStore, `import cf from
+'cloudfront'`), which is not modelled here — importing it fails the same way
+an unrecognized Node module would.
+"""
+
+from __future__ import annotations
+
+import logging
+
+from ministack.core import node_pool
+
+logger = logging.getLogger("cloudfront_functions")
+
+# Real CloudFront Functions quota execution at ~1ms (CloudFront Developer
+# Guide, "Quotas on functions"), too tight for a subprocess-per-call Node
+# evaluation. This instead guards only against a runaway (infinite-loop)
+# function, borrowing Lambda@Edge's 5s viewer-request/response timeout as
+# the closest documented CloudFront figure.
+_EVAL_TIMEOUT = 5.0
+_MAX_OLD_SPACE_MB = 256
+_RECYCLE_AFTER = 1000
+_MAX_IDLE = 2
+
+# stdout carries the protocol, so anything the function logs goes to stderr.
+_WORKER_SCRIPT = r"""
+const readline = require("readline");
+const vm = require("vm");
+const crypto = require("crypto");
+const querystring = require("querystring");
+const bufferModule = require("buffer");
+
+const _stderrWrite = process.stderr.write.bind(process.stderr);
+const _stdoutWrite = process.stdout.write.bind(process.stdout);
+process.stdout.write = (chunk, enc, cb) => _stderrWrite(chunk, enc, cb);
+
+// CloudFront Functions documents a fixed require()/import surface (CloudFront
+// Developer Guide, "JavaScript runtime 2.0 features for CloudFront
+// Functions"): crypto, querystring, and the Buffer module. Node exposes a
+// much larger module graph; refusing everything else here makes a function
+// that imports e.g. "fs" or "http" fail the way it would on the real edge
+// runtime, instead of silently succeeding against Node's surface.
+const _ALLOWED_MODULES = { crypto, querystring, buffer: bufferModule };
+function sandboxRequire(name) {
+ if (Object.prototype.hasOwnProperty.call(_ALLOWED_MODULES, name)) return _ALLOWED_MODULES[name];
+ throw new Error("require('" + name + "') is not supported by CloudFront Functions");
+}
+
+const compiled = new Map();
+
+function compile(code) {
+ const key = crypto.createHash("sha256").update(code).digest("hex");
+ const hit = compiled.get(key);
+ if (hit) return hit;
+
+ // Runtime 1.0 code calls require() directly; runtime 2.0 code uses ES
+ // module `import` statements for the same three modules (CloudFront
+ // Developer Guide, "JavaScript runtime 2.0 features for CloudFront
+ // Functions"). Rather than a full ESM loader, a default or namespace
+ // import is rewritten to the equivalent `require()` call — sandboxRequire
+ // still enforces the allowed module set either way, and a named import
+ // (e.g. `import { createHash } from 'crypto'`) is rewritten the same way.
+ const stripped = code.replace(
+ /^\s*import\s+(?:(\*\s*as\s*(\w+))|\{([^}]*)\}|(\w+))\s+from\s+['"]([^'"]+)['"]\s*;?\s*$/gm,
+ (_m, _star, starName, named, deflt, source) => {
+ if (named) {
+ return named.split(",").map((part) => {
+ const [orig, alias] = part.split(/\s+as\s+/).map((x) => x.trim());
+ return orig ? `const ${alias || orig} = require(${JSON.stringify(source)}).${orig};` : "";
+ }).join("\n");
+ }
+ const name = starName || deflt;
+ return name ? `const ${name} = require(${JSON.stringify(source)});` : "";
+ },
+ );
+
+ const src = `${stripped}\n;globalThis.__handler = typeof handler === "function" ? handler : null;`;
+ const sandbox = {
+ require: sandboxRequire, console, JSON, Math, Date, Object, Array, String,
+ Number, Boolean, RegExp, Map, Set, Promise, Error, TypeError, RangeError,
+ TextEncoder, TextDecoder, atob, btoa, Buffer: bufferModule.Buffer,
+ };
+ vm.createContext(sandbox);
+ new vm.Script(src, { filename: "function.js" }).runInContext(sandbox);
+ const entry = { handler: sandbox.__handler };
+ compiled.set(key, entry);
+ return entry;
+}
+
+async function run(req) {
+ const { code, event } = req;
+ const entry = compile(code);
+ if (typeof entry.handler !== "function") {
+ return { status: "error", message: "function code does not define a top-level handler(event) function" };
+ }
+ try {
+ let value = entry.handler(event);
+ // cloudfront-js-2.0 allows async handlers (CloudFront Developer Guide,
+ // "JavaScript runtime 2.0 features"); 1.0 handlers return a plain value.
+ if (value && typeof value.then === "function") {
+ value = await value;
+ }
+ return { status: "ok", value: value === undefined ? null : value };
+ } catch (err) {
+ return { status: "error", message: String((err && err.stack) || err) };
+ }
+}
+
+const rl = readline.createInterface({ input: process.stdin });
+rl.on("line", async (line) => {
+ if (!line.trim()) return;
+ let out;
+ try {
+ out = await run(JSON.parse(line));
+ } catch (e) {
+ out = { status: "error", message: String((e && e.stack) || e) };
+ }
+ _stdoutWrite(JSON.stringify(out) + "\n");
+});
+"""
+
+
+class CloudFrontFunctionError(Exception):
+ """A function threw, failed to evaluate, or the worker could not run it
+ at all — CloudFront answers every one of these with a 503 (CloudFront
+ Developer Guide, "HTTP 503 status code" > "Lambda@Edge or CloudFront
+ Function execution error")."""
+
+
+_pool = node_pool.NodeWorkerPool(
+ _WORKER_SCRIPT, log_prefix="cloudfront-js", logger=logger, timeout=_EVAL_TIMEOUT,
+ max_old_space_mb=_MAX_OLD_SPACE_MB, recycle_after=_RECYCLE_AFTER, max_idle=_MAX_IDLE,
+)
+
+
+def evaluate(code: bytes, event: dict):
+ """Run a published function's code against `event`; return its returned
+ request/response object (a dict), or raise CloudFrontFunctionError."""
+ try:
+ out = _pool.call({"code": code.decode("utf-8"), "event": event}, timeout=_EVAL_TIMEOUT)
+ except node_pool.NodeWorkerTimeout as exc:
+ raise CloudFrontFunctionError(
+ f"function evaluation exceeded {int(_EVAL_TIMEOUT)} seconds and was cancelled"
+ ) from exc
+ except node_pool.NodeWorkerError as exc:
+ if exc.reason == "missing_node":
+ raise CloudFrontFunctionError("CloudFront Functions need Node, which was not found") from exc
+ if exc.reason == "closed":
+ raise CloudFrontFunctionError("CloudFront Functions worker closed unexpectedly") from exc
+ raise CloudFrontFunctionError(f"CloudFront Functions worker failed: {exc.cause_text}") from exc
+
+ if out["status"] == "error":
+ raise CloudFrontFunctionError(out.get("message", "function execution error"))
+ return out.get("value")
+
+
+def reset():
+ """Drop every worker, so a reset leaves no compiled-function cache behind."""
+ _pool.reset()
+
+
+def available():
+ """Whether CloudFront Functions can be evaluated at all in this environment."""
+ return node_pool.available()
diff --git a/ministack/core/node_pool.py b/ministack/core/node_pool.py
new file mode 100644
index 00000000..2f1270a1
--- /dev/null
+++ b/ministack/core/node_pool.py
@@ -0,0 +1,235 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
+"""A pool of Node worker processes evaluating one script over JSON lines.
+
+Evaluating JS (AppSync resolvers, CloudFront Functions) needs a JavaScript
+engine, and the image already ships Node for the `nodejs*` Lambda runtimes
+(`Dockerfile`, `core/lambda_runtime.py`), so this reuses it rather than adding
+a dependency.
+
+A pool of workers handles calls over a JSON-line protocol, the same shape
+`lambda_runtime` uses: a request per line on stdin, a reply per line on
+stdout, one call in flight per worker. A free worker is leased per call and
+one is spawned when none is free — never queued behind, which would recreate
+the re-entrancy deadlock `lambda_runtime` documents. Calls are bounded by a
+timeout that kills and respawns a stuck process, worker processes are
+recycled after serving many calls, and the script's own stderr is surfaced
+through a caller-supplied logger.
+
+A `NodeWorkerPool` is bound to one worker script; two callers needing
+different scripts (AppSync resolvers vs. CloudFront Functions) each keep
+their own pool instance.
+"""
+
+from __future__ import annotations
+
+import json
+import subprocess
+import threading
+
+_NODE_BINARY = "node"
+
+
+class NodeWorkerTimeout(RuntimeError):
+ """A call outlived its deadline; the worker process was killed."""
+
+
+class NodeWorkerError(RuntimeError):
+ """The worker could not be used at all: ``reason`` is ``"missing_node"``
+ (no node on PATH), ``"io_error"`` (write failed; ``cause_text`` holds the
+ underlying error), or ``"closed"`` (the process ended without a reply)."""
+
+ def __init__(self, message: str, reason: str, cause_text: str | None = None):
+ super().__init__(message)
+ self.reason = reason
+ self.cause_text = cause_text
+
+
+def _terminate(proc):
+ if proc is None:
+ return
+ try:
+ proc.terminate()
+ try:
+ proc.wait(timeout=2)
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ proc.wait(timeout=2)
+ except Exception:
+ pass
+
+
+def _pump_stderr(proc, logger, log_prefix):
+ try:
+ for line in proc.stderr:
+ line = line.rstrip("\n")
+ if line:
+ logger.info("[%s] %s", log_prefix, line)
+ except Exception:
+ pass
+
+
+class _Worker:
+ """One Node process running `pool.script`, one call at a time.
+
+ A worker holds no Python-side state beyond what the script itself caches
+ within its process, so any free worker can serve any call. Single-flight
+ per worker matters beyond throughput whenever the script keeps call-scoped
+ mutable state in its process-level JS runtime (e.g. a compiled module's
+ closure): two calls sharing one process at once would corrupt each
+ other's view of it.
+ """
+
+ def __init__(self, pool):
+ self._pool = pool
+ self._proc = None
+ self._lock = threading.Lock()
+ self.in_use = False
+ self.evals = 0
+
+ def _ensure(self):
+ if self._proc is not None and self._proc.poll() is None:
+ return self._proc
+ self._proc = subprocess.Popen(
+ [_NODE_BINARY, f"--max-old-space-size={self._pool.max_old_space_mb}",
+ "-e", self._pool.script],
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ text=True, bufsize=1,
+ )
+ # The script's console.log lands on stderr (stdout carries the
+ # protocol); surface it through the caller's logger rather than
+ # dropping it.
+ threading.Thread(
+ target=_pump_stderr, args=(self._proc, self._pool.logger, self._pool.log_prefix),
+ daemon=True,
+ ).start()
+ return self._proc
+
+ def _take_proc(self):
+ proc, self._proc = self._proc, None
+ return proc
+
+ def call(self, request, timeout):
+ """Send `request` as one JSON line, return the decoded reply line.
+
+ Raises NodeWorkerTimeout when the call outlives `timeout` (the worker
+ is killed — an infinite loop cannot be interrupted from inside Node —
+ and a fresh process spawns on the next call), and NodeWorkerError
+ when the worker cannot be used at all.
+ """
+ payload = json.dumps(request) + "\n"
+ with self._lock:
+ self.evals += 1
+ try:
+ proc = self._ensure()
+ proc.stdin.write(payload)
+ proc.stdin.flush()
+ except FileNotFoundError as cause: # no node on PATH
+ raise NodeWorkerError("node was not found on PATH", reason="missing_node") from cause
+ except (BrokenPipeError, OSError) as cause:
+ self._proc = None
+ raise NodeWorkerError(f"node worker failed: {cause}", reason="io_error",
+ cause_text=str(cause)) from cause
+
+ # readline on a thread so a stuck script is bounded by `timeout`
+ # rather than holding this worker forever — the same shape
+ # core/lambda_runtime.py uses for a handler that never returns.
+ box = []
+
+ def _read():
+ try:
+ box.append(proc.stdout.readline())
+ except Exception:
+ box.append("")
+
+ reader = threading.Thread(target=_read, daemon=True)
+ reader.start()
+ reader.join(timeout)
+ if reader.is_alive():
+ _terminate(self._take_proc())
+ raise NodeWorkerTimeout(
+ f"evaluation exceeded {int(timeout)} seconds and was cancelled")
+ line = box[0] if box else ""
+ if not line:
+ self._proc = None
+ raise NodeWorkerError("node worker closed unexpectedly", reason="closed")
+
+ return json.loads(line)
+
+ def shutdown(self):
+ with self._lock:
+ _terminate(self._take_proc())
+
+
+class NodeWorkerPool:
+ """Lease-or-spawn pool of `_Worker`s, all running the same script.
+
+ Never queues: see the module docstring's re-entrancy note. Idle workers
+ beyond `max_idle` are folded on release, and a worker is recycled
+ (process killed, respawned lazily on its next lease) after serving
+ `recycle_after` calls, bounding whatever a long-lived Node process
+ accumulates.
+ """
+
+ def __init__(self, script, *, log_prefix, logger, timeout,
+ max_old_space_mb=256, recycle_after=1000, max_idle=2):
+ self.script = script
+ self.log_prefix = log_prefix
+ self.logger = logger
+ self.timeout = timeout
+ self.max_old_space_mb = max_old_space_mb
+ self.recycle_after = recycle_after
+ self.max_idle = max_idle
+ self._lock = threading.Lock()
+ self._workers: list = []
+
+ def _acquire(self):
+ with self._lock:
+ for worker in self._workers:
+ if not worker.in_use:
+ worker.in_use = True
+ return worker
+ worker = _Worker(self)
+ worker.in_use = True
+ self._workers.append(worker)
+ return worker
+
+ def _release(self, worker):
+ to_kill = None
+ with self._lock:
+ worker.in_use = False
+ if worker.evals >= self.recycle_after:
+ worker.evals = 0
+ to_kill = worker._take_proc()
+ else:
+ idle = [w for w in self._workers if not w.in_use]
+ if len(idle) > self.max_idle and worker is not self._workers[0]:
+ # A burst leaves surplus processes behind; keep a couple
+ # warm beyond the first and fold the rest.
+ self._workers.remove(worker)
+ to_kill = worker._take_proc()
+ _terminate(to_kill)
+
+ def call(self, request, timeout=None):
+ """Lease a worker, send `request`, return its decoded JSON reply."""
+ worker = self._acquire()
+ try:
+ return worker.call(request, self.timeout if timeout is None else timeout)
+ finally:
+ self._release(worker)
+
+ def reset(self):
+ """Drop every worker, so a reset leaves no per-process cache behind."""
+ with self._lock:
+ workers, self._workers[:] = self._workers[:], []
+ for worker in workers:
+ worker.shutdown()
+
+
+def available():
+ """Whether a Node binary is on PATH and runnable at all."""
+ try:
+ subprocess.run([_NODE_BINARY, "--version"], capture_output=True, timeout=5)
+ return True
+ except Exception:
+ return False
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 7194cc58..71092acb 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -10001,11 +10001,12 @@ def _cf_distribution_create(logical_id, props, stack_name):
# missing a member the SDKs expect. The update keeps it.
caller_reference = new_uuid()
config_el = _cf_distribution_config(props, caller_reference)
+ domain_name = _cf._new_distribution_domain()
_cf._distributions[dist_id] = {
"Id": dist_id,
"ARN": arn,
"Status": "Deployed",
- "DomainName": f"{dist_id}.cloudfront.net",
+ "DomainName": domain_name,
"LastModifiedTime": _cf._now_iso(),
"ETag": new_uuid(),
"CallerReference": caller_reference,
@@ -10014,7 +10015,7 @@ def _cf_distribution_create(logical_id, props, stack_name):
}
_cf._invalidations[dist_id] = []
_cf._tags[arn] = [{"Key": k, "Value": v} for k, v in _tag_map(props.get("Tags")).items()]
- return dist_id, {"Arn": arn, "DomainName": f"{dist_id}.cloudfront.net", "Id": dist_id}
+ return dist_id, {"Arn": arn, "DomainName": domain_name, "Id": dist_id}
def _cf_distribution_update(physical_id, old_props, new_props, stack_name, logical_id=None):
diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.py
index 8345128b..8bdf15a1 100644
--- a/ministack/services/cloudfront.py
+++ b/ministack/services/cloudfront.py
@@ -18,13 +18,21 @@ Supports:
KeyValueStore: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores,
UpdateKeyValueStore, DeleteKeyValueStore
Cache policies: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig,
- UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId
+ UpdateCachePolicy, DeleteCachePolicy, ListCachePolicies,
+ ListDistributionsByCachePolicyId — includes the fixed catalog
+ of AWS-managed policies (Type=managed), which are immutable
Origin request policies: CreateOriginRequestPolicy, GetOriginRequestPolicy,
GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy,
- DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId
+ DeleteOriginRequestPolicy, ListOriginRequestPolicies,
+ ListDistributionsByOriginRequestPolicyId — includes the
+ AWS-managed policy catalog (Type=managed), immutable
Response headers policies: CreateResponseHeadersPolicy, GetResponseHeadersPolicy,
GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy,
- DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId
+ DeleteResponseHeadersPolicy, ListResponseHeadersPolicies,
+ ListDistributionsByResponseHeadersPolicyId — includes the
+ AWS-managed policy catalog (Type=managed), immutable
+ Monitoring subscriptions: CreateMonitoringSubscription, GetMonitoringSubscription,
+ DeleteMonitoringSubscription
Public keys: CreatePublicKey, GetPublicKey, GetPublicKeyConfig,
UpdatePublicKey, DeletePublicKey, ListPublicKeys
Key groups: CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig,
@@ -52,6 +60,7 @@ import logging
import random
import re
import string
+import uuid
from datetime import datetime, timezone
from urllib.parse import unquote
from xml.etree.ElementTree import Element, SubElement, tostring
@@ -184,6 +193,10 @@ def reset():
_connection_groups.clear()
_distribution_tenants.clear()
_tenant_invalidations.clear()
+ # Drop the JS workers so their compiled-function cache does not outlive a
+ # reset (mirrors appsync.reset()'s appsync_js.reset()).
+ from ministack.core import cloudfront_js
+ cloudfront_js.reset()
def get_state():
@@ -264,10 +277,23 @@ def _conn_group_id() -> str:
return "cg_" + "".join(random.choices(_KSUID_CHARS, k=27))
-def _routing_endpoint() -> str:
+def _cloudfront_domain() -> str:
+ """AWS's edge domain shape: 'd' + 13 lowercase alphanumerics +
+ '.cloudfront.net' (e.g. d111111abcdef8.cloudfront.net) — assigned to both
+ a distribution's DomainName and a connection group's RoutingEndpoint."""
return "d" + "".join(random.choices(string.ascii_lowercase + string.digits, k=13)) + ".cloudfront.net"
+def _new_distribution_domain() -> str:
+ """A `_cloudfront_domain()` value not already used by a distribution in
+ this account."""
+ existing = {d["DomainName"] for d in _distributions.values()}
+ domain = _cloudfront_domain()
+ while domain in existing:
+ domain = _cloudfront_domain()
+ return domain
+
+
def _now_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
@@ -1069,6 +1095,110 @@ def _build_cache_policy_xml(parent, policy):
_build_cache_policy_config_xml(cfg_el, policy["Config"])
+# ---------------------------------------------------------------------------
+# AWS-managed policies (cache / origin request / response headers).
+#
+# Real CloudFront ships a fixed catalog of these under every account; Terraform
+# modules reference them by name (e.g. "Managed-CachingDisabled") via the
+# aws_cloudfront_cache_policy/aws_cloudfront_origin_request_policy/
+# aws_cloudfront_response_headers_policy data sources, and ListCachePolicies
+# et al. must report them with Type=managed. They are immutable (Update/Delete
+# error) and identical for every account, so they are seeded once as module
+# constants rather than per-account state — nothing resets or persists them
+# because nothing ever mutates them.
+#
+# Evidence: AWS docs — "Use managed cache policies", "Use managed origin
+# request policies", "Use managed response headers policies" (CloudFront
+# Developer Guide) for names/ids/configs. The literal "Managed-" Name prefix
+# (docs show only the short console name) is confirmed by ops-v2's
+# modules/cloudfront-api, which already looks policies up by e.g.
+# "Managed-CachingDisabled". LastModifiedTime below is a stable placeholder,
+# not an AWS-observed value.
+# ---------------------------------------------------------------------------
+
+_MANAGED_POLICY_LAST_MODIFIED = "2020-05-20T04:29:32.290Z"
+
+
+def _managed_etag(policy_id: str) -> str:
+ """A stable, unique ETag for a managed policy (real AWS ETags are opaque)."""
+ return str(uuid.uuid5(uuid.NAMESPACE_URL, f"ministack-cloudfront-managed-policy/{policy_id}"))
+
+
+def _managed_policy(policy_id: str, config: dict) -> dict:
+ return {
+ "Id": policy_id,
+ "ETag": _managed_etag(policy_id),
+ "LastModifiedTime": _MANAGED_POLICY_LAST_MODIFIED,
+ "Config": config,
+ }
+
+
+def _cache_params(gzip, brotli, header_behavior, headers, cookie_behavior, cookies, qs_behavior, qs):
+ return {
+ "EnableAcceptEncodingGzip": gzip,
+ "EnableAcceptEncodingBrotli": brotli,
+ "HeaderBehavior": header_behavior, "Headers": list(headers),
+ "CookieBehavior": cookie_behavior, "Cookies": list(cookies),
+ "QueryStringBehavior": qs_behavior, "QueryStrings": list(qs),
+ }
+
+
+_MANAGED_CACHE_POLICIES = {
+ pid: _managed_policy(pid, cfg)
+ for pid, cfg in {
+ "2e54312d-136d-493c-8eb9-b001f22f67d2": {
+ "Name": "Managed-Amplify", "Comment": "", "MinTTL": 2, "DefaultTTL": 2, "MaxTTL": 600,
+ "Parameters": _cache_params(
+ True, True, "whitelist", ["Authorization", "CloudFront-Viewer-Country", "Host"],
+ "all", [], "all", [],
+ ),
+ },
+ "4135ea2d-6df8-44a3-9df3-4b5a84be39ad": {
+ "Name": "Managed-CachingDisabled", "Comment": "", "MinTTL": 0, "DefaultTTL": 0, "MaxTTL": 0,
+ "Parameters": _cache_params(False, False, "none", [], "none", [], "none", []),
+ },
+ "658327ea-f89d-4fab-a63d-7e88639e58f6": {
+ "Name": "Managed-CachingOptimized", "Comment": "", "MinTTL": 1, "DefaultTTL": 86400, "MaxTTL": 31536000,
+ "Parameters": _cache_params(True, True, "none", [], "none", [], "none", []),
+ },
+ "b2884449-e4de-46a7-ac36-70bc7f1ddd6d": {
+ "Name": "Managed-CachingOptimizedForUncompressedObjects", "Comment": "",
+ "MinTTL": 1, "DefaultTTL": 86400, "MaxTTL": 31536000,
+ "Parameters": _cache_params(False, False, "none", [], "none", [], "none", []),
+ },
+ "08627262-05a9-4f76-9ded-b50ca2e3a84f": {
+ "Name": "Managed-Elemental-MediaPackage", "Comment": "",
+ "MinTTL": 0, "DefaultTTL": 86400, "MaxTTL": 31536000,
+ "Parameters": _cache_params(
+ True, False, "whitelist", ["Origin"], "none", [],
+ "whitelist", ["aws.manifestfilter", "start", "end", "m"],
+ ),
+ },
+ "83da9c7e-98b4-4e11-a168-04f0df8e2c65": {
+ "Name": "Managed-UseOriginCacheControlHeaders", "Comment": "",
+ "MinTTL": 0, "DefaultTTL": 0, "MaxTTL": 31536000,
+ "Parameters": _cache_params(
+ True, True, "whitelist",
+ ["Host", "Origin", "X-HTTP-Method-Override", "X-HTTP-Method", "X-Method-Override"],
+ "all", [], "none", [],
+ ),
+ },
+ "4cc15a8a-d715-48a4-82b8-cc0b614638fe": {
+ "Name": "Managed-UseOriginCacheControlHeaders-QueryStrings", "Comment": "",
+ "MinTTL": 0, "DefaultTTL": 0, "MaxTTL": 31536000,
+ "Parameters": _cache_params(
+ True, True, "whitelist",
+ ["Host", "Origin", "X-HTTP-Method-Override", "X-HTTP-Method", "X-Method-Override"],
+ "all", [], "all", [],
+ ),
+ },
+ }.items()
+}
+
+_ILLEGAL_UPDATE_MSG = "The specified CloudFront managed policy cannot be updated."
+_ILLEGAL_DELETE_MSG = "The specified CloudFront managed policy cannot be deleted."
+
+
def _value_contains(obj, target):
"""Best-effort recursive search for a policy Id anywhere in a distribution record."""
if isinstance(obj, str):
@@ -1091,7 +1221,7 @@ def _create_cache_policy(body):
cfg, err = _parse_cache_policy_config(el)
if err is not None:
return err
- for existing in _cache_policies.values():
+ for existing in (*_cache_policies.values(), *_MANAGED_CACHE_POLICIES.values()):
if existing["Config"]["Name"] == cfg["Name"]:
return _error("CachePolicyAlreadyExists", "A cache policy with the same name already exists.", 409)
policy_id = new_uuid()
@@ -1110,7 +1240,7 @@ def _create_cache_policy(body):
def _get_cache_policy(policy_id):
- policy = _cache_policies.get(policy_id)
+ policy = _cache_policies.get(policy_id) or _MANAGED_CACHE_POLICIES.get(policy_id)
if not policy:
return _error("NoSuchCachePolicy", "The cache policy does not exist.", 404)
@@ -1121,7 +1251,7 @@ def _get_cache_policy(policy_id):
def _get_cache_policy_config(policy_id):
- policy = _cache_policies.get(policy_id)
+ policy = _cache_policies.get(policy_id) or _MANAGED_CACHE_POLICIES.get(policy_id)
if not policy:
return _error("NoSuchCachePolicy", "The cache policy does not exist.", 404)
@@ -1132,6 +1262,8 @@ def _get_cache_policy_config(policy_id):
def _update_cache_policy(policy_id, headers, body):
+ if policy_id in _MANAGED_CACHE_POLICIES:
+ return _error("IllegalUpdate", _ILLEGAL_UPDATE_MSG, 400)
policy = _cache_policies.get(policy_id)
if not policy:
return _error("NoSuchCachePolicy", "The cache policy does not exist.", 404)
@@ -1150,7 +1282,7 @@ def _update_cache_policy(policy_id, headers, body):
cfg, err = _parse_cache_policy_config(el)
if err is not None:
return err
- for existing in _cache_policies.values():
+ for existing in (*_cache_policies.values(), *_MANAGED_CACHE_POLICIES.values()):
if existing["Id"] != policy_id and existing["Config"]["Name"] == cfg["Name"]:
return _error("CachePolicyAlreadyExists", "A cache policy with the same name already exists.", 409)
new_etag = new_uuid()
@@ -1166,6 +1298,8 @@ def _update_cache_policy(policy_id, headers, body):
def _delete_cache_policy(policy_id, headers):
+ if policy_id in _MANAGED_CACHE_POLICIES:
+ return _error("IllegalDelete", _ILLEGAL_DELETE_MSG, 400)
policy = _cache_policies.get(policy_id)
if not policy:
return _error("NoSuchCachePolicy", "The cache policy does not exist.", 404)
@@ -1190,7 +1324,7 @@ def _delete_cache_policy(policy_id, headers):
def _list_distributions_by_cache_policy(policy_id):
- if not _cache_policies.get(policy_id):
+ if not _cache_policies.get(policy_id) and policy_id not in _MANAGED_CACHE_POLICIES:
return _error("NoSuchCachePolicy", "The cache policy does not exist.", 404)
dist_ids = _distributions_using_cache_policy(policy_id)
@@ -1279,6 +1413,11 @@ def _policy_precheck_if_match(headers, policy):
return None
+def _policy_lookup(store, spec, pid):
+ """Custom store first, then the type's AWS-managed catalog."""
+ return store.get(pid) or spec["managed"].get(pid)
+
+
def _policy_create(store, spec, body):
el = _parse_body(body)
if el is None:
@@ -1286,7 +1425,7 @@ def _policy_create(store, spec, body):
cfg, err = spec["parse"](el)
if err is not None:
return err
- for existing in store.values():
+ for existing in (*store.values(), *spec["managed"].values()):
if existing["Config"]["Name"] == cfg["Name"]:
return _error(spec["dup"], f"A {spec['label']} with the same name already exists.", 409)
pid = new_uuid()
@@ -1301,7 +1440,7 @@ def _policy_create(store, spec, body):
def _policy_get(store, spec, pid):
- policy = store.get(pid)
+ policy = _policy_lookup(store, spec, pid)
if not policy:
return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
return _xml_response(spec["resource_tag"], lambda r: spec["build_resource"](r, policy),
@@ -1309,7 +1448,7 @@ def _policy_get(store, spec, pid):
def _policy_get_config(store, spec, pid):
- policy = store.get(pid)
+ policy = _policy_lookup(store, spec, pid)
if not policy:
return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
return _xml_response(spec["config_tag"], lambda r: spec["build_config"](r, policy["Config"]),
@@ -1317,6 +1456,8 @@ def _policy_get_config(store, spec, pid):
def _policy_update(store, spec, pid, headers, body):
+ if pid in spec["managed"]:
+ return _error("IllegalUpdate", _ILLEGAL_UPDATE_MSG, 400)
policy = store.get(pid)
if not policy:
return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
@@ -1329,7 +1470,7 @@ def _policy_update(store, spec, pid, headers, body):
cfg, err = spec["parse"](el)
if err is not None:
return err
- for existing in store.values():
+ for existing in (*store.values(), *spec["managed"].values()):
if existing["Id"] != pid and existing["Config"]["Name"] == cfg["Name"]:
return _error(spec["dup"], f"A {spec['label']} with the same name already exists.", 409)
new_etag = new_uuid()
@@ -1341,6 +1482,8 @@ def _policy_update(store, spec, pid, headers, body):
def _policy_delete(store, spec, pid, headers):
+ if pid in spec["managed"]:
+ return _error("IllegalDelete", _ILLEGAL_DELETE_MSG, 400)
policy = store.get(pid)
if not policy:
return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
@@ -1357,7 +1500,7 @@ def _policy_delete(store, spec, pid, headers):
def _policy_list_distributions(store, spec, pid):
- if not store.get(pid):
+ if not _policy_lookup(store, spec, pid):
return _error(spec["missing"], f"The {spec['label']} does not exist.", 404)
dist_ids = _distributions_using_policy(pid)
@@ -1429,6 +1572,65 @@ def _build_orp_xml(parent, policy):
_build_orp_config_xml(cfg_el, policy["Config"])
+def _orp_cfg(name, header_behavior, headers=(), cookie_behavior="none", qs_behavior="none", qs=()):
+ return {
+ "Name": name, "Comment": "",
+ "HeaderBehavior": header_behavior, "Headers": list(headers),
+ "CookieBehavior": cookie_behavior, "Cookies": [],
+ "QueryStringBehavior": qs_behavior, "QueryStrings": list(qs),
+ }
+
+
+# Evidence: AWS docs "Use managed origin request policies" (CloudFront
+# Developer Guide) for names/ids/behaviors; see the catalog note above
+# _MANAGED_CACHE_POLICIES for the "Managed-" Name-prefix evidence.
+_MANAGED_ORIGIN_REQUEST_POLICIES = {
+ pid: _managed_policy(pid, cfg)
+ for pid, cfg in {
+ "216adef6-5c7f-47e4-b989-5492eafa07d3": _orp_cfg(
+ "Managed-AllViewer", "allViewer", cookie_behavior="all", qs_behavior="all",
+ ),
+ "33f36d7e-f396-46d9-90e0-52428a34d9dc": _orp_cfg(
+ "Managed-AllViewerAndCloudFrontHeaders-2022-06", "allViewerAndWhitelistCloudFront",
+ headers=[
+ "CloudFront-Forwarded-Proto", "CloudFront-Is-Android-Viewer", "CloudFront-Is-Desktop-Viewer",
+ "CloudFront-Is-IOS-Viewer", "CloudFront-Is-Mobile-Viewer", "CloudFront-Is-SmartTV-Viewer",
+ "CloudFront-Is-Tablet-Viewer", "CloudFront-Viewer-Address", "CloudFront-Viewer-ASN",
+ "CloudFront-Viewer-City", "CloudFront-Viewer-Country", "CloudFront-Viewer-Country-Name",
+ "CloudFront-Viewer-Country-Region", "CloudFront-Viewer-Country-Region-Name",
+ "CloudFront-Viewer-Http-Version", "CloudFront-Viewer-Latitude", "CloudFront-Viewer-Longitude",
+ "CloudFront-Viewer-Metro-Code", "CloudFront-Viewer-Postal-Code", "CloudFront-Viewer-Time-Zone",
+ "CloudFront-Viewer-TLS",
+ ],
+ cookie_behavior="all", qs_behavior="all",
+ ),
+ "b689b0a8-53d0-40ab-baf2-68738e2966ac": _orp_cfg(
+ "Managed-AllViewerExceptHostHeader", "allExcept", headers=["Host"],
+ cookie_behavior="all", qs_behavior="all",
+ ),
+ "59781a5b-3903-41f3-afcb-af62929ccde1": _orp_cfg(
+ "Managed-CORS-CustomOrigin", "whitelist", headers=["Origin"],
+ ),
+ "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf": _orp_cfg(
+ "Managed-CORS-S3Origin", "whitelist",
+ headers=["Origin", "Access-Control-Request-Headers", "Access-Control-Request-Method"],
+ ),
+ "775133bc-15f2-49f9-abea-afb2e0bf67d2": _orp_cfg(
+ "Managed-Elemental-MediaTailor-PersonalizedManifests", "whitelist",
+ headers=["Origin", "Access-Control-Request-Headers", "Access-Control-Request-Method",
+ "User-Agent", "X-Forwarded-For"],
+ qs_behavior="all",
+ ),
+ "bf0718e1-ba1e-49d1-88b1-f726733018ae": _orp_cfg(
+ "Managed-HostHeaderOnly", "whitelist", headers=["Host"],
+ ),
+ "acba4595-bd28-49b8-b9fe-13317c0390fa": _orp_cfg(
+ "Managed-UserAgentRefererHeaders", "whitelist", headers=["User-Agent", "Referer"],
+ ),
+ }.items()
+}
+
+
_ORP_SPEC = {
"label": "origin request policy", "resource_tag": "OriginRequestPolicy",
"config_tag": "OriginRequestPolicyConfig", "path": "/2020-05-31/origin-request-policy",
@@ -1436,6 +1638,7 @@ _ORP_SPEC = {
"missing": "NoSuchOriginRequestPolicy", "dup": "OriginRequestPolicyAlreadyExists",
"in_use": "OriginRequestPolicyInUse", "parse": _parse_orp_config,
"build_resource": _build_orp_xml, "build_config": _build_orp_config_xml,
+ "managed": _MANAGED_ORIGIN_REQUEST_POLICIES,
}
@@ -1629,6 +1832,69 @@ def _build_rhp_xml(parent, policy):
_build_rhp_config_xml(cfg_el, policy["Config"])
+def _rhp_managed_cors(allow_methods=(), expose_headers=None):
+ return {
+ "AllowOrigins": ["*"], "AllowHeaders": [], "AllowMethods": list(allow_methods),
+ "AllowCredentials": False, "OriginOverride": False,
+ "ExposeHeaders": expose_headers, "MaxAgeSec": None,
+ }
+
+
+# The full security-headers set shared by SecurityHeadersPolicy and both
+# combined CORS+security managed policies (identical settings per AWS docs).
+_RHP_MANAGED_SECURITY = {
+ "XSSProtection": {"Override": False, "Protection": True, "ModeBlock": True, "ReportUri": None},
+ "FrameOptions": {"Override": False, "FrameOption": "SAMEORIGIN"},
+ "ReferrerPolicy": {"Override": False, "ReferrerPolicy": "strict-origin-when-cross-origin"},
+ "ContentTypeOptions": {"Override": True},
+ "StrictTransportSecurity": {
+ "Override": False, "IncludeSubdomains": None, "Preload": None, "AccessControlMaxAgeSec": 31536000,
+ },
+}
+
+
+def _rhp_cfg(name, cors=None, security=None):
+ return {
+ "Name": name, "Comment": "", "Cors": cors, "Security": security,
+ "ServerTiming": None, "CustomHeaders": [], "RemoveHeaders": [],
+ }
+
+
+# Evidence: AWS docs "Use managed response headers policies" (CloudFront
+# Developer Guide) for names/ids/CORS+security settings; see the catalog note
+# above _MANAGED_CACHE_POLICIES for the "Managed-" Name-prefix evidence.
+_MANAGED_RESPONSE_HEADERS_POLICIES = {
+ pid: _managed_policy(pid, cfg)
+ for pid, cfg in {
+ "e61eb60c-9c35-4d20-a928-2b84e02af89c": _rhp_cfg(
+ "Managed-CORS-and-SecurityHeadersPolicy",
+ cors=_rhp_managed_cors(), security=dict(_RHP_MANAGED_SECURITY),
+ ),
+ "5cc3b908-e619-4b99-88e5-2cf7f45965bd": _rhp_cfg(
+ "Managed-CORS-With-Preflight",
+ cors=_rhp_managed_cors(
+ allow_methods=["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"],
+ expose_headers=["*"],
+ ),
+ ),
+ "eaab4381-ed33-4a86-88ca-d9558dc6cd63": _rhp_cfg(
+ "Managed-CORS-with-preflight-and-SecurityHeadersPolicy",
+ cors=_rhp_managed_cors(
+ allow_methods=["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"],
+ expose_headers=["*"],
+ ),
+ security=dict(_RHP_MANAGED_SECURITY),
+ ),
+ "67f7725c-6f97-4210-82d7-5512b31e9d03": _rhp_cfg(
+ "Managed-SecurityHeadersPolicy", security=dict(_RHP_MANAGED_SECURITY),
+ ),
+ "60669652-455b-4ae9-85a4-c4c02393f86c": _rhp_cfg(
+ "Managed-SimpleCORS", cors=_rhp_managed_cors(),
+ ),
+ }.items()
+}
+
+
_RHP_SPEC = {
"label": "response headers policy", "resource_tag": "ResponseHeadersPolicy",
"config_tag": "ResponseHeadersPolicyConfig", "path": "/2020-05-31/response-headers-policy",
@@ -1636,6 +1902,7 @@ _RHP_SPEC = {
"missing": "NoSuchResponseHeadersPolicy", "dup": "ResponseHeadersPolicyAlreadyExists",
"in_use": "ResponseHeadersPolicyInUse", "parse": _parse_rhp_config,
"build_resource": _build_rhp_xml, "build_config": _build_rhp_config_xml,
+ "managed": _MANAGED_RESPONSE_HEADERS_POLICIES,
}
@@ -2003,18 +2270,27 @@ def _list_anycast_ip_lists(query_params):
def _list_cache_policies(query_params):
- """CachePolicyList wrapping stored custom cache policies (Type=custom)."""
+ """CachePolicyList: managed policies plus stored custom ones.
+
+ ``Type`` optionally filters to ``managed`` or ``custom``; omitted, AWS
+ returns both (ListCachePolicies API reference).
+ """
max_items = _qval(query_params, "MaxItems", _DEFAULT_MAX_ITEMS) or _DEFAULT_MAX_ITEMS
- policies = list(_cache_policies.values())
+ type_filter = _qval(query_params, "Type", "")
+ entries = []
+ if type_filter in ("", "managed"):
+ entries.extend(("managed", p) for p in _MANAGED_CACHE_POLICIES.values())
+ if type_filter in ("", "custom"):
+ entries.extend(("custom", p) for p in _cache_policies.values())
def build(root):
SubElement(root, "MaxItems").text = max_items
- SubElement(root, "Quantity").text = str(len(policies))
- if policies:
+ SubElement(root, "Quantity").text = str(len(entries))
+ if entries:
items_el = SubElement(root, "Items")
- for policy in policies:
+ for kind, policy in entries:
summary = SubElement(items_el, "CachePolicySummary")
- SubElement(summary, "Type").text = "custom"
+ SubElement(summary, "Type").text = kind
cp = SubElement(summary, "CachePolicy")
_build_cache_policy_xml(cp, policy)
@@ -2022,39 +2298,96 @@ def _list_cache_policies(query_params):
def _list_policies(store, spec, query_params):
- """Generic ``*PolicyList`` wrapping stored custom policies (Type=custom).
+ """Generic ``*PolicyList``: managed policies plus stored custom ones.
Shared by origin request policies and response headers policies; the
- summary member and resource tag come from ``spec``.
+ summary member and resource tag come from ``spec``. ``Type`` optionally
+ filters to ``managed`` or ``custom``; omitted, AWS returns both.
"""
max_items = _qval(query_params, "MaxItems", _DEFAULT_MAX_ITEMS) or _DEFAULT_MAX_ITEMS
- policies = list(store.values())
+ type_filter = _qval(query_params, "Type", "")
+ entries = []
+ if type_filter in ("", "managed"):
+ entries.extend(("managed", p) for p in spec["managed"].values())
+ if type_filter in ("", "custom"):
+ entries.extend(("custom", p) for p in store.values())
def build(root):
SubElement(root, "MaxItems").text = max_items
- SubElement(root, "Quantity").text = str(len(policies))
- if policies:
+ SubElement(root, "Quantity").text = str(len(entries))
+ if entries:
items_el = SubElement(root, "Items")
- for policy in policies:
+ for kind, policy in entries:
summary = SubElement(items_el, spec["summary_tag"])
- SubElement(summary, "Type").text = "custom"
+ SubElement(summary, "Type").text = kind
res = SubElement(summary, spec["resource_tag"])
spec["build_resource"](res, policy)
return _xml_response(spec["list_tag"], build)
+def _build_monitoring_subscription_xml(parent, sub):
+ cfg = SubElement(parent, "RealtimeMetricsSubscriptionConfig")
+ SubElement(cfg, "RealtimeMetricsSubscriptionStatus").text = sub["RealtimeMetricsSubscriptionStatus"]
+
+
+_MONITORING_SUB_STATUSES = {"Enabled", "Disabled"}
+
+
+def _create_monitoring_subscription(dist_id, body):
+ """CreateMonitoringSubscription (POST, 200): NoSuchDistribution when the
+ distribution is unknown. terraform-provider-aws's
+ aws_cloudfront_monitoring_subscription resource wires its Update to this
+ same Create operation (UpdateWithoutTimeout: resourceMonitoringSubscriptionCreate)
+ with no AlreadyExists handling, so Create on a distribution that already
+ has a subscription must overwrite its status rather than error."""
+ dist = _distributions.get(dist_id)
+ if not dist:
+ return _error("NoSuchDistribution", "The specified distribution does not exist.", 404)
+ el = _parse_body(body)
+ cfg_el = _find(el, "RealtimeMetricsSubscriptionConfig") if el is not None else None
+ status = _text(cfg_el, "RealtimeMetricsSubscriptionStatus") if cfg_el is not None else ""
+ if status not in _MONITORING_SUB_STATUSES:
+ return _error(
+ "InvalidArgument", "RealtimeMetricsSubscriptionStatus must be Enabled or Disabled.", 400
+ )
+ sub = {"RealtimeMetricsSubscriptionStatus": status}
+ dist["MonitoringSubscription"] = sub
+ logger.info("CreateMonitoringSubscription dist=%s status=%s", dist_id, status)
+ return _xml_response("MonitoringSubscription", lambda r: _build_monitoring_subscription_xml(r, sub))
+
+
def _get_monitoring_subscription(dist_id):
- """MiniStack does not persist monitoring subscriptions. Real AWS returns
- NoSuchDistribution (404) for an unknown distribution and
+ """NoSuchDistribution (404) for an unknown distribution,
NoSuchMonitoringSubscription (404) when none is configured."""
- if dist_id not in _distributions:
+ dist = _distributions.get(dist_id)
+ if not dist:
return _error("NoSuchDistribution", "The specified distribution does not exist.", 404)
- return _error(
- "NoSuchMonitoringSubscription",
- "A monitoring subscription does not exist for the specified distribution.",
- 404,
- )
+ sub = dist.get("MonitoringSubscription")
+ if not sub:
+ return _error(
+ "NoSuchMonitoringSubscription",
+ "A monitoring subscription does not exist for the specified distribution.",
+ 404,
+ )
+ return _xml_response("MonitoringSubscription", lambda r: _build_monitoring_subscription_xml(r, sub))
+
+
+def _delete_monitoring_subscription(dist_id):
+ """DeleteMonitoringSubscription: 200 with an empty body per the API model
+ (unlike the 204-with-empty-body shape most other CloudFront deletes use)."""
+ dist = _distributions.get(dist_id)
+ if not dist:
+ return _error("NoSuchDistribution", "The specified distribution does not exist.", 404)
+ if not dist.get("MonitoringSubscription"):
+ return _error(
+ "NoSuchMonitoringSubscription",
+ "A monitoring subscription does not exist for the specified distribution.",
+ 404,
+ )
+ dist["MonitoringSubscription"] = None
+ logger.info("DeleteMonitoringSubscription dist=%s", dist_id)
+ return 200, {}, b""
# ---------------------------------------------------------------------------
@@ -2465,8 +2798,14 @@ async def handle_request(method, path, headers, body, query_params):
return _list_anycast_ip_lists(query_params)
m = _MONITORING_SUB_RE.match(path)
- if m and method == "GET":
- return _get_monitoring_subscription(m.group(1))
+ if m:
+ dist_id = m.group(1)
+ if method == "GET":
+ return _get_monitoring_subscription(dist_id)
+ if method == "POST":
+ return _create_monitoring_subscription(dist_id, body)
+ if method == "DELETE":
+ return _delete_monitoring_subscription(dist_id)
return _error("NoSuchResource", f"No route for {method} {path}", 404)
@@ -2506,7 +2845,7 @@ def _create_distribution(headers, body):
"Id": dist_id,
"ARN": f"arn:aws:cloudfront::{get_account_id()}:distribution/{dist_id}",
"Status": "Deployed",
- "DomainName": f"{dist_id}.cloudfront.net",
+ "DomainName": _new_distribution_domain(),
"LastModifiedTime": now,
"ETag": etag,
"CallerReference": caller_ref,
@@ -3539,7 +3878,7 @@ def _create_connection_group(body):
"CreatedTime": now,
"LastModifiedTime": now,
"Ipv6Enabled": _xbool(el, "Ipv6Enabled", True),
- "RoutingEndpoint": _routing_endpoint(),
+ "RoutingEndpoint": _cloudfront_domain(),
"AnycastIpListId": _opt_text(el, "AnycastIpListId"),
"Enabled": _xbool(el, "Enabled", True),
"IsDefault": False,
@@ -3564,7 +3903,7 @@ def _default_connection_group():
"CreatedTime": now,
"LastModifiedTime": now,
"Ipv6Enabled": True,
- "RoutingEndpoint": _routing_endpoint(),
+ "RoutingEndpoint": _cloudfront_domain(),
"AnycastIpListId": None,
"Enabled": True,
"IsDefault": True,
@@ -4217,3 +4556,200 @@ def _list_distributions_by_connection_mode(mode):
return _error("InvalidArgument", "Invalid ConnectionMode value.", 400)
items = [d for d in _distributions.values() if _dist_connection_mode(d) == mode]
return _xml_response("DistributionList", lambda root: _build_distribution_list_xml(root, items))
+
+
+# ---------------------------------------------------------------------------
+# Data-plane support: cloudfront_dataplane serves traffic through this
+# accessor surface rather than the module-private stores directly.
+# ---------------------------------------------------------------------------
+
+
+def find_distribution_for_label(label: str):
+ """The distribution whose DomainName is ``<label>.cloudfront.net``.
+
+ A viewer request carries no credentials, so this scans the ambient
+ account's distributions the same way ``alb.find_lb_for_host`` scans
+ ``_lbs.values()`` for ALB's own host-routed data plane — not a
+ cross-account search, just the same ambient-scoped lookup every other
+ host-routed service uses here.
+ """
+ domain = f"{label}.cloudfront.net"
+ for dist in _distributions.values():
+ if dist.get("DomainName", "").lower() == domain:
+ return dist
+ return None
+
+
+# The regional/legacy REST-endpoint domain shapes an Origin's DomainName
+# takes when the origin is an S3 bucket (CloudFront Developer Guide, "Amazon
+# S3 origin"). A website-endpoint domain (s3-website-*) isn't matched here:
+# AWS itself requires that shape to be configured as a CustomOriginConfig,
+# never S3OriginConfig, so it is correctly left to the custom-origin path.
+_S3_ORIGIN_DOMAIN_RE = re.compile(
+ r"^(?P<bucket>[a-z0-9][a-z0-9.-]*[a-z0-9])\.s3(?:\.[a-z0-9-]+|-[a-z0-9-]+)?\.amazonaws\.com$"
+)
+
+
+def _s3_origin_bucket(domain_name: str):
+ m = _S3_ORIGIN_DOMAIN_RE.match((domain_name or "").strip().lower())
+ return m.group("bucket") if m else None
+
+
+def _dataplane_custom_headers(origin_el):
+ """Origin.CustomHeaders — wire name "CustomHeaders" (CloudFormation's
+ OriginCustomHeaders is a JSON-only rename, see _CFN_DISTRIBUTION_CONFIG_RENAMES
+ above); Items/OriginCustomHeader/{HeaderName,HeaderValue} per the botocore
+ cloudfront service-2.json (2020-05-31) shape."""
+ headers = []
+ ch_el = _find(origin_el, "CustomHeaders")
+ items_el = _find(ch_el, "Items") if ch_el is not None else None
+ if items_el is not None:
+ for it in items_el:
+ if _local_tag_name(it) == "OriginCustomHeader":
+ name = _text(it, "HeaderName")
+ if name:
+ headers.append((name, _text(it, "HeaderValue")))
+ return headers
+
+
+def _dataplane_origin(origin_el):
+ custom = _find(origin_el, "CustomOriginConfig")
+ s3cfg = _find(origin_el, "S3OriginConfig")
+ domain_name = _text(origin_el, "DomainName")
+ return {
+ "id": _text(origin_el, "Id"),
+ "domain_name": domain_name,
+ "origin_path": _opt_text(origin_el, "OriginPath") or "",
+ "custom_headers": _dataplane_custom_headers(origin_el),
+ # Origin carries exactly one of S3OriginConfig or CustomOriginConfig
+ # (AWS's schema is mutually exclusive); the bucket name itself is
+ # only ever recoverable from DomainName, never a separate field.
+ "s3_bucket": _s3_origin_bucket(domain_name) if s3cfg is not None else None,
+ "http_port": int(_text(custom, "HTTPPort") or "80") if custom is not None else 80,
+ "https_port": int(_text(custom, "HTTPSPort") or "443") if custom is not None else 443,
+ "protocol_policy": _text(custom, "OriginProtocolPolicy") if custom is not None else "match-viewer",
+ # Response timeout doc ("Origin response timeout"): default 30s.
+ "read_timeout": int(_text(custom, "OriginReadTimeout") or "30") if custom is not None else 30,
+ }
+
+
+def _dataplane_function_associations(behavior_el):
+ associations = {}
+ fa_el = _find(behavior_el, "FunctionAssociations")
+ items_el = _find(fa_el, "Items") if fa_el is not None else None
+ if items_el is None:
+ return associations
+ for it in items_el:
+ if _local_tag_name(it) == "FunctionAssociation":
+ associations[_text(it, "EventType")] = _text(it, "FunctionARN")
+ return associations
+
+
+def _dataplane_forwarded_values(behavior_el):
+ """Legacy ``ForwardedValues`` (botocore cloudfront service-2.json), used
+ by a behavior that has no CachePolicyId. Returns None when the behavior
+ carries no ForwardedValues block either (nothing legacy to forward)."""
+ fv_el = _find(behavior_el, "ForwardedValues")
+ if fv_el is None:
+ return None
+ cookies_el = _find(fv_el, "Cookies")
+ return {
+ "query_string": _xbool(fv_el, "QueryString", False),
+ "headers": _parse_name_items(fv_el, "Headers"),
+ "cookies_forward": _text(cookies_el, "Forward", "none") if cookies_el is not None else "none",
+ "cookies_whitelist": _parse_name_items(cookies_el, "WhitelistedNames") if cookies_el is not None else [],
+ }
+
+
+def _dataplane_allowed_methods(behavior_el):
+ """The behavior's AllowedMethods.Items, or None when the block is absent
+ (nothing to enforce — see cloudfront_dataplane.py's method check)."""
+ am_el = _find(behavior_el, "AllowedMethods")
+ if am_el is None:
+ return None
+ items_el = _find(am_el, "Items")
+ methods = []
+ if items_el is not None:
+ for it in items_el:
+ if _local_tag_name(it) == "Method":
+ methods.append(it.text or "")
+ return methods
+
+
+def _dataplane_behavior(behavior_el, path_pattern=None):
+ return {
+ "path_pattern": path_pattern,
+ "target_origin_id": _text(behavior_el, "TargetOriginId"),
+ "viewer_protocol_policy": _text(behavior_el, "ViewerProtocolPolicy", "allow-all"),
+ "allowed_methods": _dataplane_allowed_methods(behavior_el),
+ "cache_policy_id": _opt_text(behavior_el, "CachePolicyId"),
+ "origin_request_policy_id": _opt_text(behavior_el, "OriginRequestPolicyId"),
+ "response_headers_policy_id": _opt_text(behavior_el, "ResponseHeadersPolicyId"),
+ "forwarded_values": _dataplane_forwarded_values(behavior_el),
+ "functions": _dataplane_function_associations(behavior_el),
+ }
+
+
+def parse_distribution_dataplane_config(dist: dict) -> dict:
+ """A distribution's stored config XML, reduced to what the data plane
+ dispatches against: the default root object, origins by id, the default
+ behavior, and ordered behaviors in declaration order (first
+ ``path_pattern`` match wins)."""
+ config_el = _dist_config_el(dist)
+
+ origins = {}
+ origins_el = _find(config_el, "Origins")
+ items_el = _find(origins_el, "Items") if origins_el is not None else None
+ if items_el is not None:
+ for it in items_el:
+ if _local_tag_name(it) == "Origin":
+ origin = _dataplane_origin(it)
+ origins[origin["id"]] = origin
+
+ default_el = _find(config_el, "DefaultCacheBehavior")
+ default_behavior = _dataplane_behavior(default_el) if default_el is not None else None
+
+ ordered_behaviors = []
+ behaviors_el = _find(config_el, "CacheBehaviors")
+ items_el = _find(behaviors_el, "Items") if behaviors_el is not None else None
+ if items_el is not None:
+ for it in items_el:
+ if _local_tag_name(it) == "CacheBehavior":
+ ordered_behaviors.append(_dataplane_behavior(it, _text(it, "PathPattern")))
+
+ return {
+ "default_root_object": _opt_text(config_el, "DefaultRootObject") or "",
+ "origins": origins,
+ "default_behavior": default_behavior,
+ "ordered_behaviors": ordered_behaviors,
+ }
+
+
+def cache_policy_params(policy_id):
+ """A cache policy's Parameters dict (managed or custom), or None when the
+ policy has no ParametersInCacheKeyAndForwardedToOrigin block (nothing
+ beyond the defaults CloudFront always includes) or doesn't exist."""
+ policy = _cache_policies.get(policy_id) or _MANAGED_CACHE_POLICIES.get(policy_id)
+ return policy["Config"].get("Parameters") if policy else None
+
+
+def origin_request_policy_config(policy_id):
+ """A custom or AWS-managed origin request policy's Config dict, or None."""
+ policy = _origin_request_policies.get(policy_id) or _MANAGED_ORIGIN_REQUEST_POLICIES.get(policy_id)
+ return policy["Config"] if policy else None
+
+
+def response_headers_policy_config(policy_id):
+ """A custom or AWS-managed response-headers policy's Config dict, or None."""
+ policy = _response_headers_policies.get(policy_id) or _MANAGED_RESPONSE_HEADERS_POLICIES.get(policy_id)
+ return policy["Config"] if policy else None
+
+
+def live_function_code(function_arn: str):
+ """The published (LIVE) JS body for a FunctionAssociation's FunctionARN,
+ or None when the ARN doesn't resolve to a published function."""
+ name = function_arn.rsplit("/", 1)[-1] if function_arn else ""
+ fn = _functions.get(name)
+ if not fn or not fn.get("live_etag"):
+ return None
+ return _function_view(fn, "LIVE").get("code", fn["code"])
diff --git a/ministack/services/cloudfront_dataplane.py b/ministack/services/cloudfront_dataplane.py
new file mode 100644
index 00000000..8634ac90
--- /dev/null
+++ b/ministack/services/cloudfront_dataplane.py
@@ -0,0 +1,1026 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
+"""CloudFront data plane — actually serves a distribution's traffic.
+
+``ministack/services/cloudfront.py`` is the CloudFront control plane: it
+persists distributions, functions, and policies and answers the CloudFront
+REST/XML API. It never serves a byte of viewer traffic — MiniStack's
+CloudFront is otherwise control-plane only. This module is the data plane:
+routed in by ``app.py`` whenever a viewer request's Host is
+``<label>.cloudfront.net`` or ``<label>.cloudfront.<MINISTACK_HOST>`` and
+``<label>`` matches a stored distribution's DomainName.
+
+Modelled, at AWS parity for the pieces real deployments use:
+ * a custom origin (``CustomOriginConfig``) or an S3 origin
+ (``S3OriginConfig``), selected per matched cache behavior —
+ ``DefaultCacheBehavior`` plus ordered ``CacheBehaviors``, first
+ ``PathPattern`` match wins, CloudFront glob semantics (``*`` any chars
+ incl. ``/``, ``?`` one char, case-sensitive);
+ * what reaches the origin: the union of a cache policy's
+ ParametersInCacheKeyAndForwardedToOrigin and an attached origin request
+ policy (CloudFront Developer Guide, "Understand how origin request
+ policies and cache policies work together"), or a legacy
+ ``ForwardedValues`` block when the behavior has no cache policy at all;
+ * ``ViewerProtocolPolicy``, ``AllowedMethods``, ``DefaultRootObject``,
+ and a custom origin's ``OriginPath``/``CustomHeaders``;
+ * ``viewer-request``/``viewer-response`` CloudFront Functions
+ (``cloudfront-js-1.0``/``-2.0``), executed via ``core.cloudfront_js``;
+ * a response-headers policy's custom headers, security headers, and
+ removed headers;
+ * the response headers CloudFront itself adds (``Via``, ``X-Cache``,
+ ``X-Amz-Cf-Pop``, ``X-Amz-Cf-Id``);
+ * a stacked distribution (an origin that is itself a CloudFront
+ distribution) answers 403 instead of recursing.
+
+Not modelled: caching itself (every request reaches the origin —
+``CachingDisabled`` is the simplest fit and nothing here varies by TTL), WAF,
+logging, geo/IP restrictions, custom error pages, signed URLs/cookies,
+Lambda@Edge, CORS preflight responses (CloudFront's response-headers-policy
+CORS docs describe header values added to whatever response already exists;
+nothing documents CloudFront itself answering an OPTIONS preflight instead of
+forwarding it), the two "all viewer headers plus synthetic
+``CloudFront-*``" origin request policies' extra geo/device headers (the
+viewer headers themselves still forward; the ~20 synthesized
+``CloudFront-Viewer-*``/``CloudFront-Is-*-Viewer`` headers do not, since
+nothing here needs them), and origins addressed by an AWS hostname (ALB,
+Function URL, execute-api, S3 website endpoints) — refused with CloudFront's
+502 before a socket opens, since this emulator never dials a real AWS host.
+A custom origin is otherwise dialed at its own DomainName/HTTPPort or
+HTTPSPort exactly as configured; an explicit ``:port`` in its DomainName,
+which MiniStack's own endpoints carry, overrides those.
+"""
+
+import base64
+import fnmatch
+import http.client
+import logging
+import os
+import random
+import ssl
+import string
+from re import compile as _re_compile
+from re import escape as _re_escape
+from urllib.parse import quote, unquote, urlencode
+
+from ministack.core import cloudfront_js
+from ministack.core.concurrency import run_offloop, run_reentrant
+from ministack.core.responses import new_uuid
+from ministack.services import cloudfront, s3
+
+logger = logging.getLogger("cloudfront_dataplane")
+
+_HOP_BY_HOP_REQUEST_HEADERS = {"host", "cookie", "content-length", "connection", "transfer-encoding"}
+# A narrower set used only at the actual socket boundary (_forward_to_origin):
+# by that point Host and Cookie have already been deliberately recomputed by
+# the caller (per the forwarding policy and the Host-forwarding rule) and
+# must survive, unlike the other hop-by-hop headers HTTP itself manages.
+_CONNECTION_MANAGEMENT_HEADERS = {"content-length", "connection", "transfer-encoding"}
+
+# Forwarded regardless of any cache-policy/origin-request-policy header
+# configuration (CloudFront Developer Guide, "HTTP request headers and
+# CloudFront behavior (custom and Amazon S3 origins)": Range and the
+# conditional-GET headers are listed as always forwarded, not gated on
+# whether you've configured CloudFront to forward them).
+_ALWAYS_FORWARD_REQUEST_HEADERS = {
+ "range", "if-match", "if-modified-since", "if-none-match", "if-range", "if-unmodified-since",
+}
+
+# Dropped from an origin's response before it reaches the viewer — connection
+# management is this module's concern, not the origin's.
+_HOP_BY_HOP_RESPONSE_HEADERS = {"connection", "keep-alive", "transfer-encoding", "upgrade", "trailer"}
+
+
+class CloudFrontFunctionError(Exception):
+ """A CloudFront Function threw, timed out, or its code didn't define a
+ handler — CloudFront answers every one of these with a 503."""
+
+
+# ---------------------------------------------------------------------------
+# CloudFront Functions event <-> HTTP conversion (functions-event-structure.html)
+# ---------------------------------------------------------------------------
+
+
+def _title_case_header_name(name: str) -> str:
+ """"example-header-name" -> "Example-Header-Name" — the conversion
+ CloudFront Functions applies to every header name when it converts the
+ event object back into an HTTP request or response. ASCII letters only;
+ left untouched otherwise, matching the documented behavior."""
+ return "-".join(part[:1].upper() + part[1:] if part[:1].isascii() else part for part in name.split("-"))
+
+
+def _event_items(values: dict) -> dict:
+ """A querystring/headers object: one field per name, carrying a
+ ``multiValue`` array when the same name repeated (functions-event-
+ structure.html, "Duplicate query strings, headers, and cookies")."""
+ out = {}
+ for name, value in values.items():
+ items = value if isinstance(value, list) else [value]
+ out[name] = {"value": items[0]}
+ if len(items) > 1:
+ out[name]["multiValue"] = [{"value": v} for v in items]
+ return out
+
+
+def _event_cookies_from_header(cookie_header: str) -> dict:
+ cookies = {}
+ for part in (cookie_header or "").split(";"):
+ name, sep, value = part.strip().partition("=")
+ if sep and name:
+ cookies[name] = {"value": value.strip()}
+ return cookies
+
+
+def _event_cookies_from_set_cookie(set_cookie) -> dict:
+ """The response.cookies event object built from one or more raw
+ Set-Cookie header values, grouped by cookie name with a ``multiValue``
+ array for repeats and an ``attributes`` string carrying everything after
+ the first ``;`` (functions-event-structure.html, "Cookie attributes")."""
+ values = set_cookie if isinstance(set_cookie, list) else ([set_cookie] if set_cookie else [])
+ cookies = {}
+ for raw in values:
+ name_value, _, attrs = raw.partition(";")
+ name, _, value = name_value.partition("=")
+ name = name.strip()
+ if not name:
+ continue
+ entry = {"value": value.strip()}
+ if attrs.strip():
+ entry["attributes"] = attrs.strip()
+ if name in cookies:
+ existing = cookies[name]
+ existing.setdefault("multiValue", [{k: v for k, v in existing.items() if k != "multiValue"}])
+ existing["multiValue"].append(entry)
+ else:
+ cookies[name] = entry
+ return cookies
+
+
+def _set_cookie_values_from_event(cookies: dict) -> list:
+ out = []
+ for name, field in (cookies or {}).items():
+ if not isinstance(field, dict):
+ continue
+ entries = field.get("multiValue") or [field]
+ for entry in entries:
+ line = f"{name}={entry.get('value', '')}"
+ if entry.get("attributes"):
+ line += f"; {entry['attributes']}"
+ out.append(line)
+ return out
+
+
+def _event_querystring(query_params: dict) -> dict:
+ return _event_items({name: values for name, values in (query_params or {}).items()})
+
+
+def _merge_function_headers(original_headers_obj: dict, returned_headers_obj: dict) -> dict:
+ """A name(lowercase)->value dict built from a function's returned headers
+ object. functions-event-structure.html: a header whose ``multiValue``
+ field is unchanged from what the event carried is read only for its
+ primary ``value`` (the function is assumed to have edited just that),
+ and the rest of that header's original values carry through unedited; a
+ header whose ``multiValue`` did change is read in full instead."""
+ original = original_headers_obj or {}
+ out = {}
+ for name, field in (returned_headers_obj or {}).items():
+ if not isinstance(field, dict):
+ continue
+ orig_field = original.get(name)
+ orig_multi = orig_field.get("multiValue") if isinstance(orig_field, dict) else None
+ new_multi = field.get("multiValue")
+ if new_multi and new_multi != orig_multi:
+ out[name.lower()] = [str(v.get("value", "")) for v in new_multi]
+ elif orig_multi:
+ out[name.lower()] = [str(field.get("value", ""))] + [str(v.get("value", "")) for v in orig_multi[1:]]
+ elif "value" in field:
+ out[name.lower()] = str(field["value"])
+ return out
+
+
+def _build_function_event(event_type: str, dist_id: str, dist_domain: str, request_id: str,
+ method: str, uri: str, query_params: dict, headers: dict, client_ip: str) -> dict:
+ request_headers = {k: v for k, v in headers.items() if k != "cookie"}
+ return {
+ "version": "1.0",
+ "context": {
+ "distributionDomainName": dist_domain,
+ "distributionId": dist_id,
+ "eventType": event_type,
+ "requestId": request_id,
+ },
+ # functions-event-structure.html: the TCP peer, not a header value.
+ "viewer": {"ip": client_ip},
+ "request": {
+ "method": method,
+ "uri": uri,
+ "querystring": _event_querystring(query_params),
+ "headers": _event_items(request_headers),
+ "cookies": _event_cookies_from_header(headers.get("cookie", "")),
+ },
+ }
+
+
+def _response_event_fields(status: int, reason: str, headers: dict) -> dict:
+ response_headers = {k: v for k, v in headers.items() if k != "set-cookie"}
+ return {
+ "statusCode": status,
+ "statusDescription": reason or "",
+ "headers": _event_items(response_headers),
+ "cookies": _event_cookies_from_set_cookie(headers.get("set-cookie")),
+ }
+
+
+def _body_from_response_event(response: dict, fallback: bytes) -> bytes:
+ body = response.get("body")
+ if body is None:
+ return fallback
+ if isinstance(body, str):
+ return body.encode("utf-8")
+ if isinstance(body, dict):
+ data = body.get("data", "")
+ if body.get("encoding") == "base64":
+ return base64.b64decode(data)
+ return str(data).encode("utf-8")
+ return fallback
+
+
+async def _run_function(function_arn: str, event: dict) -> dict:
+ code = cloudfront.live_function_code(function_arn)
+ if code is None:
+ raise CloudFrontFunctionError(f"{function_arn} has no published (LIVE) code")
+ try:
+ # A function cannot call back into MiniStack (no network access in
+ # its sandbox — see core.cloudfront_js), so this is run_offloop work,
+ # same as the S3 origin fetch (core.concurrency).
+ return await run_offloop(cloudfront_js.evaluate, code, event)
+ except cloudfront_js.CloudFrontFunctionError as exc:
+ raise CloudFrontFunctionError(str(exc)) from exc
+
+
+# ---------------------------------------------------------------------------
+# Cache behavior matching — CloudFront path-pattern glob, first match wins.
+# ---------------------------------------------------------------------------
+
+
+def _glob_to_regex(pattern: str):
+ # "You can optionally include a slash (/) at the beginning of the path
+ # pattern... If you don't specify the / at the beginning of the path,
+ # this character is automatically implied; CloudFront treats the path
+ # the same with or without the leading /" (CloudFront Developer Guide,
+ # "Path pattern").
+ if not pattern.startswith("/"):
+ pattern = "/" + pattern
+ out = [".*" if ch == "*" else "." if ch == "?" else _re_escape(ch) for ch in pattern]
+ return _re_compile("^" + "".join(out) + "$")
+
+
+def _match_behavior(parsed: dict, path: str):
+ for behavior in parsed["ordered_behaviors"]:
+ if _glob_to_regex(behavior["path_pattern"]).match(path):
+ return behavior
+ return parsed["default_behavior"]
+
+
+# ---------------------------------------------------------------------------
+# Origin-request forwarding — CloudFront Developer Guide, "Understand how
+# origin request policies and cache policies work together" (the table of
+# None/All/Allow-list/Block-list combinations this reproduces) plus legacy
+# ``ForwardedValues`` ("Control origin requests with a policy": "With legacy
+# cache settings, CloudFront forwards the headers to your origin by
+# default").
+# ---------------------------------------------------------------------------
+
+
+def _behavior_kind(behavior: str, names, lowercase: bool = False):
+ """A cache-policy or origin-request-policy behavior value, reduced to a
+ (kind, set) pair the combinator below understands. ``allViewer`` and
+ ``allViewerAndWhitelistCloudFront`` (origin-request-policy-only header
+ behaviors) both forward every viewer header, so both map to "all" —
+ this module doesn't synthesize the ~20 CloudFront-added geo/device
+ headers the second one also adds (see module docstring)."""
+ names = [n.lower() for n in (names or [])] if lowercase else list(names or [])
+ if behavior in ("all", "allViewer", "allViewerAndWhitelistCloudFront"):
+ return ("all", None)
+ if behavior == "whitelist":
+ return ("whitelist", set(names))
+ if behavior == "allExcept":
+ return ("allExcept", set(names))
+ return ("none", None)
+
+
+def _combine_forward(cache_kind_set, orp_kind_set, name: str) -> bool:
+ """Whether ``name`` reaches the origin, per the cache-policy /
+ origin-request-policy combination table. ``orp_kind_set`` is None when
+ the behavior has no origin request policy at all (same result as an
+ origin request policy whose behavior is "none" for every category)."""
+ cache_kind, cache_set = cache_kind_set
+ if cache_kind == "all":
+ return True # a cache policy's "all" always wins, even over an ORP block list
+ orp_kind, orp_set = orp_kind_set if orp_kind_set is not None else ("none", None)
+ if orp_kind == "all":
+ return True
+ if cache_kind == "none":
+ if orp_kind == "whitelist":
+ return name in orp_set
+ if orp_kind == "allExcept":
+ return name not in orp_set
+ return False
+ if cache_kind == "whitelist":
+ if name in cache_set:
+ return True
+ if orp_kind == "whitelist":
+ return name in orp_set
+ if orp_kind == "allExcept":
+ return name not in orp_set
+ return False
+ if cache_kind == "allExcept":
+ if name not in cache_set:
+ if orp_kind == "allExcept":
+ return name not in orp_set
+ return True # cache policy already allows it through unless ORP also blocks it
+ return orp_kind == "whitelist" and name in orp_set # ORP allow-list rescues a cache-blocked name
+ return False
+
+
+def _legacy_predicates(fv: dict):
+ """Forwarding predicates for a behavior that has no CachePolicyId — a
+ legacy ``ForwardedValues`` block instead (AWS requires a cache policy
+ before an origin request policy can be attached, so legacy behaviors
+ never carry one)."""
+ header_names = {n.lower() for n in (fv.get("headers") or [])}
+ forward_all_headers = "*" in (fv.get("headers") or [])
+ cookies_forward = fv.get("cookies_forward", "none")
+ cookies_whitelist = fv.get("cookies_whitelist") or []
+ forward_qs = bool(fv.get("query_string"))
+
+ def header_allowed(name):
+ return forward_all_headers or name.lower() in header_names
+
+ def cookie_allowed(name):
+ if cookies_forward == "all":
+ return True
+ if cookies_forward == "whitelist":
+ return any(fnmatch.fnmatchcase(name, pattern) for pattern in cookies_whitelist)
+ return False
+
+ def qs_allowed(_name):
+ return forward_qs
+
+ return header_allowed, cookie_allowed, qs_allowed
+
+
+def _policy_predicates(behavior: dict):
+ cache_params = cloudfront.cache_policy_params(behavior.get("cache_policy_id")) or {}
+ orp_cfg = (
+ cloudfront.origin_request_policy_config(behavior["origin_request_policy_id"])
+ if behavior.get("origin_request_policy_id") else None
+ )
+
+ cache_headers = _behavior_kind(cache_params.get("HeaderBehavior", "none"), cache_params.get("Headers"), lowercase=True)
+ cache_cookies = _behavior_kind(cache_params.get("CookieBehavior", "none"), cache_params.get("Cookies"))
+ cache_qs = _behavior_kind(cache_params.get("QueryStringBehavior", "none"), cache_params.get("QueryStrings"))
+
+ if orp_cfg is not None:
+ orp_headers = _behavior_kind(orp_cfg["HeaderBehavior"], orp_cfg["Headers"], lowercase=True)
+ orp_cookies = _behavior_kind(orp_cfg["CookieBehavior"], orp_cfg["Cookies"])
+ orp_qs = _behavior_kind(orp_cfg["QueryStringBehavior"], orp_cfg["QueryStrings"])
+ else:
+ orp_headers = orp_cookies = orp_qs = None
+
+ def header_allowed(name):
+ return _combine_forward(cache_headers, orp_headers, name.lower())
+
+ def cookie_allowed(name):
+ return _combine_forward(cache_cookies, orp_cookies, name)
+
+ def qs_allowed(name):
+ return _combine_forward(cache_qs, orp_qs, name)
+
+ return header_allowed, cookie_allowed, qs_allowed
+
+
+def _forwarding_predicates(behavior: dict):
+ if behavior.get("cache_policy_id"):
+ return _policy_predicates(behavior)
+ if behavior.get("forwarded_values") is not None:
+ return _legacy_predicates(behavior["forwarded_values"])
+ # Neither a cache policy nor legacy ForwardedValues: nothing beyond the
+ # defaults every origin request always carries (Host, User-Agent).
+ return (lambda _n: False), (lambda _n: False), (lambda _n: False)
+
+
+# ---------------------------------------------------------------------------
+# Response-headers policy
+# ---------------------------------------------------------------------------
+
+
+def _cors_origin_allowed(allow_origins: list, viewer_origin: str) -> bool:
+ """AccessControlAllowOrigins entries: "*" or an origin value matched the
+ way the response-headers-policy CORS doc's wildcard examples show
+ (``*`` only ever a glob over the whole value here, not AWS's narrower
+ leftmost-subdomain validation rule — that rule constrains what a policy
+ may be *created* with, not what this module needs to match against)."""
+ return any(p == "*" or fnmatch.fnmatchcase(viewer_origin, p) for p in allow_origins or [])
+
+
+def _apply_cors_headers(headers: dict, cors_cfg: dict, viewer_origin: str, method: str) -> dict:
+ """Response-headers-policy CORS config (CloudFront Developer Guide,
+ "Understand response headers policies" > "CORS headers"): these are
+ headers added to a response to a CORS request, so nothing here applies
+ unless the viewer request carried an Origin header. Access-Control-
+ Allow-Headers/-Methods/-Max-Age are documented as values for
+ *preflight* responses specifically; Allow-Origin/-Credentials/-Expose-
+ Headers apply to any CORS response."""
+ if not cors_cfg or not viewer_origin or not _cors_origin_allowed(cors_cfg.get("AllowOrigins"), viewer_origin):
+ return headers
+ result = dict(headers)
+ override = cors_cfg.get("OriginOverride", False)
+
+ def _set(name, value):
+ key = name.lower()
+ if key in result and not override:
+ return
+ result[key] = value
+
+ _set("access-control-allow-origin", "*" if "*" in (cors_cfg.get("AllowOrigins") or []) else viewer_origin)
+ if cors_cfg.get("AllowCredentials"):
+ _set("access-control-allow-credentials", "true")
+ if cors_cfg.get("ExposeHeaders"):
+ _set("access-control-expose-headers", ", ".join(cors_cfg["ExposeHeaders"]))
+ if method == "OPTIONS":
+ if cors_cfg.get("AllowHeaders"):
+ _set("access-control-allow-headers", ", ".join(cors_cfg["AllowHeaders"]))
+ if cors_cfg.get("AllowMethods"):
+ _set("access-control-allow-methods", ", ".join(cors_cfg["AllowMethods"]))
+ if cors_cfg.get("MaxAgeSec") is not None:
+ _set("access-control-max-age", str(cors_cfg["MaxAgeSec"]))
+ return result
+
+
+def _apply_response_headers_policy(headers: dict, policy_cfg: dict, viewer_origin: str, method: str) -> dict:
+ if not policy_cfg:
+ return headers
+ result = _apply_cors_headers(headers, policy_cfg.get("Cors"), viewer_origin, method)
+
+ def _set(name, value, override):
+ key = name.lower()
+ if key in result and not override:
+ return
+ result[key] = value
+
+ # CustomHeaders/RemoveHeaders are None (not []) when the create request
+ # never supplied that block at all, distinct from supplying an empty one
+ # — `or []` covers both "absent" and "empty".
+ for item in policy_cfg.get("CustomHeaders") or []:
+ _set(item["Header"], item["Value"], item["Override"])
+
+ sec = policy_cfg.get("Security") or {}
+ if "ContentTypeOptions" in sec:
+ _set("x-content-type-options", "nosniff", sec["ContentTypeOptions"]["Override"])
+ if "FrameOptions" in sec:
+ _set("x-frame-options", sec["FrameOptions"]["FrameOption"], sec["FrameOptions"]["Override"])
+ if "XSSProtection" in sec:
+ xp = sec["XSSProtection"]
+ value = "1" if xp["Protection"] else "0"
+ if xp["Protection"] and xp.get("ModeBlock"):
+ value += "; mode=block"
+ if xp.get("ReportUri"):
+ value += f"; report={xp['ReportUri']}"
+ _set("x-xss-protection", value, xp["Override"])
+ if "ReferrerPolicy" in sec:
+ _set("referrer-policy", sec["ReferrerPolicy"]["ReferrerPolicy"], sec["ReferrerPolicy"]["Override"])
+ if "ContentSecurityPolicy" in sec:
+ _set("content-security-policy", sec["ContentSecurityPolicy"]["ContentSecurityPolicy"],
+ sec["ContentSecurityPolicy"]["Override"])
+ if "StrictTransportSecurity" in sec:
+ hsts = sec["StrictTransportSecurity"]
+ value = f"max-age={hsts['AccessControlMaxAgeSec']}"
+ if hsts.get("IncludeSubdomains"):
+ value += "; includeSubDomains"
+ if hsts.get("Preload"):
+ value += "; preload"
+ _set("strict-transport-security", value, hsts["Override"])
+
+ for item in policy_cfg.get("RemoveHeaders") or []:
+ result.pop(item["Header"].lower(), None)
+
+ return result
+
+
+# ---------------------------------------------------------------------------
+# CloudFront's own added headers and synthesized error pages
+# ---------------------------------------------------------------------------
+
+
+def _cf_request_id() -> str:
+ return base64.b64encode(os.urandom(42)).decode().rstrip("=")
+
+
+def _cf_via_hash() -> str:
+ return "".join(random.choices(string.ascii_lowercase + string.digits, k=13))
+
+
+def _add_cloudfront_headers(headers: dict, x_cache: str, request_id: str | None = None) -> dict:
+ result = dict(headers)
+ result["via"] = f"1.1 {_cf_via_hash()}.cloudfront.net (CloudFront)"
+ result["x-cache"] = x_cache
+ result["x-amz-cf-pop"] = "LOC50-C1"
+ result["x-amz-cf-id"] = request_id or _cf_request_id()
+ return result
+
+
+def _render_headers(headers: dict, title_case: bool) -> dict:
+ if not title_case:
+ return dict(headers)
+ return {_title_case_header_name(name): value for name, value in headers.items()}
+
+
+# Mirrors the fixed body CloudFront's own synthesized error pages use — a
+# "The request could not be satisfied" title/heading across status codes,
+# with only the numeric status and explanation paragraph varying — observed
+# from live responses rather than published in the CloudFront Developer
+# Guide, so the per-status explanation text below is inference, not an
+# AWS-confirmed value.
+_CF_ERROR_TEMPLATE = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
+<TITLE>ERROR: The request could not be satisfied</TITLE>
+</HEAD><BODY>
+<H1>{status} ERROR</H1>
+<H2>The request could not be satisfied.</H2>
+<HR noshade size="1px">
+{explanation}
+<BR clear="all">
+<HR noshade size="1px">
+<PRE>
+Generated by cloudfront (CloudFront)
+Request ID: {request_id}
+</PRE>
+</BODY></HTML>"""
+
+
+def _cf_error_response(status: int, explanation: str, x_cache: str) -> tuple:
+ request_id = _cf_request_id()
+ body = _CF_ERROR_TEMPLATE.format(status=status, explanation=explanation,
+ request_id=request_id).encode("utf-8")
+ headers = _add_cloudfront_headers({"content-type": "text/html"}, x_cache, request_id=request_id)
+ return status, _render_headers(headers, title_case=True), body
+
+
+# CloudFront Developer Guide, http-502-bad-gateway.html: "the SSL/TLS
+# handshake fails, and CloudFront returns an HTTP status code 502 (Bad
+# Gateway) and sets the X-Cache header to Error from cloudfront" — the only
+# documented X-Cache value for a CloudFront-synthesized error; applied here
+# to every such response (unreachable origin, blocked method, protocol
+# policy, function failure), not only the SSL case the doc names.
+_ERROR_X_CACHE = "Error from cloudfront"
+
+
+# ---------------------------------------------------------------------------
+# Origin forwarding
+# ---------------------------------------------------------------------------
+
+
+class OriginUnreachable(Exception):
+ pass
+
+
+class OriginTimeout(Exception):
+ """The origin didn't respond within OriginReadTimeout."""
+
+
+# A custom origin's DomainName can be AWS-owned-shaped — an execute-api/ALB/
+# Lambda-URL/S3 host Terraform computed from a MiniStack-run resource, using
+# AWS's own naming template. It is never dialed: refused with a 502 before a
+# socket opens (module docstring's Not-modelled list). Every other origin,
+# including a plain MiniStack-local host, is dialed at its own DomainName and
+# port exactly as configured (sns.py's HTTP delivery precedent).
+_AWS_OWNED_HOST_SUFFIXES = (".amazonaws.com", ".amazonaws.com.cn", ".on.aws", ".api.aws", ".cloudfront.net")
+
+
+def _normalize_origin_host(host: str) -> str:
+ h = (host or "").strip().lower()
+ return h[:-1] if h.endswith(".") else h
+
+
+def _is_aws_owned_host(host: str) -> bool:
+ return any(host == suffix.removeprefix(".") or host.endswith(suffix) for suffix in _AWS_OWNED_HOST_SUFFIXES)
+
+
+def _origin_connect_target(origin: dict, viewer_is_https: bool) -> tuple:
+ """(connect_host, connect_port, use_https) — the origin's own
+ DomainName and HTTPPort/HTTPSPort, dialed exactly as configured."""
+ policy = origin["protocol_policy"]
+ use_https = policy == "https-only" or (policy == "match-viewer" and viewer_is_https)
+ # MiniStack's own endpoints carry the gateway port (an HTTP API's apiEndpoint
+ # is `https://<id>.execute-api.<host>:4566`), so an origin built from one
+ # names it; AWS itself takes ports only from HTTPPort/HTTPSPort.
+ host, sep, port = origin["domain_name"].rpartition(":")
+ if sep and port.isdigit():
+ return host, int(port), use_https
+ port = origin["https_port"] if use_https else origin["http_port"]
+ return origin["domain_name"], port, use_https
+
+
+def _origin_ssl_context() -> ssl.SSLContext:
+ """System roots, plus the gateway's own TLS cert under USE_SSL
+ (lambda_svc.py's container CA-trust precedent) — a custom https origin
+ may itself be a MiniStack-served endpoint sharing that cert."""
+ from ministack.core import tls
+
+ ctx = ssl.create_default_context()
+ if tls.use_ssl_enabled():
+ ctx.load_verify_locations(tls.resolve_tls_material()[0])
+ ctx.verify_flags |= ssl.VERIFY_X509_PARTIAL_CHAIN
+ return ctx
+
+
+def _set_header_ci(headers: dict, name: str, value) -> None:
+ """Set ``name: value``, dropping any existing key that matches
+ case-insensitively first — otherwise a lowercase viewer-forwarded key and
+ a freshly Title-Cased one (e.g. a forwarded "host" plus this module's own
+ "Host") both survive as distinct dict keys and become two header lines."""
+ for existing in [k for k in headers if k.lower() == name.lower()]:
+ del headers[existing]
+ headers[name] = value
+
+
+def _origin_x_forwarded_for(viewer_xff: str, client_ip: str) -> str:
+ """CloudFront Developer Guide, "Client IP addresses": appends the TCP
+ peer's address to an existing viewer-sent X-Forwarded-For, or adds a
+ fresh one carrying just that address."""
+ return f"{viewer_xff}, {client_ip}" if viewer_xff else client_ip
+
+
+def _forward_to_origin(origin: dict, method: str, uri: str, query_string: str,
+ headers: dict, body: bytes, viewer_is_https: bool, client_ip: str = "127.0.0.1",
+ viewer_x_forwarded_for: str = "") -> tuple:
+ """Blocking: run under core.concurrency.run_reentrant (the origin may be
+ MiniStack itself). Returns (status, reason, headers, body)."""
+ conn_host, conn_port, use_https = _origin_connect_target(origin, viewer_is_https)
+ if _is_aws_owned_host(_normalize_origin_host(conn_host)):
+ raise OriginUnreachable(f"refusing to dial AWS-owned origin host {conn_host!r}")
+ target_path = uri + (f"?{query_string}" if query_string else "")
+
+ fwd_headers = {k: v for k, v in headers.items() if k not in _CONNECTION_MANAGEMENT_HEADERS}
+ for name, value in origin.get("custom_headers") or []:
+ _set_header_ci(fwd_headers, name, value)
+ # Host defaults to the origin's own configured DomainName — CloudFront's
+ # own default Host selection — unless the forwarding policy carries the
+ # viewer's.
+ _set_header_ci(fwd_headers, "Host", headers.get("host", origin["domain_name"]))
+ _set_header_ci(fwd_headers, "Via", f"1.1 {_cf_via_hash()}.cloudfront.net (CloudFront)")
+ _set_header_ci(fwd_headers, "X-Amz-Cf-Id", _cf_request_id())
+ # Always appended regardless of the forwarding policy (CloudFront
+ # Developer Guide, "Client IP addresses"), from the viewer's own XFF, not
+ # whatever the policy let through into `headers`.
+ _set_header_ci(fwd_headers, "X-Forwarded-For",
+ _origin_x_forwarded_for(viewer_x_forwarded_for, client_ip))
+ # RFC 9110 §5.3: a repeated field value is one comma-joined line.
+ send_headers = {
+ name: ", ".join(str(v) for v in value) if isinstance(value, list) else value
+ for name, value in fwd_headers.items()
+ }
+
+ conn_cls = http.client.HTTPSConnection if use_https else http.client.HTTPConnection
+ timeout = origin.get("read_timeout", 30)
+ conn = (
+ conn_cls(conn_host, conn_port, timeout=timeout, context=_origin_ssl_context())
+ if use_https else conn_cls(conn_host, conn_port, timeout=timeout)
+ )
+ try:
+ conn.request(method, target_path, body=body if body else None, headers=send_headers)
+ resp = conn.getresponse()
+ data = resp.read()
+ return resp.status, resp.reason, resp.getheaders(), data
+ except TimeoutError as e:
+ raise OriginTimeout(str(e)) from e
+ except Exception as e:
+ # Covers an unreachable origin and a custom origin's untrusted/
+ # self-signed certificate failing verification (see
+ # http-502-bad-gateway.html's self-signed/expired/invalid-chain case).
+ raise OriginUnreachable(str(e)) from e
+ finally:
+ conn.close()
+
+
+# ---------------------------------------------------------------------------
+# S3 origin — served from MiniStack's own S3 store in-process, never a socket.
+# ---------------------------------------------------------------------------
+
+
+def _s3_object_key(origin: dict, uri: str) -> str:
+ """CloudFront concatenates OriginPath directly in front of the request
+ URI to build what reaches the origin. S3 keys are stored decoded, so the
+ percent-encoding forwarded verbatim to a custom origin is decoded here
+ instead, and the leading slash dropped."""
+ return unquote((origin.get("origin_path") or "") + uri).lstrip("/")
+
+
+def _serve_s3_origin(origin: dict, method: str, uri: str, headers: dict) -> tuple:
+ """Blocking: run under core.concurrency.run_offloop (an in-process store
+ read, never re-entrant). Mirrors _forward_to_origin's calling convention
+ (status, headers, body — no reason phrase; the caller derives one from
+ the status)."""
+ return s3.serve_cloudfront_origin_fetch(origin["s3_bucket"], _s3_object_key(origin, uri), method, headers)
+
+
+# ---------------------------------------------------------------------------
+# Multi-value header handling — a name -> value map where a repeated name
+# (duplicate response headers, multiple Set-Cookie) becomes a name -> list.
+# ---------------------------------------------------------------------------
+
+
+def _multidict_from_pairs(pairs) -> dict:
+ out: dict = {}
+ for name, value in pairs:
+ key = name.lower()
+ if key in out:
+ existing = out[key]
+ if isinstance(existing, list):
+ existing.append(value)
+ else:
+ out[key] = [existing, value]
+ else:
+ out[key] = value
+ return out
+
+
+def _strip_hop_by_hop_response_headers(headers: dict) -> dict:
+ """Connection management is this module's concern, not the origin's."""
+ return {k: v for k, v in headers.items() if k not in _HOP_BY_HOP_RESPONSE_HEADERS
+ and not k.startswith("proxy-")}
+
+
+# ---------------------------------------------------------------------------
+# Behavior-level settings: ViewerProtocolPolicy, AllowedMethods, DefaultRootObject
+# ---------------------------------------------------------------------------
+
+
+def _check_viewer_protocol_policy(behavior: dict, method: str, viewer_is_https: bool, request_uri: str,
+ query_string: str, headers: dict):
+ """A redirect/forbid response, or None to continue. CloudFront Developer
+ Guide, "Require HTTPS for communication between viewers and CloudFront" >
+ "Require HTTPS for viewers": redirect-to-https sends 301 (GET/HEAD) or
+ 307 (other methods, HTTP/1.1+ — "This guarantees that the request is
+ sent again to the new location using the same method and body payload")
+ with the HTTPS URL; https-only answers plain HTTP with 403. (The same
+ page also documents a 403 for a non-idempotent method on pre-HTTP/1.1 —
+ out of scope here: this module has no HTTP-version signal.)"""
+ policy = behavior.get("viewer_protocol_policy", "allow-all")
+ if viewer_is_https or policy == "allow-all":
+ return None
+ if policy == "redirect-to-https":
+ host = headers.get("host", "")
+ location = f"https://{host}{request_uri}" + (f"?{query_string}" if query_string else "")
+ redirect_status = 301 if method in ("GET", "HEAD") else 307
+ status, resp_headers, body = _cf_error_response(redirect_status, "The document has moved.", _ERROR_X_CACHE)
+ resp_headers["Location"] = location
+ return status, resp_headers, body
+ if policy == "https-only":
+ return _cf_error_response(403, "Viewers must use HTTPS to request this object.", _ERROR_X_CACHE)
+ return None
+
+
+def _check_allowed_methods(behavior: dict, method: str):
+ allowed = behavior.get("allowed_methods")
+ if allowed is not None and method not in allowed:
+ # Community-observed CloudFront 403 page text for this exact case
+ # (see _CF_ERROR_TEMPLATE's citation note).
+ return _cf_error_response(
+ 403,
+ "This distribution is not configured to allow the HTTP request method that was used for this "
+ "request. The distribution supports only cachable requests.",
+ _ERROR_X_CACHE,
+ )
+ return None
+
+
+def _apply_default_root_object(default_root_object: str, request_uri: str) -> str:
+ """CloudFront Developer Guide, "Default root object": the configured
+ object is requested from the origin when a viewer requests the
+ distribution's root URL."""
+ if default_root_object and request_uri == "/":
+ return "/" + default_root_object
+ return request_uri
+
+
+def _check_stacked_distribution(headers: dict):
+ """CloudFront Developer Guide, http-403-permission-denied.html, "Stacked
+ distributions cause a 403 error": a request that already carries this
+ hop's own Via marker (`_forward_to_origin` always stamps one) is refused
+ rather than forwarded again, so a distribution fronting itself fails
+ fast instead of recursing forever."""
+ if "(cloudfront)" in headers.get("via", "").lower():
+ return _cf_error_response(
+ 403, "This distribution is configured to serve another CloudFront distribution as its origin.",
+ _ERROR_X_CACHE,
+ )
+ return None
+
+
+# ---------------------------------------------------------------------------
+# Main entry point
+# ---------------------------------------------------------------------------
+
+
+async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_query_string: str,
+ headers: dict, body: bytes, query_params: dict, client_ip: str = "127.0.0.1") -> tuple:
+ """Serve one viewer request against ``dist`` (a cloudfront.py distribution
+ record). Returns an (status, headers, body) tuple in the shape the ASGI
+ gateway sends directly to the client.
+
+ ``path`` is the ASGI-decoded path (used only for cache-behavior
+ path-pattern matching); ``raw_uri`` and ``raw_query_string`` are the
+ request target exactly as the viewer sent it, percent-encoding intact,
+ for forwarding byte-exact when nothing rewrites them (CloudFront
+ Developer Guide, "Restrictions on all edge functions" > "URI, query
+ string, and headers encoding"). ``client_ip`` is the TCP peer's address.
+ """
+ stacked_response = _check_stacked_distribution(headers)
+ if stacked_response is not None:
+ return stacked_response
+
+ dist_id = dist["Id"]
+ dist_domain = dist["DomainName"]
+ request_id = new_uuid()
+ viewer_is_https = headers.get("x-forwarded-proto", "http") == "https"
+
+ parsed = cloudfront.parse_distribution_dataplane_config(dist)
+ behavior = _match_behavior(parsed, path)
+ if behavior is None:
+ return _cf_error_response(502, "The request could not be satisfied.", _ERROR_X_CACHE)
+
+ protocol_response = _check_viewer_protocol_policy(
+ behavior, method, viewer_is_https, raw_uri, raw_query_string, headers,
+ )
+ if protocol_response is not None:
+ return protocol_response
+ methods_response = _check_allowed_methods(behavior, method)
+ if methods_response is not None:
+ return methods_response
+
+ request_headers = dict(headers)
+ request_uri = _apply_default_root_object(parsed.get("default_root_object", ""), raw_uri)
+ request_query = dict(query_params or {})
+ literal_query_string = None # set when a function rearranges querystring into a literal string
+ function_touched_querystring = False # set when a function's returned request names a querystring object
+
+ request_function_arn = behavior["functions"].get("viewer-request")
+ if request_function_arn:
+ event = _build_function_event(
+ "viewer-request", dist_id, dist_domain, request_id,
+ method, request_uri, request_query, request_headers, client_ip,
+ )
+ try:
+ result = await _run_function(request_function_arn, event)
+ except CloudFrontFunctionError:
+ logger.exception("viewer-request function error (distribution=%s)", dist_id)
+ return _cf_error_response(503, "The Lambda function failed to execute.", _ERROR_X_CACHE)
+
+ if isinstance(result, dict) and "statusCode" in result:
+ # Function-generated response: CloudFront returns it to the
+ # viewer WITHOUT forwarding to the origin, running the
+ # viewer-response function, or applying the response-headers
+ # policy (functions-event-structure.html).
+ resp_headers = _merge_function_headers(None, result.get("headers"))
+ resp_headers = _add_cloudfront_headers(resp_headers, "FunctionGeneratedResponse from cloudfront")
+ set_cookie_values = _set_cookie_values_from_event(result.get("cookies"))
+ if set_cookie_values:
+ resp_headers["set-cookie"] = (
+ set_cookie_values if len(set_cookie_values) > 1 else set_cookie_values[0]
+ )
+ # statusDescription has no outlet: ASGI's http.response.start
+ # carries only a numeric status, no reason-phrase field, so
+ # there is nowhere to forward it to the viewer wire response.
+ resp_body = _body_from_response_event(result, b"")
+ return result["statusCode"], _render_headers(resp_headers, title_case=True), resp_body
+
+ # Request-side headers stay lowercase-keyed internally; every
+ # viewer-facing response is Title-Cased once, at its single return
+ # point, rather than a decision repeated per header.
+ req = result if isinstance(result, dict) else event["request"]
+ request_uri = req.get("uri", request_uri)
+ request_headers = _merge_function_headers(event["request"]["headers"], req.get("headers"))
+ cookie_pairs = [f"{n}={(f.get('value') if isinstance(f, dict) else f)}"
+ for n, f in (req.get("cookies") or {}).items()]
+ if cookie_pairs:
+ request_headers["cookie"] = "; ".join(cookie_pairs)
+ qs_field = req.get("querystring")
+ if isinstance(qs_field, str):
+ # A function rearranged the querystring into a literal string
+ # (functions-event-structure.html, "Query strings values or
+ # query string objects"). It can no longer be decomposed by
+ # parameter name for cache/origin-request-policy filtering, so
+ # it is forwarded to the origin exactly as returned.
+ literal_query_string = qs_field
+ request_query = {}
+ elif qs_field is not None:
+ function_touched_querystring = True
+ request_query = {}
+ for name, field in qs_field.items():
+ if not isinstance(field, dict):
+ continue
+ values = [field.get("value", "")]
+ values += [v.get("value", "") for v in field.get("multiValue", [])[1:]]
+ request_query[name] = values
+
+ origin = parsed["origins"].get(behavior["target_origin_id"])
+ if origin is None:
+ return _cf_error_response(502, "The request could not be satisfied.", _ERROR_X_CACHE)
+
+ header_allowed, cookie_allowed, qs_allowed = _forwarding_predicates(behavior)
+ request_cookies = _event_cookies_from_header(request_headers.get("cookie", ""))
+ forward_host = header_allowed("host")
+
+ fwd_headers = {
+ name: value for name, value in request_headers.items()
+ if name not in _HOP_BY_HOP_REQUEST_HEADERS
+ and (header_allowed(name) or name in _ALWAYS_FORWARD_REQUEST_HEADERS)
+ }
+ if header_allowed("user-agent") and "user-agent" in request_headers:
+ fwd_headers["user-agent"] = request_headers["user-agent"]
+ else:
+ # CloudFront Developer Guide, "User-Agent header": "CloudFront adds
+ # this header regardless of whether the request from the viewer
+ # includes a User-Agent header. If the request from the viewer
+ # includes a User-Agent header, CloudFront removes it" — unless the
+ # policy forwards it (handled above), replace it unconditionally.
+ fwd_headers["user-agent"] = "Amazon CloudFront"
+ if forward_host:
+ fwd_headers["host"] = request_headers.get("host", "")
+ cookie_pairs = [f"{name}={field['value']}" for name, field in request_cookies.items() if cookie_allowed(name)]
+ if cookie_pairs:
+ fwd_headers["cookie"] = "; ".join(cookie_pairs)
+ if literal_query_string is not None:
+ forward_qs = literal_query_string
+ elif not function_touched_querystring and all(qs_allowed(name) for name in request_query):
+ # Byte-exact: nothing rewrote the querystring and the policy forwards
+ # every parameter, so there is no reason to re-encode it.
+ forward_qs = raw_query_string
+ else:
+ forward_qs = urlencode(
+ [(name, v) for name, values in request_query.items() if qs_allowed(name) for v in values],
+ quote_via=quote,
+ )
+
+ if origin.get("s3_bucket"):
+ status, origin_headers_list, origin_body = await run_offloop(
+ _serve_s3_origin, origin, method, request_uri, fwd_headers,
+ )
+ reason = http.client.responses.get(status, "")
+ origin_headers = _multidict_from_pairs(origin_headers_list.items() if isinstance(origin_headers_list, dict)
+ else origin_headers_list)
+ else:
+ full_uri = (origin.get("origin_path") or "") + request_uri
+ try:
+ # The origin may be MiniStack's own gateway (e.g. a distribution
+ # deliberately fronting another MiniStack-hosted service) — a
+ # bounded pool here could deadlock against itself.
+ status, reason, origin_headers_list, origin_body = await run_reentrant(
+ _forward_to_origin, origin, method, full_uri, forward_qs, fwd_headers, body, viewer_is_https,
+ client_ip, request_headers.get("x-forwarded-for", ""),
+ )
+ except OriginTimeout:
+ logger.warning("Origin timed out for distribution=%s origin=%s", dist_id, origin["domain_name"])
+ return _cf_error_response(504, "The request could not be satisfied.", _ERROR_X_CACHE)
+ except OriginUnreachable:
+ logger.warning("Origin unreachable for distribution=%s origin=%s", dist_id, origin["domain_name"])
+ return _cf_error_response(502, "The request could not be satisfied.", _ERROR_X_CACHE)
+ origin_headers = _multidict_from_pairs(origin_headers_list)
+
+ origin_headers = _strip_hop_by_hop_response_headers(origin_headers)
+ policy_cfg = cloudfront.response_headers_policy_config(behavior.get("response_headers_policy_id"))
+ response_headers = _apply_response_headers_policy(
+ origin_headers, policy_cfg, request_headers.get("origin", ""), method,
+ )
+ response_body = origin_body
+
+ response_function_arn = behavior["functions"].get("viewer-response")
+ # CloudFront does not invoke a viewer-response function when the origin
+ # answers 400 or above (functions-event-structure.html, "Status code and
+ # body" note); the response-headers policy above still applies to that
+ # response, only the function is skipped.
+ if response_function_arn and status < 400:
+ event = _build_function_event(
+ "viewer-response", dist_id, dist_domain, request_id,
+ method, request_uri, request_query, headers, client_ip,
+ )
+ event["response"] = _response_event_fields(status, reason, response_headers)
+ try:
+ result = await _run_function(response_function_arn, event)
+ except CloudFrontFunctionError:
+ logger.exception("viewer-response function error (distribution=%s)", dist_id)
+ return _cf_error_response(503, "The Lambda function failed to execute.", _ERROR_X_CACHE)
+ response = result if isinstance(result, dict) else event["response"]
+ status = response.get("statusCode", status)
+ # Replace, not merge: the returned headers object is the complete
+ # set the function wants sent, so a header it deleted must stay gone
+ # rather than survive from the pre-function response.
+ response_headers = _merge_function_headers(event["response"]["headers"], response.get("headers"))
+ set_cookie_values = _set_cookie_values_from_event(response.get("cookies"))
+ if set_cookie_values:
+ response_headers["set-cookie"] = set_cookie_values if len(set_cookie_values) > 1 else set_cookie_values[0]
+ response_body = _body_from_response_event(response, origin_body)
+
+ final_headers = _add_cloudfront_headers(response_headers, "Miss from cloudfront")
+ return status, _render_headers(final_headers, title_case=True), response_body
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 09c1fc4b..2e013d51 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -4476,6 +4476,29 @@ def _head_object(bucket_name: str, key: str, headers: dict | None = None, query_
return 200, resp_headers, b""
+def serve_cloudfront_origin_fetch(bucket_name: str, key: str, method: str, headers: dict) -> tuple:
+ """A CloudFront data plane's origin fetch against an S3 origin, served
+ in-process (never a real wire boundary, so no signature to verify —
+ calls ``_get_object``/``_head_object`` directly).
+
+ A missing key answers 403 AccessDenied, not 404 NoSuchKey: the OAC
+ bucket policy CloudFront's console generates grants only
+ ``s3:GetObject``, and per the GetObject API reference's "Permissions"
+ note, without ``s3:ListBucket`` a missing key can't be distinguished
+ from one the caller isn't allowed to see.
+ """
+ headers = dict(headers or {})
+ bucket = _ensure_bucket(bucket_name)
+ if bucket is None:
+ return _no_such_bucket(bucket_name)
+ if key not in bucket["objects"]:
+ return _error("AccessDenied", "Access Denied", 403, f"/{bucket_name}/{key}")
+ if method == "HEAD":
+ status, resp_headers, _ = _head_object(bucket_name, key, headers, {})
+ return status, resp_headers, b""
+ return _get_object(bucket_name, key, headers, {})
+
+
def _purge_current_object(bucket_name: str, key: str, bucket: dict):
"""Remove the current object plus its key-level metadata and on-disk copy."""
bucket["objects"].pop(key, None)
diff --git a/tests/test_cloudfront.py b/tests/test_cloudfront.py
index fcb5b5ca..c79780b9 100644
--- a/tests/test_cloudfront.py
+++ b/tests/test_cloudfront.py
@@ -1,6 +1,7 @@
import copy
import json
import os
+import re
import time
import urllib.error
import urllib.parse
@@ -12,6 +13,11 @@ from botocore.exceptions import ClientError
ENDPOINT = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
+# AWS's CloudFront edge domain shape: 'd' + 13 lowercase alphanumerics +
+# '.cloudfront.net' (e.g. d111111abcdef8.cloudfront.net), unrelated to the
+# distribution's own Id.
+_CF_DOMAIN_RE = re.compile(r"^d[a-z0-9]{13}\.cloudfront\.net$")
+
_CF_DIST_CONFIG = {
"CallerReference": "cf-test-ref-1",
"Origins": {
@@ -87,7 +93,7 @@ def test_cloudfront_create_distribution(cloudfront):
resp = cloudfront.create_distribution(DistributionConfig=_CF_DIST_CONFIG)
dist = resp["Distribution"]
assert dist["Id"]
- assert dist["DomainName"].endswith(".cloudfront.net")
+ assert _CF_DOMAIN_RE.match(dist["DomainName"])
assert dist["Status"] == "Deployed"
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 201
@@ -107,7 +113,7 @@ def test_cloudfront_create_distribution_with_tags(cloudfront):
dist = resp["Distribution"]
dist_id = dist["Id"]
dist_arn = dist["ARN"]
- assert dist["DomainName"].endswith(".cloudfront.net")
+ assert _CF_DOMAIN_RE.match(dist["DomainName"])
tags = cloudfront.list_tags_for_resource(Resource=dist_arn)["Tags"]["Items"]
assert any(t["Key"] == "env" and t["Value"] == "test" for t in tags)
etag = resp["ETag"]
@@ -135,12 +141,36 @@ def test_cloudfront_get_distribution(cloudfront):
resp = cloudfront.get_distribution(Id=dist_id)
dist = resp["Distribution"]
assert dist["Id"] == dist_id
- assert dist["DomainName"] == f"{dist_id}.cloudfront.net"
+ assert _CF_DOMAIN_RE.match(dist["DomainName"])
+ assert dist_id not in dist["DomainName"]
assert dist["Status"] == "Deployed"
# terraform-provider-aws v6+ dereferences OriginGroups without a nil check
assert dist["DistributionConfig"]["OriginGroups"]["Quantity"] == 0
+def test_cloudfront_distribution_domain_name_like_aws(cloudfront):
+ """DomainName is 'd' + 13 lowercase alphanumerics, like real AWS, and is
+ stable across Get/Update/List — not derived from the distribution's Id."""
+ cfg = {**_CF_DIST_CONFIG, "CallerReference": f"cf-domain-{_uuid_mod.uuid4().hex[:12]}"}
+ create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
+ dist_id = create_resp["Distribution"]["Id"]
+ domain = create_resp["Distribution"]["DomainName"]
+ assert _CF_DOMAIN_RE.match(domain)
+ assert dist_id not in domain
+
+ get_domain = cloudfront.get_distribution(Id=dist_id)["Distribution"]["DomainName"]
+ assert get_domain == domain
+
+ disabled_cfg = {**cfg, "Enabled": False}
+ upd = cloudfront.update_distribution(
+ DistributionConfig=disabled_cfg, Id=dist_id, IfMatch=create_resp["ETag"]
+ )
+ assert upd["Distribution"]["DomainName"] == domain
+
+ listed = {d["Id"]: d["DomainName"] for d in cloudfront.list_distributions()["DistributionList"]["Items"]}
+ assert listed[dist_id] == domain
+
+
def test_cloudfront_get_distribution_config(cloudfront):
cfg = {**_CF_DIST_CONFIG, "CallerReference": "cf-getcfg-1", "Comment": "getcfg-test"}
create_resp = cloudfront.create_distribution(DistributionConfig=cfg)
@@ -1616,6 +1646,218 @@ def test_cloudfront_response_headers_policy_duplicate_and_list(cloudfront):
assert resp["DistributionIdList"]["Quantity"] == 0
+# ---------------------------------------------------------------------------
+# AWS-managed cache / origin-request / response-headers policies.
+#
+# Names, ids and configs verified against the CloudFront Developer Guide's
+# "Use managed cache policies", "Use managed origin request policies", and
+# "Use managed response headers policies" pages. Real ids used below are the
+# ones ops-v2's modules/branding-assets and modules/cloudfront-api already
+# reference by literal id/name.
+# ---------------------------------------------------------------------------
+
+_MANAGED_CACHING_DISABLED_ID = "4135ea2d-6df8-44a3-9df3-4b5a84be39ad"
+_MANAGED_CACHING_OPTIMIZED_ID = "658327ea-f89d-4fab-a63d-7e88639e58f6"
+_MANAGED_ALL_VIEWER_ID = "216adef6-5c7f-47e4-b989-5492eafa07d3"
+_MANAGED_ALL_VIEWER_EXCEPT_HOST_ID = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
+_MANAGED_SECURITY_HEADERS_ID = "67f7725c-6f97-4210-82d7-5512b31e9d03"
+
+
+def test_cloudfront_list_cache_policies_includes_aws_managed(cloudfront):
+ resp = cloudfront.list_cache_policies()["CachePolicyList"]
+ by_id = {i["CachePolicy"]["Id"]: i for i in resp["Items"]}
+ assert _MANAGED_CACHING_DISABLED_ID in by_id
+ assert _MANAGED_CACHING_OPTIMIZED_ID in by_id
+ assert by_id[_MANAGED_CACHING_DISABLED_ID]["Type"] == "managed"
+ assert by_id[_MANAGED_CACHING_DISABLED_ID]["CachePolicy"]["CachePolicyConfig"]["Name"] == "Managed-CachingDisabled"
+
+ managed_only = cloudfront.list_cache_policies(Type="managed")["CachePolicyList"]
+ assert all(i["Type"] == "managed" for i in managed_only["Items"])
+ assert _MANAGED_CACHING_DISABLED_ID in {i["CachePolicy"]["Id"] for i in managed_only["Items"]}
+
+ custom_only = cloudfront.list_cache_policies(Type="custom")["CachePolicyList"]
+ assert _MANAGED_CACHING_DISABLED_ID not in {i["CachePolicy"]["Id"] for i in custom_only.get("Items", [])}
+
+
+def test_cloudfront_get_managed_cache_policy_by_id(cloudfront):
+ got = cloudfront.get_cache_policy(Id=_MANAGED_CACHING_DISABLED_ID)
+ cfg = got["CachePolicy"]["CachePolicyConfig"]
+ assert cfg["Name"] == "Managed-CachingDisabled"
+ assert cfg["MinTTL"] == 0 and cfg["MaxTTL"] == 0 and cfg["DefaultTTL"] == 0
+ params = cfg["ParametersInCacheKeyAndForwardedToOrigin"]
+ assert params["EnableAcceptEncodingGzip"] is False
+ assert params["HeadersConfig"]["HeaderBehavior"] == "none"
+
+ # Stable ETag across repeated reads (no mutation is possible).
+ assert cloudfront.get_cache_policy(Id=_MANAGED_CACHING_DISABLED_ID)["ETag"] == got["ETag"]
+
+ cfg_resp = cloudfront.get_cache_policy_config(Id=_MANAGED_CACHING_OPTIMIZED_ID)
+ optimized = cfg_resp["CachePolicyConfig"]
+ assert optimized["Name"] == "Managed-CachingOptimized"
+ assert optimized["MinTTL"] == 1 and optimized["DefaultTTL"] == 86400 and optimized["MaxTTL"] == 31536000
+ opt_params = optimized["ParametersInCacheKeyAndForwardedToOrigin"]
+ assert opt_params["EnableAcceptEncodingGzip"] is True
+ assert opt_params["EnableAcceptEncodingBrotli"] is True
+
+
+def test_cloudfront_managed_cache_policy_is_immutable(cloudfront):
+ with pytest.raises(ClientError) as exc:
+ cloudfront.update_cache_policy(
+ Id=_MANAGED_CACHING_DISABLED_ID,
+ IfMatch=cloudfront.get_cache_policy(Id=_MANAGED_CACHING_DISABLED_ID)["ETag"],
+ CachePolicyConfig=_cache_policy_config("attempted-rename"),
+ )
+ assert exc.value.response["Error"]["Code"] == "IllegalUpdate"
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.delete_cache_policy(
+ Id=_MANAGED_CACHING_DISABLED_ID,
+ IfMatch=cloudfront.get_cache_policy(Id=_MANAGED_CACHING_DISABLED_ID)["ETag"],
+ )
+ assert exc.value.response["Error"]["Code"] == "IllegalDelete"
+
+ # Still there afterwards.
+ assert cloudfront.get_cache_policy(Id=_MANAGED_CACHING_DISABLED_ID)
+
+
+def test_cloudfront_custom_cache_policy_cannot_reuse_managed_name(cloudfront):
+ with pytest.raises(ClientError) as exc:
+ cloudfront.create_cache_policy(CachePolicyConfig=_cache_policy_config("Managed-CachingDisabled"))
+ assert exc.value.response["Error"]["Code"] == "CachePolicyAlreadyExists"
+
+
+def test_cloudfront_list_origin_request_policies_includes_aws_managed(cloudfront):
+ resp = cloudfront.list_origin_request_policies()["OriginRequestPolicyList"]
+ by_id = {i["OriginRequestPolicy"]["Id"]: i for i in resp["Items"]}
+ assert _MANAGED_ALL_VIEWER_ID in by_id
+ assert _MANAGED_ALL_VIEWER_EXCEPT_HOST_ID in by_id
+ assert by_id[_MANAGED_ALL_VIEWER_ID]["Type"] == "managed"
+ all_viewer_cfg = by_id[_MANAGED_ALL_VIEWER_ID]["OriginRequestPolicy"]["OriginRequestPolicyConfig"]
+ assert all_viewer_cfg["Name"] == "Managed-AllViewer"
+ assert all_viewer_cfg["HeadersConfig"]["HeaderBehavior"] == "allViewer"
+
+ except_host_cfg = cloudfront.get_origin_request_policy_config(
+ Id=_MANAGED_ALL_VIEWER_EXCEPT_HOST_ID
+ )["OriginRequestPolicyConfig"]
+ assert except_host_cfg["Name"] == "Managed-AllViewerExceptHostHeader"
+ assert except_host_cfg["HeadersConfig"]["HeaderBehavior"] == "allExcept"
+ assert except_host_cfg["HeadersConfig"]["Headers"]["Items"] == ["Host"]
+ assert except_host_cfg["CookiesConfig"]["CookieBehavior"] == "all"
+
+
+def test_cloudfront_managed_origin_request_policy_is_immutable(cloudfront):
+ with pytest.raises(ClientError) as exc:
+ cloudfront.delete_origin_request_policy(
+ Id=_MANAGED_ALL_VIEWER_ID,
+ IfMatch=cloudfront.get_origin_request_policy(Id=_MANAGED_ALL_VIEWER_ID)["ETag"],
+ )
+ assert exc.value.response["Error"]["Code"] == "IllegalDelete"
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.update_origin_request_policy(
+ Id=_MANAGED_ALL_VIEWER_ID,
+ IfMatch=cloudfront.get_origin_request_policy(Id=_MANAGED_ALL_VIEWER_ID)["ETag"],
+ OriginRequestPolicyConfig=_orp_config("attempted-rename"),
+ )
+ assert exc.value.response["Error"]["Code"] == "IllegalUpdate"
+
+
+def test_cloudfront_list_response_headers_policies_includes_aws_managed(cloudfront):
+ resp = cloudfront.list_response_headers_policies()["ResponseHeadersPolicyList"]
+ by_id = {i["ResponseHeadersPolicy"]["Id"]: i for i in resp["Items"]}
+ assert _MANAGED_SECURITY_HEADERS_ID in by_id
+ assert by_id[_MANAGED_SECURITY_HEADERS_ID]["Type"] == "managed"
+
+ cfg = cloudfront.get_response_headers_policy_config(Id=_MANAGED_SECURITY_HEADERS_ID)["ResponseHeadersPolicyConfig"]
+ assert cfg["Name"] == "Managed-SecurityHeadersPolicy"
+ assert "CorsConfig" not in cfg
+ sec = cfg["SecurityHeadersConfig"]
+ assert sec["FrameOptions"]["FrameOption"] == "SAMEORIGIN"
+ assert sec["ReferrerPolicy"]["ReferrerPolicy"] == "strict-origin-when-cross-origin"
+ assert sec["ContentTypeOptions"]["Override"] is True
+ assert sec["StrictTransportSecurity"]["AccessControlMaxAgeSec"] == 31536000
+
+
+def test_cloudfront_managed_response_headers_policy_is_immutable(cloudfront):
+ with pytest.raises(ClientError) as exc:
+ cloudfront.delete_response_headers_policy(
+ Id=_MANAGED_SECURITY_HEADERS_ID,
+ IfMatch=cloudfront.get_response_headers_policy(Id=_MANAGED_SECURITY_HEADERS_ID)["ETag"],
+ )
+ assert exc.value.response["Error"]["Code"] == "IllegalDelete"
+
+
+# ---------------------------------------------------------------------------
+# Monitoring subscriptions (aws_cloudfront_monitoring_subscription).
+# Wire shapes verified against botocore cloudfront service-2.json (2020-05-31)
+# and the CreateMonitoringSubscription API reference (POST, 200 response;
+# DeleteMonitoringSubscription: DELETE, 200 with an empty body).
+# ---------------------------------------------------------------------------
+
+
+def test_cloudfront_monitoring_subscription_lifecycle(cloudfront):
+ dist_id = cloudfront.create_distribution(
+ DistributionConfig=_custom_origin_distribution_config(f"mon-{_uuid_mod.uuid4().hex[:8]}")
+ )["Distribution"]["Id"]
+
+ with pytest.raises(ClientError) as exc:
+ cloudfront.get_monitoring_subscription(DistributionId=dist_id)
+ assert exc.value.response["Error"]["Code"] == "NoSuchMonitoringSubscription"
+
+ created = cloudfront.create_monitoring_subscription(
+ DistributionId=dist_id,
+ MonitoringSubscription={
+ "RealtimeMetricsSubscriptionConfig": {"RealtimeMetricsSubscriptionStatus": "Enabled"}
+ },
+ )
+ assert (
+ created["MonitoringSubscription"]["RealtimeMetricsSubscriptionConfig"]["RealtimeMetricsSubscriptionStatus"]
+ == "Enabled"
+ )
+
+ got = cloudfront.get_monitoring_subscription(DistributionId=dist_id)
+ assert (
+ got["MonitoringSubscription"]["RealtimeMetricsSubscriptionConfig"]["RealtimeMetricsSubscriptionStatus"]
+ == "Enabled"
+ )
+
+ # A second Create overwrites rather than erroring: terraform-provider-aws's
+ # aws_cloudfront_monitoring_subscription resource calls this same
+ # operation for both Create and Update.
+ cloudfront.create_monitoring_subscription(
+ DistributionId=dist_id,
+ MonitoringSubscription={
+ "RealtimeMetricsSubscriptionConfig": {"RealtimeMetricsSubscriptionStatus": "Disabled"}
+ },
+ )
+ got = cloudfront.get_monitoring_subscription(DistributionId=dist_id)
+ assert (
+ got["MonitoringSubscription"]["RealtimeMetricsSubscriptionConfig"]["RealtimeMetricsSubscriptionStatus"]
+ == "Disabled"
+ )
+
+ cloudfront.delete_monitoring_subscription(DistributionId=dist_id)
+ with pytest.raises(ClientError) as exc:
+ cloudfront.get_monitoring_subscription(DistributionId=dist_id)
+ assert exc.value.response["Error"]["Code"] == "NoSuchMonitoringSubscription"
+
+
+def test_cloudfront_monitoring_subscription_missing_distribution(cloudfront):
+ for call in (
+ lambda: cloudfront.get_monitoring_subscription(DistributionId="ENOSUCHDIST0000000"),
+ lambda: cloudfront.delete_monitoring_subscription(DistributionId="ENOSUCHDIST0000000"),
+ lambda: cloudfront.create_monitoring_subscription(
+ DistributionId="ENOSUCHDIST0000000",
+ MonitoringSubscription={
+ "RealtimeMetricsSubscriptionConfig": {"RealtimeMetricsSubscriptionStatus": "Enabled"}
+ },
+ ),
+ ):
+ with pytest.raises(ClientError) as exc:
+ call()
+ assert exc.value.response["Error"]["Code"] == "NoSuchDistribution"
+
+
# ---------------------------------------------------------------------------
# Read-only list surface — ops previously falling through the path dispatch.
# Shapes verified against botocore cloudfront service-2.json (2020-05-31).
@@ -1717,8 +1959,11 @@ def test_cloudfront_list_cache_policies_round_trip(cloudfront):
assert listed["Quantity"] == baseline + 1
names = [s["CachePolicy"]["CachePolicyConfig"]["Name"] for s in listed["Items"]]
assert name in names
- types = {s["Type"] for s in listed["Items"]}
- assert types == {"custom"}
+ by_id = {s["CachePolicy"]["Id"]: s for s in listed["Items"]}
+ assert by_id[pid]["Type"] == "custom"
+ # ListCachePolicies with no Type filter also returns the AWS-managed
+ # catalog (Type=managed) alongside custom policies.
+ assert "managed" in {s["Type"] for s in listed["Items"]}
cloudfront.delete_cache_policy(Id=pid, IfMatch=create["ETag"])
@@ -1734,7 +1979,11 @@ def test_cloudfront_list_origin_request_policies_round_trip(cloudfront):
assert listed["Quantity"] == baseline + 1
names = [s["OriginRequestPolicy"]["OriginRequestPolicyConfig"]["Name"] for s in listed["Items"]]
assert name in names
- assert {s["Type"] for s in listed["Items"]} == {"custom"}
+ by_id = {s["OriginRequestPolicy"]["Id"]: s for s in listed["Items"]}
+ assert by_id[pid]["Type"] == "custom"
+ # ListOriginRequestPolicies with no Type filter also returns the
+ # AWS-managed catalog (Type=managed) alongside custom policies.
+ assert "managed" in {s["Type"] for s in listed["Items"]}
cloudfront.delete_origin_request_policy(Id=pid, IfMatch=create["ETag"])
@@ -1750,7 +1999,11 @@ def test_cloudfront_list_response_headers_policies_round_trip(cloudfront):
assert listed["Quantity"] == baseline + 1
names = [s["ResponseHeadersPolicy"]["ResponseHeadersPolicyConfig"]["Name"] for s in listed["Items"]]
assert name in names
- assert {s["Type"] for s in listed["Items"]} == {"custom"}
+ by_id = {s["ResponseHeadersPolicy"]["Id"]: s for s in listed["Items"]}
+ assert by_id[pid]["Type"] == "custom"
+ # ListResponseHeadersPolicies with no Type filter also returns the
+ # AWS-managed catalog (Type=managed) alongside custom policies.
+ assert "managed" in {s["Type"] for s in listed["Items"]}
cloudfront.delete_response_headers_policy(Id=pid, IfMatch=create["ETag"])
diff --git a/tests/test_cloudfront_dataplane.py b/tests/test_cloudfront_dataplane.py
new file mode 100644
index 00000000..7400bc22
--- /dev/null
+++ b/tests/test_cloudfront_dataplane.py
@@ -0,0 +1,1374 @@
+# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
+# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
+"""
+CloudFront data plane tests — a distribution actually serving traffic.
+
+A CloudFront distribution's DomainName is always AWS-shaped
+(``<label>.cloudfront.net``); routing matches that host, or the resolvable
+local form built on MINISTACK_HOST (``<label>.cloudfront.localhost``), which
+this file uses to reach a distribution over the real wire.
+
+One distribution (``cf_stack``) is built once per module and exercised by
+most tests; it fronts a custom API Gateway HTTP API backed by a Lambda that
+echoes the inbound path, headers, query string, and cookies, with several
+cache behaviors covering the request-forwarding, protocol-policy, allowed-
+methods, and origin-path/custom-header surfaces. A handful of tests need
+their own distribution (S3 origin, a raw path-echo origin, a dead origin).
+"""
+
+import asyncio
+import http.client
+import http.server
+import io
+import json
+import socket
+import ssl
+import threading
+import uuid as _uuid_mod
+import zipfile
+from unittest import mock
+
+import pytest
+from conftest import GATEWAY_PORT
+
+from ministack.core import cloudfront_js
+from ministack.services import cloudfront_dataplane
+
+# Managed policy ids (CloudFront Developer Guide, "Managed origin request
+# policies" / "Managed cache policies" / "Managed response headers
+# policies").
+_ALL_VIEWER = "216adef6-5c7f-47e4-b989-5492eafa07d3"
+_ALL_VIEWER_EXCEPT_HOST = "b689b0a8-53d0-40ab-baf2-68738e2966ac"
+_CACHING_DISABLED = "4135ea2d-6df8-44a3-9df3-4b5a84be39ad"
+_MANAGED_CORS_WITH_PREFLIGHT = "5cc3b908-e619-4b99-88e5-2cf7f45965bd"
+
+_ORIGIN_LAMBDA_CODE = (
+ b"import json\n"
+ b"def handler(event, context):\n"
+ b" path = event.get('rawPath', '')\n"
+ b" headers = event.get('headers', {})\n"
+ b" # MiniStack's apigatewayv2 (payload format 2.0) does not split an\n"
+ b" # incoming Cookie header into the top-level `cookies` array real AWS\n"
+ b" # uses; it stays in `headers['cookie']` like any other header, which\n"
+ b" # is also what cloudfront_dataplane.py actually forwards.\n"
+ b" cookie_header = headers.get('cookie', '')\n"
+ b" cookies = [c.strip() for c in cookie_header.split(';') if c.strip()]\n"
+ b" resp = {\n"
+ b" 'statusCode': 200,\n"
+ b" 'headers': {'Content-Type': 'application/json', 'X-Origin-Header': 'origin-value'},\n"
+ b" 'body': json.dumps({\n"
+ b" 'path': path,\n"
+ b" 'headers': headers,\n"
+ b" 'query': event.get('queryStringParameters') or {},\n"
+ b" 'cookies': cookies,\n"
+ b" }),\n"
+ b" }\n"
+ b" if path == '/cookies':\n"
+ b" resp['cookies'] = ['resp1=a; Path=/', 'resp2=b; Path=/']\n"
+ b" if path == '/status/500':\n"
+ b" resp['statusCode'] = 500\n"
+ b" return resp\n"
+)
+
+
+def _zip_lambda(code: bytes) -> bytes:
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w") as zf:
+ zf.writestr("index.py", code)
+ return buf.getvalue()
+
+
+def _none_cache_policy_config(name: str) -> dict:
+ return {
+ "Name": name,
+ "MinTTL": 0,
+ "ParametersInCacheKeyAndForwardedToOrigin": {
+ "HeadersConfig": {"HeaderBehavior": "none"},
+ "CookiesConfig": {"CookieBehavior": "none"},
+ "QueryStringsConfig": {"QueryStringBehavior": "none"},
+ "EnableAcceptEncodingGzip": False,
+ },
+ }
+
+
+def _response_headers_policy(cloudfront, name: str, custom_headers: list) -> str:
+ resp = cloudfront.create_response_headers_policy(
+ ResponseHeadersPolicyConfig={
+ "Name": name,
+ "CustomHeadersConfig": {
+ "Quantity": len(custom_headers),
+ "Items": [{"Header": h, "Value": v, "Override": o} for (h, v, o) in custom_headers],
+ },
+ }
+ )
+ return resp["ResponseHeadersPolicy"]["Id"]
+
+
+def _create_function(cloudfront, name: str, code: bytes, runtime: str = "cloudfront-js-2.0") -> str:
+ created = cloudfront.create_function(
+ Name=name, FunctionConfig={"Comment": name, "Runtime": runtime}, FunctionCode=code,
+ )
+ published = cloudfront.publish_function(Name=name, IfMatch=created["ETag"])
+ return published["FunctionSummary"]["FunctionMetadata"]["FunctionARN"]
+
+
[email protected](scope="module")
+def cf_backend(apigw, lam):
+ """A custom API Gateway HTTP API, backed by a Lambda that echoes the
+ inbound request, addressed like a real CloudFront custom origin."""
+ suffix = _uuid_mod.uuid4().hex[:10]
+ lam.create_function(
+ FunctionName=f"cf-dp-origin-{suffix}",
+ Runtime="python3.12",
+ Role="arn:aws:iam::000000000000:role/test-role",
+ Handler="index.handler",
+ Code={"ZipFile": _zip_lambda(_ORIGIN_LAMBDA_CODE)},
+ )
+ api_id = apigw.create_api(Name=f"cf-dp-api-{suffix}", ProtocolType="HTTP")["ApiId"]
+ int_id = apigw.create_integration(
+ ApiId=api_id, IntegrationType="AWS_PROXY",
+ IntegrationUri=f"arn:aws:lambda:us-east-1:000000000000:function:cf-dp-origin-{suffix}",
+ PayloadFormatVersion="2.0",
+ )["IntegrationId"]
+ apigw.create_route(ApiId=api_id, RouteKey="ANY /{proxy+}", Target=f"integrations/{int_id}")
+ apigw.create_stage(ApiId=api_id, StageName="$default")
+ # "*.execute-api.localhost" resolves to 127.0.0.1 (RFC 6761); the origin
+ # is dialed there at the gateway's own listening port, exactly as any
+ # custom origin is dialed at its configured DomainName/HTTPPort.
+ return {"origin_domain": f"{api_id}.execute-api.localhost"}
+
+
[email protected](scope="module")
+def cf_stack(cloudfront, cf_backend):
+ """One distribution wired up per the module docstring."""
+ suffix = _uuid_mod.uuid4().hex[:10]
+ origin_domain = cf_backend["origin_domain"]
+
+ cp_none_id = cloudfront.create_cache_policy(
+ CachePolicyConfig=_none_cache_policy_config(f"cf-dp-cp-none-{suffix}")
+ )["CachePolicy"]["Id"]
+
+ default_policy_id = _response_headers_policy(
+ cloudfront, f"cf-dp-default-policy-{suffix}",
+ [("X-Origin-Header", "policy-value", True), ("X-Default-Marker", "yes", False)],
+ )
+ csv_policy_id = _response_headers_policy(
+ cloudfront, f"cf-dp-csv-policy-{suffix}", [("X-Csv-Marker", "yes", False)],
+ )
+
+ request_fn_arn = _create_function(
+ cloudfront, f"cf-dp-viewer-request-{suffix}",
+ b"function handler(event) {\n"
+ b" var request = event.request;\n"
+ b" if (request.uri === '/fn/blocked') {\n"
+ b" return { statusCode: 403, statusDescription: 'Forbidden',\n"
+ b" headers: { 'content-type': { value: 'text/plain' } },\n"
+ b" body: { encoding: 'text', data: 'blocked by viewer-request' } };\n"
+ b" }\n"
+ b" request.uri = request.uri.replace('/fn/', '/');\n"
+ b" return request;\n"
+ b"}\n",
+ )
+ response_fn_arn = _create_function(
+ cloudfront, f"cf-dp-viewer-response-{suffix}",
+ b"function handler(event) {\n"
+ b" var response = event.response;\n"
+ b" response.headers['x-viewer-response-added'] = { value: 'yes' };\n"
+ b" return response;\n"
+ b"}\n",
+ )
+ async_fn_arn = _create_function(
+ cloudfront, f"cf-dp-async-{suffix}",
+ b"async function handler(event) {\n"
+ b" var v = await Promise.resolve('yes');\n"
+ b" var request = event.request;\n"
+ b" request.headers['x-async-ran'] = { value: v };\n"
+ b" request.uri = request.uri.replace('/async/', '/');\n"
+ b" return request;\n"
+ b"}\n",
+ )
+ error_fn_arn = _create_function(
+ cloudfront, f"cf-dp-error-{suffix}",
+ b"function handler(event) { throw new Error('boom'); }\n",
+ )
+ response_delete_multi_fn_arn = _create_function(
+ cloudfront, f"cf-dp-response-delete-multi-{suffix}",
+ b"function handler(event) {\n"
+ b" var response = event.response;\n"
+ b" delete response.headers['x-origin-header'];\n"
+ b" response.headers['x-multi-resp'] = { value: 'a', multiValue: [{ value: 'a' }, { value: 'b' }] };\n"
+ b" return response;\n"
+ b"}\n",
+ )
+ viewer_ip_fn_arn = _create_function(
+ cloudfront, f"cf-dp-viewer-ip-{suffix}",
+ b"function handler(event) {\n"
+ b" var request = event.request;\n"
+ b" request.headers['x-viewer-ip'] = { value: event.viewer.ip };\n"
+ b" return request;\n"
+ b"}\n",
+ )
+
+ def behavior(path_pattern, target_origin_id="origin-apigw", **overrides):
+ b = {
+ "PathPattern": path_pattern,
+ "TargetOriginId": target_origin_id,
+ "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": cp_none_id,
+ "OriginRequestPolicyId": _ALL_VIEWER_EXCEPT_HOST,
+ "ResponseHeadersPolicyId": default_policy_id,
+ }
+ b.update(overrides)
+ return b
+
+ dist_config = {
+ "CallerReference": f"cf-dp-{suffix}",
+ "Comment": "cloudfront_dataplane test distribution",
+ "Enabled": True,
+ "DefaultRootObject": "index.html",
+ "Origins": {
+ "Quantity": 2,
+ "Items": [
+ {
+ "Id": "origin-apigw",
+ "DomainName": origin_domain,
+ "CustomOriginConfig": {
+ "HTTPPort": int(GATEWAY_PORT), "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ },
+ {
+ "Id": "origin-apigw-prefixed",
+ "DomainName": origin_domain,
+ "OriginPath": "/extra",
+ "CustomHeaders": {
+ "Quantity": 1,
+ "Items": [{"HeaderName": "X-Added-By-Origin", "HeaderValue": "yes"}],
+ },
+ "CustomOriginConfig": {
+ "HTTPPort": int(GATEWAY_PORT), "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ },
+ ],
+ },
+ "DefaultCacheBehavior": {
+ k: v for k, v in behavior("*").items() if k != "PathPattern"
+ },
+ "CacheBehaviors": {
+ "Quantity": 8,
+ "Items": [
+ {**behavior("*.csv"), "ResponseHeadersPolicyId": csv_policy_id},
+ {
+ **behavior("/fn/*"),
+ "FunctionAssociations": {
+ "Quantity": 2,
+ "Items": [
+ {"FunctionARN": request_fn_arn, "EventType": "viewer-request"},
+ {"FunctionARN": response_fn_arn, "EventType": "viewer-response"},
+ ],
+ },
+ },
+ {
+ **behavior("/async/*"),
+ "FunctionAssociations": {
+ "Quantity": 1,
+ "Items": [{"FunctionARN": async_fn_arn, "EventType": "viewer-request"}],
+ },
+ },
+ {
+ **behavior("/err/*"),
+ "FunctionAssociations": {
+ "Quantity": 1,
+ "Items": [{"FunctionARN": error_fn_arn, "EventType": "viewer-request"}],
+ },
+ },
+ {**behavior("/cd/*"), "CachePolicyId": _CACHING_DISABLED, "OriginRequestPolicyId": ""},
+ {
+ **{k: v for k, v in behavior("/legacy/*").items()
+ if k not in ("CachePolicyId", "OriginRequestPolicyId")},
+ "MinTTL": 0, "DefaultTTL": 0, "MaxTTL": 0,
+ "ForwardedValues": {
+ "QueryString": True,
+ "Cookies": {"Forward": "whitelist", "WhitelistedNames": {"Quantity": 1, "Items": ["keep"]}},
+ "Headers": {"Quantity": 1, "Items": ["X-Legacy"]},
+ },
+ },
+ {**behavior("/restricted/*"),
+ "AllowedMethods": {"Quantity": 2, "Items": ["GET", "HEAD"]}},
+ # No leading "/" — CloudFront treats it the same as "/api/*".
+ behavior("api/*"),
+ {
+ **behavior("/cors/*"), "ResponseHeadersPolicyId": _MANAGED_CORS_WITH_PREFLIGHT,
+ "AllowedMethods": {"Quantity": 3, "Items": ["GET", "HEAD", "OPTIONS"]},
+ },
+ {
+ **behavior("/respmulti/*"),
+ "FunctionAssociations": {
+ "Quantity": 1,
+ "Items": [{"FunctionARN": response_delete_multi_fn_arn, "EventType": "viewer-response"}],
+ },
+ },
+ {
+ **behavior("/viewerip/*"),
+ "FunctionAssociations": {
+ "Quantity": 1,
+ "Items": [{"FunctionARN": viewer_ip_fn_arn, "EventType": "viewer-request"}],
+ },
+ },
+ ],
+ },
+ }
+ # "/cd/*" carries no OriginRequestPolicyId at all; the empty-string
+ # placeholder above exists only so `behavior()`'s dict-merge has a key to
+ # drop — CloudFront's own XML omits the element entirely for "no ORP".
+ for item in dist_config["CacheBehaviors"]["Items"]:
+ if item.get("OriginRequestPolicyId") == "":
+ del item["OriginRequestPolicyId"]
+ dist_config["CacheBehaviors"]["Items"].append(
+ {**behavior("/redirect/*"), "ViewerProtocolPolicy": "redirect-to-https"}
+ )
+ dist_config["CacheBehaviors"]["Items"].append(
+ {**behavior("/prefixed/*", target_origin_id="origin-apigw-prefixed")}
+ )
+ dist_config["CacheBehaviors"]["Quantity"] = len(dist_config["CacheBehaviors"]["Items"])
+
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ return {"dist_host": dist["DomainName"], "origin_domain": origin_domain}
+
+
+def _get(host: str, path: str, method: str = "GET", extra_headers: dict | None = None):
+ """``host`` is a distribution's real AWS-shaped DomainName
+ (``<label>.cloudfront.net``), sent verbatim as the Host header — exactly
+ what a real viewer sends. The connection itself targets the gateway's own
+ address directly, since ``*.cloudfront.net`` doesn't resolve locally (the
+ same way ``_raw_get`` below reaches an execute-api/ALB host)."""
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ headers = {"Host": f"{host}:{GATEWAY_PORT}"}
+ headers.update(extra_headers or {})
+ conn.request(method, path, headers=headers)
+ resp = conn.getresponse()
+ return resp.status, dict(resp.getheaders()), resp.read()
+ finally:
+ conn.close()
+
+
+def _skip_without_node():
+ if not cloudfront_js.available():
+ pytest.skip("node is not available")
+
+
+# ---------------------------------------------------------------------------
+# Routing
+# ---------------------------------------------------------------------------
+
+
+def test_routes_by_distribution_domain(cf_stack):
+ status, _headers, body = _get(cf_stack["dist_host"], "/hello")
+ assert status == 200
+ assert json.loads(body)["path"] == "/hello"
+
+
+def test_path_pattern_without_leading_slash_matches_as_if_it_had_one(cf_stack):
+ # PathPattern "api/*" (no leading /) — CloudFront Developer Guide,
+ # "Path pattern": "CloudFront treats the path the same with or without
+ # the leading /".
+ status, _headers, body = _get(cf_stack["dist_host"], "/api/hello")
+ assert status == 200
+ assert json.loads(body)["path"] == "/api/hello"
+
+
+def test_unknown_distribution_label_is_not_served_by_cloudfront():
+ # Not a 200, and not CloudFront's own data plane (no X-Cache): falls
+ # through to whatever the gateway's generic router answers for an
+ # otherwise-unrouted host — CloudFront's own control-plane 404, since no
+ # other special-case host pattern matches either.
+ status, headers, body = _get("d0000000000000.cloudfront.localhost", "/anything")
+ assert "X-Cache" not in headers
+ assert status == 404
+ assert b"<Code>NoSuchResource</Code>" in body
+
+
+# ---------------------------------------------------------------------------
+# CloudFront Functions
+# ---------------------------------------------------------------------------
+
+
+def test_viewer_request_function_rewrites_uri(cf_stack):
+ _skip_without_node()
+ status, _headers, body = _get(cf_stack["dist_host"], "/fn/hello")
+ assert status == 200
+ assert json.loads(body)["path"] == "/hello"
+
+
+def test_function_generated_response_skips_the_origin(cf_stack):
+ _skip_without_node()
+ status, headers, body = _get(cf_stack["dist_host"], "/fn/blocked")
+ assert status == 403
+ assert body == b"blocked by viewer-request"
+ assert headers.get("X-Cache") == "FunctionGeneratedResponse from cloudfront"
+ assert "X-Origin-Header" not in headers # never reached the origin
+
+
+def test_viewer_response_function_adds_a_header(cf_stack):
+ _skip_without_node()
+ status, headers, _body = _get(cf_stack["dist_host"], "/fn/hello")
+ assert status == 200
+ assert headers.get("X-Viewer-Response-Added") == "yes"
+
+
+def test_async_2_0_handler_runs_and_awaits(cf_stack):
+ _skip_without_node()
+ status, _headers, body = _get(cf_stack["dist_host"], "/async/hello")
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["path"] == "/hello"
+ assert payload["headers"].get("x-async-ran") == "yes"
+
+
+def test_function_error_answers_503(cf_stack):
+ _skip_without_node()
+ status, headers, _body = _get(cf_stack["dist_host"], "/err/hello")
+ assert status == 503
+ assert headers.get("X-Cache") == "Error from cloudfront"
+
+
+def test_viewer_response_function_skipped_when_origin_status_is_400_or_above(cf_stack):
+ # functions-event-structure.html, "Status code and body" note: CloudFront
+ # does not invoke a viewer-response function when the origin answers 400+.
+ _skip_without_node()
+ status, headers, _body = _get(cf_stack["dist_host"], "/fn/status/500")
+ assert status == 500
+ assert "X-Viewer-Response-Added" not in headers
+
+
+def test_viewer_response_function_deletes_header_and_expands_multivalue(cf_stack):
+ # Replace, not merge (a deleted header stays gone), and a multiValue
+ # response header becomes repeated Set-Cookie-style lines.
+ _skip_without_node()
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", "/respmulti/hello", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_stack['dist_host']}:{GATEWAY_PORT}")
+ conn.endheaders()
+ resp = conn.getresponse()
+ resp.read()
+ multi_values = resp.msg.get_all("X-Multi-Resp") or []
+ has_origin_header = "X-Origin-Header" in resp.msg
+ finally:
+ conn.close()
+ assert sorted(multi_values) == ["a", "b"]
+ assert not has_origin_header
+
+
+def test_function_event_viewer_ip_is_the_tcp_peer_not_the_xff_header(cf_stack):
+ # functions-event-structure.html: viewer.ip is the TCP peer, regardless
+ # of what X-Forwarded-For the viewer sent.
+ _skip_without_node()
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/viewerip/hello", extra_headers={"X-Forwarded-For": "203.0.113.9"},
+ )
+ assert status == 200
+ assert json.loads(body)["headers"].get("x-viewer-ip") == "127.0.0.1"
+
+
+# ---------------------------------------------------------------------------
+# Cache behaviors and response-headers policy
+# ---------------------------------------------------------------------------
+
+
+def test_ordered_behavior_matches_csv_glob_with_its_own_policy(cf_stack):
+ status, headers, _body = _get(cf_stack["dist_host"], "/report.csv")
+ assert status == 200
+ assert headers.get("X-Csv-Marker") == "yes"
+ assert "X-Default-Marker" not in headers
+
+
+def test_response_headers_policy_overrides_origin_header_and_adds_marker(cf_stack):
+ # The origin always emits X-Origin-Header: origin-value; the default
+ # behavior's response-headers policy names the same header with
+ # Override=True, and adds X-Default-Marker unconditionally.
+ status, headers, _body = _get(cf_stack["dist_host"], "/hello")
+ assert status == 200
+ assert headers.get("X-Origin-Header") == "policy-value"
+ assert headers.get("X-Default-Marker") == "yes"
+
+
+def test_cors_headers_only_applied_when_origin_header_present(cf_stack):
+ status, headers, _body = _get(cf_stack["dist_host"], "/cors/hello")
+ assert status == 200
+ assert "Access-Control-Allow-Origin" not in headers
+
+ status, headers, _body = _get(
+ cf_stack["dist_host"], "/cors/hello", extra_headers={"Origin": "https://example.com"},
+ )
+ assert status == 200
+ assert headers.get("Access-Control-Allow-Origin") == "*"
+ assert headers.get("Access-Control-Expose-Headers") == "*"
+ # Preflight-only headers (Allow-Methods/-Headers/-Max-Age) never appear
+ # on a plain GET.
+ assert "Access-Control-Allow-Methods" not in headers
+
+
+def test_cors_preflight_headers_only_on_options(cf_stack):
+ # The response-headers policy applies to whatever status the request
+ # produces (here, the backing API Gateway HTTP API's own unconfigured-
+ # CORS OPTIONS rejection — orthogonal to the response-headers policy
+ # under test), so only the header is asserted, not the status.
+ _status, headers, _body = _get(
+ cf_stack["dist_host"], "/cors/hello", method="OPTIONS",
+ extra_headers={"Origin": "https://example.com"},
+ )
+ assert headers.get("Access-Control-Allow-Methods") == "DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT"
+
+
+def test_cloudfront_added_headers_present(cf_stack):
+ status, headers, _body = _get(cf_stack["dist_host"], "/hello")
+ assert status == 200
+ assert headers.get("X-Cache") == "Miss from cloudfront"
+ assert headers.get("Via", "").endswith(".cloudfront.net (CloudFront)")
+ assert "X-Amz-Cf-Id" in headers
+ assert "X-Amz-Cf-Pop" in headers
+
+
+# ---------------------------------------------------------------------------
+# Cache-policy / origin-request-policy forwarding
+# ---------------------------------------------------------------------------
+
+
+def test_all_viewer_except_host_header_forwards_viewer_header_not_host(cf_stack):
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/hello", extra_headers={"X-Custom-Test": "present"},
+ )
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["headers"].get("x-custom-test") == "present"
+ assert payload["headers"].get("host") == cf_stack["origin_domain"]
+
+
+def test_all_viewer_forwards_the_distributions_own_host(cf_raw_origin_stack):
+ # AllViewer forwards every viewer header including Host, so the origin
+ # sees the *distribution's* Host, not a default derived from its own
+ # DomainName. A real network-distinct origin doesn't care; this uses the
+ # raw http.server origin (not the execute-api one) because MiniStack's
+ # own execute-api addressing is itself Host-routed, so forwarding the
+ # viewer's Host there would recurse into the gateway's own routing
+ # instead of reaching the intended origin — an artifact of this
+ # emulator's single-port addressing, not of CloudFront's own behavior.
+ status, _headers, body = _get(
+ cf_raw_origin_stack, "/av/hello", extra_headers={"X-Custom-Test": "present"},
+ )
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["headers"].get("x-custom-test") == "present"
+ assert payload["headers"].get("host") == f"{cf_raw_origin_stack}:{GATEWAY_PORT}"
+
+
+def test_caching_disabled_with_no_origin_request_policy_forwards_only_defaults(cf_stack):
+ # CachingDisabled's ParametersInCacheKeyAndForwardedToOrigin is
+ # none/none/none, and the behavior carries no origin request policy at
+ # all: nothing beyond the always-included defaults (Host, User-Agent)
+ # reaches the origin.
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/cd/hello", extra_headers={"X-Custom-Test": "present"},
+ )
+ assert status == 200
+ payload = json.loads(body)
+ assert "x-custom-test" not in payload["headers"]
+ assert payload["headers"].get("host") == cf_stack["origin_domain"]
+
+
+def test_x_forwarded_for_always_appends_regardless_of_policy(cf_stack):
+ # CloudFront Developer Guide, "Client IP addresses": X-Forwarded-For is
+ # always the viewer's own chain plus the client IP, even under
+ # CachingDisabled with no origin request policy forwarding it.
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/cd/hello", extra_headers={"X-Forwarded-For": "203.0.113.9"},
+ )
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["headers"].get("x-forwarded-for") == "203.0.113.9, 127.0.0.1"
+
+
+def test_user_agent_defaults_to_amazon_cloudfront_when_not_forwarded(cf_stack):
+ # CloudFront Developer Guide, "User-Agent header": replaced unconditionally
+ # unless the forwarding policy carries the viewer's own.
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/cd/hello", extra_headers={"User-Agent": "custom-client/1.0"},
+ )
+ assert status == 200
+ assert json.loads(body)["headers"].get("user-agent") == "Amazon CloudFront"
+
+
+def test_legacy_forwarded_values_forwards_only_its_own_selections(cf_stack):
+ status, _headers, body = _get(
+ cf_stack["dist_host"], "/legacy/hello?a=1&b=2",
+ extra_headers={"X-Legacy": "yes", "X-Not-Forwarded": "no", "Cookie": "keep=1; drop=2"},
+ )
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["query"] == {"a": "1", "b": "2"} # ForwardedValues.QueryString=true
+ assert payload["headers"].get("x-legacy") == "yes"
+ assert "x-not-forwarded" not in payload["headers"]
+ assert payload["cookies"] == ["keep=1"] # only the whitelisted cookie name
+
+
+# ---------------------------------------------------------------------------
+# _combine_forward — the cache-policy / origin-request-policy combinator
+# (same function serves headers, cookies and query strings). Exercised
+# end-to-end above for "all"/"none"; "whitelist" and "allExcept" are cheaper
+# to prove directly against the pure combinator than via more distributions.
+# ---------------------------------------------------------------------------
+
+
+ "cache_kind_set,orp_kind_set,name,expected",
+ [
+ (("whitelist", {"a"}), None, "a", True),
+ (("whitelist", {"a"}), None, "b", False),
+ (("whitelist", {"a"}), ("whitelist", {"b"}), "b", True), # ORP allow-list adds to cache's
+ (("allExcept", {"a"}), None, "a", False), # blocked by the cache policy
+ (("allExcept", {"a"}), None, "b", True),
+ (("allExcept", {"a"}), ("allExcept", {"b"}), "a", False), # ORP allExcept doesn't rescue it
+ (("allExcept", {"a"}), ("whitelist", {"a"}), "a", True), # ORP allow-list rescues a blocked name
+ (("none", None), ("allExcept", {"a"}), "a", False),
+ (("none", None), ("allExcept", {"a"}), "b", True),
+ (("none", None), None, "a", False),
+ ],
+)
+def test_combine_forward_whitelist_and_allexcept_branches(cache_kind_set, orp_kind_set, name, expected):
+ assert cloudfront_dataplane._combine_forward(cache_kind_set, orp_kind_set, name) == expected
+
+
+# ---------------------------------------------------------------------------
+# Behavior-level settings
+# ---------------------------------------------------------------------------
+
+
+def test_redirect_to_https_sends_301_with_https_location(cf_stack):
+ status, headers, _body = _get(cf_stack["dist_host"], "/redirect/hello")
+ assert status == 301
+ assert headers.get("Location") == f"https://{cf_stack['dist_host']}:{GATEWAY_PORT}/redirect/hello"
+
+
+def test_allowed_methods_rejects_a_disallowed_method(cf_stack):
+ status, _headers, _body = _get(cf_stack["dist_host"], "/restricted/hello", method="POST")
+ assert status == 403
+
+
+def test_default_root_object_is_requested_for_the_root_url(cf_stack):
+ status, _headers, body = _get(cf_stack["dist_host"], "/")
+ assert status == 200
+ assert json.loads(body)["path"] == "/index.html"
+
+
+def test_origin_path_and_custom_headers_reach_the_origin(cf_stack):
+ status, _headers, body = _get(cf_stack["dist_host"], "/prefixed/hello")
+ assert status == 200
+ payload = json.loads(body)
+ assert payload["path"] == "/extra/prefixed/hello"
+ assert payload["headers"].get("x-added-by-origin") == "yes"
+
+
+def test_multiple_set_cookie_headers_all_carried_through(cf_stack):
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", "/cookies", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_stack['dist_host']}:{GATEWAY_PORT}")
+ conn.endheaders()
+ resp = conn.getresponse()
+ resp.read()
+ cookies = resp.msg.get_all("Set-Cookie") or []
+ finally:
+ conn.close()
+ assert sorted(cookies) == ["resp1=a; Path=/", "resp2=b; Path=/"]
+
+
+# ---------------------------------------------------------------------------
+# AWS-owned origin hosts — refused before a socket opens; no server or
+# distribution needed, calls the pure function. An emulator must never open
+# a socket to a real AWS host.
+# ---------------------------------------------------------------------------
+
+
+def test_aws_owned_origin_host_refused_before_any_socket():
+ origin = {
+ "domain_name": "abcd1234.execute-api.us-east-1.amazonaws.com.", # trailing dot
+ "origin_path": "", "custom_headers": [], "s3_bucket": None,
+ "http_port": 80, "https_port": 443, "protocol_policy": "https-only",
+ }
+ with mock.patch("http.client.HTTPSConnection") as https_conn, \
+ mock.patch("http.client.HTTPConnection") as http_conn:
+ with pytest.raises(cloudfront_dataplane.OriginUnreachable):
+ cloudfront_dataplane._forward_to_origin(
+ origin, "GET", "/anything", "", {}, b"", viewer_is_https=True,
+ )
+ https_conn.assert_not_called()
+ http_conn.assert_not_called()
+
+
+def test_origin_ssl_context_trusts_gateway_cert_under_use_ssl(tmp_path):
+ # Precedent: lambda_svc.py's TLS trust for Lambda containers — system
+ # roots plus the gateway's own cert, not a replacement of the store.
+ from ministack.core.x509_utils import generate_ca
+
+ ca_pem, _ca_key_pem = generate_ca(common_name="cf-dp-origin-ssl-test")
+ cert_path = tmp_path / "ca.pem"
+ cert_path.write_text(ca_pem)
+ with mock.patch("ministack.core.tls.use_ssl_enabled", return_value=True), \
+ mock.patch("ministack.core.tls.resolve_tls_material", return_value=(str(cert_path), "key")):
+ ctx = cloudfront_dataplane._origin_ssl_context()
+ assert ctx.verify_flags & ssl.VERIFY_X509_PARTIAL_CHAIN
+ subjects = [dict(rdn for pair in cert["subject"] for rdn in pair) for cert in ctx.get_ca_certs()]
+ assert any(s.get("commonName") == "cf-dp-origin-ssl-test" for s in subjects)
+
+
+def test_stacked_distribution_via_header_answers_403(cloudfront):
+ # CloudFront Developer Guide, http-403-permission-denied.html, "Stacked
+ # distributions cause a 403 error": an origin pointed back at this same
+ # distribution (via the resolvable `<label>.cloudfront.localhost` form)
+ # must answer 403 on the second hop rather than recurse into a hang.
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dist_config = {
+ "CallerReference": f"cf-dp-stacked-{suffix}",
+ "Comment": "cloudfront_dataplane stacked-distribution test",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "self-origin",
+ "DomainName": "placeholder.cloudfront.localhost",
+ "CustomOriginConfig": {
+ "HTTPPort": int(GATEWAY_PORT), "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "self-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ }
+ created = cloudfront.create_distribution(DistributionConfig=dist_config)
+ dist = created["Distribution"]
+ label = dist["DomainName"].removesuffix(".cloudfront.net")
+ dist_config["Origins"]["Items"][0]["DomainName"] = f"{label}.cloudfront.localhost"
+ cloudfront.update_distribution(DistributionConfig=dist_config, Id=dist["Id"], IfMatch=created["ETag"])
+
+ status, _headers, _body = _get(dist["DomainName"], "/anything")
+ assert status == 403
+
+
+def test_unreachable_origin_answers_502(cloudfront):
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dist_config = {
+ "CallerReference": f"cf-dp-dead-{suffix}",
+ "Comment": "cloudfront_dataplane dead-origin test",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "dead-origin",
+ "DomainName": "127.0.0.1",
+ "CustomOriginConfig": {
+ "HTTPPort": 1, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only", # nothing listens on port 1
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "dead-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ status, headers, _body = _get(dist["DomainName"], "/anything")
+ assert status == 502
+ assert headers.get("X-Cache") == "Error from cloudfront"
+
+
+def test_forward_to_origin_timeout_raises_origin_timeout():
+ # CloudFront Developer Guide, http-504-gateway-timeout.html: "The origin
+ # didn't respond before the request expired." OriginReadTimeout default
+ # 30s (DownloadDistValuesOrigin.md, "Response timeout").
+ origin = {
+ "domain_name": "127.0.0.1", "origin_path": "", "custom_headers": [], "s3_bucket": None,
+ "http_port": 1, "https_port": 443, "protocol_policy": "http-only", "read_timeout": 30,
+ }
+ with mock.patch("http.client.HTTPConnection") as mock_conn_cls:
+ mock_conn_cls.return_value.request.side_effect = TimeoutError("timed out")
+ with pytest.raises(cloudfront_dataplane.OriginTimeout):
+ cloudfront_dataplane._forward_to_origin(
+ origin, "GET", "/anything", "", {}, b"", viewer_is_https=False,
+ )
+
+
+def test_handle_request_maps_origin_timeout_to_504():
+ parsed = {
+ "default_root_object": "",
+ "origins": {"o1": {
+ "domain_name": "127.0.0.1", "origin_path": "", "custom_headers": [],
+ "s3_bucket": None, "http_port": 1, "https_port": 443,
+ "protocol_policy": "http-only", "read_timeout": 30,
+ }},
+ "default_behavior": {
+ "path_pattern": None, "target_origin_id": "o1", "viewer_protocol_policy": "allow-all",
+ "allowed_methods": None, "cache_policy_id": _CACHING_DISABLED,
+ "origin_request_policy_id": None, "response_headers_policy_id": None,
+ "forwarded_values": None, "functions": {},
+ },
+ "ordered_behaviors": [],
+ }
+ dist = {"Id": "EDUMMY00000000", "DomainName": "ddummy0000000.cloudfront.net"}
+ with mock.patch("ministack.services.cloudfront.parse_distribution_dataplane_config", return_value=parsed), \
+ mock.patch("ministack.services.cloudfront_dataplane._forward_to_origin",
+ side_effect=cloudfront_dataplane.OriginTimeout("slow")):
+ status, headers, _body = asyncio.run(
+ cloudfront_dataplane.handle_request(dist, "GET", "/anything", "/anything", "", {}, b"", {})
+ )
+ assert status == 504
+ assert headers.get("X-Cache") == "Error from cloudfront"
+
+
+# ---------------------------------------------------------------------------
+# S3 origin — served from MiniStack's own S3 store in-process.
+# ---------------------------------------------------------------------------
+
+
[email protected](scope="module")
+def cf_s3_stack(cloudfront, s3):
+ suffix = _uuid_mod.uuid4().hex[:10]
+ bucket = f"cf-dp-s3-origin-{suffix}"
+ s3.create_bucket(Bucket=bucket)
+ body = b"hello from the bucket\n"
+ s3.put_object(Bucket=bucket, Key="hello.txt", Body=body, ContentType="text/plain")
+
+ dist_config = {
+ "CallerReference": f"cf-dp-s3-{suffix}",
+ "Comment": "cloudfront_dataplane S3-origin test distribution",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "s3-origin",
+ "DomainName": f"{bucket}.s3.amazonaws.com",
+ "S3OriginConfig": {"OriginAccessIdentity": ""},
+ "OriginAccessControlId": "test-oac-id",
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "s3-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ return {"dist_host": dist["DomainName"], "body": body}
+
+
+def test_s3_origin_serves_bucket_object(cf_s3_stack):
+ status, headers, body = _get(cf_s3_stack["dist_host"], "/hello.txt")
+ assert status == 200
+ assert body == cf_s3_stack["body"]
+ assert headers.get("Content-Type") == "text/plain"
+
+
+def test_s3_origin_range_get_returns_partial_content(cf_s3_stack):
+ # Range is forwarded regardless of cache/origin-request-policy header
+ # configuration (CloudFront Developer Guide, "HTTP request headers and
+ # CloudFront behavior"), even under CachingDisabled with no ORP here.
+ status, headers, body = _get(
+ cf_s3_stack["dist_host"], "/hello.txt", extra_headers={"Range": "bytes=0-4"},
+ )
+ assert status == 206
+ assert body == cf_s3_stack["body"][:5]
+ assert headers.get("Content-Range", "").startswith("bytes 0-4/")
+
+
+def test_s3_origin_missing_key_returns_access_denied(cf_s3_stack):
+ # The OAC bucket policy CloudFront's console generates grants only
+ # s3:GetObject, never s3:ListBucket — without ListBucket, S3 answers a
+ # missing key with 403 AccessDenied rather than 404 NoSuchKey.
+ status, _headers, body = _get(cf_s3_stack["dist_host"], "/does-not-exist.txt")
+ assert status == 403
+ assert b"<Code>AccessDenied</Code>" in body
+
+
+# ---------------------------------------------------------------------------
+# Percent-encoding fidelity (CloudFront Developer Guide, "Restrictions on all
+# edge functions" > "URI, query string, and headers encoding").
+# ---------------------------------------------------------------------------
+
+
+class _PathEchoHandler(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ body = json.dumps({
+ "path": self.path,
+ "headers": {k.lower(): v for k, v in self.headers.items()},
+ }).encode()
+ self.send_response(200)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Content-Length", str(len(body)))
+ if self.path.endswith("/hopbyhop"):
+ self.send_header("Connection", "keep-alive")
+ self.send_header("X-Should-Survive", "yes")
+ if self.path.endswith("/respdup"):
+ self.send_header("X-Resp-Dup", "first")
+ self.send_header("X-Resp-Dup", "second")
+ self.end_headers()
+ self.wfile.write(body)
+
+ def do_POST(self):
+ length = int(self.headers.get("Content-Length", 0) or 0)
+ posted_body = self.rfile.read(length) if length else b""
+ body = json.dumps({
+ "path": self.path,
+ "body": posted_body.decode("utf-8", errors="replace"),
+ "content_length_header": self.headers.get("Content-Length"),
+ }).encode()
+ self.send_response(200)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
+ def log_message(self, format, *args):
+ pass
+
+
[email protected](scope="module")
+def path_echo_origin_port():
+ server = http.server.HTTPServer(("127.0.0.1", 0), _PathEchoHandler)
+ port = server.server_address[1]
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ yield port
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+
+
[email protected](scope="module")
+def cf_raw_origin_stack(cloudfront, path_echo_origin_port):
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dist_config = {
+ "CallerReference": f"cf-dp-raw-{suffix}",
+ "Comment": "cloudfront_dataplane raw-path-fidelity test distribution",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "raw-origin",
+ "DomainName": "127.0.0.1",
+ "CustomOriginConfig": {
+ "HTTPPort": path_echo_origin_port, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "raw-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ "CacheBehaviors": {
+ "Quantity": 1,
+ "Items": [{
+ "PathPattern": "/av/*",
+ "TargetOriginId": "raw-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED, "OriginRequestPolicyId": _ALL_VIEWER,
+ }],
+ },
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ return dist["DomainName"]
+
+
+def test_origin_domain_name_port_overrides_http_port(cloudfront, path_echo_origin_port):
+ """An origin built from a MiniStack endpoint names the gateway port in its DomainName."""
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dist = cloudfront.create_distribution(DistributionConfig={
+ "CallerReference": f"cf-dp-port-{suffix}",
+ "Comment": "origin DomainName with an explicit port",
+ "Enabled": True,
+ "Origins": {"Quantity": 1, "Items": [{
+ "Id": "port-origin",
+ "DomainName": f"127.0.0.1:{path_echo_origin_port}",
+ "CustomOriginConfig": {
+ "HTTPPort": 1, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }]},
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "port-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ })["Distribution"]
+ status, _body = _raw_get(dist["DomainName"], "/port-check")
+ assert status == 200
+
+def _raw_get(dist_host: str, raw_path: str):
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", raw_path, skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{dist_host}:{GATEWAY_PORT}")
+ conn.endheaders()
+ resp = conn.getresponse()
+ return resp.status, resp.read()
+ finally:
+ conn.close()
+
+
+def test_percent_encoded_path_forwarded_byte_exact(cf_raw_origin_stack):
+ for raw_path in (
+ "/echo/%21%24%26%27%28%29%2A%2B%2C%3B%3D%40",
+ "/ready_pin_guests/123%2Ejson",
+ ):
+ status, body = _raw_get(cf_raw_origin_stack, raw_path)
+ assert status == 200
+ assert json.loads(body)["path"] == raw_path
+
+
+def test_query_string_forwarded_byte_exact_when_unmodified_and_fully_allowed(cf_raw_origin_stack):
+ # "/av/*" carries AllViewer (forwards every query-string parameter) and
+ # no function: nothing should re-encode the viewer's own percent-encoding.
+ raw_path = "/av/hello?a=%68%65%6c%6c%6f&b=1"
+ status, body = _raw_get(cf_raw_origin_stack, raw_path)
+ assert status == 200
+ assert json.loads(body)["path"] == raw_path
+
+
+def test_post_body_round_trips_with_content_length(cf_raw_origin_stack):
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ payload = b'{"hello": "world"}'
+ conn.putrequest("POST", "/av/echo", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_raw_origin_stack}:{GATEWAY_PORT}")
+ conn.putheader("Content-Type", "application/json")
+ conn.putheader("Content-Length", str(len(payload)))
+ conn.endheaders(payload)
+ resp = conn.getresponse()
+ body = resp.read()
+ finally:
+ conn.close()
+ echoed = json.loads(body)
+ assert echoed["body"] == payload.decode()
+ assert echoed["content_length_header"] == str(len(payload))
+
+
+def test_hop_by_hop_response_headers_stripped(cf_raw_origin_stack):
+ status, headers, _body = _get(cf_raw_origin_stack, "/av/hopbyhop")
+ assert status == 200
+ assert "Connection" not in headers
+ assert headers.get("X-Should-Survive") == "yes"
+
+
+# ---------------------------------------------------------------------------
+# Raw request-header fidelity toward the origin — a plain http.server or
+# http.client doesn't distinguish "one folded line" from "two lines with the
+# same name", so this reads the exact bytes off the socket.
+# ---------------------------------------------------------------------------
+
+
+class _RawHeaderCaptureServer:
+ """Accepts one connection at a time, records the raw request-header
+ block exactly as received, and answers a trivial 200."""
+
+ def __init__(self):
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ self.sock.bind(("127.0.0.1", 0))
+ self.sock.listen(5)
+ self.port = self.sock.getsockname()[1]
+ self.last_header_lines: list = []
+ self._stop = False
+ self.thread = threading.Thread(target=self._serve, daemon=True)
+ self.thread.start()
+
+ def _serve(self):
+ self.sock.settimeout(0.5)
+ while not self._stop:
+ try:
+ conn, _ = self.sock.accept()
+ except socket.timeout:
+ continue
+ except OSError:
+ break
+ with conn:
+ conn.settimeout(5)
+ data = b""
+ while b"\r\n\r\n" not in data:
+ chunk = conn.recv(4096)
+ if not chunk:
+ break
+ data += chunk
+ head, _, _rest = data.partition(b"\r\n\r\n")
+ lines = head.decode("latin-1").split("\r\n")
+ self.last_header_lines = lines[1:] # drop the request line
+ body = b"ok"
+ conn.sendall(
+ b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "
+ + str(len(body)).encode() + b"\r\n\r\n" + body
+ )
+
+ def stop(self):
+ self._stop = True
+ self.sock.close()
+ self.thread.join(timeout=5)
+
+
[email protected](scope="module")
+def raw_header_server():
+ server = _RawHeaderCaptureServer()
+ try:
+ yield server
+ finally:
+ server.stop()
+
+
[email protected](scope="module")
+def cf_raw_header_stack(cloudfront, raw_header_server):
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dup_edit_fn_arn = None
+ if cloudfront_js.available():
+ # Viewer headers are already comma-joined before this module ever
+ # sees them (app.py, RFC 9110 §5.2), so a function is the only way to
+ # produce a genuine multiValue request header here.
+ dup_edit_fn_arn = _create_function(
+ cloudfront, f"cf-dp-dup-header-edit-{suffix}",
+ b"function handler(event) {\n"
+ b" var request = event.request;\n"
+ b" request.headers['x-dup'] = { value: 'a', multiValue: [{ value: 'a' }, { value: 'b' }] };\n"
+ b" return request;\n"
+ b"}\n",
+ )
+ dist_config = {
+ "CallerReference": f"cf-dp-rawheaders-{suffix}",
+ "Comment": "cloudfront_dataplane raw request-header test distribution",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "raw-header-origin",
+ "DomainName": "127.0.0.1",
+ "CustomOriginConfig": {
+ "HTTPPort": raw_header_server.port, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ # Lowercase, viewer sends Title-Case — proves the overwrite is
+ # case-insensitive, not merely a literal-name match.
+ "CustomHeaders": {"Quantity": 1, "Items": [{"HeaderName": "x-custom-test", "HeaderValue": "from-origin"}]},
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "raw-header-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED, "OriginRequestPolicyId": _ALL_VIEWER,
+ },
+ }
+ if dup_edit_fn_arn:
+ dist_config["CacheBehaviors"] = {
+ "Quantity": 1,
+ "Items": [{
+ "PathPattern": "/dupheader/*",
+ "TargetOriginId": "raw-header-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED, "OriginRequestPolicyId": _ALL_VIEWER,
+ "FunctionAssociations": {
+ "Quantity": 1,
+ "Items": [{"FunctionARN": dup_edit_fn_arn, "EventType": "viewer-request"}],
+ },
+ }],
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ return dist["DomainName"]
+
+
+def test_origin_request_headers_no_duplicates_case_insensitive_overwrite_and_xff(
+ cf_raw_header_stack, raw_header_server,
+):
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", "/anything", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_raw_header_stack}:{GATEWAY_PORT}")
+ conn.putheader("X-Custom-Test", "viewer-value")
+ conn.putheader("X-Forwarded-For", "203.0.113.5")
+ conn.endheaders()
+ resp = conn.getresponse()
+ resp.read()
+ finally:
+ conn.close()
+
+ lines = raw_header_server.last_header_lines
+ names_lower = [line.split(":", 1)[0].lower() for line in lines if line]
+ # AllViewer forwards Host too: exactly one line, not a lowercase
+ # viewer-forwarded one plus a Title-Cased one this module also adds.
+ assert names_lower.count("host") == 1
+
+ custom_test_lines = [line for line in lines if line.lower().startswith("x-custom-test:")]
+ assert len(custom_test_lines) == 1
+ assert custom_test_lines[0].split(":", 1)[1].strip() == "from-origin"
+
+ xff_lines = [line for line in lines if line.lower().startswith("x-forwarded-for:")]
+ assert len(xff_lines) == 1
+ assert xff_lines[0].split(":", 1)[1].strip() == "203.0.113.5, 127.0.0.1"
+
+
+def test_function_set_multivalue_header_forwarded_as_one_comma_joined_line(
+ cf_raw_header_stack, raw_header_server,
+):
+ # RFC 9110 §5.3: a repeated field value is one comma-joined line when
+ # forwarded to the origin, not several lines of the same name.
+ _skip_without_node()
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", "/dupheader/hello", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_raw_header_stack}:{GATEWAY_PORT}")
+ conn.endheaders()
+ resp = conn.getresponse()
+ resp.read()
+ finally:
+ conn.close()
+
+ dup_lines = [line for line in raw_header_server.last_header_lines if line.lower().startswith("x-dup:")]
+ assert len(dup_lines) == 1
+ assert dup_lines[0].split(":", 1)[1].strip() == "a, b"
+
+
[email protected](scope="module")
+def cf_response_dup_stack(cloudfront, path_echo_origin_port):
+ """A distribution whose origin (the raw path-echo server) sends a
+ genuinely repeated response header, for the response-side multiValue
+ edit-only merge rule below."""
+ suffix = _uuid_mod.uuid4().hex[:10]
+ fn_arn = None
+ if cloudfront_js.available():
+ fn_arn = _create_function(
+ cloudfront, f"cf-dp-response-dup-edit-{suffix}",
+ b"function handler(event) {\n"
+ b" var response = event.response;\n"
+ b" if (response.headers['x-resp-dup']) {\n"
+ b" response.headers['x-resp-dup'].value = 'edited';\n"
+ b" }\n"
+ b" return response;\n"
+ b"}\n",
+ )
+ behavior = {
+ "TargetOriginId": "respdup-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ }
+ if fn_arn:
+ behavior["FunctionAssociations"] = {
+ "Quantity": 1, "Items": [{"FunctionARN": fn_arn, "EventType": "viewer-response"}],
+ }
+ dist_config = {
+ "CallerReference": f"cf-dp-respdup-{suffix}",
+ "Comment": "cloudfront_dataplane response multiValue edit-only test",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "respdup-origin",
+ "DomainName": "127.0.0.1",
+ "CustomOriginConfig": {
+ "HTTPPort": path_echo_origin_port, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }],
+ },
+ "DefaultCacheBehavior": behavior,
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ return dist["DomainName"]
+
+
+def test_viewer_response_function_edits_only_value_of_repeated_header_keeps_remaining_values(
+ cf_response_dup_stack,
+):
+ # functions-event-structure.html: a header whose multiValue is unchanged
+ # from the event is read only for its primary value; the rest of its
+ # original values survive.
+ _skip_without_node()
+ conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
+ try:
+ conn.putrequest("GET", "/respdup", skip_host=True, skip_accept_encoding=True)
+ conn.putheader("Host", f"{cf_response_dup_stack}:{GATEWAY_PORT}")
+ conn.endheaders()
+ resp = conn.getresponse()
+ resp.read()
+ dup_values = resp.msg.get_all("X-Resp-Dup") or []
+ finally:
+ conn.close()
+ assert dup_values == ["edited", "second"]
+
+
+# ---------------------------------------------------------------------------
+# TLS to a custom https origin.
+# ---------------------------------------------------------------------------
+
+
+def untrusted_https_origin_port(tmp_path):
+ pytest.importorskip("cryptography")
+ from ministack.core.x509_utils import generate_ca, sign_leaf_certificate
+
+ ca_pem, ca_key_pem = generate_ca(common_name="cf-dp-untrusted-ca")
+ leaf_pem, leaf_key_pem, _public = sign_leaf_certificate(
+ ca_pem, ca_key_pem, common_name="127.0.0.1", san_ips=["127.0.0.1"],
+ )
+ cert_path = tmp_path / "leaf.pem"
+ cert_path.write_text(leaf_pem + leaf_key_pem)
+
+ server = http.server.HTTPServer(("127.0.0.1", 0), _PathEchoHandler)
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+ ctx.load_cert_chain(str(cert_path))
+ server.socket = ctx.wrap_socket(server.socket, server_side=True)
+ port = server.server_address[1]
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ yield port
+ finally:
+ server.shutdown()
+ thread.join(timeout=5)
+ server.server_close()
+
+
+def test_untrusted_origin_certificate_answers_502(cloudfront, untrusted_https_origin_port):
+ # http-502-bad-gateway.html's self-signed/untrusted-chain case: the
+ # origin's own CA isn't in the system trust store (and USE_SSL is off in
+ # this test harness, so the gateway cert isn't trusted either).
+ suffix = _uuid_mod.uuid4().hex[:10]
+ dist_config = {
+ "CallerReference": f"cf-dp-untrusted-{suffix}",
+ "Comment": "cloudfront_dataplane untrusted-origin-cert test",
+ "Enabled": True,
+ "Origins": {
+ "Quantity": 1,
+ "Items": [{
+ "Id": "untrusted-origin",
+ "DomainName": "127.0.0.1",
+ "CustomOriginConfig": {
+ "HTTPPort": 80, "HTTPSPort": untrusted_https_origin_port,
+ "OriginProtocolPolicy": "https-only",
+ "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
+ "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
+ },
+ }],
+ },
+ "DefaultCacheBehavior": {
+ "TargetOriginId": "untrusted-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED,
+ },
+ }
+ dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ status, headers, _body = _get(dist["DomainName"], "/anything")
+ assert status == 502
+ assert headers.get("X-Cache") == "Error from cloudfront"