All jobs

Fix ValueError: Invalid hash method 'sha256' After a Werkzeug Upgrade

Yes. An agent reproduces the register and login crash on Werkzeug 3, moves new hashes to scrypt, keeps old sha256 users signing in and opens a PR.

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

What the agent does

The agent clones the repository into its own cloud sandbox and reads the sign-in code first. In a small Flask app like imdhruv99/Flask-LogIn-Register, app.py imports generate_password_hash and check_password_hash at line 7, points SQLALCHEMY_DATABASE_URI at a local MySQL at line 13, hashes a new user's password at line 75 with generate_password_hash(form.password.data, method='sha256') and checks it at line 47 inside the login route. requirements.txt names Flask, Flask-Login, Flask-SQLAlchemy, Flask-WTF, SQLAlchemy, Werkzeug and WTForms with no versions, the README runs it with flask run --port=8080, and forms.py validates the email field with WTForms' Email validator.

Then it reproduces the crash without the MySQL the app expects, by pointing the URI at a SQLite file for the run. A fresh install resolves Werkzeug 3.1.9, Flask 3.1.3, Flask-SQLAlchemy 3.1.1 and WTForms 3.3.2. The first POST /register/ through the test client fails before any hashing, because WTForms' Email validator needs the email_validator package, which the requirements do not list; with it installed, the same request raises ValueError: Invalid hash method 'sha256'. from werkzeug/security.py at app.py line 75. Pinning back, Werkzeug 2.2.3 stores an 88-character sha256$salt$hex hash silently, and 2.3.8 stores the same hash with UserWarning: The 'sha256' password method is deprecated and will be removed in Werkzeug 3.0. Migrate to the 'scrypt' method. at lines 75 and 47.

The agent then checks the part an upgrade usually forgets: a user registered under 2.x and logging in under 3.x. With a sha256$ hash still in the user table, POST /login/ on Werkzeug 3.1.9 raises the same ValueError from line 47, so the upgrade locks every existing account out rather than only breaking registration. The old format is hmac.new(salt, password, sha256).hexdigest(), which the agent confirms by recomputing a stored hash from its salt and the known password.

The fix has two parts. New hashes drop the method argument so Werkzeug's default applies, scrypt:32768:8:1$... on 3.1.9, 162 characters, which fits the String(256) column. The login route checks a stored hash that starts with sha256$ by recomputing the HMAC, and on a match rewrites the row with a fresh default hash before setting the session, so the table migrates one login at a time with no password reset. email_validator goes into requirements.txt, since registration cannot run without it, and the pull request leaves the MySQL URI alone.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Registration in this Flask app fails with \"ValueError: Invalid hash method 'sha256'\" after a Werkzeug upgrade. Read requirements.txt, app.py and forms.py first, and point SQLALCHEMY_DATABASE_URI at a SQLite file for the run only. Install the current Werkzeug and Flask, drive the register route through the test client and quote the error with its line; then install Werkzeug 2.3 and show the same request succeeds, quoting any warning, and show that a hash stored under 2.3 makes the login route raise under 3.x. Change new hashes to Werkzeug's default method, add a login path that verifies a legacy sha256 hash and re-hashes it on success, and add any package the forms need that requirements.txt omits. Do not change the production database URI and do not reset any password. Open a pull request that quotes the failing and passing requests.",
  "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

What comes back is a pull request touching app.py and requirements.txt: the method='sha256' argument removed from line 75, a small check in the login route for a stored hash beginning with sha256$ that verifies it with the HMAC the old method used and replaces it with a current hash on success, and email_validator added to the requirements. The description quotes the ValueError with its line from both the register and login routes on Werkzeug 3.1.9, the deprecation warning from 2.3.8, and the redirects to /login/ and / after the fix.

A reviewer checks that the legacy branch only runs for the old prefix and never accepts a password the HMAC does not match, that the re-hash is committed before the session is set, and that the default method's hash length fits the column. The run's record holds every request with its full output, so the before and after can be read rather than taken on trust.

Run it on every 500 on the register route

A ValueError on registration reaches an error tracker, and most of those services sign their own webhook deliveries and cannot add the Authorization: Bearer header a Rush webhook trigger needs. Put a handler on your server in between: it receives the alert, keeps the ones whose message contains Invalid hash method, and posts the traceback to the trigger's URL with the trigger's secret. One run starts per alert and opens one pull request on its own branch; a run that cannot reproduce the error ends with a one-line answer saying what it tried.

A crashing form fires the same alert many times. Have the handler send an Idempotency-Key header to the trigger's URL made from the error text and the route, so repeats of one failure are recognised and start nothing new, and keep the automation's max_runs_per_hour low.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

This failure has two halves, and a laptop usually shows only one. Registration breaks loudly, but the login that locks every existing user out needs a database row written by the old Werkzeug and read by the new one. The agent's sandbox installs 2.3, writes that row, installs 3.1 and reads it back, with every request and traceback kept in the run's record, so the pull request fixes both halves and shows each.

Password code is where a quiet mistake costs the most. The agent proves the legacy verifier against a hash it watched the old version write, proves the new default round-trips, and quotes both in the pull request, which is the evidence a reviewer needs before merging anything that touches how people sign in.

Frequently asked questions

Why does the upgrade break login for existing users too?

Werkzeug 3 removed the plain hashlib methods entirely, so check_password_hash raises on a stored sha256$ hash instead of returning False. Every account registered under the old method hits that line. The pull request's legacy branch recomputes the old HMAC itself and re-hashes on success, so nobody needs a reset.

Should new hashes use scrypt or pbkdf2?

Leaving method out takes Werkzeug's default, scrypt on 3.x, which is the maintainers' recommendation and the warning's advice. pbkdf2:sha256 still works if your deployment's OpenSSL lacks scrypt; the agent says so in the pull request and the hash length of either fits a 256-character column.

Will the agent change the MySQL connection string?

No. It points the URI at SQLite only inside the run to drive the routes without a database server, and the prompt tells it to leave the production value alone. The pull request diff does not touch line 13.

Why is email_validator in the pull request?

WTForms' Email validator imports that package at validation time and raises Install 'email_validator' for email validation support without it, which blocks every registration before the hash is ever computed. The agent hit it first and adds it to requirements.txt so the fix can be exercised at all.

More Werkzeug jobs

More jobs like this: Fix a bug from an alert

Keep reading