Skip to content
XSS Disclosed report Intermediate CVE-2022-23520

Incomplete fix for CVE-2022-32209 (XSS in Rails::Html::Sanitizer under certain configurations)

The fix for CVE-2022-32209 only removed the dangerous combination of 'select' and 'style' from tag lists passed directly to the sanitize method, not from the class-level allow list configured in the application. An attacker could therefore still inject script through a crafted select/style combination when the allow list was set in configuration, resulting in cross-site scripting.

0b5cur17y Follow

Originally disclosed on HackerOne

Medium 4.7 View original
  • Triaged in 18 days
  • Fixed in 4 months
  • Disclosed after 5 months

While building a PoC for CVE-2022-32209, I noticed that I could not fix my vulnerable application by updating https://github.com/rails/rails-html-sanitizer from 1.4.2 to 1.4.3 even though the Hackerone report about this vulnerability suggested that this should fix it (see here: https://hackerone.com/reports/1530898).

I built this app with Rails 7.0.3.1 by just running "rails new", adding config.action_view.sanitized_allowed_tags = ["select", "style"] to the file config/application.rb and creating an endpoint that reflected a parameter after sanitizing it (ERB: <p>Hello <%= sanitize @name %></p>). When using the payload <select><style><script>alert("XSS")</script></style></select> for the parameter I got an alert no matter what the version of rails-html-sanitizer was.

I believe the reason is the following. There are two ways you can pass the list of allowed tags to the sanitizer. One is via a list of tags stored in a class attribute, the other is via an argument passed into the sanitize method. The fix only considered the second way but the first one was forgotten. See the commit with the fix here: https://github.com/rails/rails-html-sanitizer/commit/c871aa4034d3d80ad67cf33a3462154b0a0fb477#diff-0daf33b9062eb5ccdeae86ed8bf2662a6e8669f1a7db590802b7f3b36ea47426R159 The relevant part of the code is this:

Ruby
module Rails
  module Html
    class SafeListSanitizer < Sanitizer
      ...
      def remove_safelist_tag_combinations(tags)
        if !loofah_using_html5? && tags.include?("select") && tags.include?("style")
          warn("WARNING: #{self.class}: removing 'style' from safelist, should not be combined with 'select'")
          tags.delete("style")
        end
        tags
      end

      def allowed_tags(options)
        if options[:tags]
          remove_safelist_tag_combinations(options[:tags])
        else
          self.class.allowed_tags
        end
      end
      ...
    end
  end
end

Method remove_safelist_tag_combinations is introduced to remove style from the allow list if select is in there. However, within method allowed_tags this cleanup is only applied to the tag list in the options, not to self.class.allowed_tags, the list stored in the sanitizer class. However, it seems that the configuration in config/application.rb which I've set above put the list into the class variable (I've sprinkled a few puts here and there to confirm that).

Moreover, when moving the allow list from config/application.rb into the ERB template (<p>Hello <%= sanitize @name, tags: ["select", "style"] %></p>), the update from 1.4.2 to 1.4.3 does fix the problem.

The following patch to https://github.com/rails/rails-html-sanitizer should complete the fix for CVE-2022-32209 (also added a test, which is mostly copy&paste from the test method test_disallow_the_dangerous_safelist_combination_of_select_and_style found in the commit linked above):

From fb9882599684f5796805107ec98f6a18bba722ec Mon Sep 17 00:00:00 2001
From: Dominic Breuker <[email protected]>
Date: Fri, 29 Jul 2022 23:08:57 +0200
Subject: [PATCH] disallow select and style in safelist also when specified in
 sanitizer class allowed_tags list

---
 lib/rails/html/sanitizer.rb |  6 +-----
 test/sanitizer_test.rb      | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/lib/rails/html/sanitizer.rb b/lib/rails/html/sanitizer.rb
index 97503c8..4a0d43f 100644
--- a/lib/rails/html/sanitizer.rb
+++ b/lib/rails/html/sanitizer.rb
@@ -155,11 +155,7 @@ module Rails
       end
 
       def allowed_tags(options)
-        if options[:tags]
-          remove_safelist_tag_combinations(options[:tags])
-        else
-          self.class.allowed_tags
-        end
+        remove_safelist_tag_combinations(options[:tags] || self.class.allowed_tags)
       end
 
       def allowed_attributes(options)
diff --git a/test/sanitizer_test.rb b/test/sanitizer_test.rb
index e3ce218..3f0a88a 100644
--- a/test/sanitizer_test.rb
+++ b/test/sanitizer_test.rb
@@ -606,6 +606,26 @@ class SanitizersTest < Minitest::Test
     refute_includes(sanitized, "style")
   end
 
+  def test_disallow_the_dangerous_safelist_combination_of_select_and_style_in_class_tag_list
+    scope_allowed_tags(["select", "style"]) do
+      input = "<select><style><script>alert(1)</script></style></select>"
+      warning = /WARNING: Rails::Html::SafeListSanitizer: removing 'style' from safelist/
+      sanitized = nil
+      invocation = Proc.new { sanitized = safe_list_sanitize(input) }
+
+      if html5_mode?
+        # if Loofah is using an HTML5 parser,
+        #   then "style" should be removed by the parser as an invalid child of "select"
+        assert_silent(&invocation)
+      else
+        # if Loofah is using an HTML4 parser,
+        #   then SafeListSanitizer should remove "style" from the safelist
+        assert_output(nil, warning, &invocation)
+      end
+      refute_includes(sanitized, "style")
+    end
+  end
+
 protected
 
   def xpath_sanitize(input, options = {})
-- 
2.35.1


Impact

It is possible to bypass Rails::Html::SafeListSanitizer filtering and perform an XSS attack.

The original report as published on HackerOne, by 0b5cur17y. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The cleanup method was only called on tags supplied in the options argument, while the class-level allowed_tags list was returned without the same check.

Impact

An attacker could bypass the sanitiser and perform cross-site scripting, though the text notes the issue only applies under certain configurations and that HTML5 parsing may remove the dangerous element.

What the attacker needed

Allow list set in application config; select and style both permitted; user input reflected after sanitising

The fix

The fix should apply the tag-combination cleanup to the class-level allow list as well as to tags passed in options, and the author supplied a patch and test for this.

How it was found

  1. Create a new Rails application and add select and style to the sanitised allowed tags in the application configuration.
  2. Create an endpoint that reflects a parameter after passing it through the sanitize helper.
  3. Send a crafted select and style payload as the parameter and observe the script executing.
  4. Move the same tag list into the sanitize call in the template and confirm the updated gem blocks it.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A sanitiser is meant to clean user-supplied HTML so dangerous code cannot run. This report shows the cleaning rule was only applied in one of two places where the list of permitted HTML tags can be set, so a page configured the other way still let a script run in a visitor's browser.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

More by 0b5cur17y

View all

4 more disclosed reports, best first.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.