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

twitchextensioncsp

v1.3.2

Published

An easy way to add a CSP/Content Security Policy to an Express Server for Twitch Extension Development and Testing

Downloads

18

Readme

TwitchExtensionCSP

A simple module that provides middleware to Express, in order to add Twitch Extension Specific CSP Headers.

For Twitch Extension Development, you can provide a "local static" server to serve your Extension in a similar way to that of the Twitch CDN for Hosted Test/Released Extensions.

It uses Helmet under the hood.

Quick Start

The minimum is to provide your Extension Client ID.

Create a server.js

const express = require('express');
const app = express();

app.listen(8050, function () {
    console.log('booted express on 8050');
});

const twitchextensioncsp = require('twitchextensioncsp');
app.use(twitchextensioncsp({
    clientID: 'abcdefg123456'
}));

app.use('/extension/', express.static(__dirname + '/build/'));

This will provide, a Static Server, serving the content from /build/ on the endpoint /extension/ and uses twitchextensioncsp to define and add Content Security Policy headers.

This makes the Test server more analogous to Twitch Extensions Hosted test and Release. The CDN is on a subdomain of Twitch and your files are in a sub folder of that domain not the base/root of the domain.

Options

The following options are available and can be passed to twitchextensioncsp

| Option | Required | Type | Default | Notes | | ------ | -------- | ---- | ------- | ----- | | clientID | Yes | String | Error Thrown | Your Extension Client ID, technically not needed for "testing" but makes your CSP more accurate to Hosted Test and above | | enableRig | No | Boolean | false | If you are testing in the Twitch Extension Developer Rig, you will need to add additional items to the CSP for the Rig | | reportUri | No | URL | '' | Setup a URL to have CSP Error Reports Posted to | | imgSrc | No | Array of Strings | - | See Below | | mediaSrc | No | Array of Strings | - | See Below | | connectSrc | No | Array of Strings | - | See Below |

All of imgSrc, mediaSrc, connectSrc accept an array of Strings, (or an array of a space seperated URLs please refer to the Helmet documentation or MDN).

Generally this will be domain names and/or full paths to match what is required for a valid content security policy.

These three items "mirror" the three fields in the Capabilities tab of a Version of an Extension.

You can read more about Content Security Policy over on MDN and the various requirements for the fields.

You can test your CSP using Security Headers. Naturally this means that your Server needs to be behind SSL and accessable to the outside world! Which is generally better for testing with anyway!

NOTE: whilst you could define connectSrc as the schemasless/valud test.example.com but in some browsers this will not match wss as well as https so you are advised to specify the schema to be on the safe side. See example 3.

Capturing CSP Reports

A CSP Report is a POSTed JSON payload, with a "custom" Content Type header of application/csp-report.

To capture this in Express you'll need to tell express.json to run on a Custom Content Type

app.post('/csp/', express.json({
    type: 'application/csp-report'
}), (req,res) => {
    console.log(req.body);

    res.send('Ok');
});

Examples

  • Example 1

See test/test.js or:

const express = require('express');
const app = express();

app.listen(8050, function () {
    console.log('booted express on 8050');
});

const twitchextensioncsp = require('twitchextensioncsp');
app.use(twitchextensioncsp({
    clientID: '123123123',
    enableRig: true,
    imgSrc: [
        'https://images.example.com'
    ],
    mediaSrc: [
        'https://videos.example.com'
    ],
    connectSrc: [
        'https://api.example.com'
    ]
}));

app.use('/extension/', express.static(__dirname + '/build/'));
  • Example 2

Load Twitch Profile images from Twitch, connect to a external custom API

const express = require('express');
const app = express();

app.listen(8050, function () {
    console.log('booted express on 8050');
});

const twitchextensioncsp = require('twitchextensioncsp');
app.use(twitchextensioncsp({
    clientID: '123123123',
    imgSrc: [
        'static-cdn.jtvnw.net'
    ],
    connectSrc: [
        'https://api.example.com'
    ]
}));

app.use('/extension/', express.static(__dirname + '/build/'));
  • Example 3

Load Twitch Profile images from Twitch, connect to the Twitch API, connect to an External API and Websocket

const express = require('express');
const app = express();

app.listen(8050, function () {
    console.log('booted express on 8050');
});

const twitchextensioncsp = require('twitchextensioncsp');
app.use(twitchextensioncsp({
    clientID: '123123123',
    imgSrc: [
        'static-cdn.jtvnw.net'
    ],
    connectSrc: [
        'api.twitch.tv',
        'https://api.example.com',
        'wss://socket.example.com'
    ]
}));

app.use('/extension/', express.static(__dirname + '/build/'));
  • Example 4

A Basic CSP with a reportURI, including a reportURI Handler. This server runs at https://example.com so the reportURI is set to the same server!

const express = require('express');
const app = express();

app.listen(8050, function () {
    console.log('booted express on 8050');
});

const twitchextensioncsp = require('twitchextensioncsp');
app.use(twitchextensioncsp({
    clientID: '123123123',
    imgSrc: [
        'static-cdn.jtvnw.net'
    ],
    connectSrc: [
        'api.twitch.tv',
        'https://api.example.com',
        'wss://socket.example.com'
    ],
    reportUri: 'https://example.com/csp/'
}));

/*
This will capture any CSP Report and dump log it to console
*/
app.post('/csp/', express.json({
    type: 'application/csp-report'
}), (req,res) => {
    res.send('Ok');

    console.log(req.body);
});

app.use('/extension/', express.static(__dirname + '/build/'));

An Alternative CSP Report handler, instead of brain dumping the WHOLE report this should extract the relevant message in a more friendly way

app.post('/csp/', express.json({
    type: 'application/csp-report'
}), (req,res) => {
    res.send('Ok');

    if (req.body.hasOwnProperty('csp-report')) {
        console.error(
            "%s blocked by %s in %s",
            req.body['csp-report']['blocked-uri'],
            req.body['csp-report']['violated-directive'],
            req.body['csp-report']['source-file']
        );
        return;
    }
    console.log(req.body);
});