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

trucks-transform-csp

v1.0.5

Published

Content security policy nonce and sha checksums

Downloads

10

Readme

CSP Transform

Content security policy nonce and sha checksums

For each style and script in the shadow DOM add a nonce attribute and create content security policy HTML and text files alternatively you can use the sha option to avoid the use of attributes but you should be certain the elements will not be processed further otherwise the checksums might not match.

The generated text file is suitable for including as an HTTP header:

style-src 'self' 'nonce-9566b05df2a2e6503449f5de138e151f51a17ceb'; script-src 'self' 'nonce-fc76f6ed5eb71e5b9ceeb1298b7458e6d1bced7d'

The generated HTML file contains a <meta> element, for example:

<meta http-equiv="Content-Security-Policy" content="style-src 'self' 'nonce-9566b05df2a2e6503449f5de138e151f51a17ceb'; script-src 'self' 'nonce-fc76f6ed5eb71e5b9ceeb1298b7458e6d1bced7d'">

Install

npm i trucks-transform-csp --save-dev

For the command line interface see trucks-cli.



Usage

Use the csp key to configure this transform:

const trucks = require('trucks');

trucks(
  {
    files: ['components.html'],
    transforms: ['csp'],
    conf: {
      transforms: {
        csp: {
          sha: 'sha512',
          dir: 'build/csp'
        } 
      }
    }
  }, (err, res) => {
    if(err) {
      throw err; 
    }
    console.log(res);
  }
);

API

csp

public csp(state, conf)

Generates content security policy files for styles and scripts within the shadow DOM.

Each type is mapped to either a style_src or script_src policy using a prefix of 'self' unless disabled using the self option.

Unless the sha option is given the operation is in nonce mode which adds a nonce attribute to the matched elements, if the intention is to further process via the skate compiler you should enable the statics option so that the attribute is set as data-static-nonce.

When the sha option is specified attributes are not added but the output will be base64 encoded computed hashes of each element's content.

Generates the files csp.html containing a <meta> element describing the content security policy and a csp.txt file containing a value suitable for appending to a Content-Security-Policy HTTP header.

Use the dir, text and html options to change the output locations.

When dir is not given the default output directory is used.

Returns map of visitor functions.

See https://www.w3.org/TR/CSP2/.

  • state Object compiler state.
  • conf Object transform plugin configuration.

Options

  • self Boolean=true include 'self' in the output.
  • styles Boolean=true generate csp for styles.
  • scripts Boolean=true generate csp for scripts.
  • sha String use sha algorithm (sha256, sha384 or sha512).
  • text String=csp.txt name of the text output file.
  • html String=csp.html name of the html output file.
  • dir String override default output directory.
  • statics Boolean=false prefix attributes with data-static-.

License

MIT


Created by mkdoc on July 20, 2016