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(ses): validate email fields while accepting present empty content (#2061)
Dzmitry Shchamialiou9f038de1e10b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0084d0cf..badc4a5b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **SES**: validate source/recipient addresses, content-field presence and configuration sets while accepting present empty content and ASCII/Punycode addresses.- **Container reaping — one unreadable container no longer stops the sweep** — the boot sweep and the periodic reaper now list containers without inspecting each one, so a container whose inspect fails is skipped instead of leaving every leftover running. Reported by @iot-rocket.- **API Gateway — management IAM actions and resource paths** — with `AUTH=true`, REST and HTTP/WebSocket API management requests use `apigateway:GET`, `POST`, `PUT`, `PATCH` and `DELETE` instead of SDK operation names. Resource ARNs preserve stage names, nested resources and collection paths, and ignore trailing slashes as AWS does, so scoped grants and explicit stage denies apply to the requested resource.- **AppConfig — `GetLatestConfiguration` returns an empty body when nothing changed** — it returned the full configuration on every poll. It now also returns `Version-Label` and uses `RequiredMinimumPollIntervalInSeconds` as the poll interval, and `CreateHostedConfigurationVersion` stores `VersionLabel`. Contributed by @Jolley71717.diff --git a/ministack/services/ses.py b/ministack/services/ses.pyindex 2eace3d1..815e51bb 100644--- a/ministack/services/ses.py+++ b/ministack/services/ses.py@@ -478,7 +478,18 @@ def _send_email(params):to_addrs = _collect_list(params, "Destination.ToAddresses.member")cc_addrs = _collect_list(params, "Destination.CcAddresses.member")bcc_addrs = _collect_list(params, "Destination.BccAddresses.member")- rejected = _message_rejection(source, to_addrs + cc_addrs + bcc_addrs)+ recipients = to_addrs + cc_addrs + bcc_addrs+ if not source or not _valid_email_address(source):+ return _error("InvalidParameterValue", "The address specified is not valid.", 400)+ if not recipients or any(not _valid_email_address(address) for address in recipients):+ return _error("InvalidParameterValue", "The destination address is not valid.", 400)+ if "Message.Subject.Data" not in params:+ return _error("InvalidParameterValue", "The Subject field is required.", 400)+ if "Message.Body.Text.Data" not in params and "Message.Body.Html.Data" not in params:+ return _error("InvalidParameterValue", "The message body must contain Text or Html content.", 400)+ if config_set and config_set not in _configuration_sets:+ return _error("ConfigurationSetDoesNotExist", f"Configuration set {config_set} does not exist.", 400)+ rejected = _message_rejection(source, recipients)if rejected:return _error("MessageRejected", rejected, 400)@@ -497,6 +508,17 @@ def _send_email(params):f"<SendEmailResult><MessageId>{msg_id}</MessageId></SendEmailResult>")+def _valid_email_address(value: str) -> bool:+ if not value.isascii():+ return False+ address = parseaddr(value)[1]+ if not address or len(address) > 320:+ return False+ local, separator, domain = address.rpartition("@")+ return bool(separator and local and domain and "." in domain+ and not any(character.isspace() for character in address))++def _record_send(source, to_addrs, cc_addrs=None, bcc_addrs=None,subject="", body_text="", body_html="",type_name="SendEmail", config_set="", extra=None):diff --git a/tests/test_ses.py b/tests/test_ses.pyindex 96b2f524..0c3e4f39 100644--- a/tests/test_ses.py+++ b/tests/test_ses.py@@ -61,6 +61,63 @@ def test_ses_send(ses):)assert "MessageId" in resp+[email protected]("subject", ["", " "])+def test_ses_send_email_accepts_present_empty_content(monkeypatch, subject):+ from ministack.services import ses as ses_service++ monkeypatch.setattr(ses_service, "_message_rejection", lambda *args: None)+ monkeypatch.setattr(ses_service, "_record_send", lambda **kwargs: "empty-content")+ params = {+ "Source": ["[email protected]"],+ "Destination.ToAddresses.member.1": ["[email protected]"],+ "Message.Subject.Data": [subject],+ "Message.Body.Text.Data": [""],+ }+ assert ses_service._send_email(params)[0] == 200+ del params["Message.Subject.Data"]+ assert ses_service._send_email(params)[0] == 400+ params["Message.Subject.Data"] = [subject]+ del params["Message.Body.Text.Data"]+ assert ses_service._send_email(params)[0] == 400+++def test_ses_email_address_requires_ascii():+ from ministack.services.ses import _valid_email_address++ assert not _valid_email_address("user\[email protected]")+ assert not _valid_email_address("user@\u00e9xample.com")+ assert _valid_email_address("[email protected]")+++def test_ses_send_email_rejects_invalid_address(ses):+ ses.verify_email_identity(EmailAddress="[email protected]")+ with pytest.raises(ClientError) as exc:+ ses.send_email(+ Source="[email protected]",+ Destination={"ToAddresses": ["not-an-email"]},+ Message={+ "Subject": {"Data": "Invalid address"},+ "Body": {"Text": {"Data": "body"}},+ },+ )+ assert exc.value.response["Error"]["Code"] == "InvalidParameterValue"+++def test_ses_send_email_rejects_missing_configuration_set(ses):+ ses.verify_email_identity(EmailAddress="[email protected]")+ with pytest.raises(ClientError) as exc:+ ses.send_email(+ Source="[email protected]",+ Destination={"ToAddresses": ["[email protected]"]},+ Message={+ "Subject": {"Data": "Configuration"},+ "Body": {"Text": {"Data": "body"}},+ },+ ConfigurationSetName="missing-set",+ )+ assert exc.value.response["Error"]["Code"] == "ConfigurationSetDoesNotExist"+def test_ses_list_identities(ses):ses.verify_email_identity(EmailAddress="[email protected]")resp = ses.list_identities()