GHSA-h7cm-mrvq-wcfr: Piccolo's current `BaseUser.login` implementation is vulnerable to time based user enumeration
Summary_Short summary of the problem. Make the impact and severity as clear as possible. For example: An unsafe deserialization vulnerability allows any unauthenticated user to execute arbitrary code on the server._The current implementation of `BaseUser.login` leaks enough information to a malicious user such that they would be able to successfully generate a list of valid users on the platform. As Piccolo on it's own does not also enforce strong passwords (see [here](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#implement-proper-password-strength-controls)), these lists of valid accounts are likely to be used in a password spray attack with the outcome being attempted takeover of user accounts on the platform.The impact of this vulnerability is minor as it requires chaining with other attack vectors in order to gain more then simply a list of valid users on the underlying platform.
The likelihood of this vulnerability is possible as it requires minimal skills to pull off especially given the underlying login functionality for Piccolo based sites is open source.Details_Give all details on the vulnerability. Pointing to the incriminated source code is very helpful for the maintainer._This vulnerability relates to [this](https://github.com/piccolo-orm/piccolo/blob/master/piccolo/apps/user/tables.py#L191-L237) code. Specifically the fact that responses are not returned in constant time, but rather are based off the internal state.For example, if a user does not exist then `None` is returned immediately instead of encountering a time expensive hash comparison (Line 225). This discrepancy allows a malicious user to time requests made in order to generate a list of usernames which are valid on the underlying platform for usage in further attacks.If your curious for some more information regarding this attack avenue, I wrote a blog post awhile back with a similar chain to this with some other types of analysis. It lives [here](https://skelmis.co.nz/posts/tbue/).PoC_Complete instructions, including specific configuration details, to reproduce the vulnerability._Piccolo SetupIn a fresh environment `pip install 'piccolo[all]'` and `piccolo asgi new`For simplified testing purposes, in `piccolo_conf.py` modify Piccolo to use SQLite:In the same file, add the required apps for session authentication. The file should look like the following:Run the following migrations:Within `app.py`, mount `session_login` at the `/login` path as follows:Create a new user using `piccolo user create`, making a note of the username and password for later steps.ExploitationThe following Python script can be used to reproduce this issue. It could also be expanded to easily take in user lists to conduct user enumeration at scale, however, that is outside the scope of this report.N.B. This script makes 50 requests per username/password combination in order to be more certain of the time to response for each combinationAnalysisThe following is the output from the PoC against `pip install piccolo`
![Screenshot from 2023-09-08 18-36-45](https://user-images.githubusercontent.com/47520067/266522913-b8a0f499-e38b-47fd-a97d-292900320a01.png)The following is the output from the PoC against `pip install git+https://github.com/piccolo-orm/piccolo.git`.
![Screenshot from 2023-09-08 17-51-16](https://user-images.githubusercontent.com/47520067/266514350-82384ce6-c24f-4b89-8516-ec08282053e1.png)I have included the results from both versions to highlight that this issue is not as a result of [this](https://github.com/piccolo-orm/piccolo/pull/881) pull request but as a result of the underlying logic in usage.Both of these runs clearly show a noticeable difference in the time to response for valid and invalid users which would allow a malicious user to build up a list of users for usage in further attacks against the website. For example, after building up a user list a malicious user may then conduct a [password spray attack](https://owasp.org/www-community/attacks/Password_Spraying_Attack) using common passwords in order to takeover user accounts on the platform.Impact_What kind of vulnerability is it? Who is impacted?_This is an information disclosure vulnerability. 
It would affect any Piccolo site, and all users of said Piccolo site who can login via regular login portals.
CVSS v3.1: 5.3