New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rollup-plugin-sri

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rollup-plugin-sri

Add subresource integrity tags to all your html files 🔒

  • 1.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
835
increased by22.08%
Maintainers
1
Weekly downloads
 
Created
Source

rollup-plugin-sri

Because web security should not be difficult. This plugin adds subresource integrity attributes to all resources imported by your html files.

Usage

Just import this plugin together with another plugin that outputs a html file, like @rollup/plugin-html.
It will then generate integrity and crossorigin attributes for all script tags and all link rel="stylesheet" tags.

import html from 'some-html-plugin' // @rollup/plugin-html for example
import sri from 'rollup-plugin-sri'

export default {
	input: 'index.js',
	output: {
		file: 'out.js',
		format: 'es'
	},
	plugins: [html(), sri()]
}

Examples

Local file

You might have a script tag in your html to include a javascript file like this:

<script src="index.js"></script>

Which then gets turned into this:

<script
	src="index.js"
	integrity="sha512-nbecVo2rGsF6Q3d4sK/sF4AmMv3eIxXpjk6Larv6iDUWeaRjjYL44RyK45vPO3Aav/ep6qTgbUAebC20uEGq8g== sha384-zFyvltviTuMi40r9uTjP6Cc/kdJy3hboH2SbOT2Q7UaXK8c4+DtTEAG16VM0H4tP"
	crossorigin="anonymous"
></script>

CDN stylesheet

Let's say you're using Bootstrap on your page, but it get dynamically injected so you can't set the integrity attribute yourself.
The html will then look something like this:

<link
	rel="stylesheet"
	href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
/>

This plugin will turn it into the following:

<link
	rel="stylesheet"
	href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
	integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
	crossorigin="anonymous"
/>

Multiple hashing algorithms

You can also specify any number of hashing algorithms to be used by the plugin as long as they are supported by your current version of nodejs.

import html from '@rollup/plugin-html' // @rollup/plugin-html for example
import sri from 'rollup-plugin-sri'

export default {
	input: 'index.js',
	output: {
		file: 'out.js',
		format: 'es'
	},
	plugins: [
		html(),
		sri({
			algorithms: ['sha1', 'md5', 'sha512']
		})
	]
}

This config will generate an output like this:

There are a few things to note though:

  1. You can in theory use any hashing algorithm you want, however most browser only support the sha family of hashing algorithms like sha256, sha384 and sha512
  2. Browser vendors are actively discouraged from using md5 and sha1 as they are not considered to be secure enough. Most modern browser won't accept them at all.
<script
	src="index.js"
	integrity="sha1-uwq/zzhXJzUJKIrPcWeL6RkTC8I= md5-illu96GKKoep5d+RUptcBw== sha512-k2VO1XnXo7MN/pqEHWCJYyn4D2d5z0FSDRvIrz4WPmw4VTPNhnSMJRvAwz2Llaij45VU35+eO3eQjydVhGggLg=="
	crossorigin="anonymous"
></script>

Options

For the most up-to-date version of the options see the autogenerated docs.

Optional active

active? : undefined | false | true

Defined in index.ts:41

Can be used to disable the plugin, for example when used together with hot-module-reloading.

default true


Optional algorithms

algorithms? : string[]

Defined in index.ts:27

A list of hashing algorithms to use when computing the integrity attribute. The hashing algorithm has to be supported by the nodejs version you're running on. Standard hash functions are: sha256, sha384 and sha512.

NOTE: While browser vendors are free to support more algorithms than those stated above, they generally do not accept sha1 and md5 hashes.

default ["sha384"]


Optional crossorigin

crossorigin? : "anonymous" | "use-credentials"

Defined in index.ts:35

You can also specify the value for the crossorigin attribute. This attribute has to be set to prevent cross-origin data leakage. The default value anonymous should be okay for normal use. see: the W3C spec for details.

default "anonymous"


Optional selectors

selectors? : string[]

Defined in index.ts:18

A list of strings you can provide that the plugin will use to match. it will then try to compute an integrity attribute for the matched tag. Currently it only matches script tags and link with rel=stylesheet as per specification. see the W3C spec for more information. The selector syntax is the same as jQuery's.

default ["script","link[rel=stylesheet]"]

Contributing

For bug reports or feature requests please create an issue on github. When working on a feature contribution, please ensure the following things:

  • Keep you code clear and readable.
  • Write unit tests for your feature (run yarn test).
  • Use the convetional-commits style when writing commit messages. ( When you're unsure about how to do this, type yarn commit ) And you'll be guided through the process.
  • Use appropriate commit types, When adding tests use test: when adding a feature use feat: etc.
  • Split up the files you commit logically not by workday or something else, for example all test belonging to a new feature should be in their own commit and not bunched together with other changes or tests for other features.

License

MIT

Keywords

FAQs

Package last updated on 17 Jul 2020

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