csp-builder
A builder tool to help generate CSPs in a type-safe way
Motivation
I had to create a CSP and found the process rather unintuitive and mistake-prone. If you've created a Content Security Policy before, there are 3 paths to choose (or a mixture):
-
Use a reporting wizard (Example)
-
Use a generator wizard (Example)
-
Manually edit policy, usually with a wash-rinse-repeat recipe
Any path you choose, you eventually end up with a big string of CSP content, very hard to edit or maintain. This is especially true if you opt in to create the CSP manually.
For that reason, I wanted a builder tool to help me with generating the string, in a type-safe way, but could not find one, so I created this tool.
Install
npm install --save-dev csp-builder
Usage
; ; ;;; ;;; csp.addDirectivenew CSP.DefaultSource.addValueregularSourceDirective .addDirectivenew CSP.FontSource.addValueextensiveSourceDirective .addDirectivenew CSP.ImageSource.addValueextensiveSourceDirective .addDirectivenew CSP.MediaSource.addValuelocalSourceDirective .addDirectivenew CSP.ObjectSource.addValue .addDirectivenew CSP.FontSource.addValueextensiveSourceDirective .addDirective new CSP.PrefetchSource.addValue .addDirective new CSP.ScriptSource.addValue .addDirective new CSP.StyleSource.addValue .addDirectivenew CSP.WorkerSource.addValuelocalSourceDirective .addDirectivenew CSP.ReportUri.setValuereportUri; console.logcsp.stringify;
See more usages in the tests
Future plans
I've noticed there are possible optimizations to be done for the CSP, especially regarding deprecations and conciseness.
License
MIT © Gilad Peleg