csprefabricate 2.0.3 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.1.1](https://github.com/JamesToohey/csprefabricate/compare/v2.1.0...v2.1.1) (2026-02-21)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * Update npm-publish.yaml to fix OIDC error ([#72](https://github.com/JamesToohey/csprefabricate/issues/72)) ([7f77e3b](https://github.com/JamesToohey/csprefabricate/commit/7f77e3b4d7b8447b79d927d521f8d1042ec83655))
9
+
10
+ ## [2.1.0](https://github.com/JamesToohey/csprefabricate/compare/v2.0.22...v2.1.0) (2026-02-21)
11
+
12
+
13
+ ### Features
14
+
15
+ * Add CSP Level 3 directives and deprecation warnings ([#68](https://github.com/JamesToohey/csprefabricate/issues/68)) ([35e91fa](https://github.com/JamesToohey/csprefabricate/commit/35e91fa51374794dc6a6cb5d661fb39558605ba8))
16
+ * Improve exposed types ([#66](https://github.com/JamesToohey/csprefabricate/issues/66)) ([71028e2](https://github.com/JamesToohey/csprefabricate/commit/71028e293673353b1b966fd9acf0c59c15b54383))
17
+
18
+ ## [2.0.22](https://github.com/JamesToohey/csprefabricate/compare/v2.0.21...v2.0.22) (2026-01-30)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * **ci/cd:** Fix npm publish config (take 3) ([#47](https://github.com/JamesToohey/csprefabricate/issues/47)) ([fc6b7e6](https://github.com/JamesToohey/csprefabricate/commit/fc6b7e60e8dd4612f36ac0f58ab770b48c3ccadc))
24
+
25
+ ## [2.0.5](https://github.com/JamesToohey/csprefabricate/compare/v2.0.4...v2.0.5) (2026-01-30)
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * **ci/cd:** fix npm auth ([#45](https://github.com/JamesToohey/csprefabricate/issues/45)) ([69d60f0](https://github.com/JamesToohey/csprefabricate/commit/69d60f07b1a5f8120bf26b8571c41ee668820970))
31
+
32
+ ## [2.0.4](https://github.com/JamesToohey/csprefabricate/compare/v2.0.3...v2.0.4) (2026-01-30)
33
+
34
+
35
+ ### Bug Fixes
36
+
37
+ * **deps:** bump tar from 7.5.6 to 7.5.7 ([#42](https://github.com/JamesToohey/csprefabricate/issues/42)) ([5acbe2a](https://github.com/JamesToohey/csprefabricate/commit/5acbe2a8948934e46df8b2ef630cdacbcb336297))
38
+
3
39
  ## [2.0.3](https://github.com/JamesToohey/csprefabricate/compare/v2.0.2...v2.0.3) (2026-01-22)
4
40
 
5
41
 
package/README.md CHANGED
@@ -13,8 +13,11 @@ This project aims to make creating useful and secure CSPs a more pleasant experi
13
13
  Currently `csprefabricate`:
14
14
 
15
15
  - Validates directive names
16
+ - Supports CSP Level 3 directives (`script-src-elem`, `script-src-attr`, `style-src-elem`, `style-src-attr`, `webrtc`)
17
+ - Supports CSP Level 3 keyword sources (`'wasm-unsafe-eval'`, `'inline-speculation-rules'`, `'unsafe-allow-redirects'`, `'trusted-types-eval'`, `'report-sample'`, `'report-sha256'`, `'report-sha384'`, `'report-sha512'`, `'unsafe-webtransport-hashes'`)
16
18
  - Supports providing a list of TLDs for a given domain name
17
19
  - Provides warnings for insecure or incomplete CSP configurations, with options to disable specific warnings
20
+ - Warns about deprecated directives (`plugin-types`, `report-uri`, `block-all-mixed-content`)
18
21
 
19
22
  ## Common CSP Issues
20
23
 
@@ -25,6 +28,7 @@ By default, `csprefabricate` will warn you about common CSP issues, such as:
25
28
  - Use of `'unsafe-inline'` in `script-src`, even if nonces or hashes are present
26
29
  - Missing nonces or hashes when using `'unsafe-inline'` in `script-src`
27
30
  - Allowing `data:` in `img-src` or `media-src`
31
+ - Use of deprecated directives (`plugin-types`, `report-uri`, `block-all-mixed-content`)
28
32
 
29
33
  You can control which warnings are shown by passing an optional `WarningOptions` object to the `create` function:
30
34
 
@@ -48,6 +52,7 @@ const warningOptions: WarningOptions = {
48
52
  unsafeInline: false,
49
53
  missingNonceOrHash: false,
50
54
  dataUri: false,
55
+ deprecatedDirectives: false,
51
56
  };
52
57
 
53
58
  create(csp, warningOptions);
@@ -118,6 +123,55 @@ const cspString = create(csp);
118
123
  // "img-src 'self' *.example.com *.example.co.uk *.example.net;"
119
124
  ```
120
125
 
126
+ ### Example 4: Using CSP Level 3 Directives and Keywords
127
+
128
+ CSP Level 3 introduced more granular control over scripts and styles, plus new keyword sources:
129
+
130
+ ```typescript
131
+ import {create, Directive, ContentSecurityPolicy} from "csprefabricate";
132
+
133
+ const csp: ContentSecurityPolicy = {
134
+ [Directive.DEFAULT_SRC]: ["self"],
135
+ // Control <script> elements separately from inline event handlers
136
+ [Directive.SCRIPT_SRC_ELEM]: ["self", "https://cdn.example.com"],
137
+ [Directive.SCRIPT_SRC_ATTR]: ["none"],
138
+ // Control <style> elements separately from inline styles
139
+ [Directive.STYLE_SRC_ELEM]: ["self"],
140
+ [Directive.STYLE_SRC_ATTR]: ["unsafe-inline"],
141
+ // Allow WebAssembly compilation (but not eval)
142
+ [Directive.SCRIPT_SRC]: ["self", "wasm-unsafe-eval"],
143
+ // Control WebRTC connections
144
+ [Directive.WEBRTC]: ["allow"],
145
+ [Directive.OBJECT_SRC]: ["none"],
146
+ [Directive.BASE_URI]: ["self"],
147
+ };
148
+
149
+ const cspString = create(csp);
150
+ // "default-src 'self'; script-src-elem 'self' https://cdn.example.com; script-src-attr 'none'; style-src-elem 'self'; style-src-attr 'unsafe-inline'; script-src 'self' 'wasm-unsafe-eval'; webrtc 'allow'; object-src 'none'; base-uri 'self';"
151
+ ```
152
+
153
+ #### CSP Level 3 Keyword Sources
154
+
155
+ CSP Level 3 introduces several new keyword sources that are automatically wrapped in single quotes:
156
+
157
+ - **`'wasm-unsafe-eval'`** - Allows WebAssembly compilation without allowing JavaScript eval
158
+ - **`'inline-speculation-rules'`** - Allows inline speculation rules for prefetching
159
+ - **`'unsafe-allow-redirects'`** - Allows redirects in navigation (experimental)
160
+ - **`'trusted-types-eval'`** - Allows eval when combined with Trusted Types
161
+ - **`'report-sample'`** - Includes code samples in violation reports
162
+ - **`'report-sha256'`, `'report-sha384'`, `'report-sha512'`** - Generates hash-based reports for subresources
163
+ - **`'unsafe-webtransport-hashes'`** - Allows WebTransport connections with certificate hashes
164
+
165
+ ## Deprecated Directives
166
+
167
+ Some CSP directives have been deprecated in favor of newer alternatives. `csprefabricate` will warn you when using these directives:
168
+
169
+ - **`plugin-types`** - Never widely supported, scheduled for removal
170
+ - **`report-uri`** - Use `report-to` instead
171
+ - **`block-all-mixed-content`** - Use `upgrade-insecure-requests` instead
172
+
173
+ These directives are still functional but may be removed from future CSP specifications. You can disable these warnings by setting `deprecatedDirectives: false` in your `WarningOptions`.
174
+
121
175
  ## Baseline Recommended CSPs
122
176
 
123
177
  You can quickly generate a recommended Content Security Policy for common use cases using built-in baselines.
package/dist/helpers.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface WarningOptions {
5
5
  unsafeInline?: boolean;
6
6
  missingNonceOrHash?: boolean;
7
7
  dataUri?: boolean;
8
+ deprecatedDirectives?: boolean;
8
9
  }
9
10
  export declare function warnOnCspIssues(csp: ContentSecurityPolicy, overrides?: WarningOptions): void;
10
11
  export declare const isValidDirective: (directive: string) => boolean;
package/dist/helpers.js CHANGED
@@ -9,6 +9,7 @@ const DEFAULT_WARNINGS = {
9
9
  unsafeInline: true,
10
10
  missingNonceOrHash: true,
11
11
  dataUri: true,
12
+ deprecatedDirectives: true,
12
13
  };
13
14
  const validDirectives = [
14
15
  "default-src",
@@ -36,6 +37,12 @@ const validDirectives = [
36
37
  "trusted-types",
37
38
  "upgrade-insecure-requests",
38
39
  "block-all-mixed-content",
40
+ "script-src-elem",
41
+ "script-src-attr",
42
+ "style-src-elem",
43
+ "style-src-attr",
44
+ "webrtc",
45
+ "fenced-frame-src",
39
46
  ];
40
47
  const specialRules = [
41
48
  "none",
@@ -44,6 +51,18 @@ const specialRules = [
44
51
  "unsafe-eval",
45
52
  "strict-dynamic",
46
53
  "unsafe-hashes",
54
+ "wasm-unsafe-eval",
55
+ "inline-speculation-rules",
56
+ "unsafe-allow-redirects",
57
+ "trusted-types-eval",
58
+ "report-sample",
59
+ "report-sha256",
60
+ "report-sha384",
61
+ "report-sha512",
62
+ "unsafe-webtransport-hashes",
63
+ "allow",
64
+ "block",
65
+ "script",
47
66
  ];
48
67
  function warnOnCspIssues(csp, overrides = {}) {
49
68
  const options = { ...DEFAULT_WARNINGS, ...overrides };
@@ -102,6 +121,18 @@ function warnOnCspIssues(csp, overrides = {}) {
102
121
  }
103
122
  });
104
123
  }
124
+ // 6. Deprecated directives
125
+ if (options.deprecatedDirectives) {
126
+ if (types_1.Directive.PLUGIN_TYPES in csp) {
127
+ console.warn(`[CSPrefabricate] Directive 'plugin-types' is deprecated and may be removed in future CSP versions. This directive was never widely supported.`);
128
+ }
129
+ if (types_1.Directive.REPORT_URI in csp) {
130
+ console.warn(`[CSPrefabricate] Directive 'report-uri' is deprecated and may be removed in future CSP versions. Use 'report-to' instead.`);
131
+ }
132
+ if (types_1.Directive.BLOCK_ALL_MIXED_CONTENT in csp) {
133
+ console.warn(`[CSPrefabricate] Directive 'block-all-mixed-content' is deprecated and may be removed in future CSP versions. Use 'upgrade-insecure-requests' instead.`);
134
+ }
135
+ }
105
136
  }
106
137
  const isValidDirective = (directive) => validDirectives.includes(directive);
107
138
  exports.isValidDirective = isValidDirective;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Directive } from "./types";
2
1
  import { create } from "./utils";
3
2
  import * as Baseline from "./baseline";
4
3
  export { Baseline };
5
- export { create, Directive };
6
- export type { ContentSecurityPolicy } from "./types";
4
+ export { create };
5
+ export { Directive } from "./types";
6
+ export type { ContentSecurityPolicy, Rules, BasicDirectiveRule, BlankDirectiveRule, AllowBlockRule, RequireTrustedTypesForRule, CSPDirective, CSP, } from "./types";
7
+ export type { WarningOptions } from "./helpers";
package/dist/index.js CHANGED
@@ -34,9 +34,9 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Directive = exports.create = exports.Baseline = void 0;
37
- const types_1 = require("./types");
38
- Object.defineProperty(exports, "Directive", { enumerable: true, get: function () { return types_1.Directive; } });
39
37
  const utils_1 = require("./utils");
40
38
  Object.defineProperty(exports, "create", { enumerable: true, get: function () { return utils_1.create; } });
41
39
  const Baseline = __importStar(require("./baseline"));
42
40
  exports.Baseline = Baseline;
41
+ var types_1 = require("./types");
42
+ Object.defineProperty(exports, "Directive", { enumerable: true, get: function () { return types_1.Directive; } });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const index_1 = require("../index");
4
+ const rules = ["self", "example.com"];
5
+ const basicRule = ["self", "example.com"];
6
+ const blankRule = null;
7
+ const allowBlockRule = ["allow"];
8
+ const requireTrustedTypesForRule = ["script"];
9
+ const directive = index_1.Directive.DEFAULT_SRC;
10
+ const csp = { [directive]: rules };
11
+ const cspByName = { [index_1.Directive.SCRIPT_SRC]: basicRule };
12
+ const options = { overlyPermissive: false };
13
+ void rules;
14
+ void basicRule;
15
+ void blankRule;
16
+ void allowBlockRule;
17
+ void requireTrustedTypesForRule;
18
+ void directive;
19
+ void csp;
20
+ void cspByName;
21
+ void options;
package/dist/types.d.ts CHANGED
@@ -23,11 +23,19 @@ declare enum Directive {
23
23
  REQUIRE_TRUSTED_TYPES_FOR = "require-trusted-types-for",
24
24
  TRUSTED_TYPES = "trusted-types",
25
25
  UPGRADE_INSECURE_REQUESTS = "upgrade-insecure-requests",
26
- BLOCK_ALL_MIXED_CONTENT = "block-all-mixed-content"
26
+ BLOCK_ALL_MIXED_CONTENT = "block-all-mixed-content",
27
+ SCRIPT_SRC_ELEM = "script-src-elem",
28
+ SCRIPT_SRC_ATTR = "script-src-attr",
29
+ STYLE_SRC_ELEM = "style-src-elem",
30
+ STYLE_SRC_ATTR = "style-src-attr",
31
+ WEBRTC = "webrtc",
32
+ FENCED_FRAME_SRC = "fenced-frame-src"
27
33
  }
28
34
  type BasicDirectiveRule = Array<string | Record<string, Array<string>>>;
29
35
  type BlankDirectiveRule = null;
30
- type Rules = BasicDirectiveRule | BlankDirectiveRule;
36
+ type AllowBlockRule = Array<"allow" | "block">;
37
+ type RequireTrustedTypesForRule = ["script"];
38
+ type Rules = BasicDirectiveRule | BlankDirectiveRule | AllowBlockRule | RequireTrustedTypesForRule;
31
39
  interface ContentSecurityPolicy {
32
40
  [Directive.DEFAULT_SRC]?: BasicDirectiveRule;
33
41
  [Directive.SCRIPT_SRC]?: BasicDirectiveRule;
@@ -50,9 +58,17 @@ interface ContentSecurityPolicy {
50
58
  [Directive.MANIFEST_SRC]?: BasicDirectiveRule;
51
59
  [Directive.PREFETCH_SRC]?: BasicDirectiveRule;
52
60
  [Directive.NAVIGATE_TO]?: BasicDirectiveRule;
53
- [Directive.REQUIRE_TRUSTED_TYPES_FOR]?: BasicDirectiveRule;
61
+ [Directive.REQUIRE_TRUSTED_TYPES_FOR]?: RequireTrustedTypesForRule;
54
62
  [Directive.TRUSTED_TYPES]?: BasicDirectiveRule;
55
63
  [Directive.UPGRADE_INSECURE_REQUESTS]?: BlankDirectiveRule;
56
64
  [Directive.BLOCK_ALL_MIXED_CONTENT]?: BlankDirectiveRule;
65
+ [Directive.SCRIPT_SRC_ELEM]?: BasicDirectiveRule;
66
+ [Directive.SCRIPT_SRC_ATTR]?: BasicDirectiveRule;
67
+ [Directive.STYLE_SRC_ELEM]?: BasicDirectiveRule;
68
+ [Directive.STYLE_SRC_ATTR]?: BasicDirectiveRule;
69
+ [Directive.WEBRTC]?: AllowBlockRule;
70
+ [Directive.FENCED_FRAME_SRC]?: BasicDirectiveRule;
57
71
  }
58
- export { ContentSecurityPolicy, Rules, Directive, BasicDirectiveRule };
72
+ type CSPDirective = Directive;
73
+ type CSP = ContentSecurityPolicy;
74
+ export { ContentSecurityPolicy, Rules, Directive, BasicDirectiveRule, BlankDirectiveRule, AllowBlockRule, RequireTrustedTypesForRule, CSPDirective, CSP, };
package/dist/types.js CHANGED
@@ -28,4 +28,10 @@ var Directive;
28
28
  Directive["TRUSTED_TYPES"] = "trusted-types";
29
29
  Directive["UPGRADE_INSECURE_REQUESTS"] = "upgrade-insecure-requests";
30
30
  Directive["BLOCK_ALL_MIXED_CONTENT"] = "block-all-mixed-content";
31
+ Directive["SCRIPT_SRC_ELEM"] = "script-src-elem";
32
+ Directive["SCRIPT_SRC_ATTR"] = "script-src-attr";
33
+ Directive["STYLE_SRC_ELEM"] = "style-src-elem";
34
+ Directive["STYLE_SRC_ATTR"] = "style-src-attr";
35
+ Directive["WEBRTC"] = "webrtc";
36
+ Directive["FENCED_FRAME_SRC"] = "fenced-frame-src";
31
37
  })(Directive || (exports.Directive = Directive = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "csprefabricate",
3
- "version": "2.0.3",
3
+ "version": "2.1.1",
4
4
  "description": "Generate valid and secure Content Security Policies (CSP) with TypeScript.",
5
5
  "keywords": [
6
6
  "csp",
@@ -10,19 +10,25 @@
10
10
  "xss-protection",
11
11
  "typescript"
12
12
  ],
13
- "homepage": "https://github.com/jamestoohey/csprefabricate#readme",
13
+ "homepage": "https://github.com/JamesToohey/csprefabricate#readme",
14
14
  "bugs": {
15
- "url": "https://github.com/jamestoohey/csprefabricate/issues"
15
+ "url": "https://github.com/JamesToohey/csprefabricate/issues"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "git+https://github.com/jamestoohey/csprefabricate.git"
19
+ "url": "git+https://github.com/JamesToohey/csprefabricate.git"
20
20
  },
21
21
  "license": "MIT",
22
22
  "author": {
23
23
  "name": "James Toohey",
24
- "url": "https://github.com/jamestoohey"
24
+ "url": "https://github.com/JamesToohey"
25
25
  },
26
+ "funding": {
27
+ "type": "github",
28
+ "url": "https://github.com/sponsors/JamesToohey"
29
+ },
30
+ "type": "commonjs",
31
+ "sideEffects": false,
26
32
  "packageManager": "yarn@4.12.0",
27
33
  "exports": {
28
34
  ".": {
@@ -41,6 +47,8 @@
41
47
  ],
42
48
  "scripts": {
43
49
  "build": "tsc --project tsconfig.build.json",
50
+ "format": "prettier --write .",
51
+ "format:check": "prettier --check .",
44
52
  "functional-test": "yarn build && tsx --test src/test/functional/functional.test.js",
45
53
  "lint": "eslint .",
46
54
  "pack": "npm pack",