All jobs

Replace passlib in a FastAPI App Without Breaking Stored Hashes

Yes. An agent reads which hash formats your users table holds, moves off the unmaintained passlib release, and proves old passwords still verify.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The last passlib release on PyPI is 1.7.4, from October 2020, and the advice found most often is to call bcrypt directly. Whether that advice fits depends on what is already in the database, so the agent reads the hashing configuration before choosing a target. In a sample like scionoftech/FastAPI-Full-Stack-Samples, the FastAPISQLAlchamy app pins passlib==1.7.2 and bcrypt==3.1.7 in app/requirements.txt and again in app/dev_requirements.txt. Its app/util/passutil.py builds CryptContext(schemes=["sha256_crypt", "md5_crypt", "des_crypt"], deprecated=["md5_crypt", "des_crypt"]). No bcrypt scheme is listed. Every password this code stores starts with $5$, and the pinned bcrypt package is not used by this file at all.

That settles the first question with a test and not an opinion. The agent creates one hash of each configured scheme with the pinned passlib, then hands them to bcrypt.checkpw. It raises ValueError: Invalid salt. A switch to plain bcrypt would lock out every existing user unless a second verifier for the old formats is kept beside it. The agent also reproduces the failure that sends many people looking for a replacement, so the description can say whether it applies here: passlib 1.7.4 with bcrypt 5.0.0 logs module 'bcrypt' has no attribute '__about__' and then fails to hash with the bcrypt scheme. This app never reaches that code.

For this configuration the smallest safe move is libpass, a maintained fork that keeps the passlib import name, so passutil.py does not change. The agent installs libpass 1.9.4 on Python 3.13 and verifies the three hashes made by the old release, with warnings turned into errors: all verify, a wrong password is rejected, and the two deprecated formats report that they need an update. It then follows the consequence for the build. libpass requires Python 3.9 or later and the Dockerfile starts FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7, so the base image has to move in the same change.

While reading the callers the agent notes what the context is not doing. app/crud/crud_login.py and app/crud/crud_users.py call verify_password, which uses pwd_context.verify; nothing calls verify_and_update. Marking md5_crypt and des_crypt as deprecated therefore has no effect, and those hashes are never rewritten at login. It reports one more thing it finds in setup.py: 'passlib' and 'python-jose' sit on adjacent lines with no comma between them, which Python joins into a single requirement string.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This Python web app hashes passwords with passlib, whose last release is from 2020. Move it to a maintained library without invalidating any stored password. Read every requirements file, setup.py and the Dockerfile for the passlib and bcrypt pins and the Python version. Find the CryptContext and list its schemes, its deprecated list and every function that calls hash, verify or verify_and_update. Decide from that which hash prefixes can exist in the database. In a scratch environment with the pinned versions, create one hash per configured scheme. Then test each candidate against those hashes: the bcrypt package called directly, and libpass (the maintained fork that keeps the passlib import). Run the verification with warnings as errors on the newest Python you can install, and show a correct password, a wrong password and whether each hash is reported as needing an update. Choose the option that verifies every existing format; if it requires a newer Python than the Dockerfile or CI uses, change those in the same pull request. Do not change the hash format for new passwords unless every old format still verifies and you say so explicitly. Report any place where deprecated hashes are never upgraded at login, and any packaging mistake you find, without fixing unrelated code. Open a pull request with the commands and output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that swaps the passlib pin for libpass in both requirements files and moves the Dockerfile base image to a Python release the fork supports. passutil.py is not in the diff. The description leads with the table that justifies the choice: three old hashes, and for each candidate whether it verified them. Under it are the Invalid salt error from direct bcrypt and the note that the __about__ failure does not affect this app's schemes.

The reviewer looks at two findings the agent reported and did not act on. Login never upgrades deprecated hashes, so any md5_crypt rows will stay until verify_password is changed to use verify_and_update and store the result; that touches the login path and deserves its own review. And the missing comma in setup.py means installing the package by that file asks for one requirement named passlibpython-jose. The sample has no test suite, so the description states plainly that the evidence is the scratch verification, not a run of the API against PostgreSQL.

Run it on every Python version bump

The trouble with an unmaintained hashing library tends to surface when the interpreter moves. Add an automation on the Project with a github trigger on pull_request.labeled and a label such as python-upgrade, with the prompt above. When a member labels the pull request that raises the Python version, a run starts in the repository and the event is appended after the prompt.

For a fleet of services, send this body to POST /o/{org}/sessions once per repository. The answers differ by repository, because each has its own schemes and its own stored formats, which is the reason to run it per service and not copy one fix around.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

Password hashing is where a plausible change does lasting damage: the code passes review, deploys, and the first sign of trouble is users who cannot sign in. The safeguard is to verify real hashes made by the old library with the new one before merging. That takes two Python environments with different pins, which the agent creates in its sandbox, and the output of each verification is kept with the run.

The run also keeps the investigation honest about scope. It records which popular error applies to this codebase and which does not, and it separates the dependency change from the login change it would be tempting to fold in. A reviewer reads a narrow diff with the reasoning attached.

Frequently asked questions

Can I replace passlib with the bcrypt package directly?

Only if every stored hash is a bcrypt hash. bcrypt.checkpw raises ValueError: Invalid salt for a sha256_crypt hash such as the ones this app writes. With mixed or non-bcrypt formats you need something that still reads the old ones, which is what the agent tests for before choosing.

What causes the error about bcrypt having no attribute __about__?

passlib 1.7.4 reads a version attribute that recent bcrypt releases removed. In the scratch run with bcrypt 5.0.0 the message is logged and hashing with the bcrypt scheme then fails. An app whose context lists no bcrypt scheme, like this one, does not hit it.

Do stored passwords need to be rehashed?

No. Hashes made by passlib 1.7.2 for all three configured schemes verify under libpass 1.9.4 in the agent's check. Rehashing the deprecated formats is a separate improvement that needs verify_and_update in the login path.

Does the long password limit change?

It can if you adopt bcrypt later. bcrypt 5.0.0 raises ValueError for a password longer than 72 bytes where older releases truncated it. The schemes this app uses have no such limit, so nothing changes for it today.

More passlib jobs

More jobs like this: Upgrade dependencies weekly

Keep reading