A while back I ran across an interesting security hole
<a href="http://someurl.here" target="_blank">Link</a>
Looks innocuous enough, but there's a hole because, by default, the page that's being opened is allowing the opened page to call back into it via window.opener
. There are some restrictions, being cross-domain, but there's still some mischief that can be done
window.opener.location = 'http://gotcha.badstuff';
Now, HTML has a workaround
<a href="http://someurl.here" target="_blank" rel="noopener noreferrer">Link</a>
That prevents the new window from having window.opener
passed to it. That's fine and good for HTML, but what if you're using window.open
?
<button type="button" onclick="window.open('http://someurl.here', '_blank');"> Click Me </button>
How would you block the use of window.opener
being passed here?
To do this, press and hold the Alt key on your keyboard, then press the Tab key. Continue pressing the Tab key until the desired window is selected.
window. open(url, name, [args]) makes it easy for websites accepting user supplied URLs to be vulnerable when attackers can cause a collision on the the “name” parameter. For example, consider a genuine site embedding an iframe with name “myFrame”, and allows user to supply the url of the iframe.
That's where noopener and noreferrer come in. The values noopener and noreferrer belong to the rel attribute, and they tell the browser NOT to set the window. open property when opening a link in a new tab/window.
Definition and Usage The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.
The window.open()
call now supports the feature "noopener".
So calling window.open('https://www.your.url','_blank','noopener')
should open the new window/tab with a null window.opener
.
I'm having trouble finding a reliable list of supporting browsers (and versions) - MDN states here that
This is supported in modern browsers including Chrome, and Firefox 52+.
From my experimentation, I see it works for:
But doesn't work for:
(All tests on a PC running Windows 10...)
For backwards compatibility it may be better to combine this with t3__rry's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With