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(cognito): check PreviousPassword in ChangePassword (#2332)
jun-ut268f15a11cf9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bed1d67b..24a8fc7e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -15,6 +15,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **WAF v2 — `CreateIPSet` and `CreateRuleGroup` refuse a duplicate name** — a second IP set or rule group with the name and scope of an existing one is now refused with `WAFDuplicateItemException`, as AWS does and as `CreateWebACL` and `CreateRegexPatternSet` already did. Both stored the duplicate silently, so a client that retries a create after losing the response ended up with two. Contributed by @OS-kiranmalsetty.- **Route 53 — private hosted zones keep their VPC** — `CreateHostedZone` with a `VPC` made a public zone and dropped the VPC, so Terraform's `aws_route53_zone` with a `vpc` block planned to add it again on every run; the zone is now private, the response carries the `VPC` instead of a delegation set, also on a `CallerReference` replay, and `GetHostedZone` returns its `VPCs`. Contributed by @kharabasz.- **ELBv2 — listener `SslPolicy` and certificate** — `CreateListener` and `ModifyListener` dropped `SslPolicy` and `Certificates`, so `DescribeListeners` returned neither and Terraform re-planned `ssl_policy` on every HTTPS listener; HTTPS and TLS listeners now keep and return both, the policy defaults to `ELBSecurityPolicy-2016-08`, a listener moved to HTTP or TCP drops them, and more than one default certificate answers `ValidationError`. Contributed by @kharabasz.+- **Cognito — `ChangePassword` checks `PreviousPassword`** — a wrong or missing `PreviousPassword` was ignored and the password was replaced; for a user with a password, a wrong one now fails with `NotAuthorizedException` and a missing one with `InvalidParameterException`, as on AWS, and the password is unchanged. Contributed by @jun-ut.- **SQS — `MD5OfMessageAttributes` of filtered attributes** — `ReceiveMessage` with `MessageAttributeNames` selecting some of a message's attributes returned only those attributes but the digest of all of them, so SDKs that validate `MD5OfMessageAttributes` rejected the response; the digest is now computed over the attributes returned. Contributed by @Xevos117.## [1.5.30] — 2026-10-20diff --git a/ministack/services/cognito.py b/ministack/services/cognito.pyindex 4ed6e8a2..06bf7fc6 100644--- a/ministack/services/cognito.py+++ b/ministack/services/cognito.py@@ -5048,6 +5048,15 @@ def _change_password(data):user, pool = _user_pool_for_access_token(access_token)if not user:return error_response_json("NotAuthorizedException", "Invalid access token.", 400)+ # PreviousPassword is required when the user has a password+ # and is checked before ProposedPassword's policy (measured).+ # AWS names the missing parameter "OldPassword" in its message.+ if user.get("_password"):+ previous = data.get("PreviousPassword")+ if not previous:+ return error_response_json("InvalidParameterException", "Missing required parameter OldPassword", 400)+ if previous != user["_password"]:+ return error_response_json("NotAuthorizedException", "Incorrect username or password.", 400)pw_err = _validate_password(pool, proposed)if pw_err:return pw_errdiff --git a/tests/test_cognito.py b/tests/test_cognito.pyindex da08056e..f7053bd6 100644--- a/tests/test_cognito.py+++ b/tests/test_cognito.py@@ -2353,6 +2353,46 @@ def test_cognito_change_password_actually_changes(cognito_idp):)assert exc.value.response["Error"]["Code"] == "NotAuthorizedException"+def test_cognito_change_password_rejects_wrong_previous_password(cognito_idp):+ """ChangePassword with a wrong or missing PreviousPassword fails and keeps the old password."""+ pid = cognito_idp.create_user_pool(PoolName="qa-changepwd-wrong")["UserPool"]["Id"]+ cid = cognito_idp.create_user_pool_client(+ UserPoolId=pid,+ ClientName="qa-changepwd-wrong-app",+ ExplicitAuthFlows=["ALLOW_ADMIN_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],+ )["UserPoolClient"]["ClientId"]+ cognito_idp.admin_create_user(UserPoolId=pid, Username="qa-wrong-user", MessageAction="SUPPRESS")+ cognito_idp.admin_set_user_password(UserPoolId=pid, Username="qa-wrong-user", Password="OldPwd1!", Permanent=True)++ def sign_in(password):+ return cognito_idp.admin_initiate_auth(+ UserPoolId=pid,+ ClientId=cid,+ AuthFlow="ADMIN_USER_PASSWORD_AUTH",+ AuthParameters={"USERNAME": "qa-wrong-user", "PASSWORD": password},+ )++ token = sign_in("OldPwd1!")["AuthenticationResult"]["AccessToken"]+ with pytest.raises(ClientError) as exc:+ cognito_idp.change_password(AccessToken=token, PreviousPassword="WrongPwd1!", ProposedPassword="NewPwd2!")+ assert exc.value.response["Error"]["Code"] == "NotAuthorizedException"+ assert exc.value.response["Error"]["Message"] == "Incorrect username or password."++ # The wrong PreviousPassword is reported before a ProposedPassword that breaks the policy.+ with pytest.raises(ClientError) as exc:+ cognito_idp.change_password(AccessToken=token, PreviousPassword="WrongPwd1!", ProposedPassword="short")+ assert exc.value.response["Error"]["Code"] == "NotAuthorizedException"++ with pytest.raises(ClientError) as exc:+ cognito_idp.change_password(AccessToken=token, ProposedPassword="NewPwd2!")+ assert exc.value.response["Error"]["Code"] == "InvalidParameterException"+ assert exc.value.response["Error"]["Message"] == "Missing required parameter OldPassword"++ assert "AuthenticationResult" in sign_in("OldPwd1!")+ with pytest.raises(ClientError) as exc:+ sign_in("NewPwd2!")+ assert exc.value.response["Error"]["Code"] == "NotAuthorizedException"+def test_cognito_refresh_token_returns_correct_user(cognito_idp):"""REFRESH_TOKEN_AUTH must return tokens for the refreshing user, not users[0]."""pid = cognito_idp.create_user_pool(PoolName="qa-refresh-pool")["UserPool"]["Id"]