Skip to content

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

fix(s3): match AWS upload error precedence and response messages (#2292)

gakuto-cw21d8dc0c3e01f3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9f5fa943..9f62016e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,14 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
-- **CloudFormation — custom resource `ResponseURL` and `WaitConditionHandle` URL under `USE_SSL=1`** — both URLs were always `http://`, which the TLS-only gateway drops without a response, so a Python handler that PUTs its `ResponseURL` with `urllib` (CDK's bucket notifications handler does) never signalled and the stack waited out `ServiceTimeout`, and a signal to the handle URL never arrived; both are now `https://` when `USE_SSL` is on, and the Lambdas MiniStack runs also get `SSL_CERT_FILE` set to the gateway's CA bundle, which Python's `ssl` reads where it ignores `REQUESTS_CA_BUNDLE`. Contributed by @iot-rocket.
-### Added
-- **S3 Tables, Glue — Iceberg REST calls are authorized under `AUTH=true`** — the Iceberg REST catalog at `/iceberg` served S3 Tables and Glue catalog calls without checking their credentials; each call is now authorized for the catalog that serves it, whatever service it is signed for: on a table bucket as the `s3tables` actions AWS lists for its operation (`loadTable` needs `GetTableMetadataLocation`, then `GetTableData`) on the bucket or table (`table/*` for one that does not exist), on the Glue catalog as its `glue` action on the catalog, then the database, then the table, and `transactions/commit` as `updateTable` on each table it changes. A denial answers 403 in the Iceberg error shape naming the action and resource ("Access Denied" for a later S3 Tables action, no body for `HEAD`), and a `{prefix}` that is neither a table bucket ARN nor `<account>:s3tablescatalog/<bucket>` answers 400 `bad_request`, as AWS does. Contributed by @iot-rocket.
-- **RDS Data, S3 Control — calls are authorized under `AUTH=true`** — RDS Data calls and S3 Control tagging calls were served whatever the caller's policies allowed; each RDS Data call is now authorized as `rds-data:<operation>` on the cluster its `resourceArn` names, and `ListTagsForResource`, `TagResource` and `UntagResource` as `s3:<operation>` on the bucket, with a 403 denial (`AccessDeniedException` from RDS Data, `AccessDenied` from S3 Control) naming the action and resource, as on AWS. Contributed by @iot-rocket.
-- **SES v2 — calls are authorized under `AUTH=true`** — SES v2 calls were served whatever the caller's policies allowed; each is now authorized as `ses:<operation>` on the configuration set, contact list, custom verification email template, dedicated IP pool, identity or template its path or create body names (`*` otherwise), and a denial answers 403 `AccessDeniedException` naming the action and resource, as on AWS. Contributed by @iot-rocket.
-
-### Fixed
-- **Auth — requests that map to no IAM action are authenticated** — under `AUTH=true` a request MiniStack maps to no IAM action (SES v2, RDS Data, S3 Control, or a service MiniStack does not implement such as X-Ray) was served with a wrong secret, an unknown key, a session key without its token or a stale signature; it now answers the credential error AWS sends for the service, for example `InvalidSignatureException` 403 from SES v2 and RDS Data and `SignatureDoesNotMatch` 403 from S3 Control. Contributed by @iot-rocket.
+- **S3 upload errors under `AUTH=true`** — ordinary header-signed `PutObject` requests now check invalid bucket names, regional endpoint routing and signing regions before credentials, and missing buckets after authentication, matching observed AWS error precedence. The six upload errors include AWS's fixed messages, XML detail fields and matching request identifiers. Pre-authentication regional endpoint checks use `s3.<region>.<host>` hosts. Reserved root credentials and `AUTH=false` keep their existing authentication behavior; bucket home-region checks still apply.
## [1.5.29] — 2026-10-17
diff --git a/ministack/app.py b/ministack/app.py
index f739eec6..6ac3273d 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -2293,6 +2293,9 @@ async def _handle_s3_vhost_request(host: str, path: str, method: str, headers: d
# IAM enforcement for S3 virtual-hosted requests
if AUTH:
+ upload_error = _get_module("s3").validate_upload_request(method, vhost_path, headers, query_params)
+ if upload_error is not None:
+ return upload_error
from ministack.core.iam_actions import (
_s3_action,
extract_resource_arn,
@@ -2348,9 +2351,11 @@ def _with_data_plane_headers(response, request_id: str, include_s3_id: bool = Fa
headers["Access-Control-Allow-Origin"] = "*"
# An API Gateway gateway response already carries the id it rendered.
request_id = headers.setdefault("x-amzn-requestid", request_id)
- headers["x-amz-request-id"] = request_id
if include_s3_id:
- headers["x-amz-id-2"] = base64.b64encode(os.urandom(48)).decode()
+ headers.setdefault("x-amz-request-id", request_id)
+ headers.setdefault("x-amz-id-2", base64.b64encode(os.urandom(48)).decode())
+ else:
+ headers["x-amz-request-id"] = request_id
return status, headers, body
@@ -2854,8 +2859,12 @@ async def _dispatch_service_request(
if service == "unknown_query":
return _unknown_query_error(body, request_id)
- # AppSync authorizes a GraphQL request by the API's own authorization types.
- if AUTH and not (service == "appsync" and method == "POST" and _APPSYNC_GRAPHQL_RE.match(path)):
+ if AUTH and service == "s3":
+ upload_error = _get_module("s3").validate_upload_request(method, path, headers, query_params)
+ if upload_error is not None:
+ return upload_error
+
+ if AUTH:
from ministack.core.iam_actions import (
access_denied_response,
agentcore_endpoint_arn,
diff --git a/ministack/core/iam_evaluator.py b/ministack/core/iam_evaluator.py
index 8cf1221a..923177f6 100644
--- a/ministack/core/iam_evaluator.py
+++ b/ministack/core/iam_evaluator.py
@@ -86,6 +86,7 @@ class AuthError:
"""Authentication failure (before policy evaluation)."""
code: str # "InvalidClientTokenId", "ExpiredTokenException", etc.
message: str
+ details: dict[str, str] = field(default_factory=dict)
# ---------------------------------------------------------------------------
@@ -1357,7 +1358,24 @@ def authenticate(access_key_id: str, account_id: str) -> AuthError | ResolvedCre
request.query_params, request.headers, request.body, presigned=request.presigned,
explain=request.handshake,
)
- return AuthError(*failure) if failure else credential
+ if failure:
+ details = {}
+ if signature.service == "s3" and failure[0] == "SignatureDoesNotMatch":
+ from ministack.core.sigv4 import header_signing_documents
+
+ canonical, string_to_sign = header_signing_documents(
+ signature, request.method, request.raw_path, request.query_params, request.headers, request.body,
+ )
+ details = {
+ "AWSAccessKeyId": access_key_id,
+ "StringToSign": string_to_sign,
+ "SignatureProvided": signature.signature,
+ "StringToSignBytes": string_to_sign.encode("utf-8").hex(" "),
+ "CanonicalRequest": canonical,
+ "CanonicalRequestBytes": canonical.encode("utf-8").hex(" "),
+ }
+ return AuthError(*failure, details=details)
+ return credential
def check_credentials(access_key_id: str) -> AuthError | None:
diff --git a/ministack/core/sigv4.py b/ministack/core/sigv4.py
index fcc12415..5448a767 100644
--- a/ministack/core/sigv4.py
+++ b/ministack/core/sigv4.py
@@ -285,9 +285,8 @@ def _remove_dot_segments(path: str) -> str:
_cached_signing_key = functools.lru_cache(maxsize=256)(derive_signing_key)
-def header_signature_failure(
+def header_signing_documents(
signature: HeaderSignature,
- secret: str,
method: str,
raw_path: str,
query_params: Mapping[str, Any],
@@ -295,13 +294,8 @@ def header_signature_failure(
body: bytes,
*,
presigned: bool = False,
- explain: bool = False,
-) -> tuple[str, str] | None:
- """Recompute a header (or, ``presigned``, query-string) signature; S3 signs its path as sent, other services
- the normalized path encoded again."""
- if signature.date_stamp != signature.amz_date[:8]:
- return "CredentialDateMismatch", (
- "Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP. ")
+) -> tuple[str, str]:
+ """The canonical request and string to sign, also used in S3 signature errors."""
path = raw_path if signature.service == "s3" else uri_encode(_remove_dot_segments(raw_path), encode_slash=False)
payload_hash = headers.get("x-amz-content-sha256") or hashlib.sha256(body).hexdigest()
excluded = ("x-amz-signature",) if presigned else ()
@@ -315,6 +309,31 @@ def header_signature_failure(
string_to_sign = build_string_to_sign(
signature.amz_date, signature.date_stamp, signature.region, signature.service, canonical_request
)
+ return canonical_request, string_to_sign
+
+
+def header_signature_failure(
+ signature: HeaderSignature,
+ secret: str,
+ method: str,
+ raw_path: str,
+ query_params: Mapping[str, Any],
+ headers: Mapping[str, Any],
+ body: bytes,
+ *,
+ presigned: bool = False,
+) -> tuple[str, str] | None:
+ """Recompute a header (or, ``presigned``, query-string) signature; S3 signs its path as sent, other services
+ the normalized path encoded again."""
+ if signature.date_stamp != signature.amz_date[:8]:
+ return "CredentialDateMismatch", (
+ "Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP. ")
+ _, string_to_sign = header_signing_documents(
+ signature, method, raw_path, query_params, headers, body, presigned=presigned,
+ )
+ # Only an explicit streaming marker requires chunk verification; an absent
+ # header's body hash was already computed in header_signing_documents.
+ payload_hash = headers.get("x-amz-content-sha256") or ""
key = _cached_signing_key(secret, signature.date_stamp, signature.region, signature.service)
computed = hmac.new(key, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
if not signatures_match(computed, signature.signature) or (
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 06778a7d..7b4735f7 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -1334,6 +1334,74 @@ def _validate_bucket_name(name: str) -> bool:
return True
+def _upload_error(code: str, message: str, status: int, **fields) -> tuple:
+ """Error envelope observed on real S3 PutObject (Osaka, 2026-10-16)."""
+ request_id = "".join(random.choices(string.ascii_uppercase + string.digits, k=16))
+ host_id = base64.b64encode(os.urandom(57)).decode()
+ root = Element("Error")
+ for name, value in {"Code": code, "Message": message, **fields,
+ "RequestId": request_id, "HostId": host_id}.items():
+ SubElement(root, name).text = value
+ return status, {"Content-Type": "application/xml", "x-amz-request-id": request_id,
+ "x-amz-id-2": host_id}, _xml_body(root)
+
+
+def validate_upload_request(method: str, path: str, headers: dict, query_params: dict) -> tuple | None:
+ """Before IAM, check ordinary header-signed uploads in S3's observed order.
+
+ Called only with AUTH=true. Regional hosts distinguish the endpoint region
+ from the signing region. The handler still enforces the bucket's home
+ region for bare local endpoints. Copy, multipart, presigned and
+ bucket-subresource requests keep their existing validation paths.
+ """
+ from ministack.core.iam_actions import _auth_failure
+ from ministack.core.iam_evaluator import AuthError, authenticate
+ from ministack.core.sigv4 import parse_header_signature
+
+ bucket, key = _parse_bucket_key(path, headers)
+ if (method != "PUT" or not key or "x-amz-copy-source" in headers
+ or query_params.keys() - {"x-id"}
+ or not headers.get("authorization", "").startswith("AWS4-HMAC-SHA256")):
+ return None
+ if bucket.endswith((_ACCESS_POINT_ALIAS_SUFFIX, _OBJECT_LAMBDA_ALIAS_SUFFIX)):
+ return None # The S3 handler resolves access point aliases before bucket lookup.
+ if not _validate_bucket_name(bucket):
+ return _upload_error("InvalidBucketName", "The specified bucket is not valid.", 400, BucketName=bucket)
+ signature = parse_header_signature(headers)
+ if isinstance(signature, tuple):
+ return None # Keep the shared malformed-signature handling.
+ owner = _bucket_owner_account(bucket)
+ record = _buckets.get_scoped(owner, None, bucket) if owner is not None else None
+ host = headers.get("host", "").lower()
+ regional = re.search(r"(?:^|\.)s3[.-]([a-z]{2}(?:-[a-z]+)+-\d+)\.(.+)$", host)
+ if regional:
+ endpoint_region, suffix = regional.groups()
+ bucket_region = (record or {}).get("region") or os.environ.get("MINISTACK_REGION", "us-east-1")
+ if record is not None and bucket_region != endpoint_region:
+ response = _upload_error(
+ "PermanentRedirect", "The bucket you are attempting to access must be addressed using the specified "
+ "endpoint. Please send all future requests to this endpoint.", 301,
+ Endpoint=f"{bucket}.s3.{bucket_region}.{suffix}", Bucket=bucket,
+ )
+ response[1]["x-amz-bucket-region"] = bucket_region
+ return response
+ if signature.region != endpoint_region:
+ return _upload_error(
+ "AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ f"the region '{signature.region}' is wrong; expecting '{endpoint_region}'", 400, Region=endpoint_region,
+ )
+ denied = authenticate(signature.access_key_id, get_account_id())
+ if isinstance(denied, AuthError):
+ code, status, message = _auth_failure("s3", denied.code, denied.message, headers)
+ fields = denied.details
+ if code == "InvalidAccessKeyId":
+ fields = {"AWSAccessKeyId": signature.access_key_id}
+ return _upload_error(code, message, status, **fields)
+ if record is None:
+ return _upload_error("NoSuchBucket", "The specified bucket does not exist", 404, BucketName=bucket)
+ return None
+
+
def _url_encode(value: str) -> str:
# S3's `encoding-type=url` percent-encodes key names (spaces, `+`, unicode,
# control chars) but leaves the forward slash intact, so a delimiter-collapsed
diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.py
index 0ec3bafa..b487490e 100644
--- a/tests/test_iam_auth.py
+++ b/tests/test_iam_auth.py
@@ -4931,11 +4931,12 @@ class TestCredentialErrors:
def user_key(monkeypatch):
"""An IAM user allowed everything, created through the app under AUTH=true."""
from ministack import app as app_mod
+ from ministack.services import iam as iam_svc
monkeypatch.setattr(app_mod, "AUTH", True)
iam = _local_client("iam")
name = f"signer-{time.time_ns()}"
- iam.create_user(UserName=name)
+ user = iam.create_user(UserName=name)["User"]
iam.put_user_policy(UserName=name, PolicyName="all", PolicyDocument=json.dumps(
{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "*", "Resource": "*"}]}))
key = iam.create_access_key(UserName=name)["AccessKey"]
@@ -4943,6 +4944,8 @@ def user_key(monkeypatch):
iam.delete_access_key(UserName=name, AccessKeyId=key["AccessKeyId"])
iam.delete_user_policy(UserName=name, PolicyName="all")
iam.delete_user(UserName=name)
+ # DeleteUserPolicy leaves an empty map after its last policy is deleted.
+ iam_svc._user_inline_policies.pop_scoped(user["Arn"].split(":")[4], None, name, None)
def _chunked_put(key, secret, url, data, *, bad_chunk=False):
@@ -4982,6 +4985,254 @@ def _chunked_put(key, secret, url, data, *, bad_chunk=False):
return _asgi_send(AWSRequest(method="PUT", url=url, headers=headers, data=body).prepare())
+class TestS3UploadErrorParity:
+ """HTTP observations from AWS.Tools.S3 5.0.318, Osaka, 2026-10-16.
+
+ Fixed messages, statuses and XML child order were captured on real AWS
+ using path addressing and MaxErrorRetry=0, with Tokyo as the wrong region.
+ These tests replay the observations locally; they do not contact AWS.
+ Live identifiers are omitted; signing diagnostics use synthetic credentials.
+ """
+
+ @pytest.fixture
+ def upload_bucket(self, user_key):
+ bucket = f"upload-errors-{time.time_ns()}"
+ root = _local_client("s3")
+ root.create_bucket(Bucket=bucket, CreateBucketConfiguration={"LocationConstraint": "ap-northeast-3"})
+ yield bucket
+ root.delete_bucket(Bucket=bucket)
+
+ @staticmethod
+ def upload(bucket, credentials, *, endpoint_region="ap-northeast-3", region="ap-northeast-3",
+ virtual=False, host=None):
+ from urllib.parse import quote
+
+ from botocore.auth import S3SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
+ host = host or f"s3.{endpoint_region}.localhost:4566"
+ path = "/probe.txt" if virtual else f"/{quote(bucket)}/probe.txt"
+ host = f"{bucket}.{host}" if virtual else host
+ request = AWSRequest(method="PUT", url=f"http://{host}{path}", data=b"probe")
+ S3SigV4Auth(Credentials(*credentials), "s3", region).add_auth(request)
+ return _asgi_send(request.prepare())
+
+ @pytest.mark.parametrize("virtual", [False, True])
+ @pytest.mark.parametrize("case,status,code,message,children", [
+ (
+ "invalid-bucket-name", 400, "InvalidBucketName", "The specified bucket is not valid.",
+ ["Code", "Message", "BucketName", "RequestId", "HostId"],
+ ),
+ (
+ "wrong-regional-endpoint", 301, "PermanentRedirect",
+ "The bucket you are attempting to access must be addressed using the specified endpoint. "
+ "Please send all future requests to this endpoint.",
+ ["Code", "Message", "Endpoint", "Bucket", "RequestId", "HostId"],
+ ),
+ (
+ "wrong-signing-region", 400, "AuthorizationHeaderMalformed",
+ "The authorization header is malformed; the region 'ap-northeast-1' is wrong; expecting 'ap-northeast-3'",
+ ["Code", "Message", "Region", "RequestId", "HostId"],
+ ),
+ (
+ "unknown-access-key", 403, "InvalidAccessKeyId",
+ "The AWS Access Key Id you provided does not exist in our records.",
+ ["Code", "Message", "AWSAccessKeyId", "RequestId", "HostId"],
+ ),
+ (
+ "wrong-secret", 403, "SignatureDoesNotMatch",
+ "The request signature we calculated does not match the signature you provided. "
+ "Check your key and signing method.",
+ ["Code", "Message", "AWSAccessKeyId", "StringToSign", "SignatureProvided", "StringToSignBytes",
+ "CanonicalRequest", "CanonicalRequestBytes", "RequestId", "HostId"],
+ ),
+ (
+ "missing-bucket", 404, "NoSuchBucket", "The specified bucket does not exist",
+ ["Code", "Message", "BucketName", "RequestId", "HostId"],
+ ),
+ ])
+ def test_observed_error_envelopes(self, user_key, upload_bucket, case, status, code, message, children, virtual):
+ import base64
+ import re
+ from xml.etree.ElementTree import fromstring
+
+ if case == "invalid-bucket-name" and virtual:
+ pytest.skip("Invalid bucket syntax was observed using path addressing")
+ bucket, credentials, options = upload_bucket, user_key, {"virtual": virtual}
+ if case == "invalid-bucket-name":
+ bucket, credentials = "invalid bucket", ("AKIAUNKNOWNKEY000001", "wrong")
+ elif case == "wrong-regional-endpoint":
+ credentials = ("AKIAUNKNOWNKEY000001", "wrong")
+ options.update(endpoint_region="ap-northeast-1", region="ap-northeast-1")
+ elif case == "wrong-signing-region":
+ credentials = ("AKIAUNKNOWNKEY000001", "wrong")
+ options["region"] = "ap-northeast-1"
+ elif case == "unknown-access-key":
+ bucket, credentials = "missing-upload-bucket", ("AKIAUNKNOWNKEY000001", "wrong")
+ elif case == "wrong-secret":
+ bucket, credentials = "missing-upload-bucket", (user_key[0], "wrong")
+ elif case == "missing-bucket":
+ bucket = "missing-upload-bucket"
+ response = self.upload(bucket, credentials, **options)
+ error = fromstring(response.content)
+ assert response.status_code == status
+ assert error.findtext("Code") == code
+ assert error.findtext("Message") == message
+ assert [child.tag for child in error] == children
+ assert response.headers["content-type"] == "application/xml"
+ assert error.findtext("RequestId") == response.headers["x-amz-request-id"]
+ assert re.fullmatch(r"[A-Z0-9]{16}", error.findtext("RequestId"))
+ assert error.findtext("HostId") == response.headers["x-amz-id-2"]
+ assert len(base64.b64decode(error.findtext("HostId"), validate=True)) >= 48
+ assert "location" not in response.headers
+ if case == "wrong-regional-endpoint":
+ assert response.headers["x-amz-bucket-region"] == "ap-northeast-3"
+ assert error.findtext("Endpoint") == f"{bucket}.s3.ap-northeast-3.localhost:4566"
+ assert error.findtext("Bucket") == bucket
+ else:
+ assert "x-amz-bucket-region" not in response.headers
+ if case in {"invalid-bucket-name", "missing-bucket"}:
+ assert error.findtext("BucketName") == bucket
+ if case == "wrong-signing-region":
+ assert error.findtext("Region") == "ap-northeast-3"
+ if case in {"unknown-access-key", "wrong-secret"}:
+ assert error.findtext("AWSAccessKeyId") == credentials[0]
+ if case == "wrong-secret":
+ import hashlib
+
+ from botocore.auth import S3SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
+ for name in ("CanonicalRequest", "StringToSign"):
+ assert bytes.fromhex(error.findtext(name + "Bytes")).decode() == error.findtext(name)
+ signed_path = "/probe.txt" if virtual else f"/{bucket}/probe.txt"
+ assert error.findtext("CanonicalRequest").startswith(f"PUT\n{signed_path}\n")
+ document = error.findtext("StringToSign")
+ assert document.splitlines()[-1] == hashlib.sha256(error.findtext("CanonicalRequest").encode()).hexdigest()
+ request = AWSRequest()
+ request.context["timestamp"] = document.splitlines()[1]
+ signer = S3SigV4Auth(Credentials(*credentials), "s3", "ap-northeast-3")
+ assert signer.signature(document, request) == error.findtext("SignatureProvided")
+
+ @pytest.mark.parametrize("virtual", [False, True])
+ @pytest.mark.parametrize("missing", [False, True])
+ @pytest.mark.parametrize("credential_kind", ["unknown", "wrong-secret", "valid"])
+ @pytest.mark.parametrize("endpoint_region,region", [
+ ("ap-northeast-3", "ap-northeast-3"),
+ ("ap-northeast-1", "ap-northeast-1"),
+ ("ap-northeast-3", "ap-northeast-1"),
+ ("ap-northeast-1", "ap-northeast-3"),
+ ])
+ def test_combined_failures(self, user_key, upload_bucket, virtual, missing, credential_kind,
+ endpoint_region, region):
+ from xml.etree.ElementTree import fromstring
+
+ bucket = "missing-upload-bucket" if missing else upload_bucket
+ credentials = {"unknown": ("AKIAUNKNOWNKEY000001", "wrong"),
+ "wrong-secret": (user_key[0], "wrong"), "valid": user_key}[credential_kind]
+ if not missing and endpoint_region != "ap-northeast-3":
+ expected = (301, "PermanentRedirect")
+ elif region != endpoint_region:
+ expected = (400, "AuthorizationHeaderMalformed")
+ elif credential_kind == "unknown":
+ expected = (403, "InvalidAccessKeyId")
+ elif credential_kind == "wrong-secret":
+ expected = (403, "SignatureDoesNotMatch")
+ elif missing:
+ expected = (404, "NoSuchBucket")
+ else:
+ expected = (200, None)
+ response = self.upload(bucket, credentials, virtual=virtual, endpoint_region=endpoint_region, region=region)
+ try:
+ code = fromstring(response.content).findtext("Code") if response.status_code != 200 else None
+ assert (response.status_code, code) == expected
+ finally:
+ if response.status_code == 200:
+ _local_client("s3").delete_object(Bucket=bucket, Key="probe.txt")
+
+ def test_invalid_name_precedes_scope_and_credentials(self, user_key):
+ from xml.etree.ElementTree import fromstring
+
+ response = self.upload("invalid bucket", ("AKIAUNKNOWNKEY000001", "wrong"), region="ap-northeast-1")
+ assert response.status_code == 400
+ assert fromstring(response.content).findtext("Code") == "InvalidBucketName"
+
+ @pytest.mark.parametrize("mode", [
+ "auth-disabled", "auth-disabled-wrong-region", "reserved-root", "bare-endpoint-wrong-region",
+ ])
+ def test_auth_modes_and_bare_endpoint(self, monkeypatch, user_key, upload_bucket, mode):
+ from ministack import app as app_mod
+
+ credentials, options = user_key, {}
+ if mode in {"auth-disabled", "auth-disabled-wrong-region"}:
+ monkeypatch.setattr(app_mod, "AUTH", False)
+ credentials = ("AKIAUNKNOWNKEY000001", "wrong")
+ if mode == "auth-disabled-wrong-region":
+ options.update(endpoint_region="ap-northeast-1", region="ap-northeast-1")
+ elif mode == "reserved-root":
+ credentials = ("test", "any-secret")
+ else:
+ options.update(host="localhost:4566", region="ap-northeast-1")
+ response = self.upload(upload_bucket, credentials, **options)
+ try:
+ if mode in {"auth-disabled-wrong-region", "bare-endpoint-wrong-region"}:
+ from xml.etree.ElementTree import fromstring
+
+ assert response.status_code == 301
+ assert fromstring(response.content).findtext("Code") == "PermanentRedirect"
+ assert response.headers["x-amz-bucket-region"] == "ap-northeast-3"
+ else:
+ assert response.status_code == 200
+ finally:
+ _local_client("s3").delete_object(Bucket=upload_bucket, Key="probe.txt")
+
+ @pytest.mark.parametrize("virtual", [False, True])
+ def test_policy_denial_still_applies(self, user_key, upload_bucket, virtual):
+ from ministack.services import iam as iam_svc
+
+ name = iam_svc._access_keys[user_key[0]]["UserName"]
+ iam = _local_client("iam")
+ iam.put_user_policy(UserName=name, PolicyName="deny-upload", PolicyDocument=json.dumps({
+ "Version": "2012-10-17", "Statement": [{"Effect": "Deny", "Action": "s3:PutObject", "Resource": "*"}],
+ }))
+ try:
+ assert self.upload(upload_bucket, user_key, virtual=virtual).status_code == 403
+ finally:
+ iam.delete_user_policy(UserName=name, PolicyName="deny-upload")
+
+ def test_missing_bucket_after_authentication_before_policy(self, user_key, upload_bucket):
+ from xml.etree.ElementTree import fromstring
+
+ from ministack.services import iam as iam_svc
+
+ name = iam_svc._access_keys[user_key[0]]["UserName"]
+ _local_client("iam").put_user_policy(UserName=name, PolicyName="all", PolicyDocument=json.dumps({
+ "Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "s3:PutObject",
+ "Resource": f"arn:aws:s3:::{upload_bucket}/*"}],
+ }))
+ response = self.upload("missing-upload-bucket", user_key)
+ assert response.status_code == 404
+ assert fromstring(response.content).findtext("Code") == "NoSuchBucket"
+
+ def test_foreign_bucket_routing_does_not_grant_access(self, user_key):
+ from xml.etree.ElementTree import fromstring
+
+ owner = _local_client("s3", "333333333333", "root-secret")
+ bucket = f"foreign-upload-{time.time_ns()}"
+ owner.create_bucket(Bucket=bucket, CreateBucketConfiguration={"LocationConstraint": "ap-northeast-3"})
+ try:
+ response = self.upload(bucket, ("AKIAUNKNOWNKEY000001", "wrong"), endpoint_region="ap-northeast-1")
+ assert response.status_code == 301
+ assert fromstring(response.content).findtext("Code") == "PermanentRedirect"
+ response = self.upload(bucket, user_key)
+ assert response.status_code == 403
+ assert fromstring(response.content).findtext("Code") == "AccessDenied"
+ finally:
+ owner.delete_bucket(Bucket=bucket)
+
+
class TestHeaderSignatures:
@pytest.mark.parametrize("service,code,status", [
("iam", "SignatureDoesNotMatch", 403),
diff --git a/tests/test_s3_access_points.py b/tests/test_s3_access_points.py
index b1636bc6..ba51672c 100644
--- a/tests/test_s3_access_points.py
+++ b/tests/test_s3_access_points.py
@@ -238,6 +238,31 @@ def test_access_point_alias_works_as_a_bucket_name(bucket, access_point):
assert s3.get_object(Bucket=bucket, Key="copied.txt")["Body"].read() == b"hello"
[email protected]("addressing_style", ["path", "virtual"])
[email protected]("allow_upload", [False, True], ids=["no-permission", "allow"])
+def test_access_point_alias_upload_permission_contract(
+ bucket, access_point, auth_enabled, iam_user, addressing_style, allow_upload,
+):
+ _, created = access_point
+ policy = {"Statement": [{"Effect": "Allow", "Action": "s3:PutObject", "Resource": "*"}]}
+ client = iam_user(policy=policy if allow_upload else None).client(
+ "s3", config=Config(s3={"addressing_style": addressing_style}),
+ )
+ root = _client("s3", "us-east-1")
+ if auth_enabled and not allow_upload:
+ with pytest.raises(ClientError) as exc:
+ client.put_object(Bucket=created["Alias"], Key="contract-upload", Body=b"via alias")
+ assert exc.value.response["Error"]["Code"] == "AccessDenied"
+ assert root.list_objects_v2(Bucket=bucket, Prefix="contract-upload")["KeyCount"] == 0
+ else:
+ assert client.put_object(
+ Bucket=created["Alias"], Key="contract-upload", Body=b"via alias",
+ )["ResponseMetadata"]["HTTPStatusCode"] == 200
+ with root.get_object(Bucket=bucket, Key="contract-upload")["Body"] as body:
+ assert body.read() == b"via alias"
+
+
def test_access_point_alias_from_another_account_is_denied(access_point):
_name, created = access_point
other = _client("s3", "us-east-1", account_key="111122223333")