Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user_datastore.create_user has side effects on mutable inputs #875

Closed
NoRePercussions opened this issue Nov 8, 2023 · 0 comments · Fixed by #876
Closed

user_datastore.create_user has side effects on mutable inputs #875

NoRePercussions opened this issue Nov 8, 2023 · 0 comments · Fixed by #876

Comments

@NoRePercussions
Copy link
Contributor

user_datastore.create_user will mutate mutable inputs:

roles = ["Admin"]
user_datastore.create_user(username="admin", password=hash_password("admin"), roles=roles)
# now roles is [Role("Admin")] instead of ["Admin"]

This is because _prepare_create_user_args writes to roles:

def _prepare_create_user_args(self, **kwargs):
kwargs.setdefault("active", True)
roles = kwargs.get("roles", [])
for i, role in enumerate(roles):
rn = role.name if isinstance(role, self.role_model) else role
# see if the role exists
roles[i] = self.find_role(rn)

This can cause issues in situations where a string is always expected, e.g. in a test environment where a set of expected users may be persisted for longer than each database session (and thus the new Role values will be expired in the next session).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant