]> git.openstreetmap.org Git - rails.git/commitdiff
Strip away class attributes from sanitized outputs
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 24 Mar 2021 19:15:21 +0000 (19:15 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 24 Mar 2021 19:15:21 +0000 (19:15 +0000)
There's a lot of shenanigans that are possible when you can apply
arbitrary classes to the rendered output.

config/initializers/sanitize.rb
test/lib/rich_text_test.rb

index 609bb8734c8c51006420281ee1dc474f3b7b543a..b98523805a3e188806dd0eed7aaacc3d5449a6e2 100644 (file)
@@ -4,6 +4,7 @@ Sanitize::Config::OSM = Sanitize::Config.merge(
   :add_attributes => { "a" => { "rel" => "nofollow noopener noreferrer" } },
   :remove_contents => %w[script style],
   :transformers => lambda do |env|
+    env[:node].remove_class
     env[:node].add_class("table table-sm w-auto") if env[:node_name] == "table"
   end
 )
index e287f03f5fe5a540d401d30aacda65cc6bc70022..5b590bc3e41ae1a60253884ff1154622af7c436e 100644 (file)
@@ -52,6 +52,12 @@ class RichTextTest < ActiveSupport::TestCase
     assert_html r do
       assert_select "table[class='table table-sm w-auto']"
     end
+
+    r = RichText.new("html", "<p class='btn btn-warning'>Click Me</p>")
+    assert_html r do
+      assert_select "p[class='btn btn-warning']", false
+      assert_select "p", /^Click Me$/
+    end
   end
 
   def test_html_to_text
@@ -155,6 +161,13 @@ class RichTextTest < ActiveSupport::TestCase
     assert_html r do
       assert_select "table[class='table table-sm w-auto']"
     end
+
+    r = RichText.new("markdown", "Click Me\n{:.btn.btn-warning}")
+    # raise r.to_html
+    assert_html r do
+      assert_select "p[class='btn btn-warning']", false
+      assert_select "p", /^Click Me$/
+    end
   end
 
   def test_markdown_to_text