Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(s3): presign URL using credential (#1241)
Nahuel Nucera024ac81b393b
diff --git a/README.md b/README.mdindex fccdc019..c19034a1 100644--- a/README.md+++ b/README.md@@ -140,13 +140,13 @@ MiniStack supports lightweight multi-tenancy without any configuration. If the ````bash# Team A — gets account 111111111111export AWS_ACCESS_KEY_ID=111111111111-export AWS_SECRET_ACCESS_KEY=anything+export AWS_SECRET_ACCESS_KEY=testaws --endpoint-url=http://localhost:4566 sts get-caller-identity# → { "Account": "111111111111", ... }# Team B — gets account 222222222222export AWS_ACCESS_KEY_ID=222222222222-export AWS_SECRET_ACCESS_KEY=anything+export AWS_SECRET_ACCESS_KEY=testaws --endpoint-url=http://localhost:4566 sts get-caller-identity# → { "Account": "222222222222", ... }```diff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex 59743da3..2b0e9bea 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -27,6 +27,7 @@ import contextvarsimport copyimport datetime as _dtimport hashlib+import hmacimport jsonimport loggingimport os@@ -748,11 +749,122 @@ def _object_response_headers(obj: dict, bucket_name: str = "", key: str = "",# ---------------------------------------------------------------------------+_SIGV4_UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD"+++def _uri_encode(value: str, encode_slash: bool = True) -> str:+ """RFC3986 encoding per the SigV4 spec: unreserved chars (A-Za-z0-9-_.~)+ stay literal, everything else is percent-encoded. ``/`` is preserved in+ the canonical URI (path separators) and encoded everywhere else."""+ safe = "-_.~" + ("" if encode_slash else "/")+ return url_quote(value, safe=safe)+++def _sigv4_signing_key(secret: str, date_stamp: str, region: str, service: str) -> bytes:+ def _h(key, msg):+ return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()+ k_date = _h(("AWS4" + secret).encode("utf-8"), date_stamp)+ k_region = _h(k_date, region)+ k_service = _h(k_region, service)+ return _h(k_service, "aws4_request")+++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-+ signed and anonymous requests are handled elsewhere / left lax).++ MiniStack has no IAM secret store, so it verifies against its own secret+ (``AWS_SECRET_ACCESS_KEY``, default ``test``) — the same credential the+ server and its Lambda runtimes use. A URL signed with any other secret, or+ one whose signed headers (content-type, content-length, ...) were tampered+ with after signing, does not recompute to the same signature and is+ rejected with 403 SignatureDoesNotMatch, matching real S3.+ """+ signature = (_qp(query_params, "X-Amz-Signature", "")+ or _qp(query_params, "x-amz-signature", ""))+ if not signature:+ return None # not a presigned URL+ algorithm = (_qp(query_params, "X-Amz-Algorithm", "")+ or _qp(query_params, "x-amz-algorithm", ""))+ if algorithm != "AWS4-HMAC-SHA256":+ return None # only SigV4 presigned URLs are verified++ def _bad_signature():+ return _error(+ "SignatureDoesNotMatch",+ "The request signature we calculated does not match the signature "+ "you provided. Check your key and signing method.",+ 403, path,+ )++ credential = (_qp(query_params, "X-Amz-Credential", "")+ or _qp(query_params, "x-amz-credential", ""))+ amz_date = (_qp(query_params, "X-Amz-Date", "")+ or _qp(query_params, "x-amz-date", ""))+ signed_headers = (_qp(query_params, "X-Amz-SignedHeaders", "")+ or _qp(query_params, "x-amz-signedheaders", ""))+ cred_parts = credential.split("/")+ if len(cred_parts) != 5 or not amz_date or not signed_headers:+ return _bad_signature()+ _akid, date_stamp, region, service, _terminator = cred_parts++ # Canonical query string: every query param except X-Amz-Signature,+ # RFC3986-encoded, sorted by encoded key then value.+ pairs = []+ for name, values in query_params.items():+ if name.lower() == "x-amz-signature":+ continue+ vlist = values if isinstance(values, list) else [values]+ for v in vlist:+ pairs.append((_uri_encode(name), _uri_encode(v)))+ pairs.sort()+ canonical_qs = "&".join(f"{k}={v}" for k, v in pairs)++ # Canonical headers: the signed headers, lowercased names, trimmed values.+ canonical_headers = ""+ for hname in (h for h in signed_headers.split(";") if h):+ raw = headers.get(hname, headers.get(hname.lower(), ""))+ canonical_headers += f"{hname.lower()}:{' '.join(str(raw).split())}\n"++ canonical_request = "\n".join([+ method,+ _uri_encode(path, encode_slash=False),+ canonical_qs,+ canonical_headers,+ signed_headers,+ _SIGV4_UNSIGNED_PAYLOAD,+ ])++ string_to_sign = "\n".join([+ "AWS4-HMAC-SHA256",+ amz_date,+ f"{date_stamp}/{region}/{service}/aws4_request",+ hashlib.sha256(canonical_request.encode("utf-8")).hexdigest(),+ ])++ secret = os.environ.get("AWS_SECRET_ACCESS_KEY", "test")+ signing_key = _sigv4_signing_key(secret, date_stamp, region, service)+ computed = hmac.new(signing_key, string_to_sign.encode("utf-8"),+ hashlib.sha256).hexdigest()++ if not hmac.compare_digest(computed, signature):+ return _bad_signature()+ return None++async def handle_request(method: str, path: str, headers: dict, body: bytes, query_params: dict) -> tuple:bucket, key = _parse_bucket_key(path, headers)+ sig_error = _verify_presigned_sigv4(method, path, headers, query_params)+ if sig_error is not None:+ status, resp_headers, resp_body = sig_error+ resp_headers.setdefault("x-amz-request-id", new_uuid())+ resp_headers.setdefault("x-amz-id-2", base64.b64encode(os.urandom(48)).decode())+ return status, resp_headers, resp_body+result = _dispatch(method, bucket, key, headers, body, query_params)status, resp_headers, resp_body = resultdiff --git a/tests/test_s3.py b/tests/test_s3.pyindex d3ac434d..c778931a 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -2992,6 +2992,69 @@ def test_s3_get_object_non_latest_version_last_modified_is_rfc7231_http_date(s3))+def test_s3_presigned_url_signature_is_verified():+ """Presigned SigV4 URLs are verified against the server secret: a bogus-+ credential signature, or a signed header (content-type / content-length)+ tampered with after signing, is rejected with 403 SignatureDoesNotMatch —+ matching real S3. A valid, untampered presigned URL still succeeds."""+ import urllib.error+ import urllib.request++ import boto3+ from botocore.config import Config++ ep = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")+ # Explicit s3v4 + path style so ContentType / ContentLength are signed into+ # X-Amz-SignedHeaders (matches the reporter's config); otherwise boto3 would+ # not sign those headers and tampering them would be legitimately allowed.+ path_cfg = Config(signature_version="s3v4", s3={"addressing_style": "path"})+ good = boto3.client(+ "s3", endpoint_url=ep, region_name="us-east-1",+ aws_access_key_id="test", aws_secret_access_key="test", config=path_cfg,+ )+ bogus = boto3.client(+ "s3", endpoint_url=ep, region_name="us-east-1",+ aws_access_key_id="wrongkey", aws_secret_access_key="wrongsecret",+ config=path_cfg,+ )+ bucket = "presign-verify-bkt"+ good.create_bucket(Bucket=bucket)+ good.put_object(Bucket=bucket, Key="hello.txt", Body=b"hi")++ def status(req):+ try:+ return urllib.request.urlopen(req).status+ except urllib.error.HTTPError as exc:+ return exc.code++ # Bogus-credential signature is rejected.+ u = bogus.generate_presigned_url(+ "get_object", Params={"Bucket": bucket, "Key": "hello.txt"}, ExpiresIn=300)+ assert status(urllib.request.Request(u, method="GET")) == 403++ # Signed content-type tampered after signing is rejected.+ u = good.generate_presigned_url(+ "put_object",+ Params={"Bucket": bucket, "Key": "up.txt", "ContentType": "text/plain"},+ ExpiresIn=300)+ assert status(urllib.request.Request(+ u, data=b"x", method="PUT",+ headers={"Content-Type": "application/octet-stream"})) == 403++ # Signed content-length tampered after signing is rejected.+ u = good.generate_presigned_url(+ "put_object",+ Params={"Bucket": bucket, "Key": "up2.txt", "ContentLength": 2},+ ExpiresIn=300)+ assert status(urllib.request.Request(+ u, data=b"way more than two bytes", method="PUT")) == 403++ # A valid, untampered presigned URL still succeeds.+ u = good.generate_presigned_url(+ "get_object", Params={"Bucket": bucket, "Key": "hello.txt"}, ExpiresIn=300)+ assert status(urllib.request.Request(u, method="GET")) == 200++def test_s3_eventbridge_notification_on_delete(s3, sqs, eb):"""S3 delete_object should send EventBridge event when EventBridgeConfiguration is enabled."""bucket = "s3-eb-del-bkt"