cspeasy
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

cspeasy

A simple way to setup Content Security Policy even with inline scripts and styles!

It digests HTML documents so you can have a strong CSP with necessary inline scripts and styles (e.g. Google Analytics).

Example

import { createServer } from "http";
import { readFile } from "fs";
import { ContentSecurityPolicy, Policies } from "../src/main";
import { resolve } from "path";
 
const server = createServer((req, res) => {
    const csp = new ContentSecurityPolicy({
        defaultSrc: Policies.None,
    });
 
    readFile(resolve("test/index.html"), null, (error, data) => {
        if (error) {
            throw error;            
        }
 
        const indexString = data.toString();
 
        const hashedCsp = csp.addDocument(indexString);
 
        res.setHeader("Content-Security-Policy", hashedCsp.getHeaderValue());
 
        res.end(indexString);
    });
});
 
const port = 3000;
 
server.listen(port, () => console.log(`listening on ${port}`));

/cspeasy/

    Package Sidebar

    Install

    npm i cspeasy

    Weekly Downloads

    5

    Version

    0.4.0

    License

    MIT

    Unpacked Size

    11.5 kB

    Total Files

    16

    Last publish

    Collaborators

    • jamesrichford