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

csp-by-api

v2.3.0

Published

Easily build a Content Security Policy (CSP) by specifying APIs by name

Downloads

70

Readme

Prevent Cross Site Scripting by building a CSP policy specifying services you use

csp-by-api makes CSP management easier by letting developers specify the services they use by name - these are then merged into the base policy to create the final CSP.

Included policies

This package itself knows the required CSP policies for:

Official policies are used wherever they're made available, and all are tested in a production app.

Usage

CSP By API doesn't implement CSP in node. Use an existing node CSP implementation like Helmet or express-csp for that. Instead, CSP By API significantly cuts down on:

  • the amount of CSP research needed
  • the amount of CSP management

For your app. For example:

var cspByAPI = require('csp-by-api')

// This is the policy for your own app only. You don't need to worry about third parties at all!
var basePolicy = {
	defaultSrc: [CSP_SELF],
	scriptSrc:  [CSP_SELF],
	styleSrc: [CSP_SELF, CSP_UNSAFE_INLINE],
	fontSrc: [],
	imgSrc: [CSP_SELF, 'data:'],
	connectSrc: [CSP_SELF],
	frameSrc: [],
	reportUri: "/csp-violation",
	reportOnly: true
}

Then add the apps you use. csp-by-api will combine them for you:

var policy = cspByAPI(basePolicy, [
	'twitter',
	'mixpanel',
	'googleFonts'
]);

Then, for example, using Express and Helmet:

var helmet = require('helmet');

app.use(helmet.contentSecurityPolicy({
	directives: policy
}));

Need another API?

Add more policies! Send a pull request to add more policies. Include a reference to an official policy if it exists, or state that there is no official policy if none exists.

I want to steal this and port it to Ruby / Elixir / Python / Java / etc.

Go for it! Just take policies.js (it's just JSON plus comments, hence .js) and make sure you regularly update from this project!

Adding custom policies

You can also create your custom policies and provide them to the API: if exampleThing is not provided by this library, you can still define it yourself and use it:

var exampleThing = {
	scriptSrc:  ['js.example.com', 'api.example.com'],
	imgSrc: ['q.example.com'],
	connectSrc: ['api.example.com'],
	frameSrc: ['js.example.com']
}

cspByAPI(basePolicy, [
	exampleThing,
	'googleFonts'
])

You should still send a pull request though!

Note

Some of these are just general notes about CSP, but you'll still find them useful

Avoiding use of script-src unsafe-inline:

You will likely need to move the content of inline scripts (<script> tags without a src) to a seperate <script src=""> tag on your server.

To include server variables in the browser without using inline JavaScript, make a non-executable <script> tag, eg:

In your server-side template:

{{# serverVars }}
	<script class="server-vars" type="application/x-configuration">
		{{{ . }}}
	</script>
{{/ serverVars }}

Then in a script tag on your server:

var serverVarsElement = document.getElementsByClassName('server-vars')[0]
if ( serverVarsElement ) {
	window.serverVars = JSON.parse(serverVarsElement.textContent);
}

Extra meta tag needed for Twitter oembed API

For Twitter, you'll also need this meta tag - see https://dev.twitter.com/web/embedded-tweets/faq:

<meta name="twitter:widgets:csp" content="on">