TLDR: Checkout the implementation example here: Live Demo
<a href="https://malicious-domain.netlify.com" target="_blank">Visit Malicious Website!</a>Okay, here we have a href attribute to a malicious website and target as _blank attribute to make it to open in a new tab.
Let us say that, the user clicks on "Visit Malicious Website!" from the above code. He gets redirected to the malicious website in a new tab.
The flow seems so plain and simple what's the possible Security risk that the user has here?
window variable's content of your current website to window.opener variable of the malicious website.window variable through window.opener it can redirect your previous website to a new Phishing website which could look similar to the actual website you opened and might even ask you to login again.The above change can be done in the malicious website by just writing the following code
if (window.opener) {
window.opener.location = 'https://www.dhilipkmr.dev';
}
A simple way is to add a rel attribute with noopener to the <a> tag.
<a href="https://malicious-domain.netlify.com" rel="noopener" target="_blank">Visit Malicious Website!</a>rel="noopener" indicates the browser to not to attach the current website's window variable to the newly opened malicious website.window.opener of the malicious website to have null as its value.So be careful when you navigate your users to a new domain that is not maintained by you.
Not always we open a new tab with a tag there are cases where you have to open it through executing javascript's window.open() like below,
function openInNewTab() {
// Some code
window.open('https://malicious-domain.netlify.com');
}<span class="link" onclick="openInNewTab()">Visit Malicious Website!</span>Here there is no mention of noopener so this results in passing window of the current website to the malicious website.
function openInNewTabWithoutOpener() {
var newTab = window.open();
newTab.opener = null;
newTab.location='https://malicious-domain.netlify.com';
}<span class="link" onclick="openInNewTabWithoutOpener()">Visit Malicious Website!</span>Here,
window.open() which opens about:blank, so it means it has not redirected to the malicious website yet.opener value of the new tab to nullopener would have been null, due to which it cannot access the window variable of the first website.Problem Solved.
But this method wont be possible in older versions of Safari, so we again have a problem.
function openInNewTabWithNoopener() {
const aTag = document.createElement('a');
aTag.rel = 'noopener';
aTag.target = "_blank";
aTag.href = 'https://malicious-domain.netlify.com';
aTag.click();
}<span class="link" onclick="openInNewTabWithNoopener()">Visit Malicious Website!</span>Here we mimic clicking on an anchor tag.
<a> tag and assign the required attributes then execute click() over it, which behaves the same way as the link is clicked.rel attribute to the tag here.Other facts:
CMD + LINK on anchor tag, chrome, firefox and Safari considers makes window.opener of the malicious website as nullCMD + LINK on an element where new tab opening is handled through javascript, the browser attaches window variable and sends it to the new tab.window.opener when used with anchor tag for all cases, to pass the window info to the new tab you have to explicitly specify rel='opener'Checkout the live implementation example here: Live Demo
None shall bypass your Security.

Thats all Folks!!!
