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

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

The fix for an earlier XSS in Rails::Html::Sanitizer only cleaned up the tag list passed as a method argument, not the class-level allowed tags set through application configuration. An attacker could therefore inject script content that survived sanitising when both select and style were allowed, leading to cross-site scripting.

0b5cur17y Follow

Originally disclosed on HackerOne

Bounty$2,400 Medium View original
  • Fixed in 21 days
  • Paid in 21 days
  • Disclosed after 21 days

Summary by the program

###Summary

There is a possible XSS vulnerability with certain configurations of Rails::Html::Sanitizer. This is due to an incomplete fix of CVE-2022-32209.

  • Versions affected: ALL
  • Not affected: NONE
  • Fixed versions: 1.4.4

###Impact

A possible XSS vulnerability with certain configurations of Rails::Html::Sanitizer may allow an attacker to inject content if the application developer has overridden the sanitizer's allowed tags to allow both "select" and "style" elements.

Code is only impacted if allowed tags are being overridden using either of the following two mechanisms:

  1. Using the Rails configuration config.action_view.sanitized_allow_tags=:
#In config/application.rb
config.action_view.sanitized_allowed_tags = ["select", "style"]

(see https://guides.rubyonrails.org/configuring.html#configuring-action-view)

  1. Using the class method Rails::Html::SafeListSanitizer.allowed_tags=:
#class-level option
Rails::Html::SafeListSanitizer.allowed_tags = ["select", "style"]

All users overriding the allowed tags by either of the above mechanisms to include both "select" and "style" should either upgrade or use one of the workarounds immediately.

NOTE: Code is not impacted if allowed tags are overridden using either of the following mechanisms:

  • the :tags option to the Action View helper method sanitize.
  • the :tags option to the instance method SafeListSanitizer#sanitize.

###Workarounds

Remove either "select" or "style" from the overridden allowed tags.

###References

CWE - CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (4.9) https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-32209 https://hackerone.com/reports/1654310

The report

The following is from: https://hackerone.com/reports/1654310

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.

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 that removes 'style' when 'select' is allowed was applied only to the per-call options tag list and not to the class-level allowed_tags list.

Impact

An attacker could bypass the sanitiser and run script in a victim's browser, but only where the developer had overridden allowed tags to include both select and style.

What the attacker needed

App allows both select and style tags; user input is sanitised and reflected

The fix

It was fixed in version 1.4.4; as a workaround, remove either select or style from the overridden allowed tags.

How it was found

  1. Create a new Rails app and add the configuration allowing both select and style tags.
  2. Create an endpoint that reflects a parameter after sanitising it.
  3. Send a crafted value combining select, style and script elements.
  4. Observe that the script executes with the versions tried, including 1.4.3.

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 strip dangerous HTML from user input before a page shows it. Here the library tried to remove one risky tag combination, but only when the list of allowed tags was passed directly to the cleaning method, not when it was set globally in the app's configuration. So a crafted input could still slip a script through and run in a victim'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 Internet Bug Bounty

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.