Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mentions being formatted with Markdown #2433

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/lib/advanced_text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
private

def format_markdown(html)
html = markdown_formatter.render(html)
# Force escape usernames in mentions before formatting
html = markdown_formatter.render(html.gsub(Account::MENTION_RE) { |re| re.gsub('_', '\\_') })

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.
Copy link
Author

@Plastikmensch Plastikmensch Oct 8, 2023

Choose a reason for hiding this comment

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

MENTION_RE doesn't match backslashes, so re cannot contain escaped underscores.
The reason escaping here works is because redcarpet converts escaped underscores to underscores instead of u elements before mentions are processed in rewrite
I haven't looked into why manually escaping usernames doesn't produce a mention, but I think that's expected behaviour.

html.delete("\r").delete("\n")
end

Expand Down
9 changes: 9 additions & 0 deletions spec/lib/advanced_text_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
it 'creates a mention link' do
expect(subject).to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
end

context 'when username contains underscores' do
let(:preloaded_accounts) { [Fabricate(:account, username: '_bob_')] }
let(:text) { '@_bob_' }

it 'creates a mention link' do
expect(subject).to include '<a href="https://cb6e6126.ngrok.io/@_bob_" class="u-url mention">@<span>_bob_</span></a></span>'
end
end
end

context 'with text containing unlinkable mentions' do
Expand Down
Loading