about summary refs log tree commit diff
path: root/app/lib/sanitize_config.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/sanitize_config.rb')
-rw-r--r--app/lib/sanitize_config.rb21
1 files changed, 20 insertions, 1 deletions
diff --git a/app/lib/sanitize_config.rb b/app/lib/sanitize_config.rb
index 9cf9b3db0..f09288fcd 100644
--- a/app/lib/sanitize_config.rb
+++ b/app/lib/sanitize_config.rb
@@ -4,6 +4,21 @@ class Sanitize
   module Config
     HTTP_PROTOCOLS ||= ['http', 'https', :relative].freeze
 
+    CLASS_WHITELIST_TRANSFORMER = lambda do |env|
+      node = env[:node]
+      class_list = node['class']&.split(' ')
+
+      return unless class_list
+
+      class_list.keep_if do |e|
+        return true if e =~ /^(h|p|u|dt|e)-/ # microformats classes
+        return true if e =~ /^(mention|hashtag)$/ # semantic classes
+        return true if e =~ /^(ellipsis|invisible)$/ # link formatting classes
+      end
+
+      node['class'] = class_list.join(' ')
+    end
+
     MASTODON_STRICT ||= freeze_config(
       elements: %w(p br span a),
 
@@ -21,7 +36,11 @@ class Sanitize
 
       protocols: {
         'a' => { 'href' => HTTP_PROTOCOLS },
-      }
+      },
+
+      transformers: [
+        CLASS_WHITELIST_TRANSFORMER,
+      ]
     )
 
     MASTODON_OEMBED ||= freeze_config merge(