npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

insert-nonces

v1.0.7

Published

insert a string (nonce) for each script, style, and link (rel="stylesheet" only) element in a HTML file.

Downloads

10

Readme

What is insert-nonces ?

This package is a function which takes a string of html and a nonce value as inputs and returns the html with nonce attributes with the given nonce value for each inline style, script, and link rel="stylesheet" element. You must provide the html and nonce value.

<style>/*some style*/</style> becomes <style nonce="your-nonce-value">/*some style*/</style>

Why ?

To be able to use inline styles/scripts securely, by setting a whitelisting Content Security Policy (CSP). It is recommended to not use 'unsafe-inline' due to the potential for an XSS attack, and instead use a nonce or hash. This solution uses nonces.

How ?

  1. Set the CSP using the same nonce you use in the HTML. I recommend Helmet which allows you to easily set CSP headers.
  2. Insert the same nonce into your HTML on request. Change the nonce value for each request, or else an attacker can just use the static nonce when attempting to inject code. Follow the usage instructions below to complete this step.

Usage

insertNonces(htmlString, nonceString);

or

insertNonces({html:htmlString, nonce:nonceString});

If you use an object, you can disable style, script, and/or link nonces from being generated.

//this will only generate nonces on style elements in your html file
insertNonces({html:htmlString, nonce:nonceString, script:false, link:false});
Example:

It makes more sense to use a stream, than to read the whole html file into memory, but here is a naive example to get you started.

const fs = require('fs'); //to obtain your html as a string
const uuidV4 = require('uuid/v4'); //to generate a unique nonce
const insertNonces = require('insert-nonces'); //to insert this nonce into your html
let nonce;

//the below should be run on each request, to generate a new nonce and insert it into the html.
nonce = uuidV4();

//a stream would make more sense here...
fs.readFile('index.html', 'utf-8', (err, html) => {
	if (err) return err;
	let noncifiedHTML = insertNonces(html, nonce);
	console.log("nocifiedHTML:", noncifiedHTML);
	//For example in Express you may want to use: res.send(noncifiedHTML);
}));

Installation

npm install --save insert-nonces
For newbies:
  1. Install NodeJS if not already done.
  2. Open your chosen CLI and navigate to your project folder.
  3. If you have not yet initialized the project, use npm init to generate a package.json
  4. Use npm install --save insert-nonces to save this package as a dependency of your project.
  5. Require the package in your javascript with const insertNonces = require("insert-nonces");
  6. Run the project via CLI using node yourFile.js

Limitations

When setting nonce for link css elements, it must be exactly: '<link rel="stylesheet"' in order for it to insert the nonce ('<link nonce="your-nonce-value" rel="stylesheet"').

License

MIT