Skip to content

Make user_auto_avatar resilient against invalid usernames - #2187

Open
Oaphi wants to merge 1 commit into
developfrom
0valt/2185/auto-avatar-fix
Open

Oaphi wants to merge 1 commit into
developfrom
0valt/2185/auto-avatar-fix

Conversation

@Oaphi

@Oaphi Oaphi commented Dec 5, 2026

Copy link
Copy Markdown
Member

closes #2185

Comment thread app/helpers/avatar_helper.rb Dismissed
Comment thread app/models/user.rb
# @return [String] safe username
def rtl_safe_username
"#{username}\u202D"
"#{username.gsub("\u0000", '').presence || "user#{id}"}\u202D"

@Oaphi Oaphi Dec 5, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I opted to extend the rtl_safe_username as we already use it pretty much everywhere where usernames would be considered untrusted user input. We should rename it to just safe_username or make it a generic sanitizer, but that's outside of scope of this PR.

The extension also makes sure that if the user tricks our validations with 3 null chars, they get user<id> displayed instead - same as with deleted users.

@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.69%. Comparing base (989747b) to head (cc50416).

Additional details and impacted files
Components Coverage Δ
controllers 78.12% <ø> (ø)
helpers 88.47% <100.00%> (+2.85%) ⬆️
jobs 79.93% <ø> (ø)
models 93.51% <100.00%> (ø)
tasks 61.11% <ø> (ø)
scripts ∅ <ø> (∅)
initializers ∅ <ø> (∅)

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

require 'rmagick'

module Users::AvatarHelper
module AvatarHelper

@Oaphi Oaphi Dec 5, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This PR also moves avatar helpers to top level for consistency as this is the only scoped one (for users that is).

@Oaphi Oaphi added this to the v0.12.5 milestone Dec 5, 2026
@cellio

cellio commented Dec 6, 2026

Copy link
Copy Markdown
Member

For testing: how do I create a bad username through the UI? (That is, how did people who've done this do it?) Or is that no longer possible but we have legacy usernames from before it was fixed?

@Oaphi

Oaphi commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

For testing: how do I create a bad username through the UI? (That is, how did people who've done this do it?) Or is that no longer possible but we have legacy usernames from before it was fixed?

The best way is to not use the UI, just start the Rails repl in uwsgi and update any of the user's name to \u0000 (Ruby's unicode escape sequences are as you'd expect) + 2+ more characters to pass the min length validation. Don't forget to clear the cache afterwards as the generated avatar is cached.

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.

500 error on missing avatar

3 participants