Skip to content

fix(models): remove in-memory bcrypt rehash from User.Authenticate - #2946

Open
hf wants to merge 1 commit into
masterfrom
stojan/remove-inmemory-bcrypt-rehash
Open

hf wants to merge 1 commit into
masterfrom
stojan/remove-inmemory-bcrypt-rehash

Conversation

@hf

@hf hf commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

What

Removes the block in User.Authenticate that regenerated a default-cost bcrypt hash when the stored cost was above the default (10) or at the minimum.

Why

  • It only replaced u.EncryptedPassword on the in-memory user. The sign-in handler never saves that value. The one write (ReEncryptPassword) runs only on the DB-encryption path, which regenerates the hash itself.
  • It ran whether or not the password matched, so a wrong password left the in-memory user holding a hash of that wrong password.
  • Authenticate takes a tx that it never uses, which suggests the rehash was meant to be saved and was not (introduced in feat: add password_hash and id fields to admin create user #1641).

Stored hashes are not changed by this PR: before and after, a high-cost bcrypt hash stays as it is in the database.

Behaviour kept

A stored hash that is neither Argon2 nor Firebase scrypt and cannot be parsed by bcrypt.Cost still makes Authenticate return an error.

Tests

TestAuthenticate now asserts that the stored hash is untouched after both a wrong and a correct password, for cost 10 and cost 11.

Not run locally: the models tests need a database and the local Postgres credentials were rejected. Built and vetted only.

🤖 Generated with Claude Code

Authenticate replaced u.EncryptedPassword with a fresh default-cost hash of
the submitted password whenever the stored bcrypt cost was above the default
or at the minimum. The sign-in handler never saves that value (the only
write is the DB-encryption re-encryption path, which regenerates the hash
itself), so the rehash had no durable effect. It also ran regardless of
whether the password matched, leaving the in-memory user holding a hash of a
possibly wrong password.

Remove the rehash and keep the parse-error behaviour for malformed stored
hashes. Update TestAuthenticate to assert that the stored hash is left
untouched for both correct and incorrect passwords.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hf
hf requested a review from a team as a code owner November 20, 2026 15:33
@lovromazgon

Copy link
Copy Markdown

Seems like there's some stale comments now and code that could be simplified because we're not rehashing the password

  • auth/internal/api/token.go

    Lines 124 to 125 in 4c0e939

    // Authenticate may replace user.EncryptedPassword in memory (bcrypt cost
    // rehash), so capture the stored value first for ReEncryptPassword.
  • auth/internal/api/token.go

    Lines 274 to 275 in 4c0e939

    // Authenticate can replace the hash in memory (bcrypt cost rehash), so
    // capture the stored value first.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

2 participants