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): xml statuses (#2253)
Nahuel Nucera9b9ebf398bba
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 25a8051b..e97fa065 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **S3 — bucket configuration validation** — `PutBucketVersioning` accepted any `Status` or `MfaDelete` and stored it, `PutBucketLifecycleConfiguration` accepted any rule `Status` or transition `StorageClass` and non-positive days, and `PutBucketEncryption` accepted any `SSEAlgorithm`. Out-of-enum values now return 400 `MalformedXML` (`The XML you provided was not well-formed or did not validate against our published schema`, now the message for every `MalformedXML`). A zero or negative `Expiration` `Days` or `NoncurrentVersionExpiration` `NoncurrentDays` returns 400 `InvalidArgument` (`'Days' for Expiration action must be a positive integer`).+## [1.5.29] — 2026-10-17### Addeddiff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex e026e4c8..e22442bb 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -360,6 +360,7 @@ _object_versions = AccountScopedDict() # (bucket, key) -> [{version_id, obj_rec_bucket_object_lock = AccountScopedDict()_bucket_replication = AccountScopedDict()+_MALFORMED_XML = "The XML you provided was not well-formed or did not validate against our published schema"_object_retention = AccountScopedDict()_object_legal_hold = AccountScopedDict()@@ -2815,11 +2816,15 @@ def _put_bucket_versioning(name: str, body: bytes):return _no_such_bucket(name)try:xml_root = fromstring(body)- status_el = _find_xml_tag(xml_root, "Status")- if status_el is not None and status_el.text:- _bucket_versioning[name] = status_el.textexcept Exception:- pass+ return _error("MalformedXML", _MALFORMED_XML, 400)+ status_el = _find_xml_tag(xml_root, "Status")+ mfa_el = _find_xml_tag(xml_root, "MfaDelete")+ if (status_el is not None and status_el.text not in ("Enabled", "Suspended")) or (+ mfa_el is not None and mfa_el.text not in ("Enabled", "Disabled")):+ return _error("MalformedXML", _MALFORMED_XML, 400)+ if status_el is not None:+ _bucket_versioning[name] = status_el.textreturn 200, {}, b""@@ -2844,9 +2849,18 @@ def _get_bucket_encryption(name: str):return 200, {"Content-Type": "application/xml"}, default+_SSE_ALGORITHMS = ("AES256", "aws:fsx", "aws:backup", "aws:kms", "aws:kms:dsse")++def _put_bucket_encryption(name: str, body: bytes):if name not in _buckets:return _no_such_bucket(name)+ try:+ root = fromstring(body)+ except Exception:+ return _error("MalformedXML", _MALFORMED_XML, 400)+ if any(el.tag.rsplit("}", 1)[-1] == "SSEAlgorithm" and el.text not in _SSE_ALGORITHMS for el in root.iter()):+ return _error("MalformedXML", _MALFORMED_XML, 400)_bucket_encryption[name] = body.decode("utf-8", errors="replace")return 200, {}, b""@@ -3109,10 +3123,31 @@ def _put_bucket_lifecycle(name: str, body: bytes):# Fallback: store raw if parsing fails_bucket_lifecycle[name] = body.decode("utf-8", errors="replace")return 200, {"x-amz-transition-default-minimum-object-size": "all_storage_classes_128K"}, b""+ err = _lifecycle_rules_error(rules)+ if err:+ return err_bucket_lifecycle[name] = rulesreturn 200, {"x-amz-transition-default-minimum-object-size": "all_storage_classes_128K"}, b""+_TRANSITION_STORAGE_CLASSES = ("GLACIER", "STANDARD_IA", "ONEZONE_IA", "INTELLIGENT_TIERING", "DEEP_ARCHIVE", "GLACIER_IR")+++def _lifecycle_rules_error(rules: list) -> tuple | None:+ """The refusals real S3 returns for an out-of-schema or non-positive lifecycle value."""+ for rule in rules:+ if rule.get("Status") not in ("Enabled", "Disabled") or any(+ t.get("StorageClass") not in _TRANSITION_STORAGE_CLASSES for t in rule.get("Transitions", [])):+ return _error("MalformedXML", _MALFORMED_XML, 400)+ if "Days" in rule.get("Expiration", {}) and rule["Expiration"]["Days"] <= 0:+ return _error("InvalidArgument", "'Days' for Expiration action must be a positive integer", 400)+ nve = rule.get("NoncurrentVersionExpiration", {})+ if "NoncurrentDays" in nve and nve["NoncurrentDays"] <= 0:+ return _error("InvalidArgument",+ "'NoncurrentDays' for NoncurrentVersionExpiration action must be a positive integer", 400)+ return None++def _delete_bucket_lifecycle(name: str):if name not in _buckets:return _no_such_bucket(name)@@ -3225,7 +3260,7 @@ def _put_bucket_tagging(name: str, body: bytes):try:tags = _parse_tags_xml(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)if len(tags) > 50:return _error("BadRequest", "Object tags cannot be greater than 50", 400)_bucket_tags[name] = tags@@ -6142,7 +6177,7 @@ def _put_object_tagging(bucket_name: str, key: str, body: bytes, query_params: dtry:tags = _parse_tags_xml(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)if len(tags) > 10:return _error("BadRequest", "Object tags cannot be greater than 10", 400)gone = _no_such_version(bucket_name, key, query_params)@@ -6664,30 +6699,30 @@ def _put_object_lock_configuration(bucket_name: str, body: bytes):try:xml_root = fromstring(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)enabled_el = _find_xml_tag(xml_root, "ObjectLockEnabled")if enabled_el is None or enabled_el.text != "Enabled":- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)default_retention = Nonerule_el = _find_xml_tag(xml_root, "Rule")if rule_el is not None:ret_el = _find_xml_tag(rule_el, "DefaultRetention")if ret_el is None:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)mode_el = _find_xml_tag(ret_el, "Mode")days_el = _find_xml_tag(ret_el, "Days")years_el = _find_xml_tag(ret_el, "Years")if mode_el is None or mode_el.text not in ("GOVERNANCE", "COMPLIANCE"):- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)has_days = days_el is not None and days_el.texthas_years = years_el is not None and years_el.textif (has_days and has_years) or (not has_days and not has_years):- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)default_retention = {"Mode": mode_el.text}try:@@ -6696,7 +6731,7 @@ def _put_object_lock_configuration(bucket_name: str, body: bytes):if has_years:default_retention["Years"] = int(years_el.text)except (ValueError, TypeError):- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)_bucket_object_lock[bucket_name] = {"enabled": True,@@ -6757,15 +6792,15 @@ def _put_object_retention(bucket_name: str, key: str, body: bytes, headers: dicttry:xml_root = fromstring(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)mode_el = _find_xml_tag(xml_root, "Mode")date_el = _find_xml_tag(xml_root, "RetainUntilDate")if mode_el is None or mode_el.text not in ("GOVERNANCE", "COMPLIANCE"):- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)if date_el is None or not date_el.text:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)retain_until = date_el.text@@ -6837,11 +6872,11 @@ def _put_object_legal_hold(bucket_name: str, key: str, body: bytes, query_paramstry:xml_root = fromstring(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)status_el = _find_xml_tag(xml_root, "Status")if status_el is None or status_el.text not in ("ON", "OFF"):- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)_object_legal_hold[lock_key] = status_el.textreturn 200, {"Content-Type": "application/xml"}, b""@@ -7085,7 +7120,7 @@ def _put_bucket_replication(bucket_name: str, body: bytes):try:xml_root = fromstring(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)role_el = _find_xml_tag(xml_root, "Role")role = role_el.text if role_el is not None and role_el.text else ""@@ -7145,7 +7180,7 @@ def _put_bucket_replication(bucket_name: str, body: bytes):rules.append(rule)if not rules:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)_bucket_replication[bucket_name] = {"Role": role, "Rules": rules}return 200, {"Content-Type": "application/xml"}, b""@@ -7394,7 +7429,7 @@ def _delete_objects(bucket_name: str, body: bytes, headers: dict = None):try:xml_root = fromstring(body)except Exception:- return _error("MalformedXML", "The XML you provided was not well-formed", 400)+ return _error("MalformedXML", _MALFORMED_XML, 400)quiet = Falsequiet_el = _find_xml_tag(xml_root, "Quiet")@@ -8694,7 +8729,7 @@ def _mrap_async(op_segment: str, body: bytes, signed_region: str | None) -> tupltry:root = fromstring(body)except Exception:- return s3control_error("MalformedXML", "The XML you provided was not well-formed", 400)+ return s3control_error("MalformedXML", _MALFORMED_XML, 400)details = _child(root, "Details")name = _child_text(details, "Name")token_arn = (f"arn:aws:s3:{signed_region or get_region()}:{get_account_id()}:async-request/mrap/"@@ -8822,7 +8857,7 @@ def _mrap_submit_routes(record: dict, body: bytes) -> tuple:try:root = fromstring(body)except Exception:- return s3control_error("MalformedXML", "The XML you provided was not well-formed", 400)+ return s3control_error("MalformedXML", _MALFORMED_XML, 400)routes = mrap_routes(record)by_region = {bucket_region(b): b for b in routes}updated = dict(routes)@@ -8988,7 +9023,7 @@ def _access_point_create_request(name: str, body: bytes) -> tuple:try:root = fromstring(body) if body else Element("CreateAccessPointRequest")except Exception:- return s3control_error("MalformedXML", "The XML you provided was not well-formed", 400)+ return s3control_error("MalformedXML", _MALFORMED_XML, 400)vpc_el = _child(root, "VpcConfiguration")tags_el = _child(root, "Tags")try:@@ -9029,7 +9064,7 @@ def _access_point_policy(name: str, method: str, body: bytes) -> tuple:try:record["Policy"] = _child_text(fromstring(body), "Policy")except Exception:- return s3control_error("MalformedXML", "The XML you provided was not well-formed", 400)+ return s3control_error("MalformedXML", _MALFORMED_XML, 400)return 200, {"Content-Type": "application/xml"}, b""if method == "DELETE":record["Policy"] = None@@ -9207,7 +9242,7 @@ def _object_lambda_request(name: str, method: str, body: bytes, sub: str) -> tupexcept AccessPointError as exc:return exc.response()except Exception:- return s3control_error("MalformedXML", "The XML you provided was not well-formed", 400)+ return s3control_error("MalformedXML", _MALFORMED_XML, 400)out = Element("CreateAccessPointForObjectLambdaResult", xmlns=S3CONTROL_NS)SubElement(out, "ObjectLambdaAccessPointArn").text = object_lambda_arn(name)_object_lambda_alias_xml(out, record)diff --git a/tests/test_s3.py b/tests/test_s3.pyindex 5fbca136..a2cc895c 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -9535,3 +9535,45 @@ def test_s3_copy_and_multipart_refuse_object_lock_headers_without_object_lock(s3s3.delete_object(Bucket=bkt, Key=v["Key"], VersionId=v["VersionId"])s3.delete_bucket(Bucket=bkt)+++def test_s3_bucket_config_enum_and_day_validation(s3):+ bucket = f"cfgval-{uuid.uuid4().hex[:10]}"+ s3.create_bucket(Bucket=bucket)+ malformed = "The XML you provided was not well-formed or did not validate against our published schema"++ def err(fn):+ with pytest.raises(ClientError) as exc:+ fn()+ return exc.value.response["Error"]["Code"], exc.value.response["Error"]["Message"]++ for status in ("Disabled", "banana", "enabled"):+ assert err(lambda: s3.put_bucket_versioning(Bucket=bucket, VersioningConfiguration={"Status": status})) == (+ "MalformedXML", malformed)+ assert err(lambda: s3.put_bucket_versioning(+ Bucket=bucket, VersioningConfiguration={"Status": "Enabled", "MFADelete": "banana"}))[0] == "MalformedXML"+ assert "Status" not in s3.get_bucket_versioning(Bucket=bucket)++ def rule(**kw):+ return {"Rules": [{"ID": "r", "Status": "Enabled", "Filter": {"Prefix": ""}, **kw}]}++ for days in (0, -1):+ assert err(lambda: s3.put_bucket_lifecycle_configuration(+ Bucket=bucket, LifecycleConfiguration=rule(Expiration={"Days": days}))) == (+ "InvalidArgument", "'Days' for Expiration action must be a positive integer")+ assert err(lambda: s3.put_bucket_lifecycle_configuration(+ Bucket=bucket, LifecycleConfiguration=rule(NoncurrentVersionExpiration={"NoncurrentDays": 0}))) == (+ "InvalidArgument", "'NoncurrentDays' for NoncurrentVersionExpiration action must be a positive integer")+ assert err(lambda: s3.put_bucket_lifecycle_configuration(Bucket=bucket, LifecycleConfiguration={"Rules": [+ {"ID": "r", "Status": "banana", "Filter": {"Prefix": ""}, "Expiration": {"Days": 1}}]}))[0] == "MalformedXML"+ assert err(lambda: s3.put_bucket_lifecycle_configuration(+ Bucket=bucket, LifecycleConfiguration=rule(Transitions=[{"Days": 30, "StorageClass": "banana"}])))[0] == "MalformedXML"+ assert err(lambda: s3.put_bucket_encryption(Bucket=bucket, ServerSideEncryptionConfiguration={"Rules": [+ {"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "banana"}}]}))[0] == "MalformedXML"++ s3.put_bucket_versioning(Bucket=bucket, VersioningConfiguration={"Status": "Suspended"})+ s3.put_bucket_lifecycle_configuration(Bucket=bucket, LifecycleConfiguration=rule(+ Expiration={"Days": 1}, Transitions=[{"Days": 30, "StorageClass": "GLACIER_IR"}]))+ s3.put_bucket_encryption(Bucket=bucket, ServerSideEncryptionConfiguration={"Rules": [+ {"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "aws:kms:dsse"}}]})+ s3.delete_bucket(Bucket=bucket)