Skip to content
XSS Write-up Intermediate

Bypassing CSP to abusing XSS filter in edge

A CSP bypass in Microsoft Edge allowed an attacker to disable a page's CSP by appending a meta tag to the URL, which the browser's XSS filter would then modify, breaking the CSP and allowing script execution. An attacker could exploit this to run malicious scripts on a page that otherwise had CSP protection.

Xiaoyin Liu Follow

Published on medium.com

Bounty$1,500 View original

{{media:0}}

Xiaoyin Liu, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

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. 5 min read.

Context

This is a Content Security Policy (CSP) bypass in Microsoft Edge, discovered in December 2016 and disclosed in April 2018. The bypass abuses the browser's XSS filter, which is ironic: both the XSS filter and CSP exist to protect users from XSS, yet here one protection mechanism is used to defeat the other.

The XSS filter first appeared in IE 8. Its purpose is to mitigate reflected XSS attacks. When the browser loads a URL, the filter checks whether some parameters of the URL may contain XSS payloads, using a set of predefined regular expressions. For instance, http://example.com/index.php?id=100 is clearly harmless, but for a URL like http://example.com/index.php?id=<script>alert(1)</script>, the value of parameter id may be an XSS payload. To decide whether it really is a reflected XSS attack, IE/Edge checks whether the returned HTML contains the substring <script>alert(1)</script>. If it does, IE/Edge assume it is reflected from the id parameter. That assumption may not hold: the substring could be hardcoded in the page while the id parameter has no effect at all.

The XSS filter has two modes. In the default mode, IE/Edge try to destroy XSS payloads by modifying the corresponding HTML tags: if Edge assumes <script>alert(1)</script> is XSS, it changes the element to <sc#ipt>alert(1)</script>, so the DOM parser does not parse that segment as a <script> element and it does not execute. In block mode, enabled by a site with the HTTP header X-XSS-Protection: 1; mode=block, IE/Edge block the entire HTML from rendering.

A site can set a CSP in two ways: through the Content-Security-Policy HTTP response header field, or through a <meta> tag. An example of such a meta element is:

HTML
<meta http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

Setup

No accounts, roles or special tools are described. The precondition is a page that delivers its CSP through a <meta> element rather than an HTTP header, and a victim using Edge with the XSS filter in its default mode. The researcher's example page is served at http://example.com/xss.html.

Finding it

The XSS filter's matching is based on substrings in the returned HTML, not on the actual origin of those substrings. Because the filter reacts to any URL parameter whose value looks like a payload and then finds that same string in the response, a parameter that merely repeats markup already present in the page is treated as a reflected XSS. The filter then rewrites that markup in the response, even though the parameter itself had no effect on the page. When the markup being rewritten is a CSP <meta> element, the rewrite disables the policy.

Exploitation

Step 1: a page whose CSP comes from a meta tag

The page at http://example.com/xss.html contains:

HTML
<!DOCTYPE html><html> <head> <title>CSP Test</title>  <meta http-equiv="Content-Security-Policy" content="script-src 'self'"> </head> <body> <script>alert(document.domain);</script> </body></html>

The script-src 'self' policy should block alert(document.domain) from executing.

Step 2: append the meta element as a URL parameter

To bypass the CSP, the researcher asks users to visit:

Text
http://example.com/xss.html?<meta http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

The meta element is simply appended as a parameter to the URL. The XSS filter sees a parameter value that looks like markup, then finds the same string in the returned HTML (the page's own CSP meta tag), and concludes it is a reflected XSS.

Step 3: the filter rewrites the meta tag and the CSP dies

By default, Edge's XSS filter modifies the meta tag to:

HTML
<me#a http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

This kills the CSP, and the alert is executed.

Image for post
Figure 1 Screenshot of a successful CSP bypass From the original on medium.com

Figure 1 shows a screenshot of a successful CSP bypass.

Root cause

The XSS filter decides that a parameter is reflected by searching the response for the parameter's value as a substring, without verifying that the value actually originated from the parameter. A page that already contains the same markup is therefore treated as if it reflected the attacker's input, and the filter's default-mode mitigation, which corrupts the tag name (<meta> becomes <me#a), is applied to the page's own CSP meta element. The corrupted element is no longer a valid meta tag, so the policy it declared is never applied. The CSP itself was not flawed; the browser's own protective rewriting removed it before it took effect.

Impact

An attacker who can get a victim using Edge to visit a crafted URL can disable a CSP that the site delivers through a <meta> element, after which script on the page executes. The bypass depends on the CSP being delivered via <meta>: policies delivered through the Content-Security-Policy HTTP header are not vulnerable to this attack.

Fix

The vulnerability was fixed in MS17-007, released in March 2017, and assigned CVE-2017-0135. It was fixed by blocking the whole HTML page when the XSS filter detects that the query string matches any meta element, regardless of mode.

Even with the fix in place, the researcher recommends setting X-XSS-Protection: 1; mode=block, and notes that CSP policies delivered via HTTP headers are not vulnerable to this bypass.

Variants to test

  1. Deliver a CSP through a <meta> element and append that same meta element as a URL parameter. A positive result is the page rendering with the meta tag corrupted (for example <me#a ...>) and inline script executing despite the policy.
  2. A positive result is the directive being neutralised in the rendered DOM.
  3. A positive result is the meta-delivered policy being bypassed while the header-delivered one still blocks the script.
  4. A positive result would be the bypass still working; the report's fix blocks the page in both modes, so on a patched browser the page should not render at all.

Timeline

  • 12/2/2016: vulnerability reported to MSRC
  • 3/14/2017: vulnerability fixed in MS17-007 (bug bounty: $1500)

References

  • Nava, E. V. and Lindsay, D. "Abusing Internet Explorer 8's XSS Filters"
  • W3C, "Content Security Policy Level 2"
  • IE 8 XSS Filter Architecture / Implementation

Acknowledgements

The researcher thanks MSRC for fixing the bug and awarding the bounty. The article was first published in Chinese on FreeBuf.

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. 11 min read.

What happened, in plain words

A security researcher found a way to switch off one of a website's protections by using another protection built into the browser. The browser is Microsoft Edge. The protection that gets switched off is called Content Security Policy, or CSP. The protection that does the switching off is Edge's XSS filter.

That is the odd part. Both features exist to stop the same kind of attack, called cross-site scripting, or XSS. Here, one of them is turned against the other. The researcher found this in December 2016 and the details were made public in April 2018.

To follow the story you only need to know what a web page is, what a URL is, and that a browser reads the page's instructions and then runs any small programs (scripts) the page contains. Everything else is explained as it comes up.

The two protections, from zero

What XSS is

A web page can contain small programs written in a language called JavaScript. They run inside your browser. A script can do useful things, but it can also read information on the page, such as what you type into a form. Cross-site scripting (XSS) is when an attacker manages to get their own script into a page you are viewing, so that it runs as if the website itself had put it there.

One common form is reflected XSS. The attacker builds a link that carries a piece of script inside the URL. When you click it, the website takes that piece from the URL and pastes it into the page it sends back. Your browser then runs it. The website has, in effect, reflected the attacker's text back at you.

What the XSS filter does

The XSS filter is a guard built into the browser. It first appeared in Internet Explorer 8, an older Microsoft browser, and Edge inherited it. Its job is to catch reflected XSS before the script runs.

When the browser loads a URL, the filter looks at the parts of the URL that carry values, called parameters, and checks whether any of them look like a script. It does this with a set of predefined patterns, called regular expressions, which are simply rules for matching text shapes.

For example, this URL is clearly harmless:

Text
http://example.com/index.php?id=100

Here id is a parameter and 100 is its value. Nothing about 100 looks like code.

But this URL is suspicious:

Text
http://example.com/index.php?id=<script>alert(1)</script>

Here the value of id is <script>alert(1)</script>, which is the shape of a script tag. A script tag is the piece of HTML markup that tells the browser "run the code between these markers". alert(1) is a tiny script that pops up a box saying 1, and it is the standard harmless test used to prove that script can run.

So the filter has a suspect. To confirm, it checks whether the page the server sent back contains that same text, <script>alert(1)</script>. If it does, the browser assumes the website copied the parameter into the page, and that this is a reflected XSS attack.

That assumption can be wrong. The text might simply be written into the page by the website's author, while the id parameter does nothing at all. The filter cannot tell the difference, because it only searches for the text, not for where the text came from.

The filter's two modes

The filter can behave in two ways.

  1. Default mode. The browser tries to defuse the suspected script by altering the markup. If Edge thinks <script>alert(1)</script> is an attack, it rewrites the tag to <sc#ipt>alert(1)</script>. The # breaks the tag name. The browser's HTML parser, the part that reads markup and decides what each element is, no longer recognises <sc#ipt> as a script element, so it does not run the code inside.
  2. Block mode. A website can ask for this by sending an HTTP header, which is a small piece of information the server attaches to its reply, reading X-XSS-Protection: 1; mode=block. In this mode the browser refuses to display the whole page.

What CSP is

Content Security Policy is a set of rules a website gives the browser about what the page is allowed to do. The most common rule limits which scripts may run. A typical rule is script-src 'self', which means: only run scripts that come from this same website, and refuse anything else. If an attacker injects a script, the policy should stop it.

A website can deliver its CSP in two ways:

  1. In an HTTP response header, the same kind of attachment used for block mode above.
  2. In a <meta> tag inside the page's HTML. A meta tag is a general-purpose instruction to the browser that sits in the page's head section. An example of a CSP meta element is:
HTML
<meta http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

Read it piece by piece: http-equiv says "treat this as if it were an HTTP header", Content-Security-Policy names the header being imitated, and content holds the actual rule, here script-src 'self'.

The setup

No accounts, special roles or tools are needed to understand this. Two things have to be true for the trick to work:

  1. The page must deliver its CSP through a <meta> tag rather than through an HTTP header.
  2. The visitor must be using Edge with the XSS filter in its default mode.

The researcher's example page lives at http://example.com/xss.html. example.com is a placeholder domain used in documentation, not a real site.

Finding it

There is a gap in how the filter decides something is an attack. The filter matches on text found in the returned page, not on where that text came from. It reacts to any URL parameter whose value looks like markup, then searches the response for that same string. If the page already contains that markup for its own legitimate reasons, the filter treats the parameter as if it had been reflected into the page, even though the parameter changed nothing.

The filter then rewrites that markup in the response. If the markup it rewrites happens to be the page's own CSP <meta> element, the rewrite disables the policy. The website's protection is removed by the browser's other protection.

Exploitation, step by step

Step 1: a page whose CSP comes from a meta tag

The page at http://example.com/xss.html contains:

HTML
<!DOCTYPE html><html> <head> <title>CSP Test</title>  <meta http-equiv="Content-Security-Policy" content="script-src 'self'"> </head> <body> <script>alert(document.domain);</script> </body></html>

Line by line:

  • <!DOCTYPE html> tells the browser this is a modern HTML page.
  • <html> opens the page and </html> closes it.
  • <head> opens the section that holds information about the page rather than visible content.
  • <title>CSP Test</title> sets the text shown in the browser tab.
  • <meta http-equiv="Content-Security-Policy" content="script-src 'self'"> is the CSP rule. It says only scripts from the same site may run.
  • </head> closes the head section and <body> opens the visible part.
  • <script>alert(document.domain);</script> is a script that shows a pop-up containing the page's domain name. document.domain is a built-in way for a script to ask "what site am I on?".
  • </body></html> closes the page.

The point of the page is that the CSP rule script-src 'self' should stop alert(document.domain) from running. The script is written directly into the page rather than loaded from a separate file, and the policy is meant to forbid that. So on a correctly protected page, nothing should pop up.

Step 2: append the meta element as a URL parameter

To get around the CSP, the researcher asks users to visit this address:

Text
http://example.com/xss.html?<meta http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

The only change from the normal address is everything after the ?. In a URL, the ? marks the start of the query string, the part that carries parameters. Here the whole CSP meta element has been pasted in as a parameter, with no name and no value, just the raw markup.

Why do this? Because the XSS filter will now see a parameter whose value looks like markup. It then searches the returned page for that same string. The page's own CSP meta tag contains exactly that string, so the filter finds a match and concludes this is a reflected XSS attack. It has been fooled by the page's own legitimate markup.

Step 3: the filter rewrites the meta tag and the CSP dies

In default mode, Edge's XSS filter now does what it always does to a suspected script: it damages the tag. It changes the meta tag to:

HTML
<me#a http-equiv=”Content-Security-Policy” content=”script-src ‘self’”>

The # has been inserted into the tag name, turning meta into me#a. The browser no longer recognises this as a meta element, so the CSP rule inside it is never applied. With the policy gone, the page's own script is free to run, and the alert appears.

Image for post
Figure 1 Screenshot of a successful CSP bypass From the original on medium.com

Figure 1 is a screenshot showing the bypass working: the alert has executed even though the page declared a policy that should have prevented it.

Why it worked

The filter decides a parameter was reflected by searching the response for the parameter's value as plain text. It never checks that the value actually came from the parameter. A page that already contains the same markup is therefore treated as if it had echoed the attacker's input. The filter's default-mode remedy, corrupting the tag name, is then applied to the page's own CSP meta element. A corrupted element is not a valid meta tag, so the policy it declared is never applied.

The CSP itself had no flaw. The browser's own protective rewriting removed it before it could take effect.

What an attacker could do with it

Someone who can persuade a visitor using Edge to open a crafted link can switch off a CSP that the site delivers through a <meta> element. Once the policy is gone, script on the page runs. The trick only works against policies delivered in a <meta> tag. Policies delivered through the Content-Security-Policy HTTP header are not affected.

The fix

The vulnerability was fixed in MS17-007, a Microsoft security update released in March 2017, and was given the identifier CVE-2017-0135. CVE stands for Common Vulnerabilities and Exposures: a public catalogue number so that everyone can refer to the same bug unambiguously.

The fix works by blocking the whole HTML page whenever the XSS filter detects that the query string matches any meta element, in either mode. In other words, the browser stops trusting the page at all in that situation, rather than trying to repair it.

Even with the fix in place, the researcher recommends that sites set X-XSS-Protection: 1; mode=block, and points out again that CSP policies delivered through HTTP headers are not vulnerable to this bypass.

Things worth testing yourself

  1. Deliver a CSP through a <meta> element and append that same meta element as a URL parameter. A positive result is the page rendering with the meta tag corrupted, for example <me#a ...>, and inline script running despite the policy.
  2. A positive result is the directive being neutralised in the page as the browser actually builds it.
  3. A positive result is the meta-delivered policy being bypassed while the header-delivered one still blocks the script.
  4. A positive result would be the bypass still working. The fix blocks the page in both modes, so on a patched browser the page should not render at all.

Timeline

  • 12/2/2016: vulnerability reported to MSRC, Microsoft's security response centre.
  • 3/14/2017: vulnerability fixed in MS17-007 (bug bounty: $1500).

References

  • Nava, E. V. and Lindsay, D. "Abusing Internet Explorer 8's XSS Filters"
  • W3C, "Content Security Policy Level 2"
  • IE 8 XSS Filter Architecture / Implementation

Acknowledgements

The researcher thanks MSRC for fixing the bug and awarding the bounty. The article was first published in Chinese on FreeBuf.

Glossary

  • Alert: a small pop-up box a script can show; alert(1) is the standard harmless test that proves script can run.
  • Block mode: an XSS filter setting, requested by a site with the header X-XSS-Protection: 1; mode=block, in which the browser refuses to display the whole page instead of repairing it.
  • Browser: the program you use to view web pages, such as Edge.
  • Content Security Policy (CSP): a set of rules a website gives the browser about what the page may do, most often which scripts may run.
  • CVE: Common Vulnerabilities and Exposures, a public catalogue that gives each known vulnerability a unique number.
  • Default mode: the XSS filter's normal behaviour, in which it tries to defuse suspected scripts by altering the markup.
  • DOM parser: the part of the browser that reads HTML markup and decides what each element is.
  • Edge: Microsoft's web browser, which inherited the XSS filter from Internet Explorer.
  • HTTP header: a small piece of information a web server attaches to its reply, separate from the page content itself.
  • Internet Explorer 8 (IE 8): an older Microsoft browser, the first to include the XSS filter.
  • JavaScript: the programming language used for scripts that run inside web pages.
  • Meta tag: an instruction to the browser placed in the head section of a page; it can carry a CSP rule.
  • MSRC: Microsoft Security Response Center, the team that receives and handles reports of Microsoft security bugs.
  • MS17-007: the Microsoft security update, released in March 2017, that fixed this bug.
  • Parameter: a named value carried in a URL's query string, the part after the ?.
  • Query string: the part of a URL after the ? that carries parameters.
  • Reflected XSS: a cross-site scripting attack in which the attacker's script travels in a URL and is pasted into the page the site sends back.
  • Regular expression: a rule for matching text shapes, used by the filter to spot values that look like scripts.
  • Script: a small program embedded in or loaded by a web page.
  • Script tag: the HTML markup, <script>...</script>, that tells the browser to run the code between the markers.
  • URL: the address of a page, such as http://example.com/xss.html.
  • XSS: cross-site scripting, an attack in which someone else's script ends up running in a page you are viewing.
  • XSS filter: a browser guard that tries to detect and defuse reflected XSS before the script runs.

More by Xiaoyin Liu

View all

2 more disclosed reports, best first.

More from Microsoft

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.