@jamesbrightman/pretty-csp 0.1.0 → 0.2.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 +50 -0
- package/README.md +61 -25
- package/dist/{chunk-TE3J7Y25.js → chunk-JAXLODN4.js} +47 -363
- package/dist/chunk-JAXLODN4.js.map +1 -0
- package/dist/{chunk-MDUQKU6Y.js → chunk-YMAXUCI4.js} +2 -3
- package/dist/{chunk-MDUQKU6Y.js.map → chunk-YMAXUCI4.js.map} +1 -1
- package/dist/cli.cjs +57 -675
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +43 -352
- package/dist/cli.js.map +1 -1
- package/dist/{index-D6PmUWjD.d.cts → index-CvwFjLiu.d.cts} +4 -29
- package/dist/{index-D6PmUWjD.d.ts → index-CvwFjLiu.d.ts} +4 -29
- package/dist/index.cjs +48 -364
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -51
- package/dist/index.d.ts +14 -51
- package/dist/index.js +13 -17
- package/dist/testing.cjs +1 -2
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +7 -4
- package/dist/testing.d.ts +7 -4
- package/dist/testing.js +1 -1
- package/package.json +18 -7
- package/website/static/img/how-it-works.svg +1 -1
- package/website/static/img/pretty-csp-icon.svg +6 -6
- package/dist/chunk-TE3J7Y25.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -20,28 +20,26 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
|
|
23
|
+
NONCE: () => NONCE,
|
|
24
|
+
NONE: () => NONE,
|
|
24
25
|
Policy: () => Policy,
|
|
26
|
+
REPORT_SAMPLE: () => REPORT_SAMPLE,
|
|
27
|
+
SELF: () => SELF,
|
|
28
|
+
STRICT_DYNAMIC: () => STRICT_DYNAMIC,
|
|
29
|
+
WASM_UNSAFE_EVAL: () => WASM_UNSAFE_EVAL,
|
|
25
30
|
audit: () => audit,
|
|
26
31
|
baseDirectives: () => baseDirectives,
|
|
27
32
|
baseSchema: () => baseSchema,
|
|
28
33
|
cspHash: () => cspHash,
|
|
29
34
|
definePolicy: () => definePolicy,
|
|
30
35
|
defineRuleSet: () => defineRuleSet,
|
|
31
|
-
diffPolicies: () => diffPolicies,
|
|
32
36
|
generateNonce: () => generateNonce,
|
|
33
|
-
importHeader: () => importHeader,
|
|
34
|
-
importedPolicyToDraft: () => importedPolicyToDraft,
|
|
35
37
|
matchPolicy: () => matchPolicy,
|
|
36
|
-
parseViolationReports: () => parseViolationReports,
|
|
37
38
|
policyConfigSchema: () => policyConfigSchema,
|
|
38
|
-
replayReports: () => replayReports,
|
|
39
39
|
resourceDirectives: () => resourceDirectives,
|
|
40
40
|
ruleSetSchema: () => ruleSetSchema,
|
|
41
41
|
rulesSchema: () => rulesSchema,
|
|
42
|
-
serializeImportedPolicy: () => serializeImportedPolicy,
|
|
43
42
|
sourceEntrySchema: () => sourceEntrySchema,
|
|
44
|
-
sourceExpressions: () => sourceExpressions,
|
|
45
43
|
unsafeEval: () => unsafeEval,
|
|
46
44
|
unsafeHashes: () => unsafeHashes,
|
|
47
45
|
unsafeInline: () => unsafeInline
|
|
@@ -50,6 +48,34 @@ module.exports = __toCommonJS(src_exports);
|
|
|
50
48
|
|
|
51
49
|
// src/schemas.ts
|
|
52
50
|
var import_zod = require("zod");
|
|
51
|
+
|
|
52
|
+
// src/keywords.ts
|
|
53
|
+
var SELF = "'self'";
|
|
54
|
+
var NONE = "'none'";
|
|
55
|
+
var NONCE = "'nonce-{nonce}'";
|
|
56
|
+
var STRICT_DYNAMIC = "'strict-dynamic'";
|
|
57
|
+
var REPORT_SAMPLE = "'report-sample'";
|
|
58
|
+
var WASM_UNSAFE_EVAL = "'wasm-unsafe-eval'";
|
|
59
|
+
function generateNonce() {
|
|
60
|
+
const bytes = new Uint8Array(16);
|
|
61
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
62
|
+
return btoa(String.fromCharCode(...bytes));
|
|
63
|
+
}
|
|
64
|
+
var unsafeInline = ({ reason }) => ({
|
|
65
|
+
source: "'unsafe-inline'",
|
|
66
|
+
reason
|
|
67
|
+
});
|
|
68
|
+
var unsafeEval = ({ reason }) => ({
|
|
69
|
+
source: "'unsafe-eval'",
|
|
70
|
+
reason
|
|
71
|
+
});
|
|
72
|
+
var unsafeHashes = ({ reason }) => ({
|
|
73
|
+
source: "'unsafe-hashes'",
|
|
74
|
+
reason
|
|
75
|
+
});
|
|
76
|
+
var cspHash = (algorithm, digest) => `'${algorithm}-${digest}'`;
|
|
77
|
+
|
|
78
|
+
// src/schemas.ts
|
|
53
79
|
var resourceDirectives = {
|
|
54
80
|
scripts: "script-src",
|
|
55
81
|
connect: "connect-src",
|
|
@@ -77,11 +103,10 @@ var keywords = /* @__PURE__ */ new Set([
|
|
|
77
103
|
"'wasm-unsafe-eval'",
|
|
78
104
|
...risky
|
|
79
105
|
]);
|
|
80
|
-
var NONCE_PLACEHOLDER = "'nonce-{nonce}'";
|
|
81
106
|
function isSource(value) {
|
|
82
107
|
if (!value || /[\x00-\x20\x7f;,]/.test(value) || /^[a-z][a-z0-9+.-]*\/\//i.test(value))
|
|
83
108
|
return false;
|
|
84
|
-
if (keywords.has(value.toLowerCase()) || value ===
|
|
109
|
+
if (keywords.has(value.toLowerCase()) || value === NONCE || value === "*" || /^[a-z][a-z0-9+.-]*:\/\/\*(?::(?:\d+|\*))?(?:\/[^\s;,#?]*)?$/i.test(value))
|
|
85
110
|
return true;
|
|
86
111
|
if (/^'(nonce-[A-Za-z0-9+/_-]+={0,2}|sha(256|384|512)-[A-Za-z0-9+/_-]+={0,2})'$/i.test(
|
|
87
112
|
value
|
|
@@ -179,8 +204,7 @@ var reportingSchema = import_zod.z.object({
|
|
|
179
204
|
var policyConfigSchema = import_zod.z.object({
|
|
180
205
|
base: baseSchema.default({}),
|
|
181
206
|
ruleSets: import_zod.z.array(ruleSetSchema).default([]),
|
|
182
|
-
reporting: reportingSchema.optional()
|
|
183
|
-
mode: import_zod.z.enum(["enforce", "report-only"]).default("enforce")
|
|
207
|
+
reporting: reportingSchema.optional()
|
|
184
208
|
}).strict().superRefine((config, ctx) => {
|
|
185
209
|
const names = /* @__PURE__ */ new Set();
|
|
186
210
|
config.ruleSets.forEach((rule, i) => {
|
|
@@ -194,34 +218,6 @@ var policyConfigSchema = import_zod.z.object({
|
|
|
194
218
|
});
|
|
195
219
|
});
|
|
196
220
|
|
|
197
|
-
// src/keywords.ts
|
|
198
|
-
var sourceExpressions = {
|
|
199
|
-
self: "'self'",
|
|
200
|
-
none: "'none'",
|
|
201
|
-
nonce: NONCE_PLACEHOLDER,
|
|
202
|
-
strictDynamic: "'strict-dynamic'",
|
|
203
|
-
reportSample: "'report-sample'",
|
|
204
|
-
wasmUnsafeEval: "'wasm-unsafe-eval'"
|
|
205
|
-
};
|
|
206
|
-
function generateNonce() {
|
|
207
|
-
const bytes = new Uint8Array(16);
|
|
208
|
-
globalThis.crypto.getRandomValues(bytes);
|
|
209
|
-
return btoa(String.fromCharCode(...bytes));
|
|
210
|
-
}
|
|
211
|
-
var unsafeInline = ({ reason }) => ({
|
|
212
|
-
source: "'unsafe-inline'",
|
|
213
|
-
reason
|
|
214
|
-
});
|
|
215
|
-
var unsafeEval = ({ reason }) => ({
|
|
216
|
-
source: "'unsafe-eval'",
|
|
217
|
-
reason
|
|
218
|
-
});
|
|
219
|
-
var unsafeHashes = ({ reason }) => ({
|
|
220
|
-
source: "'unsafe-hashes'",
|
|
221
|
-
reason
|
|
222
|
-
});
|
|
223
|
-
var cspHash = (algorithm, digest) => `'${algorithm}-${digest}'`;
|
|
224
|
-
|
|
225
221
|
// src/matching/index.ts
|
|
226
222
|
var friendly = {
|
|
227
223
|
scripts: "script-src",
|
|
@@ -447,113 +443,6 @@ function matchPolicy(directives, directive, input, options = {}) {
|
|
|
447
443
|
return decision("blocked", effective, "No source matched");
|
|
448
444
|
}
|
|
449
445
|
|
|
450
|
-
// src/reports/index.ts
|
|
451
|
-
function record(value) {
|
|
452
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
453
|
-
}
|
|
454
|
-
function field(obj, ...keys) {
|
|
455
|
-
for (const key of keys)
|
|
456
|
-
if (typeof obj[key] === "string" && obj[key]) return obj[key];
|
|
457
|
-
return void 0;
|
|
458
|
-
}
|
|
459
|
-
function parseViolationReports(input) {
|
|
460
|
-
if (typeof input === "string") {
|
|
461
|
-
try {
|
|
462
|
-
input = JSON.parse(input);
|
|
463
|
-
} catch {
|
|
464
|
-
throw new Error("Invalid report JSON");
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
const items = Array.isArray(input) ? input : [input];
|
|
468
|
-
return items.map((item, index) => {
|
|
469
|
-
const envelope = record(item);
|
|
470
|
-
if (!envelope) throw new Error(`Unsupported report at index ${index}`);
|
|
471
|
-
const body = (envelope.type === "csp" ? record(envelope.csp) : void 0) ?? record(envelope["csp-report"]) ?? (envelope.type === "csp-violation" ? record(envelope.body) : void 0) ?? envelope;
|
|
472
|
-
const directive = field(
|
|
473
|
-
body,
|
|
474
|
-
"effective-directive",
|
|
475
|
-
"effectiveDirective",
|
|
476
|
-
"effective_directive",
|
|
477
|
-
"violated-directive",
|
|
478
|
-
"violatedDirective",
|
|
479
|
-
"violated_directive"
|
|
480
|
-
);
|
|
481
|
-
const blockedURL = field(
|
|
482
|
-
body,
|
|
483
|
-
"blocked-uri",
|
|
484
|
-
"blockedURL",
|
|
485
|
-
"blockedUri",
|
|
486
|
-
"blocked_uri"
|
|
487
|
-
);
|
|
488
|
-
const documentURL = field(
|
|
489
|
-
body,
|
|
490
|
-
"document-uri",
|
|
491
|
-
"documentURL",
|
|
492
|
-
"documentUri",
|
|
493
|
-
"document_uri"
|
|
494
|
-
);
|
|
495
|
-
if (!directive || !blockedURL)
|
|
496
|
-
throw new Error(`Unsupported or incomplete report at index ${index}`);
|
|
497
|
-
return {
|
|
498
|
-
directive: directive.split(" ")[0],
|
|
499
|
-
blockedURL,
|
|
500
|
-
...documentURL ? { documentURL } : {}
|
|
501
|
-
};
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
function replayReports(directives, reports) {
|
|
505
|
-
return reports.map((report) => {
|
|
506
|
-
let origin;
|
|
507
|
-
try {
|
|
508
|
-
if (report.documentURL) {
|
|
509
|
-
const parsedOrigin = new URL(report.documentURL).origin;
|
|
510
|
-
if (parsedOrigin !== "null") origin = parsedOrigin;
|
|
511
|
-
}
|
|
512
|
-
} catch {
|
|
513
|
-
}
|
|
514
|
-
let blocked;
|
|
515
|
-
try {
|
|
516
|
-
blocked = new URL(report.blockedURL);
|
|
517
|
-
} catch {
|
|
518
|
-
return {
|
|
519
|
-
report,
|
|
520
|
-
result: {
|
|
521
|
-
status: "indeterminate",
|
|
522
|
-
allowed: null,
|
|
523
|
-
reason: "Report does not contain a replayable URL"
|
|
524
|
-
}
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
if (!["http:", "https:", "ws:", "wss:"].includes(blocked.protocol)) {
|
|
528
|
-
return {
|
|
529
|
-
report,
|
|
530
|
-
result: {
|
|
531
|
-
status: "indeterminate",
|
|
532
|
-
allowed: null,
|
|
533
|
-
reason: "Report does not contain a replayable network URL"
|
|
534
|
-
}
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
const result = matchPolicy(
|
|
538
|
-
directives,
|
|
539
|
-
report.directive,
|
|
540
|
-
report.blockedURL,
|
|
541
|
-
origin ? { origin } : {}
|
|
542
|
-
);
|
|
543
|
-
if (result.status === "allowed" && result.matchedSource && result.matchedSource.replace(/^[a-z]+:\/\//i, "").includes("/")) {
|
|
544
|
-
return {
|
|
545
|
-
report,
|
|
546
|
-
result: {
|
|
547
|
-
status: "indeterminate",
|
|
548
|
-
allowed: null,
|
|
549
|
-
reason: "Report may omit the request path"
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
}
|
|
553
|
-
return { report, result };
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
|
|
557
446
|
// src/core.ts
|
|
558
447
|
var import_zod2 = require("zod");
|
|
559
448
|
function freeze(value) {
|
|
@@ -585,7 +474,6 @@ function entryReason(entry) {
|
|
|
585
474
|
}
|
|
586
475
|
var Policy = class _Policy {
|
|
587
476
|
config;
|
|
588
|
-
headerName;
|
|
589
477
|
constructor(input) {
|
|
590
478
|
const result = policyConfigSchema.safeParse(input);
|
|
591
479
|
if (!result.success) {
|
|
@@ -602,7 +490,6 @@ var Policy = class _Policy {
|
|
|
602
490
|
}
|
|
603
491
|
const config = result.data;
|
|
604
492
|
this.config = freeze(config);
|
|
605
|
-
this.headerName = config.mode === "enforce" ? "Content-Security-Policy" : "Content-Security-Policy-Report-Only";
|
|
606
493
|
Object.freeze(this);
|
|
607
494
|
}
|
|
608
495
|
/** Sources and their contributing rule sets. Duplicate values retain all contributors. */
|
|
@@ -662,44 +549,26 @@ var Policy = class _Policy {
|
|
|
662
549
|
) : [];
|
|
663
550
|
return contributors.length ? { ...result, contributors } : result;
|
|
664
551
|
}
|
|
665
|
-
/** Replay supported violation report JSON without inventing missing request context. */
|
|
666
|
-
check(reports) {
|
|
667
|
-
return replayReports(this.toObject(), parseViolationReports(reports));
|
|
668
|
-
}
|
|
669
552
|
/** Render a header value. Pretty output is for display only and is not a valid HTTP header value. */
|
|
670
553
|
toHeader(options = {}) {
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
render(directives, options) {
|
|
554
|
+
const directives = this.toObject();
|
|
674
555
|
const hasNonce = Object.values(directives).some(
|
|
675
|
-
(sources) => sources.includes(
|
|
556
|
+
(sources) => sources.includes(NONCE)
|
|
676
557
|
);
|
|
677
558
|
if (hasNonce && (!options.nonce || !/^[A-Za-z0-9+/_-]+={0,2}$/.test(options.nonce)))
|
|
678
559
|
throw new Error("A valid base64 nonce is required");
|
|
679
560
|
return Object.entries(directives).map(([directive, values]) => {
|
|
680
561
|
const rendered = values.map(
|
|
681
|
-
(value) => value ===
|
|
562
|
+
(value) => value === NONCE ? `'nonce-${options.nonce}'` : value
|
|
682
563
|
);
|
|
683
564
|
return directive + (rendered.length ? ` ${rendered.join(" ")}` : "");
|
|
684
565
|
}).join(options.pretty ? ";\n" : "; ");
|
|
685
566
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
if (this.config.mode === "report-only")
|
|
689
|
-
throw new Error(
|
|
690
|
-
"Report-only policies cannot be delivered in a meta element"
|
|
691
|
-
);
|
|
692
|
-
const directives = this.toObject();
|
|
693
|
-
delete directives["frame-ancestors"];
|
|
694
|
-
delete directives["report-uri"];
|
|
695
|
-
delete directives["report-to"];
|
|
696
|
-
delete directives.sandbox;
|
|
697
|
-
return this.render(directives, options);
|
|
698
|
-
}
|
|
699
|
-
withRuleSet(ruleSet) {
|
|
567
|
+
withRuleSet(ruleSets) {
|
|
568
|
+
const additions = Array.isArray(ruleSets) ? ruleSets : [ruleSets];
|
|
700
569
|
return new _Policy({
|
|
701
570
|
...this.config,
|
|
702
|
-
ruleSets: [...this.config.ruleSets,
|
|
571
|
+
ruleSets: [...this.config.ruleSets, ...additions]
|
|
703
572
|
});
|
|
704
573
|
}
|
|
705
574
|
without(name) {
|
|
@@ -932,213 +801,28 @@ function audit(policy, options = {}) {
|
|
|
932
801
|
)
|
|
933
802
|
);
|
|
934
803
|
}
|
|
935
|
-
|
|
936
|
-
// src/diff.ts
|
|
937
|
-
function diffPolicies(before, after) {
|
|
938
|
-
const key = (row) => JSON.stringify([
|
|
939
|
-
row.directive,
|
|
940
|
-
row.source,
|
|
941
|
-
row.ruleSet,
|
|
942
|
-
row.reason ?? null
|
|
943
|
-
]);
|
|
944
|
-
const oldRows = new Map(before.explain().map((row) => [key(row), row]));
|
|
945
|
-
const newRows = new Map(after.explain().map((row) => [key(row), row]));
|
|
946
|
-
const changes = [];
|
|
947
|
-
for (const [id, row] of oldRows)
|
|
948
|
-
if (!newRows.has(id))
|
|
949
|
-
changes.push({
|
|
950
|
-
directive: row.directive,
|
|
951
|
-
source: row.source,
|
|
952
|
-
ruleSet: row.ruleSet,
|
|
953
|
-
change: "removed",
|
|
954
|
-
...row.reason ? { detail: row.reason } : {}
|
|
955
|
-
});
|
|
956
|
-
for (const [id, row] of newRows)
|
|
957
|
-
if (!oldRows.has(id))
|
|
958
|
-
changes.push({
|
|
959
|
-
directive: row.directive,
|
|
960
|
-
source: row.source,
|
|
961
|
-
ruleSet: row.ruleSet,
|
|
962
|
-
change: "added",
|
|
963
|
-
...row.reason ? { detail: row.reason } : {}
|
|
964
|
-
});
|
|
965
|
-
const beforeObj = before.toObject(), afterObj = after.toObject();
|
|
966
|
-
for (const directive of /* @__PURE__ */ new Set([
|
|
967
|
-
...Object.keys(beforeObj),
|
|
968
|
-
...Object.keys(afterObj)
|
|
969
|
-
])) {
|
|
970
|
-
if (JSON.stringify(beforeObj[directive]) !== JSON.stringify(afterObj[directive]) && !changes.some((c) => c.directive === directive))
|
|
971
|
-
changes.push({
|
|
972
|
-
directive,
|
|
973
|
-
source: "",
|
|
974
|
-
ruleSet: "base",
|
|
975
|
-
change: "modified",
|
|
976
|
-
detail: "Directive value changed"
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
if (before.config.mode !== after.config.mode)
|
|
980
|
-
changes.push({
|
|
981
|
-
directive: "(mode)",
|
|
982
|
-
source: "",
|
|
983
|
-
ruleSet: "base",
|
|
984
|
-
change: "modified",
|
|
985
|
-
detail: `${before.config.mode} \u2192 ${after.config.mode}`
|
|
986
|
-
});
|
|
987
|
-
changes.sort(
|
|
988
|
-
(a, b) => a.directive.localeCompare(b.directive) || a.source.localeCompare(b.source) || a.ruleSet.localeCompare(b.ruleSet) || a.change.localeCompare(b.change)
|
|
989
|
-
);
|
|
990
|
-
const headersChanged = JSON.stringify(beforeObj) !== JSON.stringify(afterObj) || before.config.mode !== after.config.mode;
|
|
991
|
-
const dangerous = changes.some(
|
|
992
|
-
(change) => change.change === "added" && !(beforeObj[change.directive] ?? []).includes(change.source) && /^(?:\*|https?:|data:|'unsafe-(?:inline|eval|hashes)')$/.test(
|
|
993
|
-
change.source
|
|
994
|
-
)
|
|
995
|
-
);
|
|
996
|
-
return {
|
|
997
|
-
changes,
|
|
998
|
-
risk: dangerous ? "increased" : headersChanged ? "unknown" : "unchanged"
|
|
999
|
-
};
|
|
1000
|
-
}
|
|
1001
|
-
|
|
1002
|
-
// src/importer/index.ts
|
|
1003
|
-
function importHeader(header) {
|
|
1004
|
-
if (/[\x00-\x08\x0a-\x1f\x7f,]/.test(header))
|
|
1005
|
-
throw new Error(
|
|
1006
|
-
"Header contains a control character or multiple policies; import one policy at a time"
|
|
1007
|
-
);
|
|
1008
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1009
|
-
const warnings = [];
|
|
1010
|
-
const directives = [];
|
|
1011
|
-
for (const segment of header.split(";")) {
|
|
1012
|
-
const trimmed = segment.trim();
|
|
1013
|
-
if (!trimmed) continue;
|
|
1014
|
-
const [rawName, ...values] = trimmed.split(/[ \t]+/);
|
|
1015
|
-
if (!rawName || !/^[a-zA-Z][a-zA-Z0-9-]*$/.test(rawName))
|
|
1016
|
-
throw new Error(`Invalid directive name: ${rawName ?? ""}`);
|
|
1017
|
-
const name = rawName.toLowerCase();
|
|
1018
|
-
const ignored = seen.has(name);
|
|
1019
|
-
if (ignored)
|
|
1020
|
-
warnings.push(`Duplicate ${name} is ignored by CSP; retained for review`);
|
|
1021
|
-
seen.add(name);
|
|
1022
|
-
directives.push({ name, values, ignored });
|
|
1023
|
-
}
|
|
1024
|
-
return { directives, warnings };
|
|
1025
|
-
}
|
|
1026
|
-
function serializeImportedPolicy(policy) {
|
|
1027
|
-
return policy.directives.map(({ name, values }) => {
|
|
1028
|
-
if (!/^[a-z][a-z0-9-]*$/.test(name) || values.some((value) => !value || /[\x00-\x20\x7f;,]/.test(value))) {
|
|
1029
|
-
throw new Error("Invalid imported directive or value");
|
|
1030
|
-
}
|
|
1031
|
-
return [name, ...values].join(" ");
|
|
1032
|
-
}).join("; ");
|
|
1033
|
-
}
|
|
1034
|
-
function importedPolicyToDraft(policy) {
|
|
1035
|
-
if (policy.directives.some((directive) => directive.ignored))
|
|
1036
|
-
throw new Error(
|
|
1037
|
-
"Duplicate directives require manual review before conversion"
|
|
1038
|
-
);
|
|
1039
|
-
const base = {};
|
|
1040
|
-
const baseRaw = {};
|
|
1041
|
-
const ruleRaw = {};
|
|
1042
|
-
const baseOnly2 = /* @__PURE__ */ new Set([
|
|
1043
|
-
"default-src",
|
|
1044
|
-
"object-src",
|
|
1045
|
-
"base-uri",
|
|
1046
|
-
"frame-ancestors"
|
|
1047
|
-
]);
|
|
1048
|
-
const supported = /* @__PURE__ */ new Set([
|
|
1049
|
-
"default-src",
|
|
1050
|
-
"object-src",
|
|
1051
|
-
"base-uri",
|
|
1052
|
-
"frame-ancestors",
|
|
1053
|
-
"script-src",
|
|
1054
|
-
"script-src-elem",
|
|
1055
|
-
"script-src-attr",
|
|
1056
|
-
"style-src",
|
|
1057
|
-
"style-src-elem",
|
|
1058
|
-
"style-src-attr",
|
|
1059
|
-
"img-src",
|
|
1060
|
-
"connect-src",
|
|
1061
|
-
"font-src",
|
|
1062
|
-
"frame-src",
|
|
1063
|
-
"child-src",
|
|
1064
|
-
"form-action",
|
|
1065
|
-
"media-src",
|
|
1066
|
-
"worker-src",
|
|
1067
|
-
"manifest-src"
|
|
1068
|
-
]);
|
|
1069
|
-
const risky2 = /* @__PURE__ */ new Set([
|
|
1070
|
-
"'unsafe-inline'",
|
|
1071
|
-
"'unsafe-eval'",
|
|
1072
|
-
"'unsafe-hashes'"
|
|
1073
|
-
]);
|
|
1074
|
-
for (const directive of policy.directives) {
|
|
1075
|
-
if (directive.name === "upgrade-insecure-requests") {
|
|
1076
|
-
if (directive.values.length)
|
|
1077
|
-
throw new Error(
|
|
1078
|
-
"upgrade-insecure-requests with values requires manual review"
|
|
1079
|
-
);
|
|
1080
|
-
base.upgradeInsecureRequests = true;
|
|
1081
|
-
continue;
|
|
1082
|
-
}
|
|
1083
|
-
if (!supported.has(directive.name) || directive.values.some(
|
|
1084
|
-
(value) => value.toLowerCase() === "'nonce-{nonce}'"
|
|
1085
|
-
)) {
|
|
1086
|
-
throw new Error(
|
|
1087
|
-
`Directive ${directive.name} contains unsupported syntax; review manually`
|
|
1088
|
-
);
|
|
1089
|
-
}
|
|
1090
|
-
if (directive.values.length === 0 || ["report-uri", "report-to", "sandbox"].includes(directive.name)) {
|
|
1091
|
-
throw new Error(
|
|
1092
|
-
`Directive ${directive.name} requires manual review before conversion`
|
|
1093
|
-
);
|
|
1094
|
-
}
|
|
1095
|
-
const entries = directive.values.map(
|
|
1096
|
-
(source) => risky2.has(source.toLowerCase()) ? { source, reason: "Imported legacy policy; review required" } : source
|
|
1097
|
-
);
|
|
1098
|
-
(baseOnly2.has(directive.name) ? baseRaw : ruleRaw)[directive.name] = entries;
|
|
1099
|
-
}
|
|
1100
|
-
if (Object.keys(baseRaw).length) base.directives = baseRaw;
|
|
1101
|
-
const draft = {
|
|
1102
|
-
base,
|
|
1103
|
-
ruleSets: Object.keys(ruleRaw).length ? [
|
|
1104
|
-
{
|
|
1105
|
-
name: "Imported policy",
|
|
1106
|
-
purpose: "Imported sources awaiting ownership review",
|
|
1107
|
-
rules: { directives: ruleRaw }
|
|
1108
|
-
}
|
|
1109
|
-
] : []
|
|
1110
|
-
};
|
|
1111
|
-
const result = policyConfigSchema.safeParse(draft);
|
|
1112
|
-
if (!result.success)
|
|
1113
|
-
throw new Error(
|
|
1114
|
-
`Imported policy contains unsupported sources: ${result.error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join("; ")}`
|
|
1115
|
-
);
|
|
1116
|
-
return draft;
|
|
1117
|
-
}
|
|
1118
804
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1119
805
|
0 && (module.exports = {
|
|
1120
|
-
|
|
806
|
+
NONCE,
|
|
807
|
+
NONE,
|
|
1121
808
|
Policy,
|
|
809
|
+
REPORT_SAMPLE,
|
|
810
|
+
SELF,
|
|
811
|
+
STRICT_DYNAMIC,
|
|
812
|
+
WASM_UNSAFE_EVAL,
|
|
1122
813
|
audit,
|
|
1123
814
|
baseDirectives,
|
|
1124
815
|
baseSchema,
|
|
1125
816
|
cspHash,
|
|
1126
817
|
definePolicy,
|
|
1127
818
|
defineRuleSet,
|
|
1128
|
-
diffPolicies,
|
|
1129
819
|
generateNonce,
|
|
1130
|
-
importHeader,
|
|
1131
|
-
importedPolicyToDraft,
|
|
1132
820
|
matchPolicy,
|
|
1133
|
-
parseViolationReports,
|
|
1134
821
|
policyConfigSchema,
|
|
1135
|
-
replayReports,
|
|
1136
822
|
resourceDirectives,
|
|
1137
823
|
ruleSetSchema,
|
|
1138
824
|
rulesSchema,
|
|
1139
|
-
serializeImportedPolicy,
|
|
1140
825
|
sourceEntrySchema,
|
|
1141
|
-
sourceExpressions,
|
|
1142
826
|
unsafeEval,
|
|
1143
827
|
unsafeHashes,
|
|
1144
828
|
unsafeInline
|