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(signer): check signing material in PutSigningProfile under AUTH=true (#2151)
iot-rocketaac3752bd479
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f1e91407..89953c7f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **Signer — signing material checks in `PutSigningProfile` under `AUTH=true`** — `PutSigningProfile` accepted any certificate ARN, or no `signingMaterial` at all, on every platform; with `AUTH=true` the IoT and FreeRTOS platforms now refuse a profile without `signingMaterial` and a certificate ARN that is malformed, in another region or account, unknown to ACM or not issued, and the Lambda and Notation platforms refuse any `signingMaterial`, with AWS's errors. Contributed by @iot-rocket.+### Added- **IoT — MQTT publish, subscribe and receive evaluate IoT policies under `AUTH=true`** — after the CONNECT a certificate session could publish, subscribe and receive on any topic; it now needs `iot:Publish` on the topic (and `iot:RetainPublish` for a retained message or will), `iot:Subscribe` per topic filter and `iot:Receive` per delivered message. A refused PUBLISH or SUBSCRIBE closes an MQTT 3.1.1 connection (disconnect reason `CLIENT_ERROR`), MQTT 5 answers PUBACK 0x87, DISCONNECT 0x87 at QoS 0 (`AUTH_ERROR`) or 0x87 for the refused filter, which the `subscribed` event leaves out (no event when none is granted), a refused will refuses the CONNECT, and a message the subscriber may not receive is dropped. Contributed by @iot-rocket.diff --git a/ministack/services/signer.py b/ministack/services/signer.pyindex fd6d96d4..a7e749fa 100644--- a/ministack/services/signer.py+++ b/ministack/services/signer.py@@ -45,6 +45,11 @@ Behaviour that follows the live service (measured 2026-08-26 unless noted):`signingParameters`, and `signatureExpiresAt` = createdAt plus theprofile's `signatureValidityPeriod` (default 135 months, per thePutSigningProfile reference).+ * Under AUTH=true, PutSigningProfile on the IoT and FreeRTOS platforms+ refuses a profile without `signingMaterial` and a certificate ARN that+ is malformed, in another region or account, unknown to ACM or not+ issued; on the Lambda and Notation platforms it refuses any+ `signingMaterial`. Without AUTH all of these are accepted.Deliberate divergences from AWS, each pinned by a test:@@ -97,6 +102,7 @@ from datetime import datetime, timedelta, timezoneimport ministack.services.acm as acm_svcimport ministack.services.s3 as s3_svc+from ministack.core.arn import ArnParseError, parse_arnfrom ministack.core.responses import (REST_JSON_CONTENT_TYPE,AccountRegionScopedDict,@@ -178,6 +184,10 @@ _LAMBDA_PLATFORM_ID = "AWSLambda-SHA384-ECDSA"_IOT_PLATFORM_ID = "AWSIoTDeviceManagement-SHA256-ECDSA"# Platforms that sign with the profile's ACM certificate on AWS._ACM_SIGNED_PLATFORMS = (_IOT_PLATFORM_ID, "AmazonFreeRTOS-Default", "AmazonFreeRTOS-TI-CC3220SF")+# Platforms that sign with Signer's own keys and take no signingMaterial.+_SIGNER_KEY_PLATFORMS = (_LAMBDA_PLATFORM_ID, "Notation-OCI-SHA384-ECDSA")+_CERTIFICATE_RESOURCE_RE = re.compile(+ r"certificate/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")def _now():@@ -282,7 +292,7 @@ def _validate_overrides(overrides):def _validate_signing_material(material):- """certificateArn is required; StartSigningJob resolves it, so an ARN ACM does not hold is accepted."""+ """certificateArn is required; the certificate itself is checked only under AUTH=true."""if material is None:return Noneif not isinstance(material, dict):@@ -294,6 +304,45 @@ def _validate_signing_material(material):return None+def _signing_material_problem(platform_id, material):+ """The refusal of signingMaterial the platform cannot use, checked before the profile name."""+ if platform_id in _SIGNER_KEY_PLATFORMS:+ if material is None:+ return None+ return _coded_error(400, "ValidationException", "InvalidProfileParameters",+ f"Signing material should not be present when using {platform_id} platform.")+ if platform_id not in _ACM_SIGNED_PLATFORMS:+ return None+ if material is None:+ return _coded_error(400, "ValidationException", "ValidationException",+ "signing material cannot be null.")+ arn = material["certificateArn"]+ try:+ spec = parse_arn(arn)+ except ArnParseError:+ spec = None+ if (spec is None or spec.service != "acm" or spec.region != get_region()+ or not _ACCOUNT_ID_RE.fullmatch(spec.account_id)+ or not _CERTIFICATE_RESOURCE_RE.fullmatch(spec.resource)):+ return _coded_error(400, "ValidationException", "ValidationException",+ f"{arn} is not a valid ACM certificate ARN")+ if spec.account_id != get_account_id():+ return _coded_error(400, "ValidationException", "ValidationException",+ f"{arn} is not owned by {get_account_id()}")+ return None+++def _certificate_problem(arn):+ """The refusal of a certificate ACM does not hold or has not issued, checked after the profile name."""+ cert = acm_svc._get_local_certificate(arn)+ if cert is None:+ return _coded_error(404, "ResourceNotFoundException", "CertificateNotFound",+ f"Could not find an issued certificate {arn}")+ if cert.get("Status") != "ISSUED":+ return _coded_error(400, "ValidationException", "ValidationException", "Certificate is not Issued")+ return None++def _validate_tags(tags):"""The map constraints PutSigningProfile documents: at most 200 entries,keys 1..128 matching `^(?!aws:)[a-zA-Z+-=._:/]+$`, values at most 256."""@@ -812,10 +861,19 @@ def _put_signing_profile(name, body):_validate_tags(body.get("tags"))):if problem is not None:return problem+ from ministack.app import AUTH+ platform_id, material = body["platformId"], body.get("signingMaterial")+ problem = _signing_material_problem(platform_id, material) if AUTH else None+ if problem is not None:+ return problemif name in _profiles:# A profile name is taken for good, Active or Canceled.return _coded_error(400, "ValidationException", "ProfileAlreadyExists",f"Profile with name {name} already exists")+ if AUTH and platform_id in _ACM_SIGNED_PLATFORMS:+ problem = _certificate_problem(material["certificateArn"])+ if problem is not None:+ return problemprofile = _register_profile(name, body)return json_response({"arn": profile["arn"],diff --git a/tests/test_signer.py b/tests/test_signer.pyindex b3e1dffb..cf71d2f4 100644--- a/tests/test_signer.py+++ b/tests/test_signer.py@@ -66,11 +66,13 @@ def buckets(s3):@pytest.fixture()-def profile(signer):+def profile(signer, acm_client):"""A signing profile on the IoT platform (the one whose signed-object- key is exactly prefix + jobId)."""+ key is exactly prefix + jobId), with a requested certificate, which has no key here."""name = f"profile_{_uid()}"- signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM)+ cert_arn = acm_client.request_certificate(DomainName=f"{name}.example.invalid")["CertificateArn"]+ signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM,+ signingMaterial={"certificateArn": cert_arn})return name@@ -200,13 +202,14 @@ def test_signer_iot_platform_rsa_certificate_fails_the_job(signer, s3, acm_clienassert job_id in [j["jobId"] for j in failed]-def test_signer_iot_platform_without_an_imported_key_writes_the_receipt(signer, s3, buckets):+def test_signer_iot_platform_without_an_imported_key_writes_the_receipt(signer, s3, acm_client, buckets):"""Control: with no key in ACM for the certificate ARN the receipt is written as before."""src, dst = bucketss3.put_object(Bucket=src, Key="r.cfg", Body=b"r")name = f"receipt_{_uid()}"+ cert_arn = acm_client.request_certificate(DomainName=f"{name}.example.invalid")["CertificateArn"]signer.put_signing_profile(profileName=name, platformId=_IOT_PLATFORM,- signingMaterial={"certificateArn": _CERT_ARN})+ signingMaterial={"certificateArn": cert_arn})job_id = _start(signer, src, dst, key="r.cfg", profile=name)["jobId"]@@ -636,13 +639,13 @@ def test_signer_describe_job_succeeded_with_signed_object(signer, s3, buckets, passert job["requestedBy"]-def test_signer_describe_job_carries_the_profile_material_and_expiry(signer, s3, buckets):+def test_signer_describe_job_carries_the_profile_material_and_expiry(signer, s3, acm_client, buckets):"""signingMaterial, overrides and signingParameters come from theprofile; signatureExpiresAt is createdAt plus the profile'ssignatureValidityPeriod."""src, dst = bucketsname = f"material_{_uid()}"- cert = "arn:aws:acm:us-east-1:000000000000:certificate/9ec626ca-0bbb-4be5-83a2-ee563f8386ca"+ cert = acm_client.request_certificate(DomainName=f"{name}.example.invalid")["CertificateArn"]signer.put_signing_profile(profileName=name,platformId=_IOT_PLATFORM,@@ -1205,7 +1208,7 @@ def test_signer_put_profile_accepts_a_partial_validity_period(signer):name = f"partial_{_uid()}"signer.put_signing_profile(profileName=name,- platformId=_IOT_PLATFORM,+ platformId=_LAMBDA_PLATFORM,signatureValidityPeriod=period,)assert signer.get_signing_profile(profileName=name)["profileName"] == name@@ -1223,7 +1226,7 @@ def test_signer_put_profile_rejects_an_unknown_override_enum(signer):with pytest.raises(ClientError) as exc:signer.put_signing_profile(profileName=f"ovr_{_uid()}",- platformId=_IOT_PLATFORM,+ platformId=_LAMBDA_PLATFORM,overrides=overrides,)assert exc.value.response["Error"]["Code"] == "ValidationException"@@ -1232,7 +1235,7 @@ def test_signer_put_profile_rejects_an_unknown_override_enum(signer):name = f"ovr_{_uid()}"signer.put_signing_profile(profileName=name,- platformId=_IOT_PLATFORM,+ platformId=_LAMBDA_PLATFORM,overrides={"signingImageFormat": "JSONDetached","signingConfiguration": {"encryptionAlgorithm": "ECDSA",@@ -1258,6 +1261,130 @@ def test_signer_put_profile_requires_the_signing_material_certificate(signer):assert exc.value.response["Error"]["Code"] == "ValidationException"+_SCOPE = ("000000000000", "eu-central-1")+_SCOPED_CERT = "arn:aws:acm:eu-central-1:{account}:certificate/{id}"+++def in_process(monkeypatch):+ """PutSigningProfile and ACM ImportCertificate in this process under AUTH=true; yields (put, certificate)."""+ from ministack import app as app_mod+ from ministack.core.responses import request_scope+ from ministack.services import acm as acm_module+ from ministack.services import signer as signer_module++ monkeypatch.setattr(app_mod, "AUTH", True)+ names, arns = [], []++ def put(platform, arn=None, name=None):+ name = name or f"auth_{_uid()}"+ names.append(name)+ body = {"platformId": platform}+ if arn is not None:+ body["signingMaterial"] = {"certificateArn": arn}+ with request_scope(*_SCOPE):+ status, _headers, payload = signer_module._put_signing_profile(name, body)+ error = json.loads(payload) if status >= 400 else {}+ return status, error.get("__type"), error.get("code"), error.get("message")++ def certificate():+ with request_scope(*_SCOPE):+ _status, _headers, payload = acm_module._import_certificate({+ "Certificate": "-----BEGIN CERTIFICATE-----\nBODY\n-----END CERTIFICATE-----\n",+ "PrivateKey": _ec_private_key_pem(),+ })+ arns.append(json.loads(payload)["CertificateArn"])+ return arns[-1]++ yield put, certificate+ for name in names:+ signer_module._profiles.pop_scoped(*_SCOPE, name, None)+ for arn in arns:+ acm_module._certificates.pop_scoped(*_SCOPE, arn, None)+++_NO_MATERIAL = (400, "ValidationException", "ValidationException", "signing material cannot be null.")+_NOT_VALID = (400, "ValidationException", "ValidationException", "{arn} is not a valid ACM certificate ARN")+_NOT_FOUND = (404, "ResourceNotFoundException", "CertificateNotFound", "Could not find an issued certificate {arn}")++[email protected]("auth", [True, False], ids=["auth", "no-auth"])[email protected]("platform, arn, refusal", [+ pytest.param(_IOT_PLATFORM, None, _NO_MATERIAL, id="iot-no-material"),+ pytest.param("AmazonFreeRTOS-TI-CC3220SF", None, _NO_MATERIAL, id="freertos-no-material"),+ pytest.param(_IOT_PLATFORM, "not-an-arn", _NOT_VALID, id="not-an-arn"),+ pytest.param(_IOT_PLATFORM, "arn:aws:s3:::bucket", _NOT_VALID, id="other-service"),+ pytest.param(_IOT_PLATFORM, "arn:aws:acm:eu-central-1:000000000000:certificate/", _NOT_VALID, id="empty-id"),+ pytest.param(_IOT_PLATFORM, _SCOPED_CERT.format(account="000000000000", id="not-a-uuid"), _NOT_VALID,+ id="id-not-a-uuid"),+ pytest.param(_IOT_PLATFORM, _SCOPED_CERT.format(account="notanaccount", id=uuid.uuid4()), _NOT_VALID,+ id="account-not-digits"),+ pytest.param(_IOT_PLATFORM, f"arn:aws:acm:us-east-1:000000000000:certificate/{uuid.uuid4()}", _NOT_VALID,+ id="other-region"),+ pytest.param(_IOT_PLATFORM, _SCOPED_CERT.format(account="111122223333", id=uuid.uuid4()),+ (400, "ValidationException", "ValidationException", "{arn} is not owned by 000000000000"),+ id="other-account"),+ pytest.param(_IOT_PLATFORM, _SCOPED_CERT.format(account="000000000000", id=uuid.uuid4()), _NOT_FOUND,+ id="unknown-certificate"),+ pytest.param(_IOT_PLATFORM, _SCOPED_CERT.format(account="000000000000", id=str(uuid.uuid4()).upper()),+ _NOT_FOUND, id="unknown-certificate-uppercase-id"),+ pytest.param("AmazonFreeRTOS-Default", _SCOPED_CERT.format(account="000000000000", id=uuid.uuid4()), _NOT_FOUND,+ id="freertos-unknown-certificate"),+ pytest.param(_LAMBDA_PLATFORM, "not-an-arn",+ (400, "ValidationException", "InvalidProfileParameters",+ "Signing material should not be present when using AWSLambda-SHA384-ECDSA platform."),+ id="lambda-with-material"),+ pytest.param("Notation-OCI-SHA384-ECDSA", _SCOPED_CERT.format(account="000000000000", id=uuid.uuid4()),+ (400, "ValidationException", "InvalidProfileParameters",+ "Signing material should not be present when using Notation-OCI-SHA384-ECDSA platform."),+ id="notation-with-material"),+])+def test_signer_put_profile_refuses_unusable_signing_material_under_auth(monkeypatch, in_process, auth, platform,+ arn, refusal):+ """Under AUTH=true PutSigningProfile refuses what AWS refuses; without AUTH every profile is created."""+ from ministack import app as app_mod++ monkeypatch.setattr(app_mod, "AUTH", auth)+ put, _certificate = in_process+ status, error_type, code, message = put(platform, arn)+ if auth:+ assert (status, error_type, code, message) == (*refusal[:3], refusal[3].format(arn=arn))+ else:+ assert (status, error_type, code, message) == (200, None, None, None)+++def test_signer_put_profile_under_auth_needs_an_issued_certificate(in_process):+ """An imported or requested certificate is issued here; one in any other status is refused."""+ from ministack.core.responses import request_scope+ from ministack.services import acm as acm_module++ put, certificate = in_process+ imported = certificate()+ assert put(_IOT_PLATFORM, imported)[0] == 200+ with request_scope(*_SCOPE):+ _status, _headers, payload = acm_module._request_certificate({"DomainName": "signer-auth.example.invalid"})+ requested = json.loads(payload)["CertificateArn"]+ try:+ assert put(_IOT_PLATFORM, requested)[0] == 200+ finally:+ acm_module._certificates.pop_scoped(*_SCOPE, requested, None)+ acm_module._certificates.get_scoped(*_SCOPE, imported)["Status"] = "PENDING_VALIDATION"+ assert put(_IOT_PLATFORM, imported) == (400, "ValidationException", "ValidationException",+ "Certificate is not Issued")+++def test_signer_put_profile_under_auth_checks_the_arn_before_the_name_and_acm_after_it(in_process):+ """On a taken name the material and ARN shape are refused first; an unknown certificate is not."""+ put, certificate = in_process+ name, arn = f"taken_{_uid()}", certificate()+ assert put(_IOT_PLATFORM, arn, name=name)[0] == 200+ assert put(_IOT_PLATFORM, None, name=name)[3] == "signing material cannot be null."+ assert put(_IOT_PLATFORM, "not-an-arn", name=name)[3] == "not-an-arn is not a valid ACM certificate ARN"+ assert put(_LAMBDA_PLATFORM, arn, name=name)[2] == "InvalidProfileParameters"+ unknown = _SCOPED_CERT.format(account="000000000000", id=uuid.uuid4())+ assert put(_IOT_PLATFORM, unknown, name=name)[2] == "ProfileAlreadyExists"++def test_signer_put_profile_enforces_the_tag_constraints(signer):"""Keys are 1..128 and match ^(?!aws:)[a-zA-Z+-=._:/]+$, values are atmost 256 characters, and the map takes at most 200 entries."""@@ -1270,12 +1397,12 @@ def test_signer_put_profile_enforces_the_tag_constraints(signer):):with pytest.raises(ClientError) as exc:signer.put_signing_profile(- profileName=f"tag_{_uid()}", platformId=_IOT_PLATFORM, tags=tags)+ profileName=f"tag_{_uid()}", platformId=_LAMBDA_PLATFORM, tags=tags)assert exc.value.response["Error"]["Code"] == "ValidationException", tagsname = f"tag_{_uid()}"signer.put_signing_profile(- profileName=name, platformId=_IOT_PLATFORM, tags={"team.name": "iot"})+ profileName=name, platformId=_LAMBDA_PLATFORM, tags={"team.name": "iot"})assert signer.get_signing_profile(profileName=name)["tags"] == {"team.name": "iot"}