Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cognito): match ListUsers Filter values case-insensitively (#1465)

ppettitauf5b8fd993ad8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 446db62e..63669964 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Cognito — `ListUsers` `Filter` matches values case-insensitively, except where AWS documents otherwise** — every filter comparison was an exact string match, so `email = "[email protected]"` missed a profile stored as `[email protected]` and a `^=` prefix search was equally literal. Values are now matched case-insensitively for `email`, `phone_number`, `name`, `given_name`, `family_name`, `preferred_username` and `sub`, while `username` and `status` stay case-sensitive as the `ListUsers` API reference specifies. Contributed by @ppettitau.
- **DynamoDB — an `UpdateExpression` alias is resolved before the key-attribute check** — `set #pk = :pk` with `#pk` mapped to a non-key attribute was refused with `Cannot update attribute pk. This attribute is part of the key`, because the check matched the key name as raw text inside the `#pk` alias and the `:pk` value reference. It now reads the alias-resolved attribute roots the update evaluator already reports, so only a path that resolves to the partition or sort key is rejected. Reported by @ppettitau.
- **RDS — `DescribeDBInstances` honors SDK `Filters`** — AWS Query clients serialize filters as `Filters.Filter.N` with `Values.Value.N`, but MiniStack only parsed its internal `Filters.member.N` form, so filters such as `db-cluster-id` were ignored and unrelated instances were returned. Both wire forms are now parsed with the existing AND-across-filters and OR-across-values semantics.
### Added
diff --git a/ministack/services/cognito.py b/ministack/services/cognito.py
index 731a3a9a..d30d37db 100644
--- a/ministack/services/cognito.py
+++ b/ministack/services/cognito.py
@@ -5851,6 +5851,11 @@ def _generate_temp_password() -> str:
return "".join(password)
+# Attributes that AWS documents as case-sensitive in the ListUsers Filter; all
+# other searchable attributes match case-insensitively.
+_CASE_SENSITIVE_FILTER_ATTRIBUTES = {"username", "status"}
+
+
def _apply_user_filter(users: list, filter_str: str) -> list:
"""
Supports Cognito filter syntax:
@@ -5858,12 +5863,18 @@ def _apply_user_filter(users: list, filter_str: str) -> list:
attribute_name = "value" (unquoted, kept for backward compatibility)
attribute_name ^= "value" (starts with)
attribute_name != "value"
+
+ Attribute names are case-sensitive. Value matching is case-insensitive for
+ most searchable attributes, but the ListUsers API reference documents
+ "username" and "status" as case-sensitive.
"""
m = re.match(r'"?(\w+)"?\s*(=|\^=|!=)\s*"([^"]*)"', filter_str.strip())
if not m:
logger.warning("Cognito: ListUsers Filter could not be parsed, ignoring: %r", filter_str)
return users
attr_name, op, value = m.group(1), m.group(2), m.group(3)
+ case_sensitive = attr_name in _CASE_SENSITIVE_FILTER_ATTRIBUTES
+ value_key = value if case_sensitive else value.casefold()
result = []
for user in users:
attr_dict = _attr_list_to_dict(user.get("Attributes", []))
@@ -5879,11 +5890,12 @@ def _apply_user_filter(users: list, filter_str: str) -> list:
field_val = "Enabled" if user.get("Enabled", True) else "Disabled"
elif attr_name == "email_verified":
field_val = attr_dict.get("email_verified", "")
- if op == "=" and field_val == value:
+ field_key = field_val if case_sensitive else field_val.casefold()
+ if op == "=" and field_key == value_key:
result.append(user)
- elif op == "^=" and field_val.startswith(value):
+ elif op == "^=" and field_key.startswith(value_key):
result.append(user)
- elif op == "!=" and field_val != value:
+ elif op == "!=" and field_key != value_key:
result.append(user)
return result
diff --git a/tests/test_cognito.py b/tests/test_cognito.py
index 46339531..d5bda1de 100644
--- a/tests/test_cognito.py
+++ b/tests/test_cognito.py
@@ -228,6 +228,40 @@ def test_cognito_list_users_filter_quoted_attribute_name(cognito_idp):
resp = cognito_idp.list_users(UserPoolId=pid, Filter='"email" = "[email protected]"')
assert resp["Users"] == []
+def test_cognito_list_users_filter_case_sensitivity(cognito_idp):
+ """The ListUsers API reference documents "username" and "status" as
+ case-sensitive; other searchable attributes match case-insensitively."""
+ pid = cognito_idp.create_user_pool(PoolName="CaseFilterUsersPool")["UserPool"]["Id"]
+ cognito_idp.admin_create_user(
+ UserPoolId=pid,
+ Username="Alpha",
+ UserAttributes=[
+ {"Name": "email", "Value": "[email protected]"},
+ {"Name": "family_name", "Value": "Reddy"},
+ ],
+ )
+ cognito_idp.admin_create_user(
+ UserPoolId=pid,
+ Username="beta",
+ UserAttributes=[{"Name": "email", "Value": "[email protected]"}],
+ )
+
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='username = "alpha"')
+ assert resp["Users"] == []
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='username = "Alpha"')
+ assert [u["Username"] for u in resp["Users"]] == ["Alpha"]
+
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='"email" = "[email protected]"')
+ assert [u["Username"] for u in resp["Users"]] == ["Alpha"]
+
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='family_name ^= "redd"')
+ assert [u["Username"] for u in resp["Users"]] == ["Alpha"]
+
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='status = "enabled"')
+ assert resp["Users"] == []
+ resp = cognito_idp.list_users(UserPoolId=pid, Filter='status = "Enabled"')
+ assert sorted(u["Username"] for u in resp["Users"]) == ["Alpha", "beta"]
+
def test_cognito_list_users_filter_status(cognito_idp):
pid = cognito_idp.create_user_pool(PoolName="StatusFilterUsersPool")["UserPool"]["Id"]
cognito_idp.admin_create_user(UserPoolId=pid, Username="active-user")