From c78280a8ce4c841dd2a454ba086e95cfa4c37438 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 20 Jun 2023 18:10:19 +0200 Subject: [PATCH] Add translate="no" to outgoing mentions and links (#25524) --- app/lib/text_formatter.rb | 4 ++-- lib/sanitize_ext/sanitize_config.rb | 10 ++++++++-- spec/lib/sanitize_config_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index 243e89289..0404cbace 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -79,7 +79,7 @@ class TextFormatter cutoff = url[prefix.length..-1].length > 30 <<~HTML.squish - #{h(display_url)} + #{h(display_url)} HTML rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError h(entity[:url]) @@ -122,7 +122,7 @@ class TextFormatter display_username = same_username_hits&.positive? || with_domains? ? account.pretty_acct : account.username <<~HTML.squish - @#{h(display_username)} + @#{h(display_username)} HTML end diff --git a/lib/sanitize_ext/sanitize_config.rb b/lib/sanitize_ext/sanitize_config.rb index 9cc500c36..bcd89af67 100644 --- a/lib/sanitize_ext/sanitize_config.rb +++ b/lib/sanitize_ext/sanitize_config.rb @@ -36,6 +36,11 @@ class Sanitize node['class'] = class_list.join(' ') end + TRANSLATE_TRANSFORMER = lambda do |env| + node = env[:node] + node.remove_attribute('translate') unless node['translate'] == 'no' + end + UNSUPPORTED_HREF_TRANSFORMER = lambda do |env| return unless env[:node_name] == 'a' @@ -63,8 +68,8 @@ class Sanitize elements: %w(p br span a del pre blockquote code b strong u i em ul ol li), attributes: { - 'a' => %w(href rel class), - 'span' => %w(class), + 'a' => %w(href rel class translate), + 'span' => %w(class translate), 'ol' => %w(start reversed), 'li' => %w(value), }, @@ -80,6 +85,7 @@ class Sanitize transformers: [ CLASS_WHITELIST_TRANSFORMER, + TRANSLATE_TRANSFORMER, UNSUPPORTED_ELEMENTS_TRANSFORMER, UNSUPPORTED_HREF_TRANSFORMER, ] diff --git a/spec/lib/sanitize_config_spec.rb b/spec/lib/sanitize_config_spec.rb index a01122bed..550ad1c52 100644 --- a/spec/lib/sanitize_config_spec.rb +++ b/spec/lib/sanitize_config_spec.rb @@ -38,6 +38,14 @@ describe Sanitize::Config do expect(Sanitize.fragment('Test', subject)).to eq 'Test' end + it 'keeps a with translate="no"' do + expect(Sanitize.fragment('Test', subject)).to eq 'Test' + end + + it 'removes "translate" attribute with invalid value' do + expect(Sanitize.fragment('Test', subject)).to eq 'Test' + end + it 'removes a with unparsable href' do expect(Sanitize.fragment('Test', subject)).to eq 'Test' end