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/accept unquoted Content-Disposition field names in PostObject (#537)
Nahuel Nucera287b9f5a5a34
diff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex f5f96241..77790200 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -1737,11 +1737,14 @@ def _parse_multipart_form(content_type: str, body: bytes) -> list[tuple]:k, _, v = line.partition(b":")part_headers[k.strip().decode("latin-1").lower()] = v.strip().decode("latin-1")cd = part_headers.get("content-disposition", "")- name_m = _re.search(r'name="([^"]*)"', cd)- fn_m = _re.search(r'filename="([^"]*)"', cd)+ # RFC 2183 allows both quoted-string ('name="x"') and token ('name=x')+ # forms. .NET's MultipartFormDataContent emits the token form for+ # ASCII-clean values; real S3 accepts both, so we do too.+ name_m = _re.search(r'name=(?:"([^"]*)"|([^;\s]+))', cd)+ fn_m = _re.search(r'filename=(?:"([^"]*)"|([^;\s]+))', cd)out.append((- name_m.group(1) if name_m else "",- fn_m.group(1) if fn_m else None,+ (name_m.group(1) or name_m.group(2)) if name_m else "",+ (fn_m.group(1) or fn_m.group(2)) if fn_m else None,part_headers,value,))diff --git a/tests/test_s3.py b/tests/test_s3.pyindex 4de70706..d2d3bf44 100644--- a/tests/test_s3.py+++ b/tests/test_s3.py@@ -1449,6 +1449,32 @@ def test_s3_post_object_storage_class(s3):assert s3.get_object(Bucket="qa-s3-post-sc", Key="cold")["StorageClass"] == "GLACIER"+def test_s3_post_object_unquoted_field_names(s3):+ """`Content-Disposition: form-data; name=key` (token form) is accepted —+ .NET's MultipartFormDataContent emits this rather than the quoted form,+ and real S3 accepts both per RFC 2183."""+ import requests+ s3.create_bucket(Bucket="qa-s3-post-tok")+ boundary = "----testboundary"+ body = (+ f"--{boundary}\r\n"+ f"Content-Disposition: form-data; name=key\r\n\r\n"+ f"hello.txt\r\n"+ f"--{boundary}\r\n"+ f"Content-Disposition: form-data; name=file; filename=hello.txt\r\n"+ f"Content-Type: application/octet-stream\r\n\r\n"+ f"hello world\r\n"+ f"--{boundary}--\r\n"+ ).encode()+ r = requests.post(+ f"http://localhost:4566/qa-s3-post-tok",+ data=body,+ headers={"Content-Type": f"multipart/form-data; boundary={boundary}"},+ )+ assert r.status_code == 204, r.text+ assert s3.get_object(Bucket="qa-s3-post-tok", Key="hello.txt")["Body"].read() == b"hello world"++def test_s3_post_object_content_length_range_enforced(s3):"""`content-length-range` condition rejects oversize uploads with EntityTooLarge."""import requests