Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prevent-window-opener-attacks

Package Overview
Dependencies
Maintainers
8
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prevent-window-opener-attacks

Prevent window.opener attacks for _blank links

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created
Source

prevent-window-opener-attacks

NPM version Size License

This extreme tiny script will prevent the default behaviour of a click on an a-tag with taget="_blank", open the link using window.open and ensure that window.opener = null.

Examples

Example without fix

Example with fix

Fix it with the rel attribute

You are able to set the rel="noreferrer noopener" attribute on an a-tag. This will also prevent to call window.opener on the location page. But this will only handle simple links.

<a href="http://example.com" target="_blank" rel="noreferrer noopener">Click me!</a>

Test your own site

Copy the following code in your dev-tools on your webpage and click on the generated link on the bottom of your page. If your page redirect after clicking the generated link, you will need this fix.

(() => {
  const a = document.createElement('a');
  a.href =
    'https://merkle-open.github.io/prevent-window-opener-attacks/example/evil-page.html';
  a.target = '_blank';
  a.innerHTML = 'Click me!';
  document.body.appendChild(a);
})();

Usage

ES Modules

Fixes the attack vector on document ready automatically

import('prevent-window-opener-attacks');

CommonJS & AMDJS

Fixes the attack vector on document ready automatically

require('prevent-window-opener-attacks');

ES Modules - direct call

Allows to call the fix explicitely

import { preventWindowOpenerAttacks } from 'prevent-window-opener-attacks/src/lib';
preventWindowOpenerAttacks();

CommonJS - direct call

Allows to call the fix explicitely

const {
  preventWindowOpenerAttacks,
} = require('prevent-window-opener-attacks/dist/lib.js');
preventWindowOpenerAttacks();

AMD JS - direct call

Allows to call the fix explicitely

require('prevent-window-opener-attacks/dist/lib.js', function ({
  preventWindowOpenerAttacks,
}) {
  preventWindowOpenerAttacks();
});

Keywords

FAQs

Package last updated on 11 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc