Skip to content

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

fix(cloudfront): a function that leaves the query string alone does not re-encode it (#2394)

jeffreyhellerfbb12b78a9e9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85c7ac78..6c310a70 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **CloudFront — a viewer-request function that leaves the query string alone no longer re-encodes it** — any function that returned the request it was given counted as a query-string rewrite, so the origin got the query rebuilt from the parsed parameters instead of as the viewer sent it: a bare key gained an `=` (`?flag` became `?flag=`). The query string is now rebuilt only when the function's returned `querystring` differs from the one in its event. Contributed by @jeffreyheller.
- **API Gateway — an HTTP API's responses carry `apigw-requestid`, not a REST API's request ids** — a response from an HTTP API's route got `x-amzn-requestid` and `x-amz-request-id`; an HTTP API names its request id `apigw-requestid`, 16 base64 characters, which it now sends instead. REST APIs keep `x-amzn-requestid`. Contributed by @jeffreyheller.
- **Lambda — publishing on create or code update leaves `$LATEST` unpublished** — `CreateFunction` and `UpdateFunctionCode` with `Publish: true` overwrote `$LATEST`'s `Version` with the new version number. `ListVersionsByFunction` then listed that number twice and no `$LATEST`, `GetFunctionConfiguration` without a qualifier reported it, and invoking `$LATEST` set `AWS_LAMBDA_FUNCTION_VERSION` to it. `$LATEST` now stays `$LATEST`, as on AWS; the create and update responses are unchanged.
### Added
diff --git a/ministack/services/cloudfront_dataplane.py b/ministack/services/cloudfront_dataplane.py
index 11cdbe3a..e10d4751 100644
--- a/ministack/services/cloudfront_dataplane.py
+++ b/ministack/services/cloudfront_dataplane.py
@@ -727,7 +727,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
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
+ function_touched_querystring = False # set when a function returns a changed querystring object
request_function_arn = behavior["functions"].get("viewer-request")
if request_function_arn:
@@ -767,7 +767,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
# A function rewrote the querystring as a literal string (functions-event-structure).
literal_query_string = qs_field
request_query = {}
- elif qs_field is not None:
+ elif qs_field is not None and qs_field != event["request"]["querystring"]:
function_touched_querystring = True
request_query = {}
for name, field in qs_field.items():
diff --git a/tests/test_cloudfront_dataplane.py b/tests/test_cloudfront_dataplane.py
index 3075553b..105a9204 100644
--- a/tests/test_cloudfront_dataplane.py
+++ b/tests/test_cloudfront_dataplane.py
@@ -1040,6 +1040,12 @@ def path_echo_origin_port():
@pytest.fixture(scope="module")
def cf_raw_origin_stack(cloudfront, path_echo_origin_port):
suffix = _uuid_mod.uuid4().hex[:10]
+ passthrough_fn_arn = None
+ if cloudfront_js.available():
+ passthrough_fn_arn = _create_function(
+ cloudfront, f"cf-dp-passthrough-{suffix}",
+ b"function handler(event) { return event.request; }\n",
+ )
dist_config = {
"CallerReference": f"cf-dp-raw-{suffix}",
"Comment": "cloudfront_dataplane raw-path-fidelity test distribution",
@@ -1069,6 +1075,16 @@ def cf_raw_origin_stack(cloudfront, path_echo_origin_port):
}],
},
}
+ if passthrough_fn_arn:
+ dist_config["CacheBehaviors"]["Quantity"] = 2
+ dist_config["CacheBehaviors"]["Items"].append({
+ "PathPattern": "/fn/*",
+ "TargetOriginId": "raw-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED, "OriginRequestPolicyId": _ALL_VIEWER,
+ "FunctionAssociations": {
+ "Quantity": 1, "Items": [{"FunctionARN": passthrough_fn_arn, "EventType": "viewer-request"}],
+ },
+ })
dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
return dist["DomainName"]
@@ -1104,6 +1120,16 @@ def test_query_string_forwarded_byte_exact_when_unmodified_and_fully_allowed(cf_
assert json.loads(body)["path"] == raw_path
+def test_function_returning_the_request_keeps_a_bare_query_key(cf_raw_origin_stack):
+ """AWS: observed 2026-10-10, a distribution whose viewer-request function returns event.request:
+ GET /locations?type[] got the response a Rails origin gives the bare key (?type[]= gets another)."""
+ _skip_without_node()
+ raw_path = "/fn/hello?flag&a=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: