Skip to content

PostgreSQL: Cache salted password and client key#4043

Open
ThomWright wants to merge 1 commit intolaunchbadge:mainfrom
ThomWright:cache-pg-sasl-client-key
Open

PostgreSQL: Cache salted password and client key#4043
ThomWright wants to merge 1 commit intolaunchbadge:mainfrom
ThomWright:cache-pg-sasl-client-key

Conversation

@ThomWright
Copy link
Contributor

@ThomWright ThomWright commented Sep 26, 2025

A simple single-entry cache for the salted password and client key.

Please let me know what you'd like to see in terms of tests.

Does your PR solve an issue?

Fixes #4032

Is this a breaking change?

No. The cache is internal.

@ThomWright ThomWright marked this pull request as ready for review September 29, 2025 16:43
@ThomWright
Copy link
Contributor Author

@abonander Sorry to bother, have you had a chance to take a look?

Comment on lines +54 to +59
host: options.host.clone(),
port: options.port,
socket: options.socket.clone(),
database: options.database.clone(),
username: options.username.clone(),
password: options.password.clone().unwrap_or_default(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

You shouldn't need to store any of this. Strictly speaking, only 3 things can change the HMAC result:

  • the salt;
  • the iteration count;
  • the password, which can only be changed through an &mut reference so PgConnectOptions::set_password() can just wipe the cache directly by overwriting it with a new instance.

Comment on lines +194 to +199
let salted_password = hi(
options.password.as_deref().unwrap_or_default(),
&cont.salt,
cont.iterations,
)
.await?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could theoretically save extra blocking threads from being spawned to redundantly calculate the same salted_password if you use an async Mutex (or RwLock) and held the lock while calculating the new result.

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.

Cache Client Key for PostgreSQL SASL

2 participants