Anchor Tag XSS Exploitation in Firefox with Target=”_blank”
The report describes a way to bypass Firefox's restriction on accessing document.cookie when an anchor tag with a javascript: URL has target="_blank". By opening a new window or writing an iframe, an attacker can still read the victim's cookies and domain, and the author used this to steal OAuth tokens.
Published on soroush.me
Commonly, we use the JavaScript schema to exploit a cross-site scripting (XSS) issue, particularly when the href attribute of an anchor tag is within our control. Here’s an example:
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.
Context
When an anchor tag's href attribute is under our control, the usual way to turn that into cross-site scripting is the javascript: scheme. A minimal example looks like this:
<a href="javascript:alert('Domain:' + document.domain + '\nCookies:' + document.cookie)">simple XSS</a>
Clicking that link runs the script in the page's origin, so document.domain and document.cookie are both readable. This is the classic reflected or stored XSS pattern that every tester reaches for first.
Modern browsers change the picture when the same anchor also carries target="_blank". In that case the browser opens the link in a new browsing context, and the JavaScript in the javascript: URL either does not execute or runs without access to significant DOM objects such as document.cookie. The report uses this markup to illustrate the blocked case:
target="_blank" has been used:
<a href="javascript:alert('Domain:' + document.domain + '\nCookies:' + document.cookie)" target="_blank">Here is a link, can you get both document.domain and document.cookie?</a>
The write-up is about finding a way around that restriction in Firefox, so that an XSS that would otherwise be neutered by target="_blank" can still reach the original window's objects.
Setup
The report does not describe a specific target application, account, role or tooling. The researcher worked against the latest version of Firefox at the time of writing and also tested Chrome. The proof-of-concept pages are hosted on the researcher's own domain, 20.rs, under /pocs/target_blank/. The only precondition is an injection point where an attacker controls the href of an anchor tag that also has target="_blank".
Finding it
The starting point was prior public work. DCLabs explained in their post at http://blog.dclabs.com.br/2021/05/the-curious-case-of-xss-and-mouse.html that a javascript: payload on a target="_blank" link can be activated with a middle-mouse click or with a SHIFT, CTRL or ALT plus click key combination. That technique works in Chrome. In Firefox, however, it still blocks access to some DOM objects such as document.cookie, while allowing document.domain.
The researcher then looked for a way to reach the original window objects in Firefox as well.
Exploitation
The report gives two proof-of-concept payloads. Both are placed in the href of the anchor and are meant to demonstrate access to the original window objects.
Method 1: Using a new window
javascript:w=window.open('https://20.rs/pocs/target_blank/anchor_pocs1.php?canbeframed=0','_blank');alert(/delay/);alert('Domain:' + w.document.domain + '\nCookies:' + w.document.cookie);
The payload assigns the result of window.open to w, opening the PoC page in a new window. The payload then calls alert(/delay/) before the final alert. The final alert then reads w.document.domain and w.document.cookie through the handle and displays both. The canbeframed=0 query parameter is part of the PoC page's own behaviour; the report does not explain it further.
Method 2: Using a frame
javascript:document.write(%22<iframe name=myfrm src='https://20.rs/pocs/target_blank/anchor_pocs1.php' onload='alert(`Domain:` %2b this.contentWindow.document.domain %2b `\nCookies:` %2b this.contentWindow.document.cookie)'></iframe>%22);document.close();
This variant writes an iframe into the document with document.write, using percent-encoded quotes (%22) and plus signs (%2b) so the markup survives inside the javascript: URL. The iframe is named myfrm and loads the same PoC page. Its onload handler reads this.contentWindow.document.domain and this.contentWindow.document.cookie and concatenates them into an alert. The trailing document.close() finishes the document written by document.write.
Both payloads were used in a real bug bounty project to amplify the impact of an XSS issue where the target attribute was set to _blank. With this method the researcher was able to take victims' OAuth tokens in both Chrome and Firefox. In that specific case the report was quickly marked as a duplicate and immediately closed.
PoC link to try
The researcher also published a proof-of-concept page for anyone who wants to try it:
https://20.rs/pocs/target_blank/anchor_pocs1.php
Root cause
The report does not analyse the underlying cause in Firefox; it only shows payloads that reach the window objects. The restriction is therefore not a complete isolation of the original window's objects, only a limit on direct access. The report notes that this was reported to Firefox about four months before writing; the vendor added it to their to-do list but, in the researcher's view, is unlikely to address it soon because it is a small bug.
Impact
Where an attacker controls the href of an anchor with target="_blank", the technique allows access to the original window objects in Firefox. In the researcher's bug bounty case this was enough to take victims' OAuth tokens in both Chrome and Firefox.
Fix
The Firefox issue was reported and placed on the vendor's to-do list, with no change made at the time of writing. The researcher's stated hope is that bug bounty hunters can earn more in future by showing that this issue affects more than just Chrome and Edge browsers using the methods described.
Variants to test
- Middle-mouse click and modifier clicks. On a
target="_blank"anchor with ajavascript:href, try a middle-mouse click and SHIFT, CTRL and ALT plus click. - Window handle via
window.open. Use the Method 1 payload against a Firefox target. A positive result is an alert showing the PoC page'sdocument.domainanddocument.cookieread through thewhandle. - Frame via
document.write. Use the Method 2 payload. A positive result is the iframe'sonloadalert showingcontentWindow.document.domainandcontentWindow.document.cookie. - OAuth token theft. Apply either method in a flow where the victim's OAuth tokens are reachable from the page. The researcher states he was able to take victims' OAuth tokens in both Chrome and Firefox in that case.
What happened, in plain words
Imagine a website lets you type a link address into a box, and the site then puts whatever you typed into a clickable link on the page. Normally that is harmless: you type https://example.com, people click, they go to example.com. But browsers also understand a special kind of link address that starts with javascript:. Instead of sending the browser to another page, that kind of link runs a small program (a script) inside the page you are already on. If an attacker can get their own javascript: link onto a site other people use, then anyone who clicks it runs the attacker's script on that site. That is the whole idea behind cross-site scripting, usually shortened to XSS: tricking a site into running code that the site's owners never intended.
A script running on a page can read things that belong to that page, such as the site's name (document.domain) and the small pieces of data the site stored in the browser (document.cookie). Cookies often hold login sessions, so reading them is a serious problem. The classic XSS demo is therefore a link that pops up an alert showing the domain and the cookies, proving the script really is running with the site's own privileges.
Modern browsers noticed this trick and added a guard. If the link is also marked to open in a new tab or window (an attribute called target="_blank"), the browser treats the script as belonging to a fresh, separate context, and it either refuses to run the script or runs it without letting it read the sensitive objects like document.cookie. That guard is what this report is about defeating.
A researcher found that in Firefox the guard can be worked around. The report gives two payloads that reach the window objects in Firefox despite target="_blank". The report gives two ready-made payloads that do exactly this, and describes how the researcher used the trick in a real bug bounty programme to steal victims' OAuth tokens, which are the credentials apps use to act on your behalf after you log in with another service. The report was marked as a duplicate and closed, but the technique itself is the interesting part.
Background: what all the pieces are
Before walking through the payloads, here is the vocabulary you need. None of it requires any prior security knowledge, just a rough idea of how web pages work.
- HTML is the language web pages are written in. It is made of tags in angle brackets, like
<a>for a link or<iframe>for an embedded frame. Each tag can carry attributes, which are extra settings written asname="value". - An anchor tag is the
<a>tag, the one that makes a clickable link. Itshrefattribute holds the address the link points to. If a site lets an attacker control that address, the attacker controls where clicking the link goes, and in the worst case what code runs. - The
javascript:scheme is a way of writing a link address so that, instead of navigating somewhere, the browser runs the text after the colon as a script. It is the oldest and simplest way to turn a controllablehrefinto XSS. target="_blank"is an attribute on a link telling the browser to open the destination in a new tab or window rather than the current one. It is extremely common on real sites, for example on links to external sites so users do not leave the page.documentis the object a script uses to talk to the page it is running in.document.domainis the site's name, anddocument.cookieis the collection of cookies the site has stored for that page.- A cookie is a small piece of text a site asks the browser to keep and send back on every request. It is how sites remember that you are logged in. If an attacker's script can read a cookie, it can often impersonate you.
- An OAuth token is a credential issued after you log in to one service and let another service act for you (the "Log in with..." buttons). Stealing one lets an attacker use the victim's account on the connected service without knowing the password.
- A window handle is a reference a script keeps to another window it opened. Once you have the handle, you can reach into that window and read its
document, which is the trick this report relies on. - An iframe is a page embedded inside another page, like a small television screen showing another channel. A script can create one on the fly and then read the embedded page through it.
document.writeis a function that inserts HTML into the page while it is still loading. It is a blunt tool, but it lets a script build new elements, such as an iframe, from a string.- Percent-encoding is how special characters are written inside a URL so they do not break it. A double quote becomes
%22, and a plus sign becomes%2b. You will see both in the second payload. - A proof of concept (PoC) is a small, safe demonstration that a bug is real, usually a page the researcher hosts themselves so anyone can try it.
The starting point: a simple XSS link
The report begins with the most basic version of the attack. When the href of an anchor tag is under our control, the usual way to turn that into cross-site scripting is the javascript: scheme. A minimal example looks like this:
<a href="javascript:alert('Domain:' + document.domain + '\nCookies:' + document.cookie)">simple XSS</a>
Line by line, in plain words:
<a ...>opens an anchor tag, the thing that makes a clickable link.href="javascript:..."sets the link's destination to a script instead of a web address. The browser will run whatever comes afterjavascript:when the link is clicked.alert(...)is the browser's built-in pop-up box. Whatever you pass to it is shown to the user.'Domain:' + document.domainbuilds the text for the pop-up: the literal wordDomain:followed by the site's own name. The+joins the two pieces of text together.'\nCookies:' + document.cookieadds a line break (\n) and then the wordCookies:followed by the page's cookies.">simple XSS</a>closes the attribute and the tag, and gives the link the visible textsimple XSS.
Clicking that link runs the script in the page's own context, so document.domain and document.cookie are both readable. This is the classic reflected or stored XSS pattern that every tester reaches for first. "Reflected" means the malicious input bounces straight back from the server in the page it returns; "stored" means the input is saved on the server and shown to other users later. Either way, the effect is the same: the attacker's script runs as if the site itself had written it.
The guard: what target="_blank" changes
Modern browsers change the picture when the same anchor also carries target="_blank". In that case the browser opens the link in a new browsing context, and the JavaScript in the javascript: URL either does not execute or runs without access to significant DOM objects such as document.cookie. The report uses this markup to illustrate the blocked case:
target="_blank" has been used:
<a href="javascript:alert('Domain:' + document.domain + '\nCookies:' + document.cookie)" target="_blank">Here is a link, can you get both document.domain and document.cookie?</a>
Line by line:
- The first line is just a note in the page, telling the reader that
target="_blank"has been used. It is not code. - The anchor tag is identical to the previous example, with the same
javascript:payload in thehref. - The new part is
target="_blank", which tells the browser to open the result in a new tab or window. - The visible link text is now a challenge:
Here is a link, can you get both document.domain and document.cookie?
The point of the example is that the same payload that worked before now fails, or at least cannot read the cookies, because the browser has put the script in a separate context. The write-up is about finding a way around that restriction in Firefox, so that an XSS that would otherwise be neutered by target="_blank" can still reach the original window's objects.
Setting up the test
The report does not describe a specific target application, account, role or tooling. The researcher worked against the latest version of Firefox at the time of writing and also tested Chrome. The proof-of-concept pages are hosted on the researcher's own domain, 20.rs, under /pocs/target_blank/. The only precondition is an injection point where an attacker controls the href of an anchor tag that also has target="_blank".
In other words, you do not need a special lab. You need a page where you can put your own link, and a browser to click it in. The researcher's own PoC page is the safe target used in the examples, so nobody else's site is involved.
Finding the way around it
The starting point was prior public work. DCLabs explained in their post at http://blog.dclabs.com.br/2021/05/the-curious-case-of-xss-and-mouse.html that a javascript: payload on a target="_blank" link can be activated with a middle-mouse click or with a SHIFT, CTRL or ALT plus click key combination. That technique works in Chrome. In Firefox, however, it still blocks access to some DOM objects such as document.cookie, while allowing document.domain.
The researcher then looked for a way to reach the original window objects in Firefox as well.
The two payloads
The report gives two proof-of-concept payloads. Both are placed in the href of the anchor and are meant to demonstrate access to the original window objects.
Method 1: Using a new window
javascript:w=window.open('https://20.rs/pocs/target_blank/anchor_pocs1.php?canbeframed=0','_blank');alert(/delay/);alert('Domain:' + w.document.domain + '\nCookies:' + w.document.cookie);
Line by line:
javascript:marks the whole thing as a script to run when the link is clicked, rather than an address to visit.w=window.open(...)asks the browser to open a new window and stores a handle to it in a variable calledw. The handle is the key: throughw, the script can later reach into that new window.'https://20.rs/pocs/target_blank/anchor_pocs1.php?canbeframed=0'is the address the new window loads. It is the researcher's own PoC page. The?canbeframed=0part is a query parameter, a setting passed to the page in the address itself.'_blank'tellswindow.opento open a fresh window rather than reusing an existing one.alert(/delay/)pops up a box containing the worddelay.alert('Domain:' + w.document.domain + '\nCookies:' + w.document.cookie)is the payoff. It reads the domain and cookies through thewhandle, that is, from inside the newly opened window, and shows them in a pop-up.
The canbeframed=0 query parameter is part of the PoC page's own behaviour; the report does not explain it further.
Method 2: Using a frame
javascript:document.write(%22<iframe name=myfrm src='https://20.rs/pocs/target_blank/anchor_pocs1.php' onload='alert(`Domain:` %2b this.contentWindow.document.domain %2b `\nCookies:` %2b this.contentWindow.document.cookie)'></iframe>%22);document.close();
Line by line:
javascript:again marks the link as a script.document.write(...)inserts HTML into the current page while it is still being built. The argument is a string of HTML, which is why the quotes inside it are encoded.%22is the percent-encoded form of a double quote. It is used so the quote does not end the surrounding string too early. The same applies to%2b, which is an encoded plus sign.<iframe name=myfrm src='https://20.rs/pocs/target_blank/anchor_pocs1.php' ...>creates an embedded frame namedmyfrmthat loads the same PoC page.onload='...'is an attribute that tells the browser to run the script inside the quotes once the frame has finished loading. This is the moment the frame's contents become readable.alert(...)builds a pop-up from two pieces joined with%2b(an encoded+):this.contentWindow.document.domainandthis.contentWindow.document.cookie.thisrefers to the iframe element, andcontentWindowis the window inside it, so the script reads the domain and cookies of the embedded page.document.close()finishes the document thatdocument.writestarted, telling the browser there is no more content to add.
Both payloads were used in a real bug bounty project to amplify the impact of an XSS issue where the target attribute was set to _blank. With this method the researcher was able to take victims' OAuth tokens in both Chrome and Firefox. In that specific case the report was quickly marked as a duplicate and immediately closed.
A duplicate means someone else had already reported the same issue.
PoC link to try
The researcher also published a proof-of-concept page for anyone who wants to try it:
https://20.rs/pocs/target_blank/anchor_pocs1.php
This is the page both payloads load. Opening it yourself lets you see the demonstration without touching anyone else's site.
Why it happens
The report does not analyse the underlying cause in Firefox; it only shows payloads that reach the window objects.
The report notes that this was reported to Firefox about four months before writing; the vendor added it to their to-do list but, in the researcher's view, is unlikely to address it soon because it is a small bug.
What the impact is
Where an attacker controls the href of an anchor with target="_blank", the technique allows access to the original window objects in Firefox. In the researcher's bug bounty case this was enough to take victims' OAuth tokens in both Chrome and Firefox.
Was it fixed?
The Firefox issue was reported and placed on the vendor's to-do list, with no change made at the time of writing. The researcher's stated hope is that bug bounty hunters can earn more in future by showing that this issue affects more than just Chrome and Edge browsers using the methods described.
Variants to test
If you want to try this yourself, here is the same set of experiments the report describes, in the order it presents them. Each one is a small, self-contained test.
- Middle-mouse click and modifier clicks. On a
target="_blank"anchor with ajavascript:href, try a middle-mouse click and SHIFT, CTRL and ALT plus click. - Window handle via
window.open. Use the Method 1 payload against a Firefox target. A positive result is an alert showing the PoC page'sdocument.domainanddocument.cookieread through thewhandle. If you see the domain and cookies in the pop-up, the guard has been bypassed. - Frame via
document.write. Use the Method 2 payload. A positive result is the iframe'sonloadalert showingcontentWindow.document.domainandcontentWindow.document.cookie. This is the same idea as Method 1, but through a frame instead of a separate window. - OAuth token theft. Apply either method in a flow where the victim's OAuth tokens are reachable from the page. The researcher states he was able to take victims' OAuth tokens in both Chrome and Firefox in that case.
Glossary
- Anchor tag: the HTML
<a>element that creates a clickable link; itshrefattribute holds the destination. - Attribute: an extra setting on an HTML tag, written as
name="value", such ashrefortarget. - Cookie: a small piece of text a site stores in the browser and sends back on each request, often used to keep you logged in.
- Cross-site scripting (XSS): a bug where an attacker gets their own script to run inside someone else's website, usually by injecting it through input the site displays.
document: the object a script uses to interact with the page it runs in;document.domainis the site name anddocument.cookieis the page's cookies.document.write: a function that inserts HTML into the page while it is loading.- DOM (Document Object Model): the browser's in-memory model of a page, which scripts read and change; "DOM objects" are the pieces of that model, such as
document. href: the attribute of an anchor tag that holds the address the link points to.- HTML: the language web pages are written in, made of tags in angle brackets.
- Iframe: a page embedded inside another page, created with the
<iframe>tag. javascript:scheme: a link address that runs the text after the colon as a script instead of navigating to a page.- OAuth token: a credential issued after you log in to one service and let another act for you; stealing one lets an attacker use your account without your password.
- Payload: the piece of code or input an attacker uses to trigger a bug.
- Percent-encoding: writing special characters in a URL as
%followed by a code, such as%22for a double quote and%2bfor a plus sign. - Proof of concept (PoC): a small, safe demonstration that a bug is real, usually hosted by the researcher.
- Query parameter: a setting passed in a URL after a
?, such ascanbeframed=0. - Reflected XSS: XSS where the malicious input is returned immediately in the server's response.
- Stored XSS: XSS where the malicious input is saved on the server and shown to other users later.
target="_blank": an attribute telling the browser to open a link in a new tab or window.- Window handle: a reference a script keeps to another window it opened, used to reach into that window's
document. window.open: a browser function that opens a new window and returns a handle to it.