csprefabricate 1.0.0 → 2.0.0

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,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.0.0](https://github.com/JamesToohey/csprefabricate/compare/v1.0.0...v2.0.0) (2025-06-13)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * issues when using csprefabricate with commonjs imports ([#31](https://github.com/JamesToohey/csprefabricate/issues/31))
9
+
10
+ ### Bug Fixes
11
+
12
+ * issues when using csprefabricate with commonjs imports ([#31](https://github.com/JamesToohey/csprefabricate/issues/31)) ([76d57b6](https://github.com/JamesToohey/csprefabricate/commit/76d57b641813d10557069e0d5b119853212f4544))
13
+
3
14
  ## [1.0.0](https://github.com/JamesToohey/csprefabricate/compare/v0.4.0...v1.0.0) (2025-06-02)
4
15
 
5
16
 
package/dist/baseline.js CHANGED
@@ -1,4 +1,7 @@
1
- import { Directive } from "./types";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GOOGLE_ANALYTICS_WITH_SIGNALS_CSP = exports.GOOGLE_ANALYTICS_CSP = exports.BASELINE_STRICT_CSP = void 0;
4
+ const types_1 = require("./types");
2
5
  // List of supported domains for Google Signals from https://www.google.com/supported_domains
3
6
  const googleSupportedTLDs = [
4
7
  ".com",
@@ -189,39 +192,39 @@ const googleSupportedTLDs = [
189
192
  ".co.zw",
190
193
  ".cat",
191
194
  ];
192
- export const BASELINE_STRICT_CSP = {
193
- [Directive.DEFAULT_SRC]: ["'self'"],
194
- [Directive.SCRIPT_SRC]: ["'self'"],
195
- [Directive.STYLE_SRC]: ["'self'"],
196
- [Directive.IMG_SRC]: ["'self'"],
197
- [Directive.OBJECT_SRC]: ["'none'"],
198
- [Directive.BASE_URI]: ["'self'"],
199
- [Directive.FORM_ACTION]: ["'self'"],
195
+ exports.BASELINE_STRICT_CSP = {
196
+ [types_1.Directive.DEFAULT_SRC]: ["'self'"],
197
+ [types_1.Directive.SCRIPT_SRC]: ["'self'"],
198
+ [types_1.Directive.STYLE_SRC]: ["'self'"],
199
+ [types_1.Directive.IMG_SRC]: ["'self'"],
200
+ [types_1.Directive.OBJECT_SRC]: ["'none'"],
201
+ [types_1.Directive.BASE_URI]: ["'self'"],
202
+ [types_1.Directive.FORM_ACTION]: ["'self'"],
200
203
  };
201
204
  /**
202
205
  * Google Analytics Content Security Policy based on the official guidelines.
203
206
  * https://developers.google.com/tag-platform/security/guides/csp#google_analytics_4_google_analytics
204
207
  */
205
- export const GOOGLE_ANALYTICS_CSP = {
206
- ...BASELINE_STRICT_CSP,
207
- [Directive.DEFAULT_SRC]: ["'self'"],
208
- [Directive.SCRIPT_SRC]: ["'self'", "*.googletagmanager.com"],
209
- [Directive.IMG_SRC]: [
208
+ exports.GOOGLE_ANALYTICS_CSP = {
209
+ ...exports.BASELINE_STRICT_CSP,
210
+ [types_1.Directive.DEFAULT_SRC]: ["'self'"],
211
+ [types_1.Directive.SCRIPT_SRC]: ["'self'", "*.googletagmanager.com"],
212
+ [types_1.Directive.IMG_SRC]: [
210
213
  "'self'",
211
214
  "https://*.google-analytics.com",
212
215
  "https://*.googletagmanager.com",
213
216
  ],
214
- [Directive.CONNECT_SRC]: [
217
+ [types_1.Directive.CONNECT_SRC]: [
215
218
  "'self'",
216
219
  "https://*.google-analytics.com",
217
220
  "https://*.analytics.google.com",
218
221
  "https://*.googletagmanager.com",
219
222
  ],
220
223
  };
221
- export const GOOGLE_ANALYTICS_WITH_SIGNALS_CSP = {
222
- ...BASELINE_STRICT_CSP,
223
- ...GOOGLE_ANALYTICS_CSP,
224
- [Directive.IMG_SRC]: [
224
+ exports.GOOGLE_ANALYTICS_WITH_SIGNALS_CSP = {
225
+ ...exports.BASELINE_STRICT_CSP,
226
+ ...exports.GOOGLE_ANALYTICS_CSP,
227
+ [types_1.Directive.IMG_SRC]: [
225
228
  "'self'",
226
229
  "https://*.google-analytics.com",
227
230
  "https://*.googletagmanager.com",
@@ -229,7 +232,7 @@ export const GOOGLE_ANALYTICS_WITH_SIGNALS_CSP = {
229
232
  "https://*.google.com",
230
233
  { "https://*.google.": googleSupportedTLDs },
231
234
  ],
232
- [Directive.CONNECT_SRC]: [
235
+ [types_1.Directive.CONNECT_SRC]: [
233
236
  "'self'",
234
237
  "https://*.google-analytics.com",
235
238
  "https://*.googletagmanager.com",
@@ -237,7 +240,7 @@ export const GOOGLE_ANALYTICS_WITH_SIGNALS_CSP = {
237
240
  "https://pagead2.googlesyndication.com",
238
241
  { "https://*.google": googleSupportedTLDs },
239
242
  ],
240
- [Directive.FRAME_SRC]: [
243
+ [types_1.Directive.FRAME_SRC]: [
241
244
  "'self'",
242
245
  "https://td.doubleclick.net",
243
246
  "https://www.googletagmanager.com",
package/dist/helpers.js CHANGED
@@ -1,4 +1,8 @@
1
- import { Directive } from "./types";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatRule = exports.isValidDirective = void 0;
4
+ exports.warnOnCspIssues = warnOnCspIssues;
5
+ const types_1 = require("./types");
2
6
  const DEFAULT_WARNINGS = {
3
7
  overlyPermissive: true,
4
8
  missingDirectives: true,
@@ -41,15 +45,15 @@ const specialRules = [
41
45
  "strict-dynamic",
42
46
  "unsafe-hashes",
43
47
  ];
44
- export function warnOnCspIssues(csp, overrides = {}) {
48
+ function warnOnCspIssues(csp, overrides = {}) {
45
49
  const options = { ...DEFAULT_WARNINGS, ...overrides };
46
50
  // 1. Overly permissive: * in script-src, style-src, etc.
47
51
  if (options.overlyPermissive) {
48
52
  [
49
- Directive.SCRIPT_SRC,
50
- Directive.STYLE_SRC,
51
- Directive.IMG_SRC,
52
- Directive.CONNECT_SRC,
53
+ types_1.Directive.SCRIPT_SRC,
54
+ types_1.Directive.STYLE_SRC,
55
+ types_1.Directive.IMG_SRC,
56
+ types_1.Directive.CONNECT_SRC,
53
57
  ].forEach((directive) => {
54
58
  const rules = csp[directive];
55
59
  if (Array.isArray(rules) && rules.includes("*")) {
@@ -60,9 +64,9 @@ export function warnOnCspIssues(csp, overrides = {}) {
60
64
  // 2. Missing important directives
61
65
  if (options.missingDirectives) {
62
66
  [
63
- Directive.OBJECT_SRC,
64
- Directive.BASE_URI,
65
- Directive.FORM_ACTION,
67
+ types_1.Directive.OBJECT_SRC,
68
+ types_1.Directive.BASE_URI,
69
+ types_1.Directive.FORM_ACTION,
66
70
  ].forEach((directive) => {
67
71
  if (!(directive in csp)) {
68
72
  console.warn(`[CSPrefabricate] Missing recommended directive: ${directive}`);
@@ -71,7 +75,7 @@ export function warnOnCspIssues(csp, overrides = {}) {
71
75
  }
72
76
  // 3. Unsafe inline
73
77
  if (options.unsafeInline) {
74
- [Directive.SCRIPT_SRC, Directive.STYLE_SRC].forEach((directive) => {
78
+ [types_1.Directive.SCRIPT_SRC, types_1.Directive.STYLE_SRC].forEach((directive) => {
75
79
  const rules = csp[directive];
76
80
  if (Array.isArray(rules) && rules.includes("'unsafe-inline'")) {
77
81
  console.warn(`[CSPrefabricate] 'unsafe-inline' found in ${directive}`);
@@ -80,7 +84,7 @@ export function warnOnCspIssues(csp, overrides = {}) {
80
84
  }
81
85
  // 4. Missing nonce or hash in script-src if 'unsafe-inline' is present
82
86
  if (options.missingNonceOrHash) {
83
- const rules = csp[Directive.SCRIPT_SRC];
87
+ const rules = csp[types_1.Directive.SCRIPT_SRC];
84
88
  if (Array.isArray(rules) && rules.includes("'unsafe-inline'")) {
85
89
  const hasNonceOrHash = rules.some((r) => typeof r === "string" &&
86
90
  (r.startsWith("'nonce-") || r.startsWith("'sha")));
@@ -91,7 +95,7 @@ export function warnOnCspIssues(csp, overrides = {}) {
91
95
  }
92
96
  // 5. Permitting data: in img-src or media-src
93
97
  if (options.dataUri) {
94
- [Directive.IMG_SRC, Directive.MEDIA_SRC].forEach((directive) => {
98
+ [types_1.Directive.IMG_SRC, types_1.Directive.MEDIA_SRC].forEach((directive) => {
95
99
  const rules = csp[directive];
96
100
  if (Array.isArray(rules) && rules.includes("data:")) {
97
101
  console.warn(`[CSPrefabricate] 'data:' allowed in ${directive}`);
@@ -99,5 +103,7 @@ export function warnOnCspIssues(csp, overrides = {}) {
99
103
  });
100
104
  }
101
105
  }
102
- export const isValidDirective = (directive) => validDirectives.includes(directive);
103
- export const formatRule = (rule) => specialRules.includes(rule) ? `'${rule}'` : rule;
106
+ const isValidDirective = (directive) => validDirectives.includes(directive);
107
+ exports.isValidDirective = isValidDirective;
108
+ const formatRule = (rule) => specialRules.includes(rule) ? `'${rule}'` : rule;
109
+ exports.formatRule = formatRule;
package/dist/index.js CHANGED
@@ -1,5 +1,42 @@
1
- import { Directive } from "./types";
2
- import { create } from "./utils";
3
- import * as Baseline from "./baseline";
4
- export { Baseline };
5
- export { create, Directive };
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
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
+ const utils_1 = require("./utils");
40
+ Object.defineProperty(exports, "create", { enumerable: true, get: function () { return utils_1.create; } });
41
+ const Baseline = __importStar(require("./baseline"));
42
+ exports.Baseline = Baseline;
package/dist/types.js CHANGED
@@ -1,3 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Directive = void 0;
1
4
  var Directive;
2
5
  (function (Directive) {
3
6
  Directive["DEFAULT_SRC"] = "default-src";
@@ -25,5 +28,4 @@ var Directive;
25
28
  Directive["TRUSTED_TYPES"] = "trusted-types";
26
29
  Directive["UPGRADE_INSECURE_REQUESTS"] = "upgrade-insecure-requests";
27
30
  Directive["BLOCK_ALL_MIXED_CONTENT"] = "block-all-mixed-content";
28
- })(Directive || (Directive = {}));
29
- export { Directive };
31
+ })(Directive || (exports.Directive = Directive = {}));
package/dist/utils.js CHANGED
@@ -1,5 +1,8 @@
1
- import { formatRule, isValidDirective, warnOnCspIssues, } from "./helpers";
2
- export const processRules = (rules) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = exports.processRules = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ const processRules = (rules) => {
3
6
  // Flatten and deduplicate rules
4
7
  const seen = new Set();
5
8
  for (const rule of rules) {
@@ -11,23 +14,24 @@ export const processRules = (rules) => {
11
14
  }
12
15
  }
13
16
  else {
14
- seen.add(formatRule(rule));
17
+ seen.add((0, helpers_1.formatRule)(rule));
15
18
  }
16
19
  }
17
20
  return Array.from(seen).join(" ");
18
21
  };
22
+ exports.processRules = processRules;
19
23
  /**
20
24
  * Creates a CSP string from a ContentSecurityPolicy object.
21
25
  * Filters out invalid directives and formats the CSP string.
22
26
  * @param obj - The ContentSecurityPolicy object.
23
27
  * @returns The formatted CSP string.
24
28
  */
25
- export const create = (obj, warningOptions) => {
26
- warnOnCspIssues(obj, warningOptions);
29
+ const create = (obj, warningOptions) => {
30
+ (0, helpers_1.warnOnCspIssues)(obj, warningOptions);
27
31
  const entries = Object.entries(obj);
28
32
  const cspString = entries
29
33
  .filter(([directive, _rules]) => {
30
- const isValid = isValidDirective(directive);
34
+ const isValid = (0, helpers_1.isValidDirective)(directive);
31
35
  if (!isValid) {
32
36
  console.warn(`[CSPrefabricate] "${directive}" is not a valid CSP directive and has been ignored.`);
33
37
  }
@@ -38,10 +42,11 @@ export const create = (obj, warningOptions) => {
38
42
  // Filter out non-string/object values at runtime
39
43
  const filtered = rules.filter((r) => typeof r === "string" ||
40
44
  (typeof r === "object" && r !== null));
41
- const processed = processRules(filtered);
45
+ const processed = (0, exports.processRules)(filtered);
42
46
  return processed ? `${directive} ${processed}` : `${directive}`;
43
47
  }
44
48
  return `${directive}`;
45
49
  });
46
50
  return cspString.length > 0 ? `${cspString.join("; ")};` : "";
47
51
  };
52
+ exports.create = create;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "csprefabricate",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Generate valid and secure Content Security Policies (CSP) with TypeScript.",
5
5
  "keywords": [
6
6
  "csp",
@@ -24,11 +24,11 @@
24
24
  "url": "https://github.com/jamestoohey"
25
25
  },
26
26
  "packageManager": "yarn@4.5.3",
27
- "type": "module",
28
27
  "exports": {
29
28
  ".": {
30
29
  "types": "./dist/index.d.ts",
31
- "import": "./dist/index.js"
30
+ "require": "./dist/index.js",
31
+ "default": "./dist/index.js"
32
32
  }
33
33
  },
34
34
  "main": "dist/index.js",