Easy method to define standard express headers and assist with Content-Security-Policy including social media widgets

npm install express-standard
76 downloads in the last month

express-standard NPM version Build Status Dependency Status License

Easy method to define standard express headers and assist with Content-Security-Policy (CSP) including social media widgets

Install :hammer:

npm install express-standard

Usage :wrench:

headers = require('express-standard');

    "x-powered-by": "Awesomeness"

    "Content-Security-Policy": "default-src 'self'"

headers.add_csp(area, value); // optional area, set to value
headers.add_csp_self(area);   // add 'self' to an area, default area is 'default'
headers.add_csp('https:');    // now "Basic-Content-Security": "default-src 'self' https:"
headers.add_csp('*','http:'); // add http: to all *-src 
headers.add_csp('style', ''); // allow PureCSS stylesheet
headers.add_csp_report('script', 'https:');    // report script-src events
headers.add_csp('report-uri', '/csp_report');  // set report callback
headers.add_csp_allow_unsafe('script', notEval); // if notEval is set to true then unsafe_eval is not included, default area is script 
headers.add_domain('', protocols); // default protocols: http://, https:// & ws:// 
headers.add_domain('', 'https://', true); // https only and all subdomain included
headers.add_domain('style', ''); // allow PureCss stylesheet over all protocols
headers.add_domain('script', ['cdnjs', '*']); // multiple domains can be supplied as an array
headers.add_domain('default,script,style', 'localhost'); // multiple areas comma delimited
headers.add_csp_social_widgets(); // default is ['facebook', 'twitter', 'google+']
headers.add_csp_youtube();  // allow youtube
headers.add_csp_firebase(); // allow firebase


add_csp areas are as per -src can be omitted, ex: instead of headers.add_csp('frame-src', 'http:') you could specify headers.add_csp('frame', 'http:')

add_csp_allow_unsafe() is best avoided as per content security policy; however, it is included to assist with transitioning to using CSP

For sample report-uri data and social media attribution see: content-security-policy

add_social_widgets() can accept a comma delimited string, ex: facebook,twitter

Powered By :wrench:

console.log(headers.get()) // { x-powered-by:"Awesomeness"}

Powered By Details from Application Package :wrench:

// package.json: {name:'Awesomeness', version: '2.0.0', ...}
app.use(headers.app_powered_by(true)); // option to include version
console.log(headers.get())             // { x-powered-by:"Awesomeness/2.0.0"}

SSL Only Basic Content Security Policy :bulb:

ssl_only = "default-src https:; script-src https: 'unsafe-inline'; style-src https: 'unsafe-inline'";

app.use(headers.set("Basic-Content-Security": ssl_only));

License: MIT :unlock:

npm loves you