Skip to content

fix(auth): use secure_compare for credential comparisons#1032

Merged
v0idpwn merged 3 commits into
supabase:mainfrom
Snehil-Shah:nit-secure-compare
Jun 9, 2026
Merged

fix(auth): use secure_compare for credential comparisons#1032
v0idpwn merged 3 commits into
supabase:mainfrom
Snehil-Shah:nit-secure-compare

Conversation

@Snehil-Shah

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Nitpicky change to use secure_compare when comparing client-fed credentials. We are already using this in https://github.com/supabase/supavisor/blob/main/lib/supavisor/client_handler/auth_methods/password.ex#L86, so it closes the inconsistency as well.

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
@Snehil-Shah Snehil-Shah requested a review from a team as a code owner June 9, 2026 12:47
Comment thread lib/supavisor/client_handler/auth_methods/password.ex
computed_stored_key = :pgo_scram.h(client_key)

if computed_stored_key == sasl_secrets.stored_key do
if Plug.Crypto.secure_compare(computed_stored_key, sasl_secrets.stored_key) do

@v0idpwn v0idpwn Jun 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this has any risk of timing attack? The value is hashed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v0idpwn Yeah, you are right. I added secure_compare to all the places where you have some client-controlled input. Does it hurt much to be extra defensive? WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the three sites tbh. Plain text comparisons is the only real risk 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud, they can reproduce the hash value locally too. So they essentially know what bytes are being compared at this line. So yeah, guessing the first byte of the stored key is possible. Although, guessing the next bytes would start to get impossible as many inputs can produce the same first few bytes of a hash.🤔🤔

@v0idpwn v0idpwn Jun 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, I just verified Postgres source as reference, and its SCRAM implementation uses memcmp for these hash comparisons, so I think we are good to go as it is

Comment thread lib/supavisor/client_handler/auth_methods/scram.ex Outdated
Comment thread lib/supavisor/helpers.ex Outdated
@v0idpwn v0idpwn merged commit c949944 into supabase:main Jun 9, 2026
14 of 15 checks passed
@v0idpwn

v0idpwn commented Jun 9, 2026

Copy link
Copy Markdown
Member

Thank you! 💚

@Snehil-Shah Snehil-Shah deleted the nit-secure-compare branch June 10, 2026 03:46
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