Skip to content
XSS Disclosed report Beginner

XSS Vulnerability on Image link parser

The markdown parser failed to properly escape image URLs when building an img tag, so a crafted URL containing a quote could break out of the attribute and inject JavaScript. An attacker could run arbitrary script in the context of a victim viewing the rendered content.

alberto__segura Follow

Originally disclosed on HackerOne

Bounty$256 High View original
  • Fixed in 38 hours
  • Paid in 24 days
  • Disclosed after 24 days

I found a XSS (Cross-Site Scripting) vulnerability, and it is present in the markdown parser when it tries to parse an image URL.

To reproduce the vulnerability you need to add a fake image url like:

http://host/path/to/image'onerror=alert(1);//.png

As you can see, we have an invalid image URL which finish with an image extension (PNG). By putting the ' we are able to break the "img" tag in which the image url is included by the parser and add custom code, allowing us to run Javascript code. A malicious user could use the $.getScript function to load a malicious script.

If you need more information about the vulnerability, please, feel free to reply.

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

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 5 min read.

What the report shows

The target is Discourse, and the feature involved is its Markdown parser, specifically the part that turns an image URL into an img tag. The researcher, alberto__segura, reported a cross-site scripting issue in that parser and the report was disclosed on 10 January 2017. The platform records it as a high severity, generic cross-site scripting finding with a bounty of 256.00 USD. The report does not describe a fix or a patch.

The reproduction is short. The researcher supplied a fake image URL that ends in a real image extension but carries a quote and an event handler before it:

Text
http://host/path/to/image'onerror=alert(1);//.png

According to the report, the URL is invalid but still ends in .png. The single quote is what matters: it breaks out of the img tag into which the parser places the image URL, so the attacker can add attributes of their own. In the example the added attribute is onerror=alert(1), and the trailing // comments out whatever the parser appends after the URL. The result is that JavaScript runs.

The report notes that a malicious user could go further than alert(1) and use the $.getScript function to load a malicious script. That is the whole of the technical content: there is no request capture, no server response, no version information and no discussion of where the parser runs (server side or in the browser).

Root cause

The report says the single quote breaks out of the img tag into which the parser places the image URL, allowing custom code to be added. It does not show the generated markup or describe how the parser builds it. A URL is not a safe string to drop between double quotes in HTML: a single quote is harmless inside a double-quoted attribute, but if the parser emits the URL unquoted, or inside single quotes, or otherwise fails to encode quote characters, the value can terminate the attribute and introduce new ones. The report implies this rather than showing the generated markup.

Impact

The report frames the impact as script execution in the context of the page that renders the Markdown. The report frames the impact as JavaScript execution through the crafted image URL, and notes that $.getScript could be used to load a malicious script. The researcher notes that a malicious user could use the $.getScript function to load a malicious script. The report does not discuss cookies, sessions, CSRF tokens or which users are affected, so the practical reach beyond script execution is not established in the text.

How this kind of bug works

Markdown renderers convert a small, friendly syntax into HTML. Image syntax carries a URL, and the renderer has to place that URL into an attribute of an img element. The bug class appears when the renderer treats the URL as trusted text and concatenates it into the output instead of encoding it for the HTML attribute context.

HTML parsing is forgiving. Once an attribute value is closed, anything that looks like name=value becomes a new attribute on the same element. Event handler attributes such as onerror, onload and onmouseover execute JavaScript when the corresponding event fires, and an img with a broken src fires onerror immediately. A comment marker such as // in JavaScript, or <!-- in HTML, can neutralise trailing characters the renderer adds after the URL.

The defences are straightforward: HTML-encode the URL when placing it in an attribute (encode at least ", ', <, > and &), always quote attribute values, and validate the URL scheme so that only http, https and perhaps relative paths are accepted. A Content Security Policy that disallows inline script and restricts script sources limits the damage when an injection does occur, but it is a mitigation, not a fix.

How to test for it

  1. Find every place user input reaches an HTML attribute: image URLs, link URLs, titles, alt text, avatar URLs, embed sources, and any field rendered inside a tag.
  2. Submit a value that closes the attribute and adds a harmless marker, for example a quote followed by an attribute whose effect is visible in the DOM, and inspect the rendered HTML rather than only the page.
  3. If the marker appears as a real attribute, escalate to an event handler that fires without user interaction, such as one on a broken image, and confirm execution with a benign action.
  4. Test both quote characters, since the parser may quote with one and not the other, and test values that end in a legitimate extension to see whether validation is only a suffix check.
  5. Check whether the renderer runs server side or client side; the payload may be stored and rendered to other users, which changes the impact.

A positive result is your injected attribute present in the parsed DOM and your handler executing. Common false positives are values that are reflected but HTML-encoded (the quote appears as &#39; or &quot;), payloads that only appear inside a text node, and cases where a CSP blocks the handler so nothing runs even though the markup is broken. Also beware of testing only your own browser: a payload that works in one renderer may be sanitised by another.

How it is fixed

Encode user-controlled values for the attribute context before concatenation, or build the element through a DOM API that sets attributes as values rather than parsing strings. Quote every attribute. Validate URL schemes against an allowlist and reject values containing quote characters or angle brackets. Where Markdown is rendered, use a maintained sanitiser on the resulting HTML as a second layer, and add a restrictive Content Security Policy to reduce the impact of any future injection.

Hunting notes

This bug class hides in any feature that turns user text into markup: comment and post renderers, profile fields, signatures, custom emoji, link previews, and rich text editors that round-trip through Markdown. Recon signals include a Markdown or BBCode parser in the stack, image or link embedding features, and any place where a URL is accepted and echoed back. When you find one, show impact beyond alert: demonstrate that a script can be loaded, and keep the proof minimal so the triager can reproduce it quickly.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 7 min read.

What happened, in plain words

Discourse is a forum platform: people post messages, and those messages can contain formatting such as bold text, links and images. That formatting is written in Markdown, a simple way of writing text that a program later turns into a web page. The program that does the turning is called a parser.

The researcher, alberto__segura, found that a crafted image URL in the Markdown parser could lead to JavaScript running. The report was disclosed on 10 January 2017, the platform records it as a high severity cross-site scripting issue, and a bounty of 256.00 USD was paid. The report does not describe a fix or a patch.

To reproduce it, the researcher used a fake image URL that ends in a real image extension but carries extra characters before it:

Text
http://host/path/to/image'onerror=alert(1);//.png

Here is what each piece does:

  1. ' is a single quote. In HTML, quotes are what mark the beginning and end of an attribute value. This quote is the trick: it closes the value early and lets the attacker start writing their own HTML.
  2. onerror=alert(1) is an event handler. An event handler is an instruction that says "when this thing happens, run this code". onerror fires when an image fails to load, and alert(1) is a tiny piece of JavaScript that pops up a box with the number 1 in it. onerror fires when an image fails to load, and alert(1) is a small piece of JavaScript that shows a pop-up.
  3. // starts a comment in JavaScript. Everything after it on that line is ignored, which cancels out whatever the parser adds after the URL.
  4. .png is the file extension for a PNG image.

The researcher explains that the single quote breaks out of the img tag into which the parser places the image URL, which is what allows custom code to be added. The result is that JavaScript runs. The report adds that a malicious user could go further than alert(1) and use the $.getScript function to load a malicious script, which means running a whole file of the attacker's code rather than a single pop-up.

That is the whole of the technical content. The report contains no captured request, no server response, no version information, and no discussion of whether the parser runs on the server or in the visitor's browser.

In general, how this kind of bug works

This is a cross-site scripting bug, usually shortened to XSS. The idea is simple: a website takes something a user typed and puts it into a page without cleaning it first, so the user's text is treated as instructions by the browser instead of as plain text. A comparison: imagine a form where you write your name and it is printed on a badge. If you write "Bob" you get a badge saying Bob. If the badge printer also obeys anything written on the form, and you write "Bob, and also print a hundred more badges", you get a hundred badges. The printer could not tell the difference between your name and an instruction.

Markdown renderers have a specific job: they turn friendly syntax into HTML, the language browsers use to build pages. An image in Markdown carries a URL, and the renderer has to place that URL inside an img element, in an attribute such as src. The bug appears when the renderer treats the URL as trusted text and glues it into the output instead of encoding it.

Encoding means replacing characters that have a special meaning in HTML with safe stand-ins. A quote becomes &#39; or &quot;, for example, so the browser shows a quote on screen but does not treat it as the end of an attribute.

HTML is forgiving. Once an attribute value is closed, anything that looks like name=value becomes a new attribute on the same element. Event handler attributes such as onerror, onload and onmouseover run JavaScript when the matching event happens, and an image with a broken address fires onerror immediately. A comment marker such as // in JavaScript can neutralise trailing characters the renderer adds after the URL.

The defences are straightforward:

  1. HTML-encode the URL when placing it in an attribute, covering at least ", ', <, > and &.
  2. Always put attribute values in quotes.
  3. Validate the URL scheme so that only http, https and perhaps relative paths are accepted.
  4. Add a Content Security Policy that disallows inline script and restricts where scripts can load from. This limits the damage when an injection happens, but it is a mitigation, not a fix.

How to test for it

  1. Find every place user input reaches an HTML attribute: image URLs, link URLs, titles, alt text, avatar URLs, embed sources, and any field rendered inside a tag.
  2. Submit a value that closes the attribute and adds a harmless marker, then inspect the rendered HTML rather than only looking at the page.
  3. If the marker appears as a real attribute, move up to an event handler that fires without the user doing anything, such as one on a broken image, and confirm it runs with a harmless action.
  4. Test both quote characters, since the parser may quote with one and not the other, and test values that end in a legitimate extension to see whether the validation is only a suffix check.
  5. Check whether the renderer runs on the server or in the browser. If the payload is stored and shown to other users, the impact is larger.

A positive result is your injected attribute present in the parsed page and your handler running. Common false positives are values that come back encoded (the quote appears as &#39; or &quot;), payloads that only appear as visible text, and cases where a Content Security Policy blocks the handler so nothing runs even though the markup is broken. Also beware of testing only in your own browser: a payload that works in one renderer may be cleaned up by another.

How it is fixed

Encode user-controlled values for the attribute context before gluing them in, or build the element through a DOM API that sets attributes as values rather than parsing strings. Quote every attribute. Validate URL schemes against an allowlist and reject values containing quote characters or angle brackets. Where Markdown is rendered, run a maintained sanitiser over the resulting HTML as a second layer, and add a restrictive Content Security Policy to reduce the impact of any future injection.

Where to look for it

This bug class hides in any feature that turns user text into markup: comment and post renderers, profile fields, signatures, custom emoji, link previews, and rich text editors that round-trip through Markdown. Signs worth watching for include a Markdown or BBCode parser in the stack, image or link embedding features, and any place where a URL is accepted and echoed back. When you find one, show impact beyond alert: demonstrate that a script can be loaded, and keep the proof minimal so the person triaging it can reproduce it quickly.

Glossary

  • Markdown: a simple way of writing formatted text, such as bold, links and images, that a program later converts into HTML.
  • Parser: a program that reads text and works out its structure, here turning Markdown into HTML.
  • HTML: the language browsers use to describe the structure of a page, including elements such as images and links.
  • Tag: a piece of HTML that marks an element, such as img for an image.
  • Attribute: a setting on an HTML element, written as name=value, such as the address of an image.
  • URL: the address of something on the web, such as an image file.
  • JavaScript: the programming language browsers run to make pages interactive.
  • Event handler: an instruction that runs code when something happens, such as an image failing to load.
  • onerror: an event handler that fires when an element, such as an image, fails to load.
  • alert(1): a JavaScript command that shows a pop-up box containing the number 1, often used as a harmless proof.
  • $.getScript: a function that loads and runs an external JavaScript file.
  • Comment: text that a program ignores; in JavaScript, // makes the rest of the line a comment.
  • Encoding: replacing characters that have a special meaning with safe stand-ins so they are shown, not obeyed.
  • Sanitiser: a tool that cleans HTML by removing or neutralising dangerous parts.
  • Content Security Policy (CSP): a set of rules a site sends to the browser saying which scripts are allowed to run.
  • Allowlist: a list of values that are explicitly permitted, with everything else rejected.
  • DOM: the browser's in-memory model of a page, which scripts can read and change.
  • Bounty: a payment made to a researcher who reports a security bug.
  • Severity: a rating of how serious a bug is, here recorded as high.

More by alberto__segura

View all

1 more disclosed report, best first.

More from Discourse

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.