{"id":15699011,"url":"https://github.com/pgilad/csp-builder","last_synced_at":"2026-03-08T11:36:48.249Z","repository":{"id":56568786,"uuid":"172722498","full_name":"pgilad/csp-builder","owner":"pgilad","description":"A builder tool to help generate Content Security Policies in a type-safe way","archived":false,"fork":false,"pushed_at":"2020-10-31T02:23:41.000Z","size":36,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-25T21:44:09.139Z","etag":null,"topics":["builder","content-security-policy","csp","generator","hacktoberfest","pika","security","typescript","web"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgilad.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-26T14:01:15.000Z","updated_at":"2020-10-31T02:23:43.000Z","dependencies_parsed_at":"2022-08-15T21:10:16.611Z","dependency_job_id":null,"html_url":"https://github.com/pgilad/csp-builder","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcsp-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcsp-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcsp-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgilad%2Fcsp-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgilad","download_url":"https://codeload.github.com/pgilad/csp-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251971014,"owners_count":21673354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["builder","content-security-policy","csp","generator","hacktoberfest","pika","security","typescript","web"],"created_at":"2024-10-03T19:37:22.319Z","updated_at":"2026-03-08T11:36:48.213Z","avatar_url":"https://github.com/pgilad.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# csp-builder\n\n\u003e A builder tool to help generate CSPs in a type-safe way\n\n[![Travis (.org)](https://img.shields.io/travis/pgilad/csp-builder.svg?style=for-the-badge)](https://travis-ci.org/pgilad/csp-builder)\n[![Codecov](https://img.shields.io/codecov/c/github/pgilad/csp-builder.svg?style=for-the-badge)](https://codecov.io/gh/pgilad/csp-builder)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=for-the-badge)](https://github.com/prettier/prettier)\n\n## Motivation\n\nI had to create a [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) and found the process rather unintuitive and mistake-prone.\nIf you've created a Content Security Policy before, there are 3 paths to choose (or a mixture):\n\n1. Use a reporting wizard ([Example](https://scotthelme.co.uk/report-uri-csp-wizard/))\n\n2. Use a generator wizard ([Example](https://www.cspisawesome.com/))\n\n3. Manually edit policy, usually with a wash-rinse-repeat recipe\n\nAny path you choose, you eventually end up with a big string of CSP content, very hard to edit or maintain. This is especially\ntrue if you opt in to create the CSP manually.\n\nFor that reason, I wanted a builder tool to help me with generating the string, in a type-safe way, but could not find one,\nso I created this tool.\n\n## Install\n\n`npm install --save-dev csp-builder`\n\n## Usage\n\n```typescript\nimport * as CSP from 'csp-builder';\n\nconst csp = new CSP.Builder();\n\nconst analyticsDomain = 'www.google-analytics.com';\nconst ownDomain = 'www.giladpeleg.com';\nconst reportUri = 'https://giladpeleg.report-uri.com/r/d/csp/enforce';\n\nconst extensiveSourceDirective = [\n    CSP.PredefinedSource.Self,\n    CSP.SchemaSource.Data,\n    analyticsDomain,\n    ownDomain,\n];\nconst regularSourceDirective = [CSP.PredefinedSource.Self, analyticsDomain, ownDomain];\nconst localSourceDirective = [CSP.PredefinedSource.Self, ownDomain];\n\ncsp.addDirective(new CSP.DefaultSource().addValue(regularSourceDirective))\n    .addDirective(new CSP.FontSource().addValue(extensiveSourceDirective))\n    .addDirective(new CSP.ImageSource().addValue(extensiveSourceDirective))\n    .addDirective(new CSP.MediaSource().addValue(localSourceDirective))\n    .addDirective(new CSP.ObjectSource().addValue([CSP.PredefinedSource.None]))\n    .addDirective(new CSP.FontSource().addValue(extensiveSourceDirective))\n    .addDirective(\n        new CSP.PrefetchSource().addValue([CSP.PredefinedSource.Self, analyticsDomain, ownDomain])\n    )\n    .addDirective(\n        new CSP.ScriptSource().addValue([\n            CSP.PredefinedSource.Self,\n            CSP.PredefinedSource.UnsafeInline,\n            analyticsDomain,\n            ownDomain,\n        ])\n    )\n    .addDirective(\n        new CSP.StyleSource().addValue([\n            CSP.PredefinedSource.Self,\n            CSP.PredefinedSource.UnsafeInline,\n            ownDomain,\n        ])\n    )\n    .addDirective(new CSP.WorkerSource().addValue(localSourceDirective))\n    .addDirective(new CSP.ReportUri().setValue(reportUri));\n\nconsole.log(csp.stringify());\n```\n\nSee more usages in the [tests](./__tests__/index.spec.ts)\n\n## Future plans\n\nI've noticed there are possible optimizations to be done for the CSP, especially regarding deprecations and conciseness.\n\n## License\n\nMIT © [Gilad Peleg](https://www.giladpeleg.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgilad%2Fcsp-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgilad%2Fcsp-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgilad%2Fcsp-builder/lists"}