Skip to content

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

feat(auth): verify SigV4 signatures, session tokens and time windows under AUTH=true (#2152)

iot-rocketeeebd29916e3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 30350d0c..1d1e6973 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,14 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Added
+- **Auth — the request time of a header-signed request is checked** — under `AUTH=true` a request signed with an IAM user or STS session key was served whatever its time; one signed more than 15 minutes (STS, IAM, SNS, SQS, DynamoDB, S3, ECR, RDS, ElastiCache) or 5 minutes (the default for other services) from the server clock answers `SignatureDoesNotMatch` / `InvalidSignatureException` with AWS's `Signature expired` or `Signature not yet current` message, `RequestTimeTooSkewed` from S3 and `AuthFailure` from EC2. Contributed by @iot-rocket.
+- **Auth — the session token of a header-signed request is checked** — under `AUTH=true` an STS session key sent without its token was accepted and now answers as an unknown key, and a wrong token, or a token with an IAM user key, answers `InvalidClientTokenId` (`UnrecognizedClientException` from JSON services, `InvalidToken` 400 from S3), as on AWS. Contributed by @iot-rocket.
+- **Auth — header signatures are verified for IAM user and STS session keys** — under `AUTH=true` a request signed with such a key and the wrong secret, or changed after signing, was served; it now answers `SignatureDoesNotMatch` (or the service's `InvalidSignatureException`), an `Authorization` header without its `Signature` or `SignedHeaders` answers `IncompleteSignature` (Lambda `MissingAuthenticationTokenException`, S3 `AuthorizationHeaderMalformed`) and a credential date other than the request date `SignatureDoesNotMatch` (S3 `AuthorizationHeaderMalformed`), S3 unsigned, streaming and signed-chunk payloads are checked the same way, and the root key still accepts any secret. Contributed by @iot-rocket.
+
+### Fixed
+- **S3 — presigned URLs keep AWS's time limits under `AUTH=true`** — an `X-Amz-Expires` over 604800 seconds or not a number, or a malformed `X-Amz-Date`, answers 400 `AuthorizationQueryParametersError`, and a URL dated more than 15 minutes ahead answers 403 `AccessDenied` `Request is not yet valid`, where they were served. Contributed by @iot-rocket.
+- **Auth — credential failures in the service's protocol** — under `AUTH=true` an unknown, inactive or expired access key answered `UnrecognizedClientException` 403 from every service; it now gets the code, status and `Content-Type` AWS sends for the service's protocol, for example `InvalidClientTokenId` 403 from IAM and SQS, `UnrecognizedClientException` 400 from DynamoDB, `InvalidAccessKeyId` 403 from S3 and `AuthFailure` 401 from EC2. Contributed by @iot-rocket.
### Fixed
- **Cognito — confirmation codes are checked under `AUTH=true`** — `ConfirmSignUp` and `ConfirmForgotPassword` accepted any code, and `ConfirmForgotPassword` changed the password even when no reset was requested; with `AUTH=true` they take only the issued code (`123456` for sign-up and `654321` for a password reset, as before), answer a wrong one with `CodeMismatchException` and a code that was never issued with `ExpiredCodeException`, and `ConfirmSignUp` refuses a user who is not `UNCONFIRMED` with `NotAuthorizedException`, as AWS does. Contributed by @iot-rocket.
- **Cognito — `ExplicitAuthFlows` is enforced for the password flows under `AUTH=true`** — with `AUTH=true` a client that lists its auth flows now refuses `USER_PASSWORD_AUTH`, `ADMIN_USER_PASSWORD_AUTH` and `ADMIN_NO_SRP_AUTH` unless the list enables them (the legacy `USER_PASSWORD_AUTH` and `ADMIN_NO_SRP_AUTH` names included), with the `InvalidParameterException` AWS returns. Contributed by @iot-rocket.
diff --git a/ministack/app.py b/ministack/app.py
index 36e44477..61a27695 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -24,7 +24,7 @@ import sys
import tempfile
import time
import uuid
-from urllib.parse import parse_qs, unquote
+from urllib.parse import parse_qs, quote, unquote
_MINISTACK_HOST = os.environ.get("MINISTACK_HOST", "localhost")
_MINISTACK_PORT = os.environ.get("GATEWAY_PORT", "4566")
@@ -218,6 +218,8 @@ from ministack.core.hypercorn_compat import install as _install_hypercorn_compat
from ministack.core.iam_evaluator import (
AmbiguousAccessKeyError,
find_iam_access_key_account,
+ pin_signed_body,
+ pin_signed_request,
)
from ministack.core.persistence import PERSIST_STATE, load_state, save_all
from ministack.core.responses import (
@@ -227,6 +229,7 @@ from ministack.core.responses import (
set_request_region,
)
from ministack.core.router import detect_service, extract_access_key_id, extract_region
+from ministack.core.sigv4 import decode_header_value
# Must run before hypercorn emits its first Expect: 100-continue reply.
# See ministack/core/hypercorn_compat.py for the rationale (issue #389).
@@ -725,6 +728,7 @@ async def _read_request_body(receive, method: str, headers: dict) -> bytes:
if not message.get("more_body", False):
break
body = b"".join(chunks)
+ pin_signed_body(body)
content_length = headers.get("content-length")
if content_length is not None:
content_length = content_length.strip()
@@ -2186,6 +2190,17 @@ def _with_data_plane_headers(response, request_id: str, include_s3_id: bool = Fa
return status, headers, body
+def _credential_failure(service: str, iam_action: str, request_id: str, denied, headers: dict):
+ """A failed authentication in the service's protocol, with the Content-Type AWS sends."""
+ from ministack.core.iam_actions import access_denied_response
+
+ status, resp_headers, body = access_denied_response(
+ service, iam_action, "", request_id, error_code=denied.code, message=denied.message, headers=headers
+ )
+ fix_error_content_type(service, status, resp_headers)
+ return status, resp_headers, body
+
+
def _enforce_data_plane(
service: str, iam_action: str, headers: dict, query_params: dict, request_id: str, resource_arn: str = "*",
service_context: dict | None = None,
@@ -2206,9 +2221,7 @@ def _enforce_data_plane(
resource_arn=resource_arn, service_context=service_context)
if denied:
if isinstance(denied, AuthError):
- return access_denied_response(
- service, iam_action, "", request_id, error_code=denied.code, message=denied.message, headers=headers
- )
+ return _credential_failure(service, iam_action, request_id, denied, headers)
return access_denied_response(service, iam_action, denied.principal_arn, request_id, headers=headers,
explicit_deny=denied.decision == "Deny")
return None
@@ -2690,15 +2703,7 @@ async def _dispatch_service_request(
denied = None
if denied:
if isinstance(denied, AuthError):
- return access_denied_response(
- service,
- iam_action,
- "",
- request_id,
- error_code=denied.code,
- message=denied.message,
- headers=headers,
- )
+ return _credential_failure(service, iam_action, request_id, denied, headers)
return access_denied_response(
service, iam_action, denied.principal_arn, request_id, headers=headers,
resource_arn=resource_arn, explicit_deny=denied.decision == "Deny",
@@ -2831,10 +2836,7 @@ async def app(scope, receive, send):
headers = {}
for name, value in scope.get("headers", []):
key = name.decode("latin-1").lower()
- try:
- decoded = value.decode("utf-8")
- except UnicodeDecodeError:
- decoded = value.decode("latin-1")
+ decoded = decode_header_value(value)
if key in headers:
# A field repeated across lines carries the same meaning as one
# comma-joined value (RFC 9110 5.2), and dropping either line
@@ -2895,6 +2897,9 @@ async def app(scope, receive, send):
):
return
+ if AUTH:
+ raw_path = scope.get("raw_path") or quote(path).encode()
+ pin_signed_request(method, raw_path.decode("latin-1"), query_params, scope.get("headers", []))
try:
body = await _read_request_body(receive, method, headers)
except _RequestBodyDisconnected:
diff --git a/ministack/core/iam_actions.py b/ministack/core/iam_actions.py
index 07b7ade7..16a2bcdf 100644
--- a/ministack/core/iam_actions.py
+++ b/ministack/core/iam_actions.py
@@ -1835,6 +1835,70 @@ def dynamodb_service_context(body: bytes) -> dict:
return context
+# Authentication failures by the evaluator's code: the Query API code and
+# status (also REST-XML and the query-compatible JSON services), the JSON code,
+# and S3's code, status and message.
+_UNKNOWN_KEY = (
+ "InvalidClientTokenId", 403, "UnrecognizedClientException",
+ ("InvalidAccessKeyId", 403, "The AWS Access Key Id you provided does not exist in our records."),
+)
+_INCOMPLETE = (
+ "IncompleteSignature", 400, "IncompleteSignatureException",
+ ("AuthorizationHeaderMalformed", 400, "The authorization header is malformed; the authorization header "
+ "requires three components: Credential, SignedHeaders, and Signature."),
+)
+_AUTH_FAILURES: dict[str, tuple[str, int, str, tuple[str, int, str]]] = {
+ "UnrecognizedClientException": _UNKNOWN_KEY,
+ "InvalidClientTokenId": _UNKNOWN_KEY,
+ "ExpiredTokenException": (
+ "ExpiredToken", 403, "ExpiredTokenException",
+ ("ExpiredToken", 400, "The provided token has expired."),
+ ),
+ "InvalidToken": (
+ "InvalidClientTokenId", 403, "UnrecognizedClientException",
+ ("InvalidToken", 400, "The provided token is malformed or otherwise invalid."),
+ ),
+ "SignatureDoesNotMatch": (
+ "SignatureDoesNotMatch", 403, "InvalidSignatureException",
+ ("SignatureDoesNotMatch", 403, "The request signature we calculated does not match the signature "
+ "you provided. Check your key and signing method."),
+ ),
+ "CredentialDateMismatch": (
+ "SignatureDoesNotMatch", 403, "InvalidSignatureException",
+ ("AuthorizationHeaderMalformed", 400, "The authorization header is malformed; Invalid credential date. "
+ "Date is not the same as X-Amz-Date."),
+ ),
+ "RequestTimeTooSkewed": (
+ "SignatureDoesNotMatch", 403, "InvalidSignatureException",
+ ("RequestTimeTooSkewed", 403, "The difference between the request time and the current time is too large."),
+ ),
+ "IncompleteSignature": _INCOMPLETE,
+ "MissingAuthorizationParameter": _INCOMPLETE,
+}
+
+_QUERY_COMPATIBLE = frozenset({"sqs"})
+
+_EC2_AUTH_FAILURE = ("AuthFailure", 401, "AWS was not able to validate the provided access credentials")
+_EC2_FAILURES = {"ExpiredTokenException": ("RequestExpired", 400, "Request has expired.")}
+
+
+def _auth_failure(service: str, code: str, message: str, headers: dict) -> tuple[str, int, str]:
+ """The code, status and message a service answers for a failed authentication."""
+ if service == "lambda" and code == "MissingAuthorizationParameter":
+ return "MissingAuthenticationTokenException", 403, "Missing Authentication Token"
+ query_code, query_status, json_code, s3_failure = _AUTH_FAILURES[code]
+ protocol = _SERVICE_PROTOCOL.get(service)
+ if service == "s3":
+ return s3_failure
+ if service == "ec2":
+ return _EC2_FAILURES.get(code, _EC2_AUTH_FAILURE)
+ if protocol == "rest-xml":
+ return query_code, 403, message
+ if protocol or service in _QUERY_COMPATIBLE:
+ return query_code, query_status, message
+ return json_code, 400 if headers.get("x-amz-target") else 403, message
+
+
def access_denied_response(service: str, action: str, principal_arn: str,
request_id: str, *, error_code: str = "",
message: str = "", headers: dict | None = None,
@@ -1868,6 +1932,9 @@ def access_denied_response(service: str, action: str, principal_arn: str,
)
message = f"User: {principal_arn} is not authorized to perform: {action} {reason}"
code = error_code or "AccessDenied"
+ status = 403
+ if code in _AUTH_FAILURES:
+ code, status, message = _auth_failure(service, code, message, headers or {})
protocol = _SERVICE_PROTOCOL.get(service, "json")
if service in _CBOR_CAPABLE:
# CloudWatch answers the legacy Query API in XML but botocore 1.42+
@@ -1885,7 +1952,7 @@ def access_denied_response(service: str, action: str, principal_arn: str,
json_type = code if code != "AccessDenied" else "AccessDeniedException"
try:
import cbor2
- return (403,
+ return (status,
{"Content-Type": "application/cbor", "smithy-protocol": "rpc-v2-cbor"},
cbor2.dumps({"__type": json_type, "message": message}))
except ImportError:
@@ -1902,7 +1969,7 @@ def access_denied_response(service: str, action: str, principal_arn: str,
f"<Message>{_esc_msg}</Message>"
f"<RequestId>{_esc_rid}</RequestId></Error>"
)
- return 403, {"Content-Type": "application/xml"}, body.encode()
+ return status, {"Content-Type": "application/xml"}, body.encode()
if protocol == "ec2-xml":
ec2_code = "UnauthorizedOperation" if code == "AccessDenied" else _esc_code
@@ -1914,7 +1981,7 @@ def access_denied_response(service: str, action: str, principal_arn: str,
f"</Error></Errors>"
f"<RequestID>{_esc_rid}</RequestID></Response>"
)
- return 403, {"Content-Type": "application/xml"}, body.encode()
+ return status, {"Content-Type": "application/xml"}, body.encode()
if protocol == "query-xml":
ns = _QUERY_XML_NS.get(service, "https://iam.amazonaws.com/doc/2010-05-08/")
@@ -1925,7 +1992,7 @@ def access_denied_response(service: str, action: str, principal_arn: str,
f"<Message>{_esc_msg}</Message></Error>"
f"<RequestId>{_esc_rid}</RequestId></ErrorResponse>"
)
- return 403, {"Content-Type": "text/xml"}, body.encode()
+ return status, {"Content-Type": "text/xml"}, body.encode()
# JSON protocol (DynamoDB, Lambda, KMS, Logs, Glue, etc.)
json_type = code if code != "AccessDenied" else "AccessDeniedException"
@@ -1934,7 +2001,7 @@ def access_denied_response(service: str, action: str, principal_arn: str,
"message": message,
})
return (
- 403,
+ status,
{
"Content-Type": "application/x-amz-json-1.0",
"x-amzn-errortype": json_type,
diff --git a/ministack/core/iam_evaluator.py b/ministack/core/iam_evaluator.py
index bbf43d56..86efac4e 100644
--- a/ministack/core/iam_evaluator.py
+++ b/ministack/core/iam_evaluator.py
@@ -20,7 +20,7 @@ import logging
import os
import re
import time
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, replace
from typing import Any
logger = logging.getLogger("ministack")
@@ -1043,16 +1043,17 @@ def _is_root_key(access_key_id: str) -> bool:
def resolve_principal(access_key_id: str,
- account_id: str) -> PrincipalInfo | AuthError:
+ account_id: str, credential: ResolvedCredential | None = None) -> PrincipalInfo | AuthError:
"""Resolve an access key to a principal and their policies.
Returns a ``PrincipalInfo`` on success or an ``AuthError`` when
authentication fails (unknown key, inactive key, expired session).
+ A ``credential`` the caller already resolved skips the key lookup.
"""
from ministack.core.responses import _account_from_sts_session
account_id = _account_from_sts_session(access_key_id) or account_id
- credential = resolve_credential(access_key_id, account_id)
+ credential = credential or resolve_credential(access_key_id, account_id)
if isinstance(credential, CredentialResolutionError):
return AuthError(credential.code, credential.message)
if credential.principal_type == "Root":
@@ -1076,6 +1077,78 @@ def resolve_principal(access_key_id: str,
)
+@dataclass(frozen=True)
+class SignedRequest:
+ """A header-signed request as it arrived."""
+
+ access_key_id: str
+ method: str
+ raw_path: str
+ query_params: dict
+ headers: dict
+ body: bytes = b""
+
+
+_signed_request: contextvars.ContextVar[SignedRequest | None] = contextvars.ContextVar(
+ "ministack_signed_request", default=None)
+
+
+def pin_signed_request(method: str, raw_path: str, query_params: dict, raw_headers: list) -> None:
+ """Keep a request header-signed with an IAM user or STS session key as sent, for its signature check."""
+ from ministack.core.router import extract_access_key_id
+ from ministack.core.sigv4 import decode_header_value
+
+ authorization = decode_header_value(next((v for n, v in raw_headers if n.lower() == b"authorization"), b""))
+ access_key_id = extract_access_key_id({"authorization": authorization})
+ if not authorization.startswith("AWS4-HMAC-SHA256") or _is_root_key(access_key_id):
+ _signed_request.set(None)
+ return
+ headers: dict[str, str] = {}
+ for name, value in raw_headers:
+ key, text = name.decode("latin-1").lower(), decode_header_value(value)
+ # SigV4 signs a header sent on several lines as its values joined by a bare comma.
+ headers[key] = f"{headers[key]},{text}" if key in headers else text
+ _signed_request.set(SignedRequest(access_key_id, method, raw_path, query_params, headers))
+
+
+def pin_signed_body(body: bytes) -> None:
+ """Add the body as it arrived, before any decoding, to the pinned request."""
+ request = _signed_request.get()
+ if request is not None:
+ _signed_request.set(replace(request, body=body))
+
+
+def authenticate(access_key_id: str, account_id: str) -> AuthError | ResolvedCredential | None:
+ """Check the pinned request's time, session token and signature: its credential, a failure, or None."""
+ from ministack.core.responses import _account_from_sts_session
+ from ministack.core.sigv4 import header_signature_failure, parse_header_signature, request_time_failure
+
+ request = _signed_request.get()
+ if request is None or request.access_key_id != access_key_id:
+ return None
+ _signed_request.set(None) # checked once; the request's later checks skip it
+ signature = parse_header_signature(request.headers)
+ if isinstance(signature, tuple):
+ return AuthError(*signature)
+ skewed = request_time_failure(signature)
+ if skewed:
+ return AuthError(*skewed)
+ token = request.headers.get("x-amz-security-token", "")
+ credential = resolve_credential(access_key_id, _account_from_sts_session(access_key_id) or account_id, token)
+ if isinstance(credential, CredentialResolutionError):
+ if credential.code == "InvalidToken":
+ # AWS treats a session key without its token as unknown, and any other token as invalid.
+ if token:
+ return AuthError("InvalidToken", "The security token included in the request is invalid")
+ credential = _unknown_access_key()
+ return AuthError(credential.code, credential.message)
+ failure = header_signature_failure(
+ signature, credential.secret_access_key, request.method, request.raw_path,
+ request.query_params, request.headers, request.body,
+ )
+ return AuthError(*failure) if failure else credential
+
+
# ---------------------------------------------------------------------------
# Top-level enforcement entry point
# ---------------------------------------------------------------------------
@@ -1090,7 +1163,7 @@ _ALWAYS_ALLOWED_ACTIONS = frozenset({
def resolve_caller_identity(access_key_id: str) -> dict | None:
"""Display identity for a caller's access key — no signature verification
- (never modeled) and no policy evaluation, just the same key resolution the
+ and no policy evaluation, just the same key resolution the
evaluator uses, shaped for API Gateway's IAM-authorized proxy events.
Returns None for an unknown key. The ``session`` value is the raw
@@ -1148,14 +1221,18 @@ def enforce(access_key_id: str, iam_action: str, service: str,
Returns ``None`` if allowed, an ``AuthError`` for authentication failures,
or an ``EvalResult`` for authorization denials.
"""
+ from ministack.core.responses import get_account_id
+
+ account_id = get_account_id()
+ checked = authenticate(access_key_id or "", account_id)
+ if isinstance(checked, AuthError):
+ return checked
+
# Actions that AWS never denies, regardless of policies
if iam_action in _ALWAYS_ALLOWED_ACTIONS:
return None
- from ministack.core.responses import get_account_id
-
- account_id = get_account_id()
- principal = resolve_principal(access_key_id or "", account_id)
+ principal = resolve_principal(access_key_id or "", account_id, checked)
# Authentication failure
if isinstance(principal, AuthError):
diff --git a/ministack/core/sigv4.py b/ministack/core/sigv4.py
index 5423468b..5baab66b 100644
--- a/ministack/core/sigv4.py
+++ b/ministack/core/sigv4.py
@@ -5,16 +5,28 @@ responsible for validating their own credential scope, resolving secrets, and
mapping verification failures to service-specific responses.
"""
+import base64
import datetime as dt
+import functools
import hashlib
import hmac
from collections.abc import Mapping
+from dataclasses import dataclass
+from email.utils import parsedate_to_datetime
from typing import Any
from urllib.parse import quote
UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD"
+def decode_header_value(value: bytes) -> str:
+ """Decode a raw header value as UTF-8, or as Latin-1 when it is not valid UTF-8."""
+ try:
+ return value.decode("utf-8")
+ except UnicodeDecodeError:
+ return value.decode("latin-1")
+
+
def uri_encode(value: str, encode_slash: bool = True) -> str:
"""Encode a SigV4 URI component using the RFC 3986 unreserved set."""
safe = "-_.~" + ("" if encode_slash else "/")
@@ -132,3 +144,167 @@ def presigned_request_is_expired(
def signatures_match(computed: str, provided: str) -> bool:
"""Compare signatures without leaking a mismatch position through timing."""
return hmac.compare_digest(computed, provided)
+
+
+SIGNATURE_MISMATCH = (
+ "The request signature we calculated does not match the signature you provided. "
+ "Check your AWS Secret Access Key and signing method. Consult the service documentation for details."
+)
+
+
+@dataclass(frozen=True)
+class HeaderSignature:
+ """The parts of an ``Authorization: AWS4-HMAC-SHA256`` header and the request date."""
+
+ access_key_id: str
+ date_stamp: str
+ region: str
+ service: str
+ signed_headers: str
+ signature: str
+ amz_date: str
+
+
+def _incomplete(authorization: str, reason: str, code: str = "IncompleteSignature") -> tuple[str, str]:
+ digest = base64.b64encode(hashlib.sha256(authorization.encode("utf-8")).digest()).decode()
+ return code, f"{reason} (Hashed with SHA-256 and encoded with Base64) Authorization={digest}"
+
+
+def parse_header_signature(headers: Mapping[str, Any]) -> HeaderSignature | tuple[str, str]:
+ """Split a header signature into its parts, or name the part that is missing."""
+ authorization = headers.get("authorization", "")
+ fields = dict(part.strip().partition("=")[::2] for part in authorization.split(" ", 1)[-1].split(","))
+ for name in ("Credential", "SignedHeaders", "Signature"):
+ if not fields.get(name):
+ return _incomplete(
+ authorization, f"Authorization header requires '{name}' parameter.", "MissingAuthorizationParameter")
+ scope = fields["Credential"].split("/")
+ if len(scope) != 5:
+ return "IncompleteSignature", (
+ "Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term, "
+ f"got '{fields['Credential']}'"
+ )
+ amz_date = headers.get("x-amz-date", "")
+ if not amz_date and headers.get("date"):
+ try:
+ signed_at = parsedate_to_datetime(headers["date"]).astimezone(dt.timezone.utc)
+ amz_date = signed_at.strftime("%Y%m%dT%H%M%SZ")
+ except (TypeError, ValueError):
+ pass
+ if not amz_date:
+ return _incomplete(
+ authorization, "Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header."
+ )
+ return HeaderSignature(*scope[:4], fields["SignedHeaders"], fields["Signature"], amz_date)
+
+
+# Services that accept a request signed up to 15 minutes from their clock; the others allow 5.
+_FIFTEEN_MINUTE_SERVICES = frozenset({"dynamodb", "ecr", "elasticache", "iam", "rds", "s3", "sns", "sqs", "sts"})
+
+
+def _amz_datetime(value: str) -> dt.datetime | None:
+ """Parse ``YYYYMMDDTHHMMSSZ`` by slicing; strptime is slow on the request path."""
+ digits = value[:8] + value[9:15]
+ if len(value) != 16 or value[8] != "T" or value[15] != "Z" or not (digits.isascii() and digits.isdigit()):
+ return None
+ try:
+ return dt.datetime(int(value[:4]), int(value[4:6]), int(value[6:8]), int(value[9:11]), int(value[11:13]),
+ int(value[13:15]), tzinfo=dt.timezone.utc)
+ except ValueError:
+ return None
+
+
+def request_time_failure(signature: HeaderSignature, *, now: dt.datetime | None = None) -> tuple[str, str] | None:
+ """Refuse a request signed further from the current time than its service allows."""
+ signed_at = _amz_datetime(signature.amz_date)
+ if signed_at is None:
+ return None
+ now = now or dt.datetime.now(dt.timezone.utc)
+ minutes = 15 if signature.service in _FIFTEEN_MINUTE_SERVICES else 5
+ window = dt.timedelta(minutes=minutes)
+
+ def stamp(value: dt.datetime) -> str:
+ return value.strftime("%Y%m%dT%H%M%SZ")
+
+ if signed_at < now - window:
+ return "RequestTimeTooSkewed", (
+ f"Signature expired: {signature.amz_date} is now earlier than {stamp(now - window)} "
+ f"({stamp(now)} - {minutes} min.)"
+ )
+ if signed_at > now + window:
+ return "RequestTimeTooSkewed", (
+ f"Signature not yet current: {signature.amz_date} is still later than {stamp(now + window)} "
+ f"({stamp(now)} + {minutes} min.)"
+ )
+ return None
+
+
+def _remove_dot_segments(path: str) -> str:
+ segments = []
+ for segment in path.split("/"):
+ if segment == "..":
+ if segments:
+ segments.pop()
+ elif segment and segment != ".":
+ segments.append(segment)
+ return "/" + "/".join(segments) + ("/" if path.endswith("/") and segments else "")
+
+
+_cached_signing_key = functools.lru_cache(maxsize=256)(derive_signing_key)
+
+
+def header_signature_failure(
+ signature: HeaderSignature,
+ secret: str,
+ method: str,
+ raw_path: str,
+ query_params: Mapping[str, Any],
+ headers: Mapping[str, Any],
+ body: bytes,
+) -> tuple[str, str] | None:
+ """Recompute a header 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. ")
+ 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()
+ canonical_request = "\n".join([
+ method, path, build_canonical_query_string(query_params, exclude=()),
+ build_canonical_headers(headers, signature.signed_headers), signature.signed_headers, payload_hash,
+ ])
+ string_to_sign = build_string_to_sign(
+ signature.amz_date, signature.date_stamp, signature.region, signature.service, canonical_request
+ )
+ 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 (
+ payload_hash.startswith("STREAMING-AWS4-HMAC-SHA256-PAYLOAD")
+ and not _chunk_signatures_match(body, computed, signature, key)
+ ):
+ return "SignatureDoesNotMatch", SIGNATURE_MISMATCH
+ return None
+
+
+def _chunk_signatures_match(body: bytes, seed: str, signature: HeaderSignature, key: bytes) -> bool:
+ """Check each aws-chunked chunk signature, chained from the seed signature."""
+ scope = f"{signature.date_stamp}/{signature.region}/{signature.service}/aws4_request"
+ empty = hashlib.sha256(b"").hexdigest()
+ previous, pos = seed, 0
+ while (end := body.find(b"\r\n", pos)) >= 0:
+ size, _, provided = body[pos:end].decode("ascii", "replace").partition(";chunk-signature=")
+ try:
+ size = int(size, 16)
+ except ValueError:
+ return False
+ chunk = body[end + 2:end + 2 + size]
+ string_to_sign = "\n".join([
+ "AWS4-HMAC-SHA256-PAYLOAD", signature.amz_date, scope, previous, empty,
+ hashlib.sha256(chunk).hexdigest(),
+ ])
+ previous = hmac.new(key, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
+ if not signatures_match(previous, provided.strip()):
+ return False
+ if size == 0:
+ return True
+ pos = end + 2 + size + 2
+ return False
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index b8ca2106..4640b0f5 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -1659,6 +1659,30 @@ def _object_response_headers(obj: dict, bucket_name: str = "", key: str = "", in
# ---------------------------------------------------------------------------
+_PRESIGN_MAX_EXPIRES = 604800
+
+
+def _presign_time_error(amz_date: str, expires: str, path: str) -> tuple | None:
+ """Under AUTH=true, refuse a presign with a malformed time, a lifetime over a week or a date in the future."""
+ from ministack.app import AUTH
+ if not AUTH:
+ return None
+ try:
+ signed_at = _dt.datetime.strptime(amz_date, "%Y%m%dT%H%M%SZ").replace(tzinfo=_dt.timezone.utc)
+ except ValueError:
+ return _error("AuthorizationQueryParametersError",
+ "X-Amz-Date must be in the ISO8601 Long Format \"yyyyMMdd'T'HHmmss'Z'\"", 400, path)
+ if expires and not expires.isdigit():
+ return _error("AuthorizationQueryParametersError", "X-Amz-Expires should be a number", 400, path)
+ if expires and int(expires) > _PRESIGN_MAX_EXPIRES:
+ return _error("AuthorizationQueryParametersError",
+ "X-Amz-Expires must be less than a week (in seconds); that is, the given X-Amz-Expires "
+ f"must be less than {_PRESIGN_MAX_EXPIRES} seconds", 400, path)
+ if signed_at > _dt.datetime.now(_dt.timezone.utc) + _dt.timedelta(minutes=15):
+ return _error("AccessDenied", "Request is not yet valid", 403, path)
+ return None
+
+
def _verify_presigned_sigv4(method, path, headers, query_params):
"""Verify a SigV4 presigned S3 URL. Returns an error tuple for a bad
signature, or None when the request is not a SigV4 presigned URL (header-
@@ -1695,6 +1719,9 @@ def _verify_presigned_sigv4(method, path, headers, query_params):
# Expiry: a presigned URL past X-Amz-Date + X-Amz-Expires is rejected by S3
# with 403 AccessDenied "Request has expired", independent of the signature.
expires = _qp(query_params, "X-Amz-Expires", "") or _qp(query_params, "x-amz-expires", "")
+ time_error = _presign_time_error(amz_date, expires, path)
+ if time_error:
+ return time_error
if expires:
try:
if presigned_request_is_expired(amz_date, expires):
diff --git a/tests/test_cloudwatch_logs.py b/tests/test_cloudwatch_logs.py
index 6b60db9b..3ac467cf 100644
--- a/tests/test_cloudwatch_logs.py
+++ b/tests/test_cloudwatch_logs.py
@@ -6,7 +6,10 @@ import uuid as _uuid_mod
import boto3
import pytest
+from botocore.auth import SigV4Auth
+from botocore.awsrequest import AWSRequest
from botocore.config import Config
+from botocore.credentials import Credentials
from botocore.exceptions import ClientError
from ministack import app as _app_mod
@@ -2170,16 +2173,15 @@ def logs_auth(monkeypatch):
async def send(message):
sent.append(message)
+ host = f"_logs_svc.{request_region}.amazonaws.com"
+ request = AWSRequest(method="POST", url=f"https://{host}/", data=body, headers={
+ "x-amz-target": f"Logs_20140328.{action}", "content-type": "application/x-amz-json-1.1",
+ })
+ SigV4Auth(Credentials(account if root else key, "test"), "logs", request_region).add_auth(request)
scope = {
"type": "http", "method": "POST", "path": "/", "query_string": b"",
- "headers": [
- (b"host", f"_logs_svc.{request_region}.amazonaws.com".encode()),
- (b"x-amz-target", f"Logs_20140328.{action}".encode()),
- (b"content-type", b"application/x-amz-json-1.1"),
- (b"authorization", (
- f"AWS4-HMAC-SHA256 Credential={account if root else key}"
- f"/20261006/{request_region}/logs/aws4_request"
- ).encode()),
+ "headers": [(b"host", host.encode())] + [
+ (name.lower().encode(), value.encode()) for name, value in request.headers.items()
],
}
asyncio.run(_app_mod.app(scope, receive, send))
@@ -2437,7 +2439,7 @@ def test_invalid_tag_arn_precedes_policy_denial_but_preserves_credential_errors(
assert b"x-amzn-errortype" not in headers
_iam_svc._access_keys.get_scoped(account, None, "AKIALOGSTAGCONTEXT")["Status"] = "Inactive"
status, body = call("UntagResource", resourceArn=arn + ":*", tagKeys=["protect"])
- assert status == 403 and body["__type"] == "InvalidClientTokenId"
+ assert status == 400 and body["__type"] == "UnrecognizedClientException"
@pytest.mark.parametrize("auth", [False, True])
diff --git a/tests/test_iam_auth.py b/tests/test_iam_auth.py
index a500c0ed..84016a74 100644
--- a/tests/test_iam_auth.py
+++ b/tests/test_iam_auth.py
@@ -11,7 +11,10 @@ the MiniStack server and work with AUTH=false.
"""
import asyncio
+import contextlib
+import io
import json
+import re
import time
import xml.etree.ElementTree as ET
from urllib.parse import urlencode
@@ -3151,7 +3154,7 @@ class TestAccessDeniedResponse:
def test_custom_error_code_preserved(self):
from ministack.core.iam_actions import access_denied_response
- s, h, b = access_denied_response("s3", "s3:PutObject", "", "r1",
+ s, h, b = access_denied_response("dynamodb", "dynamodb:PutItem", "", "r1",
error_code="ExpiredTokenException",
message="Token expired")
assert b"ExpiredTokenException" in b
@@ -3465,6 +3468,10 @@ def test_ssm_parameter_tag_actions_enforce_exact_resource_and_preserve_denied_ta
monkeypatch, policy_mode,
):
"""Run the real ASGI route, IAM evaluator, and SSM tag handlers."""
+ from botocore.auth import SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
from ministack import app as app_mod
from ministack.services import iam as iam_svc
from ministack.services import ssm as ssm_svc
@@ -3499,15 +3506,14 @@ def test_ssm_parameter_tag_actions_enforce_exact_resource_and_preserve_denied_ta
async def send(message):
sent.append(message)
+ request = AWSRequest(method="POST", url=f"https://ssm.{region}.amazonaws.com/", data=body, headers={
+ "x-amz-target": f"AmazonSSM.{action}", "content-type": "application/x-amz-json-1.1",
+ })
+ SigV4Auth(Credentials(key, "test-secret"), "ssm", region).add_auth(request)
scope = {
"type": "http", "method": "POST", "path": "/", "query_string": b"",
- "headers": [
- (b"host", b"ssm.us-east-1.amazonaws.com"),
- (b"x-amz-target", f"AmazonSSM.{action}".encode()),
- (b"content-type", b"application/x-amz-json-1.1"),
- (b"authorization", (
- f"AWS4-HMAC-SHA256 Credential={key}/20260928/{region}/ssm/aws4_request"
- ).encode()),
+ "headers": [(b"host", b"ssm.us-east-1.amazonaws.com")] + [
+ (name.lower().encode(), value.encode()) for name, value in request.headers.items()
],
}
asyncio.run(app_mod.app(scope, receive, send))
@@ -3607,6 +3613,10 @@ def test_ssm_parameter_tag_actions_enforce_exact_resource_and_preserve_denied_ta
def test_ssm_parameter_tag_authorization_is_scoped_to_request_account_and_region(monkeypatch):
+ from botocore.auth import SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
from ministack import app as app_mod
from ministack.services import iam as iam_svc
from ministack.services import ssm as ssm_svc
@@ -3635,15 +3645,14 @@ def test_ssm_parameter_tag_authorization_is_scoped_to_request_account_and_region
async def send(message):
sent.append(message)
+ request = AWSRequest(method="POST", url=f"https://ssm.{region}.amazonaws.com/", data=body, headers={
+ "x-amz-target": "AmazonSSM.AddTagsToResource", "content-type": "application/x-amz-json-1.1",
+ })
+ SigV4Auth(Credentials(key, "test-secret"), "ssm", region).add_auth(request)
scope = {
"type": "http", "method": "POST", "path": "/", "query_string": b"",
- "headers": [
- (b"host", f"ssm.{region}.amazonaws.com".encode()),
- (b"x-amz-target", b"AmazonSSM.AddTagsToResource"),
- (b"content-type", b"application/x-amz-json-1.1"),
- (b"authorization", (
- f"AWS4-HMAC-SHA256 Credential={key}/20260928/{region}/ssm/aws4_request"
- ).encode()),
+ "headers": [(b"host", f"ssm.{region}.amazonaws.com".encode())] + [
+ (name.lower().encode(), value.encode()) for name, value in request.headers.items()
],
}
asyncio.run(app_mod.app(scope, receive, send))
@@ -4164,6 +4173,442 @@ def test_http_iam_routing_respects_auth_mode(monkeypatch, auth_enabled, ambiguou
iam_svc._access_keys.pop_scoped(account, None, key, None)
+# ---------------------------------------------------------------------------
+# Credential checks over HTTP: boto3 clients whose transport is the app itself
+# ---------------------------------------------------------------------------
+
+class _Body(io.BytesIO):
+ def stream(self, **_):
+ yield self.getvalue()
+
+
+def _asgi_send(request, extra_headers=(), **_):
+ """Serve a botocore request with the in-process app instead of the network."""
+ from urllib.parse import unquote, urlsplit
+
+ from botocore.awsrequest import AWSResponse
+
+ from ministack import app as app_mod
+
+ url = urlsplit(request.url)
+ body = request.body.read() if hasattr(request.body, "read") else request.body or b""
+ body = body.encode() if isinstance(body, str) else body
+ headers = {"host": url.netloc.encode()}
+ headers.update((k.lower(), v if isinstance(v, bytes) else str(v).encode("latin-1"))
+ for k, v in request.headers.items())
+ scope = {"type": "http", "method": request.method, "path": unquote(url.path) or "/",
+ "raw_path": url.path.encode(), "query_string": url.query.encode(),
+ "headers": [(k.encode("latin-1"), v) for k, v in headers.items()] + list(extra_headers)}
+ sent = []
+
+ async def receive():
+ return {"type": "http.request", "body": body, "more_body": False}
+
+ async def send(message):
+ sent.append(message)
+
+ asyncio.run(app_mod.app(scope, receive, send))
+ response_headers = {k.decode(): v.decode() for k, v in sent[0].get("headers", [])}
+ return AWSResponse(request.url, sent[0]["status"], response_headers,
+ _Body(b"".join(m.get("body", b"") for m in sent[1:])))
+
+
+def _local_client(service, key="test", secret="test", token=None, **s3_config):
+ import boto3
+ from botocore.config import Config
+
+ client = boto3.client(
+ service, endpoint_url="http://localhost:4566", region_name="us-east-1",
+ aws_access_key_id=key, aws_secret_access_key=secret, aws_session_token=token,
+ config=Config(retries={"max_attempts": 1}, s3={"addressing_style": "path", **s3_config}),
+ )
+ client.meta.events.register("before-send", _asgi_send)
+ return client
+
+
+def _failure(call):
+ with pytest.raises(ClientError) as exc:
+ call()
+ return exc.value.response["Error"]["Code"], exc.value.response["ResponseMetadata"]["HTTPStatusCode"]
+
+
+_LIST_CALLS = {
+ "iam": lambda c: c.list_users(),
+ "sqs": lambda c: c.list_queues(),
+ "dynamodb": lambda c: c.list_tables(),
+ "lambda": lambda c: c.list_functions(),
+ "s3": lambda c: c.list_buckets(),
+ "ec2": lambda c: c.describe_regions(),
+ "ssm": lambda c: c.describe_parameters(),
+ "logs": lambda c: c.describe_log_groups(),
+}
+
+
+class TestCredentialErrors:
+ @pytest.mark.parametrize("service,code,status", [
+ ("iam", "InvalidClientTokenId", 403),
+ ("sqs", "InvalidClientTokenId", 403),
+ ("dynamodb", "UnrecognizedClientException", 400),
+ ("lambda", "UnrecognizedClientException", 403),
+ ("s3", "InvalidAccessKeyId", 403),
+ ("ec2", "AuthFailure", 401),
+ ("ssm", "UnrecognizedClientException", 400),
+ ("logs", "UnrecognizedClientException", 400),
+ ])
+ def test_unknown_key_answers_the_protocols_code(self, monkeypatch, service, code, status):
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", True)
+ client = _local_client(service, "AKIAUNKNOWNKEY000001", "secret")
+ assert _failure(lambda: _LIST_CALLS[service](client)) == (code, status)
+
+ @pytest.mark.parametrize("service,content_type", [
+ ("iam", "text/xml"),
+ ("sqs", "application/x-amz-json-1.0"),
+ ("dynamodb", "application/x-amz-json-1.0"),
+ ("lambda", "application/json"),
+ ("s3", "application/xml"),
+ ("ec2", "text/xml;charset=UTF-8"),
+ ("ssm", "application/x-amz-json-1.1"),
+ ("logs", "application/x-amz-json-1.1"),
+ ])
+ def test_unknown_key_answers_the_protocols_content_type(self, monkeypatch, service, content_type):
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", True)
+ client = _local_client(service, "AKIAUNKNOWNKEY000001", "secret")
+ with pytest.raises(ClientError) as exc:
+ _LIST_CALLS[service](client)
+ assert exc.value.response["ResponseMetadata"]["HTTPHeaders"]["content-type"] == content_type
+
+ def test_unknown_key_is_ignored_without_auth(self, monkeypatch):
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", False)
+ client = _local_client("dynamodb", "AKIAUNKNOWNKEY000001", "secret")
+ assert client.list_tables()["ResponseMetadata"]["HTTPStatusCode"] == 200
+
+ @pytest.mark.parametrize("service,code,status", [
+ ("iam", "ExpiredToken", 403),
+ ("dynamodb", "ExpiredTokenException", 400),
+ ("lambda", "ExpiredTokenException", 403),
+ ("s3", "ExpiredToken", 400),
+ ("ec2", "RequestExpired", 400),
+ ])
+ def test_expired_session_answers_the_protocols_code(self, monkeypatch, service, code, status):
+ from ministack import app as app_mod
+ from ministack.services import sts as sts_svc
+
+ monkeypatch.setattr(app_mod, "AUTH", True)
+ monkeypatch.setitem(sts_svc._sessions, "ASIAEXPIREDSESSION01", {
+ "Arn": "arn:aws:iam::000000000000:user/alice", "UserId": "AIDAALICE",
+ "SecretAccessKey": "session-secret", "SessionToken": "session-token",
+ "Expiration": time.time() - 1, "AccountId": "000000000000", "PrincipalType": "User",
+ })
+ client = _local_client(service, "ASIAEXPIREDSESSION01", "session-secret", "session-token")
+ assert _failure(lambda: _LIST_CALLS[service](client)) == (code, status)
+
+
+def user_key(monkeypatch):
+ """An IAM user allowed everything, created through the app under AUTH=true."""
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", True)
+ iam = _local_client("iam")
+ name = f"signer-{time.time_ns()}"
+ iam.create_user(UserName=name)
+ 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"]
+ yield key["AccessKeyId"], key["SecretAccessKey"]
+ iam.delete_access_key(UserName=name, AccessKeyId=key["AccessKeyId"])
+ iam.delete_user_policy(UserName=name, PolicyName="all")
+ iam.delete_user(UserName=name)
+
+
+def _chunked_put(key, secret, url, data, *, bad_chunk=False):
+ """A PutObject streamed as signed aws-chunked chunks, as the Java and Go SDKs send it."""
+ import datetime as dt
+ import hashlib
+ import hmac
+
+ from botocore.awsrequest import AWSRequest
+
+ from ministack.core.sigv4 import derive_signing_key
+
+ amz_date = dt.datetime.now(dt.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
+ scope = f"{amz_date[:8]}/us-east-1/s3/aws4_request"
+ signing_key = derive_signing_key(secret, amz_date[:8], "us-east-1", "s3")
+ chunks = [data, b""]
+ headers = {
+ "content-encoding": "aws-chunked", "host": "localhost:4566", "x-amz-date": amz_date,
+ "x-amz-content-sha256": "STREAMING-AWS4-HMAC-SHA256-PAYLOAD",
+ "x-amz-decoded-content-length": str(len(data)),
+ }
+ signed = ";".join(sorted(headers))
+ path = url.split("localhost:4566", 1)[1]
+ canonical = "\n".join(["PUT", path, "", "".join(f"{h}:{headers[h]}\n" for h in sorted(headers)), signed,
+ headers["x-amz-content-sha256"]])
+ string_to_sign = "\n".join(["AWS4-HMAC-SHA256", amz_date, scope, hashlib.sha256(canonical.encode()).hexdigest()])
+ previous = hmac.new(signing_key, string_to_sign.encode(), hashlib.sha256).hexdigest()
+ headers["authorization"] = (
+ f"AWS4-HMAC-SHA256 Credential={key}/{scope}, SignedHeaders={signed}, Signature={previous}")
+ body = b""
+ for chunk in chunks:
+ previous = hmac.new(signing_key, "\n".join([
+ "AWS4-HMAC-SHA256-PAYLOAD", amz_date, scope, previous, hashlib.sha256(b"").hexdigest(),
+ hashlib.sha256(chunk).hexdigest()]).encode(), hashlib.sha256).hexdigest()
+ signature = "0" * 64 if bad_chunk and chunk else previous
+ body += f"{len(chunk):x};chunk-signature={signature}\r\n".encode() + chunk + b"\r\n"
+ return _asgi_send(AWSRequest(method="PUT", url=url, headers=headers, data=body).prepare())
+
+
+class TestHeaderSignatures:
+ @pytest.mark.parametrize("service,code,status", [
+ ("iam", "SignatureDoesNotMatch", 403),
+ ("sqs", "SignatureDoesNotMatch", 403),
+ ("dynamodb", "InvalidSignatureException", 400),
+ ("lambda", "InvalidSignatureException", 403),
+ ("s3", "SignatureDoesNotMatch", 403),
+ ("ec2", "AuthFailure", 401),
+ ])
+ def test_wrong_secret_answers_the_protocols_code(self, user_key, service, code, status):
+ client = _local_client(service, user_key[0], "wrong-secret")
+ assert _failure(lambda: _LIST_CALLS[service](client)) == (code, status)
+
+ def test_get_caller_identity_checks_the_signature(self, user_key):
+ client = _local_client("sts", user_key[0], "wrong-secret")
+ assert _failure(client.get_caller_identity) == ("SignatureDoesNotMatch", 403)
+ assert ":user/signer-" in _local_client("sts", *user_key).get_caller_identity()["Arn"]
+
+ @pytest.mark.parametrize("service", sorted(_LIST_CALLS))
+ def test_right_secret_passes(self, user_key, service):
+ _LIST_CALLS[service](_local_client(service, *user_key))
+
+ def test_paths_and_queries_sign_as_the_sdk_encodes_them(self, user_key):
+ s3 = _local_client("s3", *user_key)
+ bucket = f"signed-{time.time_ns()}"
+ s3.create_bucket(Bucket=bucket)
+ try:
+ s3.put_object(Bucket=bucket, Key="dir/a b+c=ü.txt", Body=b"x")
+ assert s3.get_object(Bucket=bucket, Key="dir/a b+c=ü.txt")["Body"].read() == b"x"
+ assert s3.list_objects_v2(Bucket=bucket, Prefix="dir/a b")["KeyCount"] == 1
+ finally:
+ s3.delete_object(Bucket=bucket, Key="dir/a b+c=ü.txt")
+ s3.delete_bucket(Bucket=bucket)
+ lam = _local_client("lambda", *user_key)
+ function = "arn:aws:lambda:us-east-1:000000000000:function:a b"
+ assert _failure(lambda: lam.get_function(FunctionName=function)) != ("InvalidSignatureException", 403)
+ lam.list_functions(MaxItems=5, FunctionVersion="ALL")
+
+ @pytest.mark.parametrize("key", ["a%2Fb", "caf%c3%a9", "x%7Ey"])
+ def test_s3_signs_the_path_as_sent(self, user_key, key):
+ from botocore.auth import S3SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
+ s3 = _local_client("s3", *user_key)
+ bucket = f"raw-path-{time.time_ns()}"
+ s3.create_bucket(Bucket=bucket)
+ try:
+ request = AWSRequest(method="PUT", url=f"http://localhost:4566/{bucket}/{key}", data=b"x")
+ S3SigV4Auth(Credentials(*user_key), "s3", "us-east-1").add_auth(request)
+ assert _asgi_send(request.prepare()).status_code == 200
+ finally:
+ for item in s3.list_objects_v2(Bucket=bucket).get("Contents", []):
+ s3.delete_object(Bucket=bucket, Key=item["Key"])
+ s3.delete_bucket(Bucket=bucket)
+
+ def test_pin_holds_no_root_key_request_and_ends_with_the_check(self, user_key):
+ from ministack.core import iam_evaluator
+
+ def pin(key):
+ iam_evaluator.pin_signed_request("GET", "/", {}, [
+ (b"host", b"localhost:4566"),
+ (b"authorization", f"AWS4-HMAC-SHA256 Credential={key}/20260101/us-east-1/sts/aws4_request, "
+ "SignedHeaders=host, Signature=0".encode()),
+ ])
+ return iam_evaluator._signed_request.get()
+
+ assert pin("test") is None
+ assert pin(user_key[0]) is not None
+ assert isinstance(iam_evaluator.authenticate(user_key[0], "000000000000"), iam_evaluator.AuthError)
+ assert iam_evaluator._signed_request.get() is None
+
+ def test_request_changed_after_signing_is_refused(self, user_key):
+ client = _local_client("dynamodb", *user_key)
+ client.meta.events.register_first(
+ "before-send", lambda request, **_: setattr(request, "body", b'{"Limit": 2}'))
+ assert _failure(lambda: client.list_tables(Limit=1)) == ("InvalidSignatureException", 400)
+
+ @pytest.mark.parametrize("service,code,status", [
+ ("iam", "IncompleteSignature", 400),
+ ("dynamodb", "IncompleteSignatureException", 400),
+ ("lambda", "MissingAuthenticationTokenException", 403),
+ ("s3", "AuthorizationHeaderMalformed", 400),
+ ])
+ def test_incomplete_authorization_header_is_refused(self, user_key, service, code, status):
+ client = _local_client(service, *user_key)
+
+ def drop_signature(request, **_):
+ request.headers["Authorization"] = request.headers["Authorization"].split(b", Signature=")[0]
+
+ client.meta.events.register_first("before-send", drop_signature)
+ assert _failure(lambda: _LIST_CALLS[service](client)) == (code, status)
+
+ @pytest.mark.parametrize("service,code,status", [
+ ("iam", "SignatureDoesNotMatch", 403),
+ ("dynamodb", "InvalidSignatureException", 400),
+ ("s3", "AuthorizationHeaderMalformed", 400),
+ ])
+ def test_credential_date_must_match_the_request_date(self, user_key, service, code, status):
+ client = _local_client(service, *user_key)
+
+ def move_scope_date(request, **_):
+ request.headers["Authorization"] = re.sub(
+ rb"/\d{8}/", b"/20200101/", request.headers["Authorization"], count=1)
+
+ client.meta.events.register_first("before-send", move_scope_date)
+ assert _failure(lambda: _LIST_CALLS[service](client)) == (code, status)
+
+ def test_header_sent_on_two_lines_signs_as_its_comma_joined_value(self, user_key):
+ from botocore.auth import S3SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
+ s3 = _local_client("s3", *user_key)
+ bucket = f"lines-{time.time_ns()}"
+ s3.create_bucket(Bucket=bucket)
+ try:
+ request = AWSRequest(method="PUT", url=f"http://localhost:4566/{bucket}/k", data=b"x",
+ headers={"x-amz-meta-list": "a,b"})
+ S3SigV4Auth(Credentials(*user_key), "s3", "us-east-1").add_auth(request)
+ del request.headers["x-amz-meta-list"]
+ lines = [(b"x-amz-meta-list", b"a"), (b"x-amz-meta-list", b"b")]
+ assert _asgi_send(request.prepare(), lines).status_code == 200
+ finally:
+ s3.delete_object(Bucket=bucket, Key="k")
+ s3.delete_bucket(Bucket=bucket)
+
+ def test_s3_unsigned_and_chunked_payloads(self, user_key):
+ s3 = _local_client("s3", *user_key)
+ bucket = f"payloads-{time.time_ns()}"
+ s3.create_bucket(Bucket=bucket)
+ try:
+ unsigned = _local_client("s3", *user_key, payload_signing_enabled=False)
+ unsigned.put_object(Bucket=bucket, Key="unsigned", Body=b"abc")
+ wrong = _local_client("s3", user_key[0], "wrong-secret", payload_signing_enabled=False)
+ assert _failure(lambda: wrong.put_object(Bucket=bucket, Key="unsigned", Body=b"abc")) == (
+ "SignatureDoesNotMatch", 403)
+ url = f"http://localhost:4566/{bucket}/chunked"
+ assert _chunked_put(*user_key, url, b"hello chunks").status_code == 200
+ assert s3.get_object(Bucket=bucket, Key="chunked")["Body"].read() == b"hello chunks"
+ assert _chunked_put(user_key[0], "wrong-secret", url, b"x").status_code == 403
+ assert _chunked_put(*user_key, url, b"x", bad_chunk=True).status_code == 403
+ finally:
+ for name in ("unsigned", "chunked"):
+ s3.delete_object(Bucket=bucket, Key=name)
+ s3.delete_bucket(Bucket=bucket)
+
+ def test_root_key_signs_with_any_secret(self, user_key):
+ assert _local_client("dynamodb", "test", "anything").list_tables()["TableNames"] is not None
+
+ def test_signature_is_not_checked_without_auth(self, user_key, monkeypatch):
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", False)
+ _local_client("dynamodb", user_key[0], "wrong-secret").list_tables()
+
+
+class TestSessionTokens:
+ @pytest.fixture
+ def session(self, user_key):
+ return _local_client("sts", *user_key).get_session_token()["Credentials"]
+
+ @pytest.mark.parametrize("service,missing,wrong", [
+ ("sqs", ("InvalidClientTokenId", 403), ("InvalidClientTokenId", 403)),
+ ("dynamodb", ("UnrecognizedClientException", 400), ("UnrecognizedClientException", 400)),
+ ("lambda", ("UnrecognizedClientException", 403), ("UnrecognizedClientException", 403)),
+ ("s3", ("InvalidAccessKeyId", 403), ("InvalidToken", 400)),
+ ("ec2", ("AuthFailure", 401), ("AuthFailure", 401)),
+ ])
+ def test_session_key_needs_its_own_token(self, session, service, missing, wrong):
+ key, secret, token = session["AccessKeyId"], session["SecretAccessKey"], session["SessionToken"]
+ _LIST_CALLS[service](_local_client(service, key, secret, token))
+ assert _failure(lambda: _LIST_CALLS[service](_local_client(service, key, secret))) == missing
+ assert _failure(lambda: _LIST_CALLS[service](_local_client(service, key, secret, token + "x"))) == wrong
+
+ def test_wrong_token_message_has_no_full_stop(self, session):
+ client = _local_client("dynamodb", session["AccessKeyId"], session["SecretAccessKey"], "other")
+ with pytest.raises(ClientError) as exc:
+ client.list_tables()
+ assert exc.value.response["Error"]["Message"] == "The security token included in the request is invalid"
+
+ def test_long_term_key_refuses_a_token(self, user_key, session):
+ client = _local_client("iam", *user_key, session["SessionToken"])
+ assert _failure(client.list_users) == ("InvalidClientTokenId", 403)
+
+ def test_tokens_are_not_checked_without_auth(self, session, monkeypatch):
+ from ministack import app as app_mod
+
+ monkeypatch.setattr(app_mod, "AUTH", False)
+ _local_client("dynamodb", session["AccessKeyId"], session["SecretAccessKey"]).list_tables()
+
+
+def _signing_clock(seconds):
+ """Sign as if the client clock ran ``seconds`` ahead."""
+ import datetime as dt
+ from unittest import mock
+
+ import botocore.auth
+
+ now = botocore.auth.get_current_datetime() + dt.timedelta(seconds=seconds)
+ with mock.patch.object(botocore.auth, "get_current_datetime", return_value=now):
+ yield
+
+
+class TestClockSkew:
+ @pytest.mark.parametrize("service,minutes,code,status", [
+ ("sts", 15, "SignatureDoesNotMatch", 403),
+ ("dynamodb", 15, "InvalidSignatureException", 400),
+ ("s3", 15, "RequestTimeTooSkewed", 403),
+ ("lambda", 5, "InvalidSignatureException", 403),
+ ("ec2", 5, "AuthFailure", 401),
+ ])
+ def test_request_outside_the_services_window_is_refused(self, user_key, service, minutes, code, status):
+ call = _LIST_CALLS.get(service) or (lambda c: c.get_caller_identity())
+ client = _local_client(service, *user_key)
+ with _signing_clock(minutes * 60 - 60):
+ call(client)
+ with _signing_clock(-(minutes * 60 - 60)):
+ call(client)
+ with _signing_clock(minutes * 60 + 60):
+ assert _failure(lambda: call(client)) == (code, status)
+ with _signing_clock(-(minutes * 60 + 60)):
+ assert _failure(lambda: call(client)) == (code, status)
+
+ def test_message_names_both_times(self, user_key):
+ with _signing_clock(-1200), pytest.raises(ClientError) as exc:
+ _local_client("dynamodb", *user_key).list_tables()
+ assert re.fullmatch(r"Signature expired: \d{8}T\d{6}Z is now earlier than \d{8}T\d{6}Z "
+ r"\(\d{8}T\d{6}Z - 15 min\.\)", exc.value.response["Error"]["Message"])
+
+ def test_skew_is_reported_before_an_unknown_key(self, user_key):
+ with _signing_clock(1200):
+ client = _local_client("dynamodb", "AKIAUNKNOWNKEY000001", "secret")
+ assert _failure(client.list_tables) == ("InvalidSignatureException", 400)
+
+ def test_root_key_and_auth_off_ignore_the_clock(self, user_key, monkeypatch):
+ from ministack import app as app_mod
+
+ with _signing_clock(1200):
+ _local_client("dynamodb").list_tables()
+ monkeypatch.setattr(app_mod, "AUTH", False)
+ _local_client("dynamodb", *user_key).list_tables()
+
+
# ---------------------------------------------------------------------------
# AssumeRole trust policy: matching denies and STS request conditions (AUTH=true)
# ---------------------------------------------------------------------------
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 4bf50f78..315e7235 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -4906,6 +4906,12 @@ def test_lambda_cross_account_layer_verdicts(layer_name, granted_to, arn, attach
def test_lambda_cross_account_layer_under_auth_names_the_calling_user(monkeypatch):
"""Through the app with AUTH=true as an IAM user of the consuming account: the
layer policy decides, and a denial names that user the way AWS does."""
+ from urllib.parse import quote
+
+ from botocore.auth import SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
import ministack.app as app_mod
from ministack.services import iam as iam_svc
@@ -4932,13 +4938,15 @@ def test_lambda_cross_account_layer_under_auth_names_the_calling_user(monkeypatc
for granted_to in ("*", None):
sent.clear()
arn = _foreign_arn("auth-layer")
+ query = f"find=LayerVersion&Arn={quote(arn, safe='')}"
+ request = AWSRequest(method="GET", url=f"http://lambda.localhost/2018-10-31/layers?{query}")
+ SigV4Auth(Credentials(key, "s"), "lambda", _FOREIGN_REGION).add_auth(request)
with _foreign_layer("auth-layer", granted_to=granted_to):
asyncio.run(app_mod.app({
"type": "http", "method": "GET", "path": "/2018-10-31/layers",
- "query_string": f"find=LayerVersion&Arn={arn}".encode(),
- "headers": [(b"host", b"lambda.localhost"), (b"authorization", (
- f"AWS4-HMAC-SHA256 Credential={key}/20260915/{_FOREIGN_REGION}/lambda/aws4_request, "
- "SignedHeaders=host, Signature=0").encode())],
+ "query_string": query.encode(),
+ "headers": [(b"host", b"lambda.localhost")] + [
+ (name.lower().encode(), value.encode()) for name, value in request.headers.items()],
}, receive, send))
status, body = sent[0]["status"], json.loads(sent[1]["body"])
if granted_to:
diff --git a/tests/test_s3.py b/tests/test_s3.py
index 297e70f2..7e4a00c1 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -4225,6 +4225,50 @@ def test_s3_presign_verifies_in_both_auth_modes(monkeypatch, auth_enabled, bad_t
iam_svc._access_keys.pop_scoped(owner, None, key, None)
[email protected]("auth_enabled", [False, True])
[email protected]("expires,skew,change,status,code", [
+ (604800, 0, None, None, None),
+ (604801, 0, None, 400, "AuthorizationQueryParametersError"),
+ (3600, 840, None, None, None),
+ (3600, 960, None, 403, "Request is not yet valid"),
+ (3600, 0, ("X-Amz-Expires", "abc"), 400, "X-Amz-Expires should be a number"),
+ (3600, 0, ("X-Amz-Date", "2026"), 400, "X-Amz-Date must be in the ISO8601 Long Format"),
+])
+def test_s3_presign_time_limits_under_auth(monkeypatch, auth_enabled, expires, skew, change, status, code):
+ import datetime as dt
+ from unittest import mock
+ from urllib.parse import parse_qs, urlsplit
+
+ import boto3
+ import botocore.auth
+ from botocore.config import Config
+
+ from ministack import app as app_mod
+ from ministack.services import s3 as s3_svc
+
+ monkeypatch.setattr(app_mod, "AUTH", auth_enabled)
+ client = boto3.client(
+ "s3", endpoint_url="http://localhost:4566", region_name="us-east-1",
+ aws_access_key_id="test", aws_secret_access_key="test", config=Config(signature_version="s3v4"),
+ )
+ signed_at = botocore.auth.get_current_datetime() + dt.timedelta(seconds=skew)
+ with mock.patch.object(botocore.auth, "get_current_datetime", return_value=signed_at):
+ url = urlsplit(client.generate_presigned_url(
+ "get_object", Params={"Bucket": "test-bucket", "Key": "object"}, ExpiresIn=expires))
+ query = parse_qs(url.query)
+ if change:
+ query[change[0]] = [change[1]]
+
+ error = s3_svc._verify_presigned_sigv4("GET", url.path, {"host": url.netloc}, query)
+
+ if auth_enabled and status:
+ assert error[0] == status and code in error[2].decode()
+ elif change:
+ assert b"SignatureDoesNotMatch" in error[2]
+ else:
+ assert error is None
+
+
def test_presigned_mrap_resolves_alias_in_iam_owner_account(monkeypatch):
import asyncio
from urllib.parse import parse_qs, urlsplit
diff --git a/tests/test_sigv4.py b/tests/test_sigv4.py
index 308a9e0f..aaafdf23 100644
--- a/tests/test_sigv4.py
+++ b/tests/test_sigv4.py
@@ -9,7 +9,9 @@ from ministack.core.sigv4 import (
build_string_to_sign,
calculate_signature,
derive_signing_key,
+ parse_header_signature,
presigned_request_is_expired,
+ request_time_failure,
signatures_match,
uri_encode,
)
@@ -155,3 +157,66 @@ def test_presigned_request_expiry_rejects_malformed_values(amz_date, expires):
def test_signatures_match_accepts_only_equal_values():
assert signatures_match("abc123", "abc123")
assert not signatures_match("abc123", "abc124")
+
+
+_SCOPE = "AKIAIOSFODNN7EXAMPLE/20260930/eu-central-1/sts/aws4_request"
+
+
+ "authorization, code, message",
+ [
+ (
+ f"AWS4-HMAC-SHA256 Credential={_SCOPE}, Signature=abc",
+ "MissingAuthorizationParameter",
+ "Authorization header requires 'SignedHeaders' parameter. (Hashed with SHA-256 and encoded with Base64) "
+ "Authorization=NfAZ3F0AUnKOtT3DAo6CWylCcBLuRNI5BHzk96CvGwM=",
+ ),
+ (
+ "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/eu-central-1/sts/aws4_request, "
+ "SignedHeaders=host;x-amz-date, Signature=abc",
+ "IncompleteSignature",
+ "Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term, "
+ "got 'AKIAIOSFODNN7EXAMPLE/eu-central-1/sts/aws4_request'",
+ ),
+ ],
+)
+def test_parse_header_signature_names_the_missing_part_as_aws_does(authorization, code, message):
+ headers = {"authorization": authorization, "x-amz-date": "20260930T120000Z"}
+
+ assert parse_header_signature(headers) == (code, message)
+
+
+def test_parse_header_signature_takes_the_date_header_when_x_amz_date_is_absent():
+ headers = {
+ "authorization": f"AWS4-HMAC-SHA256 Credential={_SCOPE}, SignedHeaders=date;host, Signature=abc",
+ "date": "Wed, 30 Sep 2026 12:00:00 GMT",
+ }
+
+ parsed = parse_header_signature(headers)
+
+ assert (parsed.access_key_id, parsed.service, parsed.amz_date) == (
+ "AKIAIOSFODNN7EXAMPLE", "sts", "20260930T120000Z")
+
+
+ "service, amz_date, expected",
+ [
+ ("kms", "20260930T120500Z", None),
+ ("kms", "20260930T120501Z", "Signature not yet current: 20260930T120501Z is still later than "
+ "20260930T120500Z (20260930T120000Z + 5 min.)"),
+ ("sts", "20260930T114500Z", None),
+ ("sts", "20260930T114459Z", "Signature expired: 20260930T114459Z is now earlier than "
+ "20260930T114500Z (20260930T120000Z - 15 min.)"),
+ ],
+)
+def test_request_time_failure_uses_the_services_window(service, amz_date, expected):
+ headers = {
+ "authorization": f"AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/{amz_date[:8]}/us-east-1/{service}/aws4_request, "
+ "SignedHeaders=host;x-amz-date, Signature=abc",
+ "x-amz-date": amz_date,
+ }
+ now = dt.datetime(2026, 9, 30, 12, 0, tzinfo=dt.timezone.utc)
+
+ failure = request_time_failure(parse_header_signature(headers), now=now)
+
+ assert failure == (expected and ("RequestTimeTooSkewed", expected))
diff --git a/tests/test_sts.py b/tests/test_sts.py
index da6da5a6..1f8d6a81 100644
--- a/tests/test_sts.py
+++ b/tests/test_sts.py
@@ -777,22 +777,18 @@ def test_sts_get_caller_identity_json_protocol_resolves_iam_user(iam):
"""JSON protocol renders the resolved IAM-user identity, not root."""
import urllib.request
+ from botocore.auth import SigV4Auth
+ from botocore.awsrequest import AWSRequest
+ from botocore.credentials import Credentials
+
endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
with _iam_user_key(iam, "caller-identity-json-user") as (_, source):
- req = urllib.request.Request(
- endpoint,
- data=json.dumps({}).encode(),
- method="POST",
- headers={
- "Content-Type": "application/x-amz-json-1.1",
- "X-Amz-Target": "AWSSecurityTokenServiceV20110615.GetCallerIdentity",
- "Authorization": (
- "AWS4-HMAC-SHA256 "
- f"Credential={source['AccessKeyId']}/20240101/us-east-1/sts/aws4_request, "
- "SignedHeaders=host, Signature=fake"
- ),
- },
- )
+ signed = AWSRequest(method="POST", url=endpoint, data=json.dumps({}).encode(), headers={
+ "Content-Type": "application/x-amz-json-1.1",
+ "X-Amz-Target": "AWSSecurityTokenServiceV20110615.GetCallerIdentity",
+ })
+ SigV4Auth(Credentials(source["AccessKeyId"], source["SecretAccessKey"]), "sts", "us-east-1").add_auth(signed)
+ req = urllib.request.Request(endpoint, data=signed.body, method="POST", headers=dict(signed.headers))
with urllib.request.urlopen(req) as r:
assert r.status == 200
data = json.loads(r.read())