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
feat(sqs): reject XML 1.0 forbidden characters in message bodies (#987)
Yusuke Yamada030a9d2ab463
diff --git a/ministack/services/sqs.py b/ministack/services/sqs.pyindex d81f6f6c..7b207f4a 100644--- a/ministack/services/sqs.py+++ b/ministack/services/sqs.py@@ -28,6 +28,7 @@ import hashlibimport jsonimport loggingimport os+import reimport structimport threadingimport time@@ -39,6 +40,9 @@ from ministack.core.responses import AccountScopedDict, get_account_id, get_regilogger = logging.getLogger("sqs")+# XML 1.0 forbidden characters: everything below #x20 except #x9/#xA/#xD, plus #xFFFE and #xFFFF+_INVALID_SQS_CHARS_RE = re.compile("[\x00-\x08\x0b\x0c\x0e-\x1f\ufffe\uffff]")+# ── Module-level state ──────────────────────────────────────_queues = AccountScopedDict()@@ -331,6 +335,11 @@ def _act_send_message(data: dict, qurl: str) -> dict:if not body_text:raise _Err("MissingParameter","The request must contain the parameter MessageBody.")+ if _INVALID_SQS_CHARS_RE.search(body_text):+ raise _Err(+ "InvalidMessageContents",+ "The message contains characters outside the allowed set.",+ )# AWS SQS rejects messages exceeding the queue's MaximumMessageSize attribute# (default 262144 bytes; configurable up to 1 MiB / 1048576). Real AWS errordiff --git a/tests/test_sqs.py b/tests/test_sqs.pyindex bcfedd0a..3972e007 100644--- a/tests/test_sqs.py+++ b/tests/test_sqs.py@@ -1084,3 +1084,53 @@ def test_sqs_create_queue_rejects_non_numeric_visibility_timeout(sqs):assert exc.response["Error"]["Code"] == "InvalidAttributeValue"else:raise AssertionError("expected InvalidAttributeValue for non-numeric")+++def test_sqs_send_message_rejects_control_chars(sqs):+ """SendMessage must reject message bodies containing XML 1.0 forbidden characters.++ AWS SQS only allows: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]+ Control characters like NULL, BEL, VT, etc. must result in InvalidMessageContents (400).+ See: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/quotas-messages.html+ """+ url = sqs.create_queue(QueueName="intg-sqs-invalid-chars")["QueueUrl"]++ forbidden = [+ "\x00", # NULL+ "\x01", # SOH+ "\x08", # BS (last before tab)+ "\x0b", # VT (vertical tab)+ "\x0c", # FF (form feed)+ "\x0e", # SO (first after CR)+ "\x1f", # US (last C0 control char)+ "hello\x00world", # control char embedded in normal text+ "\ufffe", # non-character+ "\uffff", # non-character+ ]+ for body in forbidden:+ with pytest.raises(ClientError) as exc:+ sqs.send_message(QueueUrl=url, MessageBody=body)+ code = exc.value.response["Error"]["Code"]+ assert code == "InvalidMessageContents", (+ f"expected InvalidMessageContents for {repr(body)}, got {code}"+ )+ assert exc.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400+++def test_sqs_send_message_allows_valid_chars(sqs):+ """SendMessage must allow all XML 1.0 valid characters including tab, LF, CR, and Unicode."""+ url = sqs.create_queue(QueueName="intg-sqs-valid-chars")["QueueUrl"]++ allowed = [+ "hello world",+ "tab\there",+ "newline\nhere",+ "cr\rhere",+ "こんにちは世界",+ "héllo wörld",+ "emoji \U0001f600",+ "!@#$%^&*()",+ ]+ for body in allowed:+ resp = sqs.send_message(QueueUrl=url, MessageBody=body)+ assert "MessageId" in resp, f"send failed for {repr(body)}"