Skip to content

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

fix(s3): handle SigV2 requests through access points as AWS does (#2372)

iot-rocket9afd2961dba7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5d05a23..575e0e29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **S3 — SigV2 requests through an access point** — under `AUTH=true` a SigV2 presigned URL for an access point ARN failed its signature check, because the check signed the access point host as a bucket, and a SigV2 `Authorization` header through an access point alias or ARN was served. The presigned URL is now checked on its path as the SDK signs it, and the header request gets 400 `InvalidRequest`. Contributed by @iot-rocket.
- **IoT — thing-group membership authorized on the group and the thing** — with `AUTH=true`, `AddThingToThingGroup` and `RemoveThingFromThingGroup` were authorized on `thinggroup/addThingToThingGroup` (the request path), so a policy granting exactly the thing group and the thing was denied; both calls are now checked on the thing group and then the thing named in the body (by name or ARN), and a denial names the resource that failed. Contributed by @iot-rocket.
- **API Gateway — `AWS_IAM` methods refuse an `Authorization` header that is not SigV4** — under `AUTH=true` a REST API method with `AWS_IAM` authorization served a bearer token or an algorithm in lower case, and answered the SigV4a algorithm name without a signed `x-amz-region-set` or an unknown `AWS4-` algorithm with `UnrecognizedClientException`; it now answers 403 `IncompleteSignatureException` with API Gateway's message for each, while a method without `AWS_IAM` still serves such calls. Contributed by @iot-rocket.
- **S3 — access point policies and unsigned requests** — an access point policy was stored but never applied, and an unsigned request through an access point was served. With `AUTH=true` a request through an access point alias or ARN is allowed on the access point by an identity policy or the access point policy (a Deny in either refuses it), and an unsigned one gets 400 `InvalidRequest`; S3 Control calls on the access point are unaffected. Contributed by @iot-rocket.
diff --git a/ministack/core/iam_evaluator.py b/ministack/core/iam_evaluator.py
index d4bd8411..0d9fad98 100644
--- a/ministack/core/iam_evaluator.py
+++ b/ministack/core/iam_evaluator.py
@@ -1292,8 +1292,12 @@ def pin_signed_request(
and not _query_value(query_params or {}, "SignatureVersion"))
if sigv2 and claimed and not _is_root_key(claimed) and not authorization.startswith("AWS4-"):
headers = {name.decode("latin-1").lower(): decode_header_value(value) for name, value in raw_headers}
- from ministack.app import _extract_s3_vhost_bucket
+ from ministack.app import _ACCESS_POINT_HOST_RE, _extract_s3_vhost_bucket
+ from ministack.services.s3 import resolve_access_point_host
bucket = _extract_s3_vhost_bucket(headers.get("host", ""))
+ access_point = _ACCESS_POINT_HOST_RE.match(bucket or "")
+ if access_point and resolve_access_point_host(access_point["name"], access_point["account"], None):
+ bucket = None # an access point host signs its path without a bucket
_signed_request.set(SignedRequest(claimed, method, raw_path, query_params, headers, sigv2=True,
auth_path=f"/{bucket}{raw_path}" if bucket else raw_path))
return
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index b485398b..d421f8f4 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -2427,9 +2427,10 @@ async def handle_request(
if (access_point_region is not None or bucket.endswith(_ACCESS_POINT_ALIAS_SUFFIX)) and not internal:
from ministack.app import AUTH
- if (AUTH and not _request_access_key(method, key, headers, query_params, body)
+ if (AUTH and (not _request_access_key(method, key, headers, query_params, body)
+ or headers.get("authorization", "").startswith("AWS "))
and (access_point_region is not None or resolve_access_point_alias(bucket))):
- # Access points take signed requests only.
+ # Access points take signed requests only, a header signature in Signature Version 4.
return _error("InvalidRequest", "The authorization mechanism you have provided is not supported. "
"Please use Signature Version 4.", 400)
diff --git a/tests/test_s3_access_points.py b/tests/test_s3_access_points.py
index e6b493e2..fab0666f 100644
--- a/tests/test_s3_access_points.py
+++ b/tests/test_s3_access_points.py
@@ -3,6 +3,8 @@ access point ARN or alias."""
import json
import os
import re
+import urllib.error
+import urllib.request
import uuid
import boto3
@@ -531,6 +533,34 @@ def test_access_point_refuses_unsigned_requests_contract(auth_enabled, bucket, a
assert anon.get_object(Bucket=target, Key="pub.txt")["Body"].read() == b"public"
[email protected]("via", ["AccessPointArn", "Alias"])
+def test_access_point_sigv2_requests_contract(auth_enabled, iam_user, bucket, access_point, via):
+ """Through an access point a SigV2 presigned URL is served, a SigV2 Authorization header is refused under AUTH."""
+ _name, created = access_point
+ user = iam_user(policy=_access_point_policy(bucket, created["AccessPointArn"], "both"))
+ client = user.client("s3", region="us-east-1", config=Config(signature_version="s3"))
+ url = client.generate_presigned_url("get_object", Params={"Bucket": created[via], "Key": "k.txt"})
+ assert "AWSAccessKeyId=" in url
+ with urllib.request.urlopen(url, timeout=10) as resp:
+ assert resp.read() == b"hello"
+ tampered = re.sub(r"Signature=[^&]+", "Signature=AAAA", url)
+ if auth_enabled:
+ with pytest.raises(urllib.error.HTTPError) as exc:
+ urllib.request.urlopen(tampered, timeout=10)
+ assert exc.value.code == 403
+ assert b"<Code>SignatureDoesNotMatch</Code>" in exc.value.read()
+ with pytest.raises(ClientError) as exc:
+ client.get_object(Bucket=created[via], Key="k.txt")
+ error = exc.value.response
+ assert (error["Error"]["Code"], error["ResponseMetadata"]["HTTPStatusCode"]) == ("InvalidRequest", 400)
+ else:
+ with urllib.request.urlopen(tampered, timeout=10) as resp:
+ assert resp.read() == b"hello"
+ assert client.get_object(Bucket=created[via], Key="k.txt")["Body"].read() == b"hello"
+ assert client.get_object(Bucket=bucket, Key="k.txt")["Body"].read() == b"hello"
+
+
@pytest.mark.auth_contract
@pytest.mark.parametrize("grant, deny, condition, allowed", [
("both", None, False, True),