Skip to content

Remove the PostgreSQL random_string function#13487

Open
LawnGnome wants to merge 2 commits intorust-lang:mainfrom
LawnGnome:remove-random-string-usage
Open

Remove the PostgreSQL random_string function#13487
LawnGnome wants to merge 2 commits intorust-lang:mainfrom
LawnGnome:remove-random-string-usage

Conversation

@LawnGnome
Copy link
Copy Markdown
Contributor

random_string is implemented in terms of the built-in PostgreSQL random function, which is explicitly documented as not to be relied upon for anything security-sensitive. While we migrated API token generation to use a more secure alternative several years ago, e-mail verification and crate invitation tokens are still generated using random_string.

In practice, I'm not terribly concerned about this (hence why I'm opening it as a regular PR and not via the GitHub security features), but we may as well clean this up. I've basically done this the same way API tokens are implemented: by creating a new GenericToken type that re-uses the same generator as API tokens, and implements the right traits to be used as a field type directly with Diesel.

One user-visible change here is that tokens created after this is deployed will be 32 characters instead of 26, but I think that's fine. (We could change GenericToken::generate to take a length, but that feels unnecessary.)

@LawnGnome LawnGnome requested a review from a team April 24, 2026 00:36
@LawnGnome LawnGnome self-assigned this Apr 24, 2026
@LawnGnome LawnGnome added C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works A-backend ⚙️ labels Apr 24, 2026
@Turbo87
Copy link
Copy Markdown
Member

Turbo87 commented Apr 24, 2026

out of curiosity, is this a mythical PR?

@LawnGnome
Copy link
Copy Markdown
Contributor Author

out of curiosity, is this a mythical PR?

Yes, the issue came out of a Mythos security scan of crates.io.

@Turbo87
Copy link
Copy Markdown
Member

Turbo87 commented Apr 24, 2026

if that's the worst it could find then I'd be pretty happy 😂

@LawnGnome
Copy link
Copy Markdown
Contributor Author

if that's the worst it could find then I'd be pretty happy 😂

Don't worry; I told it to try harder.

ALTER COLUMN token
DROP DEFAULT;

DROP FUNCTION random_string;
Copy link
Copy Markdown
Member

@Turbo87 Turbo87 Apr 28, 2026

Choose a reason for hiding this comment

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

just so it doesn't get lost in Slack DMs:

the random_string function is still being used by the reconfirm_email_on_email_change database trigger that has not been replaced yet by the first commit in this PR. that is why the CI runs are currently failing with function random_string(integer) does not exist (not visible in CI, but locally with small adjustments to make the error visible).

View changes since the review

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 30, 2026

☔ The latest upstream changes (possibly #13532) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-backend ⚙️ C-enhancement ✨ Category: Adding new behavior or a change to the way an existing feature works

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants