@jamesbrightman/pretty-csp 0.0.0-stage → 0.1.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 +11 -0
- package/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/chunk-KWWYRHIP.js +224 -0
- package/dist/chunk-KWWYRHIP.js.map +1 -0
- package/dist/chunk-MDUQKU6Y.js +67 -0
- package/dist/chunk-MDUQKU6Y.js.map +1 -0
- package/dist/chunk-TE3J7Y25.js +875 -0
- package/dist/chunk-TE3J7Y25.js.map +1 -0
- package/dist/cli.cjs +1667 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +637 -0
- package/dist/cli.js.map +1 -0
- package/dist/index-D6PmUWjD.d.cts +451 -0
- package/dist/index-D6PmUWjD.d.ts +451 -0
- package/dist/index.cjs +1146 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +79 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +57 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.cjs +310 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +35 -0
- package/dist/testing.d.ts +35 -0
- package/dist/testing.js +10 -0
- package/dist/testing.js.map +1 -0
- package/package.json +91 -4
- package/website/static/img/how-it-works.svg +34 -0
- package/website/static/img/pretty-csp-icon.svg +6 -0
- package/website/static/img/pretty-csp-logo.svg +12 -0
|
@@ -0,0 +1,875 @@
|
|
|
1
|
+
// src/schemas.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var resourceDirectives = {
|
|
4
|
+
scripts: "script-src",
|
|
5
|
+
connect: "connect-src",
|
|
6
|
+
images: "img-src",
|
|
7
|
+
styles: "style-src",
|
|
8
|
+
fonts: "font-src",
|
|
9
|
+
frames: "frame-src",
|
|
10
|
+
forms: "form-action",
|
|
11
|
+
media: "media-src",
|
|
12
|
+
workers: "worker-src",
|
|
13
|
+
manifest: "manifest-src"
|
|
14
|
+
};
|
|
15
|
+
var baseDirectives = {
|
|
16
|
+
default: "default-src",
|
|
17
|
+
objects: "object-src",
|
|
18
|
+
baseUri: "base-uri",
|
|
19
|
+
frameAncestors: "frame-ancestors"
|
|
20
|
+
};
|
|
21
|
+
var risky = /* @__PURE__ */ new Set(["'unsafe-inline'", "'unsafe-eval'", "'unsafe-hashes'"]);
|
|
22
|
+
var keywords = /* @__PURE__ */ new Set([
|
|
23
|
+
"'self'",
|
|
24
|
+
"'none'",
|
|
25
|
+
"'strict-dynamic'",
|
|
26
|
+
"'report-sample'",
|
|
27
|
+
"'wasm-unsafe-eval'",
|
|
28
|
+
...risky
|
|
29
|
+
]);
|
|
30
|
+
var NONCE_PLACEHOLDER = "'nonce-{nonce}'";
|
|
31
|
+
function isSource(value) {
|
|
32
|
+
if (!value || /[\x00-\x20\x7f;,]/.test(value) || /^[a-z][a-z0-9+.-]*\/\//i.test(value))
|
|
33
|
+
return false;
|
|
34
|
+
if (keywords.has(value.toLowerCase()) || value === NONCE_PLACEHOLDER || value === "*" || /^[a-z][a-z0-9+.-]*:\/\/\*(?::(?:\d+|\*))?(?:\/[^\s;,#?]*)?$/i.test(value))
|
|
35
|
+
return true;
|
|
36
|
+
if (/^'(nonce-[A-Za-z0-9+/_-]+={0,2}|sha(256|384|512)-[A-Za-z0-9+/_-]+={0,2})'$/i.test(
|
|
37
|
+
value
|
|
38
|
+
))
|
|
39
|
+
return true;
|
|
40
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:$/.test(value)) return true;
|
|
41
|
+
return /^(?:[a-z][a-z0-9+.-]*:\/\/)?(?:\*\.)?(?:[a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+(?::(?:\d+|\*))?(?:\/[^\s;,#?]*)?$/i.test(
|
|
42
|
+
value
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
var sourceStringSchema = z.string().refine(isSource, "Invalid CSP source expression");
|
|
46
|
+
var sourceEntrySchema = z.union([
|
|
47
|
+
sourceStringSchema.refine(
|
|
48
|
+
(s) => !risky.has(s.toLowerCase()),
|
|
49
|
+
"Risky keyword requires a reason"
|
|
50
|
+
),
|
|
51
|
+
z.object({ source: sourceStringSchema, reason: z.string().trim().min(1) }).strict()
|
|
52
|
+
]);
|
|
53
|
+
var sourceList = z.array(sourceEntrySchema).min(1);
|
|
54
|
+
var sourceListDirectives = /* @__PURE__ */ new Set([
|
|
55
|
+
...Object.values(resourceDirectives),
|
|
56
|
+
...Object.values(baseDirectives),
|
|
57
|
+
"script-src-elem",
|
|
58
|
+
"script-src-attr",
|
|
59
|
+
"style-src-elem",
|
|
60
|
+
"style-src-attr",
|
|
61
|
+
"child-src"
|
|
62
|
+
]);
|
|
63
|
+
var rawDirectives = z.record(
|
|
64
|
+
z.string().regex(/^[a-z][a-z0-9-]*$/).refine(
|
|
65
|
+
(name) => sourceListDirectives.has(name),
|
|
66
|
+
"Unsupported raw directive; only known source-list directives are supported"
|
|
67
|
+
),
|
|
68
|
+
sourceList
|
|
69
|
+
);
|
|
70
|
+
var reserved = /* @__PURE__ */ new Set([
|
|
71
|
+
"report-uri",
|
|
72
|
+
"report-to",
|
|
73
|
+
"upgrade-insecure-requests"
|
|
74
|
+
]);
|
|
75
|
+
var baseOnly = /* @__PURE__ */ new Set([...Object.values(baseDirectives), ...reserved]);
|
|
76
|
+
var ruleShape = Object.fromEntries(
|
|
77
|
+
Object.keys(resourceDirectives).map((key) => [key, sourceList.optional()])
|
|
78
|
+
);
|
|
79
|
+
var rulesSchema = z.object({ ...ruleShape, directives: rawDirectives.optional() }).strict().refine(
|
|
80
|
+
(rules) => Object.entries(rules).some(
|
|
81
|
+
([key, value]) => key === "directives" ? Object.keys(value ?? {}).length > 0 : value !== void 0
|
|
82
|
+
),
|
|
83
|
+
"At least one resource directive is required"
|
|
84
|
+
).superRefine((rules, ctx) => {
|
|
85
|
+
for (const directive of Object.keys(rules.directives ?? {})) {
|
|
86
|
+
if (baseOnly.has(directive))
|
|
87
|
+
ctx.addIssue({
|
|
88
|
+
code: "custom",
|
|
89
|
+
path: ["directives", directive],
|
|
90
|
+
message: "Base-only or reserved directive"
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
var baseSchema = z.object({
|
|
95
|
+
...ruleShape,
|
|
96
|
+
default: sourceList.optional(),
|
|
97
|
+
objects: sourceList.optional(),
|
|
98
|
+
baseUri: sourceList.optional(),
|
|
99
|
+
frameAncestors: sourceList.optional(),
|
|
100
|
+
upgradeInsecureRequests: z.boolean().optional(),
|
|
101
|
+
directives: rawDirectives.optional()
|
|
102
|
+
}).strict().superRefine((base, ctx) => {
|
|
103
|
+
for (const directive of Object.keys(base.directives ?? {})) {
|
|
104
|
+
if (reserved.has(directive))
|
|
105
|
+
ctx.addIssue({
|
|
106
|
+
code: "custom",
|
|
107
|
+
path: ["directives", directive],
|
|
108
|
+
message: "Use the dedicated configuration field for this directive"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
var ruleSetSchema = z.object({
|
|
113
|
+
name: z.string().trim().min(1),
|
|
114
|
+
purpose: z.string().trim().min(1),
|
|
115
|
+
owner: z.string().trim().min(1).optional(),
|
|
116
|
+
docs: z.url().optional(),
|
|
117
|
+
tags: z.array(z.string().trim().min(1)).optional(),
|
|
118
|
+
reviewBy: z.iso.date().optional(),
|
|
119
|
+
enabled: z.boolean().default(true),
|
|
120
|
+
rules: rulesSchema
|
|
121
|
+
}).strict();
|
|
122
|
+
var reportingSchema = z.object({
|
|
123
|
+
uri: z.url().refine(
|
|
124
|
+
(value) => !/[\x00-\x20\x7f;,]/.test(value),
|
|
125
|
+
"Invalid reporting value"
|
|
126
|
+
).optional(),
|
|
127
|
+
to: z.string().regex(/^[A-Za-z0-9_-]+$/).optional()
|
|
128
|
+
}).strict();
|
|
129
|
+
var policyConfigSchema = z.object({
|
|
130
|
+
base: baseSchema.default({}),
|
|
131
|
+
ruleSets: z.array(ruleSetSchema).default([]),
|
|
132
|
+
reporting: reportingSchema.optional(),
|
|
133
|
+
mode: z.enum(["enforce", "report-only"]).default("enforce")
|
|
134
|
+
}).strict().superRefine((config, ctx) => {
|
|
135
|
+
const names = /* @__PURE__ */ new Set();
|
|
136
|
+
config.ruleSets.forEach((rule, i) => {
|
|
137
|
+
if (names.has(rule.name))
|
|
138
|
+
ctx.addIssue({
|
|
139
|
+
code: "custom",
|
|
140
|
+
path: ["ruleSets", i, "name"],
|
|
141
|
+
message: `Duplicate rule set name: ${rule.name}`
|
|
142
|
+
});
|
|
143
|
+
names.add(rule.name);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// src/keywords.ts
|
|
148
|
+
var sourceExpressions = {
|
|
149
|
+
self: "'self'",
|
|
150
|
+
none: "'none'",
|
|
151
|
+
nonce: NONCE_PLACEHOLDER,
|
|
152
|
+
strictDynamic: "'strict-dynamic'",
|
|
153
|
+
reportSample: "'report-sample'",
|
|
154
|
+
wasmUnsafeEval: "'wasm-unsafe-eval'"
|
|
155
|
+
};
|
|
156
|
+
function generateNonce() {
|
|
157
|
+
const bytes = new Uint8Array(16);
|
|
158
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
159
|
+
return btoa(String.fromCharCode(...bytes));
|
|
160
|
+
}
|
|
161
|
+
var unsafeInline = ({ reason }) => ({
|
|
162
|
+
source: "'unsafe-inline'",
|
|
163
|
+
reason
|
|
164
|
+
});
|
|
165
|
+
var unsafeEval = ({ reason }) => ({
|
|
166
|
+
source: "'unsafe-eval'",
|
|
167
|
+
reason
|
|
168
|
+
});
|
|
169
|
+
var unsafeHashes = ({ reason }) => ({
|
|
170
|
+
source: "'unsafe-hashes'",
|
|
171
|
+
reason
|
|
172
|
+
});
|
|
173
|
+
var cspHash = (algorithm, digest) => `'${algorithm}-${digest}'`;
|
|
174
|
+
|
|
175
|
+
// src/matching/index.ts
|
|
176
|
+
var friendly = {
|
|
177
|
+
scripts: "script-src",
|
|
178
|
+
connect: "connect-src",
|
|
179
|
+
images: "img-src",
|
|
180
|
+
styles: "style-src",
|
|
181
|
+
fonts: "font-src",
|
|
182
|
+
frames: "frame-src",
|
|
183
|
+
forms: "form-action",
|
|
184
|
+
media: "media-src",
|
|
185
|
+
workers: "worker-src",
|
|
186
|
+
manifest: "manifest-src",
|
|
187
|
+
default: "default-src",
|
|
188
|
+
objects: "object-src",
|
|
189
|
+
baseUri: "base-uri",
|
|
190
|
+
frameAncestors: "frame-ancestors"
|
|
191
|
+
};
|
|
192
|
+
var fallbacks = {
|
|
193
|
+
"script-src-elem": ["script-src-elem", "script-src", "default-src"],
|
|
194
|
+
"script-src-attr": ["script-src-attr", "script-src", "default-src"],
|
|
195
|
+
"style-src-elem": ["style-src-elem", "style-src", "default-src"],
|
|
196
|
+
"style-src-attr": ["style-src-attr", "style-src", "default-src"],
|
|
197
|
+
"worker-src": ["worker-src", "child-src", "script-src", "default-src"],
|
|
198
|
+
"frame-src": ["frame-src", "child-src", "default-src"]
|
|
199
|
+
};
|
|
200
|
+
for (const name of [
|
|
201
|
+
"script-src",
|
|
202
|
+
"style-src",
|
|
203
|
+
"child-src",
|
|
204
|
+
"connect-src",
|
|
205
|
+
"font-src",
|
|
206
|
+
"img-src",
|
|
207
|
+
"manifest-src",
|
|
208
|
+
"media-src",
|
|
209
|
+
"object-src"
|
|
210
|
+
]) {
|
|
211
|
+
fallbacks[name] = [name, "default-src"];
|
|
212
|
+
}
|
|
213
|
+
for (const name of [
|
|
214
|
+
"default-src",
|
|
215
|
+
"form-action",
|
|
216
|
+
"base-uri",
|
|
217
|
+
"frame-ancestors"
|
|
218
|
+
])
|
|
219
|
+
fallbacks[name] = [name];
|
|
220
|
+
function decision(status, effectiveDirective, reason, matchedSource) {
|
|
221
|
+
return {
|
|
222
|
+
status,
|
|
223
|
+
allowed: status === "indeterminate" ? null : status === "allowed",
|
|
224
|
+
...effectiveDirective && { effectiveDirective },
|
|
225
|
+
...reason && { reason },
|
|
226
|
+
...matchedSource && { matchedSource }
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function schemeMatches(source, target) {
|
|
230
|
+
source = source.toLowerCase();
|
|
231
|
+
target = target.toLowerCase();
|
|
232
|
+
return source === target || source === "http" && target === "https" || source === "ws" && ["wss", "http", "https"].includes(target) || source === "wss" && target === "https";
|
|
233
|
+
}
|
|
234
|
+
function scheme(url) {
|
|
235
|
+
return url.protocol.slice(0, -1).toLowerCase();
|
|
236
|
+
}
|
|
237
|
+
function defaultPort(s) {
|
|
238
|
+
return { http: "80", https: "443", ws: "80", wss: "443", ftp: "21" }[s];
|
|
239
|
+
}
|
|
240
|
+
function selfMatches(url, origin) {
|
|
241
|
+
if (scheme(url) === "blob") return false;
|
|
242
|
+
if (origin.origin !== "null" && origin.origin === url.origin) return true;
|
|
243
|
+
const os = scheme(origin), us = scheme(url);
|
|
244
|
+
const portsMatch = origin.port === url.port || !origin.port && !url.port;
|
|
245
|
+
return origin.hostname !== "" && origin.hostname === url.hostname && portsMatch && (us === "https" || us === "wss" || os === "http" && (us === "http" || us === "ws"));
|
|
246
|
+
}
|
|
247
|
+
function segmentBytes(segment) {
|
|
248
|
+
const bytes = [];
|
|
249
|
+
for (let i = 0; i < segment.length; ) {
|
|
250
|
+
const encoded = segment.slice(i).match(/^%([0-9a-f]{2})/i);
|
|
251
|
+
if (encoded) {
|
|
252
|
+
bytes.push(parseInt(encoded[1], 16));
|
|
253
|
+
i += 3;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const point = segment.codePointAt(i);
|
|
257
|
+
const character = String.fromCodePoint(point);
|
|
258
|
+
bytes.push(...new TextEncoder().encode(character));
|
|
259
|
+
i += character.length;
|
|
260
|
+
}
|
|
261
|
+
return bytes.join(",");
|
|
262
|
+
}
|
|
263
|
+
function pathMatches(pattern, target) {
|
|
264
|
+
if (!pattern || pattern === "/" && !target) return true;
|
|
265
|
+
const a = pattern.split("/"), b = target.split("/");
|
|
266
|
+
if (a.length > b.length || !pattern.endsWith("/") && a.length !== b.length)
|
|
267
|
+
return false;
|
|
268
|
+
if (pattern.endsWith("/")) a.pop();
|
|
269
|
+
return a.every((part, i) => segmentBytes(part) === segmentBytes(b[i]));
|
|
270
|
+
}
|
|
271
|
+
function sourceMatches(source, url, origin, redirectCount) {
|
|
272
|
+
const lower = source.toLowerCase(), targetScheme = scheme(url);
|
|
273
|
+
if (lower === "'self'")
|
|
274
|
+
return origin ? selfMatches(url, origin) ? "yes" : "no" : "unknown";
|
|
275
|
+
if (source === "*")
|
|
276
|
+
return ["http", "https"].includes(targetScheme) ? "yes" : origin ? scheme(origin) === targetScheme ? "yes" : "no" : "unknown";
|
|
277
|
+
const schemeOnly = source.match(/^([a-z][a-z0-9+.-]*):$/i);
|
|
278
|
+
if (schemeOnly)
|
|
279
|
+
return schemeMatches(schemeOnly[1], targetScheme) ? "yes" : "no";
|
|
280
|
+
const host = source.match(
|
|
281
|
+
/^(?:([a-z][a-z0-9+.-]*):\/\/)?(\*|(?:\*\.)?[a-z0-9.-]+)(?::(\*|[0-9]+))?(\/[^?#]*)?$/i
|
|
282
|
+
);
|
|
283
|
+
if (!host) return "no";
|
|
284
|
+
const [, explicitScheme, patternHost, port, path] = host;
|
|
285
|
+
if (!url.hostname || /^\[/.test(url.hostname) || /^\d+(?:\.\d+){3}$/.test(url.hostname))
|
|
286
|
+
return "no";
|
|
287
|
+
if (!explicitScheme && !origin) return "unknown";
|
|
288
|
+
if (!schemeMatches(explicitScheme ?? scheme(origin), targetScheme))
|
|
289
|
+
return "no";
|
|
290
|
+
const domain = url.hostname.toLowerCase(), pattern = patternHost.toLowerCase();
|
|
291
|
+
if (pattern !== "*" && !(pattern.startsWith("*.") ? domain.endsWith(pattern.slice(1)) : domain === pattern))
|
|
292
|
+
return "no";
|
|
293
|
+
if (port !== "*") {
|
|
294
|
+
const targetPort = url.port || void 0;
|
|
295
|
+
if (port === void 0 ? targetPort !== void 0 : Number(port) !== Number(targetPort ?? defaultPort(targetScheme)))
|
|
296
|
+
return "no";
|
|
297
|
+
}
|
|
298
|
+
if (path && !pathMatches(path, url.pathname)) {
|
|
299
|
+
if (redirectCount === void 0) return "unknown";
|
|
300
|
+
if (redirectCount === 0) return "no";
|
|
301
|
+
}
|
|
302
|
+
return "yes";
|
|
303
|
+
}
|
|
304
|
+
function matchPolicy(directives, directive, input, options = {}) {
|
|
305
|
+
const requested = friendly[directive] ?? directive.toLowerCase();
|
|
306
|
+
const name = requested === "script-src" ? "script-src-elem" : requested === "style-src" ? "style-src-elem" : requested;
|
|
307
|
+
const chain = fallbacks[name];
|
|
308
|
+
if (!chain)
|
|
309
|
+
return decision(
|
|
310
|
+
"indeterminate",
|
|
311
|
+
void 0,
|
|
312
|
+
"Unsupported directive or inline check"
|
|
313
|
+
);
|
|
314
|
+
const effective = chain.find(
|
|
315
|
+
(key) => Object.prototype.hasOwnProperty.call(directives, key)
|
|
316
|
+
);
|
|
317
|
+
if (!effective)
|
|
318
|
+
return decision("allowed", void 0, "No applicable directive");
|
|
319
|
+
if (name.endsWith("-attr"))
|
|
320
|
+
return decision(
|
|
321
|
+
"indeterminate",
|
|
322
|
+
effective,
|
|
323
|
+
"Inline checks require element content and context"
|
|
324
|
+
);
|
|
325
|
+
if (name === "frame-ancestors")
|
|
326
|
+
return decision(
|
|
327
|
+
"indeterminate",
|
|
328
|
+
effective,
|
|
329
|
+
"Frame enforcement depends on the complete ancestor chain"
|
|
330
|
+
);
|
|
331
|
+
if (options.redirectCount !== void 0 && (!Number.isInteger(options.redirectCount) || options.redirectCount < 0))
|
|
332
|
+
throw new TypeError("redirectCount must be a non-negative integer");
|
|
333
|
+
let url, origin;
|
|
334
|
+
try {
|
|
335
|
+
url = new URL(input);
|
|
336
|
+
if (options.origin !== void 0) {
|
|
337
|
+
origin = new URL(options.origin);
|
|
338
|
+
if (origin.origin === "null") throw new Error("opaque");
|
|
339
|
+
}
|
|
340
|
+
} catch {
|
|
341
|
+
throw new TypeError("Expected an absolute URL and a non-opaque origin");
|
|
342
|
+
}
|
|
343
|
+
const sources = directives[effective];
|
|
344
|
+
const script = name === "script-src" || name === "script-src-elem";
|
|
345
|
+
const style = name === "style-src" || name === "style-src-elem";
|
|
346
|
+
const hasNoncePlaceholder = sources.includes("'nonce-{nonce}'");
|
|
347
|
+
const nonceSources = sources.filter(
|
|
348
|
+
(s) => /^'nonce-[a-z0-9+/_=-]+'$/i.test(s)
|
|
349
|
+
);
|
|
350
|
+
const hashes = sources.filter(
|
|
351
|
+
(s) => /^'sha(256|384|512)-[a-z0-9+/_=-]+'$/i.test(s)
|
|
352
|
+
);
|
|
353
|
+
if ((script || style) && options.nonce && (nonceSources.some((s) => s.slice(7, -1) === options.nonce) || hasNoncePlaceholder && options.policyNonce !== void 0 && options.nonce === options.policyNonce))
|
|
354
|
+
return decision("allowed", effective, "Matching request nonce");
|
|
355
|
+
if (script && options.integrity?.length && options.integrity.every(
|
|
356
|
+
(token) => hashes.some((hash) => {
|
|
357
|
+
const [hashAlg, hashDigest] = hash.slice(1, -1).split("-", 2);
|
|
358
|
+
const [tokenAlg, tokenDigest] = token.split("-", 2);
|
|
359
|
+
return hashAlg?.toLowerCase() === tokenAlg?.toLowerCase() && hashDigest !== void 0 && hashDigest === tokenDigest;
|
|
360
|
+
})
|
|
361
|
+
))
|
|
362
|
+
return decision("allowed", effective, "Matching integrity metadata");
|
|
363
|
+
const missingNonce = (script || style) && (nonceSources.length > 0 && options.nonce === void 0 || hasNoncePlaceholder && (options.nonce === void 0 || options.nonce !== "" && options.policyNonce === void 0));
|
|
364
|
+
const missingIntegrity = script && hashes.length > 0 && options.integrity === void 0;
|
|
365
|
+
if (script && sources.some((s) => s.toLowerCase() === "'strict-dynamic'")) {
|
|
366
|
+
if (options.parserInserted === false)
|
|
367
|
+
return decision(
|
|
368
|
+
"allowed",
|
|
369
|
+
effective,
|
|
370
|
+
"Non-parser-inserted script under strict-dynamic"
|
|
371
|
+
);
|
|
372
|
+
if (options.parserInserted === void 0 || missingNonce || missingIntegrity)
|
|
373
|
+
return decision(
|
|
374
|
+
"indeterminate",
|
|
375
|
+
effective,
|
|
376
|
+
"Script nonce, integrity or parser metadata is missing"
|
|
377
|
+
);
|
|
378
|
+
return decision(
|
|
379
|
+
"blocked",
|
|
380
|
+
effective,
|
|
381
|
+
"Parser-inserted script has no matching nonce or integrity metadata"
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
let unknown = false;
|
|
385
|
+
for (const source of sources) {
|
|
386
|
+
const match = sourceMatches(source, url, origin, options.redirectCount);
|
|
387
|
+
if (match === "yes")
|
|
388
|
+
return decision("allowed", effective, void 0, source);
|
|
389
|
+
if (match === "unknown") unknown = true;
|
|
390
|
+
}
|
|
391
|
+
if (unknown || missingNonce || missingIntegrity)
|
|
392
|
+
return decision(
|
|
393
|
+
"indeterminate",
|
|
394
|
+
effective,
|
|
395
|
+
"Origin, redirect count, nonce or integrity metadata is missing"
|
|
396
|
+
);
|
|
397
|
+
return decision("blocked", effective, "No source matched");
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/reports/index.ts
|
|
401
|
+
function record(value) {
|
|
402
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
403
|
+
}
|
|
404
|
+
function field(obj, ...keys) {
|
|
405
|
+
for (const key of keys)
|
|
406
|
+
if (typeof obj[key] === "string" && obj[key]) return obj[key];
|
|
407
|
+
return void 0;
|
|
408
|
+
}
|
|
409
|
+
function parseViolationReports(input) {
|
|
410
|
+
if (typeof input === "string") {
|
|
411
|
+
try {
|
|
412
|
+
input = JSON.parse(input);
|
|
413
|
+
} catch {
|
|
414
|
+
throw new Error("Invalid report JSON");
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
const items = Array.isArray(input) ? input : [input];
|
|
418
|
+
return items.map((item, index) => {
|
|
419
|
+
const envelope = record(item);
|
|
420
|
+
if (!envelope) throw new Error(`Unsupported report at index ${index}`);
|
|
421
|
+
const body = (envelope.type === "csp" ? record(envelope.csp) : void 0) ?? record(envelope["csp-report"]) ?? (envelope.type === "csp-violation" ? record(envelope.body) : void 0) ?? envelope;
|
|
422
|
+
const directive = field(
|
|
423
|
+
body,
|
|
424
|
+
"effective-directive",
|
|
425
|
+
"effectiveDirective",
|
|
426
|
+
"effective_directive",
|
|
427
|
+
"violated-directive",
|
|
428
|
+
"violatedDirective",
|
|
429
|
+
"violated_directive"
|
|
430
|
+
);
|
|
431
|
+
const blockedURL = field(
|
|
432
|
+
body,
|
|
433
|
+
"blocked-uri",
|
|
434
|
+
"blockedURL",
|
|
435
|
+
"blockedUri",
|
|
436
|
+
"blocked_uri"
|
|
437
|
+
);
|
|
438
|
+
const documentURL = field(
|
|
439
|
+
body,
|
|
440
|
+
"document-uri",
|
|
441
|
+
"documentURL",
|
|
442
|
+
"documentUri",
|
|
443
|
+
"document_uri"
|
|
444
|
+
);
|
|
445
|
+
if (!directive || !blockedURL)
|
|
446
|
+
throw new Error(`Unsupported or incomplete report at index ${index}`);
|
|
447
|
+
return {
|
|
448
|
+
directive: directive.split(" ")[0],
|
|
449
|
+
blockedURL,
|
|
450
|
+
...documentURL ? { documentURL } : {}
|
|
451
|
+
};
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
function replayReports(directives, reports) {
|
|
455
|
+
return reports.map((report) => {
|
|
456
|
+
let origin;
|
|
457
|
+
try {
|
|
458
|
+
if (report.documentURL) {
|
|
459
|
+
const parsedOrigin = new URL(report.documentURL).origin;
|
|
460
|
+
if (parsedOrigin !== "null") origin = parsedOrigin;
|
|
461
|
+
}
|
|
462
|
+
} catch {
|
|
463
|
+
}
|
|
464
|
+
let blocked;
|
|
465
|
+
try {
|
|
466
|
+
blocked = new URL(report.blockedURL);
|
|
467
|
+
} catch {
|
|
468
|
+
return {
|
|
469
|
+
report,
|
|
470
|
+
result: {
|
|
471
|
+
status: "indeterminate",
|
|
472
|
+
allowed: null,
|
|
473
|
+
reason: "Report does not contain a replayable URL"
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
if (!["http:", "https:", "ws:", "wss:"].includes(blocked.protocol)) {
|
|
478
|
+
return {
|
|
479
|
+
report,
|
|
480
|
+
result: {
|
|
481
|
+
status: "indeterminate",
|
|
482
|
+
allowed: null,
|
|
483
|
+
reason: "Report does not contain a replayable network URL"
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
const result = matchPolicy(
|
|
488
|
+
directives,
|
|
489
|
+
report.directive,
|
|
490
|
+
report.blockedURL,
|
|
491
|
+
origin ? { origin } : {}
|
|
492
|
+
);
|
|
493
|
+
if (result.status === "allowed" && result.matchedSource && result.matchedSource.replace(/^[a-z]+:\/\//i, "").includes("/")) {
|
|
494
|
+
return {
|
|
495
|
+
report,
|
|
496
|
+
result: {
|
|
497
|
+
status: "indeterminate",
|
|
498
|
+
allowed: null,
|
|
499
|
+
reason: "Report may omit the request path"
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
return { report, result };
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/core.ts
|
|
508
|
+
import { z as z2 } from "zod";
|
|
509
|
+
function freeze(value) {
|
|
510
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
511
|
+
Object.values(value).forEach(freeze);
|
|
512
|
+
Object.freeze(value);
|
|
513
|
+
}
|
|
514
|
+
return value;
|
|
515
|
+
}
|
|
516
|
+
function defineRuleSet(input) {
|
|
517
|
+
const parsed = ruleSetSchema.safeParse(input);
|
|
518
|
+
if (!parsed.success) {
|
|
519
|
+
const name = typeof input?.name === "string" ? input.name : "<unnamed>";
|
|
520
|
+
const detail = parsed.error.issues.map((issue) => `${issue.path.join(".") || "<root>"}: ${issue.message}`).join("; ");
|
|
521
|
+
throw new z2.ZodError(
|
|
522
|
+
parsed.error.issues.map((issue) => ({
|
|
523
|
+
...issue,
|
|
524
|
+
message: `RuleSet "${name}" - ${detail}`
|
|
525
|
+
}))
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
return freeze(parsed.data);
|
|
529
|
+
}
|
|
530
|
+
function entryValue(entry) {
|
|
531
|
+
return typeof entry === "string" ? entry : entry.source;
|
|
532
|
+
}
|
|
533
|
+
function entryReason(entry) {
|
|
534
|
+
return typeof entry === "string" ? void 0 : entry.reason;
|
|
535
|
+
}
|
|
536
|
+
var Policy = class _Policy {
|
|
537
|
+
config;
|
|
538
|
+
headerName;
|
|
539
|
+
constructor(input) {
|
|
540
|
+
const result = policyConfigSchema.safeParse(input);
|
|
541
|
+
if (!result.success) {
|
|
542
|
+
throw new z2.ZodError(
|
|
543
|
+
result.error.issues.map((issue) => {
|
|
544
|
+
const index = issue.path[0] === "ruleSets" && typeof issue.path[1] === "number" ? issue.path[1] : void 0;
|
|
545
|
+
const name = index === void 0 ? void 0 : input.ruleSets?.[index]?.name;
|
|
546
|
+
return name ? {
|
|
547
|
+
...issue,
|
|
548
|
+
message: `RuleSet "${name}" - ${issue.path.slice(2).join(".")}: ${issue.message}`
|
|
549
|
+
} : issue;
|
|
550
|
+
})
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
const config = result.data;
|
|
554
|
+
this.config = freeze(config);
|
|
555
|
+
this.headerName = config.mode === "enforce" ? "Content-Security-Policy" : "Content-Security-Policy-Report-Only";
|
|
556
|
+
Object.freeze(this);
|
|
557
|
+
}
|
|
558
|
+
/** Sources and their contributing rule sets. Duplicate values retain all contributors. */
|
|
559
|
+
explain() {
|
|
560
|
+
const rows = [];
|
|
561
|
+
const add = (directive, entries, ruleSet) => {
|
|
562
|
+
for (const entry of entries) {
|
|
563
|
+
const reason = entryReason(entry);
|
|
564
|
+
rows.push({
|
|
565
|
+
directive,
|
|
566
|
+
source: entryValue(entry),
|
|
567
|
+
ruleSet,
|
|
568
|
+
...reason === void 0 ? {} : { reason }
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
const collect = (rules, name) => {
|
|
573
|
+
for (const [key, value] of Object.entries(rules)) {
|
|
574
|
+
if (key === "directives" && value) {
|
|
575
|
+
for (const [directive, entries] of Object.entries(
|
|
576
|
+
value
|
|
577
|
+
))
|
|
578
|
+
add(directive, entries, name);
|
|
579
|
+
} else if (Array.isArray(value)) {
|
|
580
|
+
const directive = resourceDirectives[key] ?? baseDirectives[key];
|
|
581
|
+
if (directive) add(directive, value, name);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
collect(this.config.base, "base");
|
|
586
|
+
for (const set of this.config.ruleSets)
|
|
587
|
+
if (set.enabled) collect(set.rules, set.name);
|
|
588
|
+
return rows.sort(
|
|
589
|
+
(a, b) => a.directive.localeCompare(b.directive, "en") || a.source.localeCompare(b.source, "en") || a.ruleSet.localeCompare(b.ruleSet, "en") || (a.reason ?? "").localeCompare(b.reason ?? "", "en")
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
/** Merged sources, deduplicated without changing path syntax. */
|
|
593
|
+
toObject() {
|
|
594
|
+
const map = /* @__PURE__ */ Object.create(null);
|
|
595
|
+
for (const row of this.explain())
|
|
596
|
+
(map[row.directive] ??= /* @__PURE__ */ new Set()).add(row.source);
|
|
597
|
+
if (this.config.base.upgradeInsecureRequests)
|
|
598
|
+
map["upgrade-insecure-requests"] = /* @__PURE__ */ new Set();
|
|
599
|
+
if (this.config.reporting?.uri)
|
|
600
|
+
map["report-uri"] = /* @__PURE__ */ new Set([this.config.reporting.uri]);
|
|
601
|
+
if (this.config.reporting?.to)
|
|
602
|
+
map["report-to"] = /* @__PURE__ */ new Set([this.config.reporting.to]);
|
|
603
|
+
return Object.fromEntries(
|
|
604
|
+
Object.entries(map).sort(([a], [b]) => a.localeCompare(b, "en")).map(([key, values]) => [key, Array.from(values).sort()])
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
/** Evaluate a URL with explicit uncertainty when browser context is missing. */
|
|
608
|
+
allows(directive, url, options = {}) {
|
|
609
|
+
const result = matchPolicy(this.toObject(), directive, url, options);
|
|
610
|
+
const contributors = result.matchedSource && result.effectiveDirective ? this.explain().filter(
|
|
611
|
+
(row) => row.directive === result.effectiveDirective && row.source === result.matchedSource
|
|
612
|
+
) : [];
|
|
613
|
+
return contributors.length ? { ...result, contributors } : result;
|
|
614
|
+
}
|
|
615
|
+
/** Replay supported violation report JSON without inventing missing request context. */
|
|
616
|
+
check(reports) {
|
|
617
|
+
return replayReports(this.toObject(), parseViolationReports(reports));
|
|
618
|
+
}
|
|
619
|
+
/** Render a header value. Pretty output is for display only and is not a valid HTTP header value. */
|
|
620
|
+
toHeader(options = {}) {
|
|
621
|
+
return this.render(this.toObject(), options);
|
|
622
|
+
}
|
|
623
|
+
render(directives, options) {
|
|
624
|
+
const hasNonce = Object.values(directives).some(
|
|
625
|
+
(sources) => sources.includes(NONCE_PLACEHOLDER)
|
|
626
|
+
);
|
|
627
|
+
if (hasNonce && (!options.nonce || !/^[A-Za-z0-9+/_-]+={0,2}$/.test(options.nonce)))
|
|
628
|
+
throw new Error("A valid base64 nonce is required");
|
|
629
|
+
return Object.entries(directives).map(([directive, values]) => {
|
|
630
|
+
const rendered = values.map(
|
|
631
|
+
(value) => value === NONCE_PLACEHOLDER ? `'nonce-${options.nonce}'` : value
|
|
632
|
+
);
|
|
633
|
+
return directive + (rendered.length ? ` ${rendered.join(" ")}` : "");
|
|
634
|
+
}).join(options.pretty ? ";\n" : "; ");
|
|
635
|
+
}
|
|
636
|
+
/** Render a meta policy; report-only is not supported in meta delivery. */
|
|
637
|
+
toMetaContent(options = {}) {
|
|
638
|
+
if (this.config.mode === "report-only")
|
|
639
|
+
throw new Error(
|
|
640
|
+
"Report-only policies cannot be delivered in a meta element"
|
|
641
|
+
);
|
|
642
|
+
const directives = this.toObject();
|
|
643
|
+
delete directives["frame-ancestors"];
|
|
644
|
+
delete directives["report-uri"];
|
|
645
|
+
delete directives["report-to"];
|
|
646
|
+
delete directives.sandbox;
|
|
647
|
+
return this.render(directives, options);
|
|
648
|
+
}
|
|
649
|
+
withRuleSet(ruleSet) {
|
|
650
|
+
return new _Policy({
|
|
651
|
+
...this.config,
|
|
652
|
+
ruleSets: [...this.config.ruleSets, ruleSet]
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
without(name) {
|
|
656
|
+
return new _Policy({
|
|
657
|
+
...this.config,
|
|
658
|
+
ruleSets: this.config.ruleSets.filter((set) => set.name !== name)
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
function definePolicy(input) {
|
|
663
|
+
return new Policy(input);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// src/diff.ts
|
|
667
|
+
function diffPolicies(before, after) {
|
|
668
|
+
const key = (row) => JSON.stringify([
|
|
669
|
+
row.directive,
|
|
670
|
+
row.source,
|
|
671
|
+
row.ruleSet,
|
|
672
|
+
row.reason ?? null
|
|
673
|
+
]);
|
|
674
|
+
const oldRows = new Map(before.explain().map((row) => [key(row), row]));
|
|
675
|
+
const newRows = new Map(after.explain().map((row) => [key(row), row]));
|
|
676
|
+
const changes = [];
|
|
677
|
+
for (const [id, row] of oldRows)
|
|
678
|
+
if (!newRows.has(id))
|
|
679
|
+
changes.push({
|
|
680
|
+
directive: row.directive,
|
|
681
|
+
source: row.source,
|
|
682
|
+
ruleSet: row.ruleSet,
|
|
683
|
+
change: "removed",
|
|
684
|
+
...row.reason ? { detail: row.reason } : {}
|
|
685
|
+
});
|
|
686
|
+
for (const [id, row] of newRows)
|
|
687
|
+
if (!oldRows.has(id))
|
|
688
|
+
changes.push({
|
|
689
|
+
directive: row.directive,
|
|
690
|
+
source: row.source,
|
|
691
|
+
ruleSet: row.ruleSet,
|
|
692
|
+
change: "added",
|
|
693
|
+
...row.reason ? { detail: row.reason } : {}
|
|
694
|
+
});
|
|
695
|
+
const beforeObj = before.toObject(), afterObj = after.toObject();
|
|
696
|
+
for (const directive of /* @__PURE__ */ new Set([
|
|
697
|
+
...Object.keys(beforeObj),
|
|
698
|
+
...Object.keys(afterObj)
|
|
699
|
+
])) {
|
|
700
|
+
if (JSON.stringify(beforeObj[directive]) !== JSON.stringify(afterObj[directive]) && !changes.some((c) => c.directive === directive))
|
|
701
|
+
changes.push({
|
|
702
|
+
directive,
|
|
703
|
+
source: "",
|
|
704
|
+
ruleSet: "base",
|
|
705
|
+
change: "modified",
|
|
706
|
+
detail: "Directive value changed"
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
if (before.config.mode !== after.config.mode)
|
|
710
|
+
changes.push({
|
|
711
|
+
directive: "(mode)",
|
|
712
|
+
source: "",
|
|
713
|
+
ruleSet: "base",
|
|
714
|
+
change: "modified",
|
|
715
|
+
detail: `${before.config.mode} \u2192 ${after.config.mode}`
|
|
716
|
+
});
|
|
717
|
+
changes.sort(
|
|
718
|
+
(a, b) => a.directive.localeCompare(b.directive) || a.source.localeCompare(b.source) || a.ruleSet.localeCompare(b.ruleSet) || a.change.localeCompare(b.change)
|
|
719
|
+
);
|
|
720
|
+
const headersChanged = JSON.stringify(beforeObj) !== JSON.stringify(afterObj) || before.config.mode !== after.config.mode;
|
|
721
|
+
const dangerous = changes.some(
|
|
722
|
+
(change) => change.change === "added" && !(beforeObj[change.directive] ?? []).includes(change.source) && /^(?:\*|https?:|data:|'unsafe-(?:inline|eval|hashes)')$/.test(
|
|
723
|
+
change.source
|
|
724
|
+
)
|
|
725
|
+
);
|
|
726
|
+
return {
|
|
727
|
+
changes,
|
|
728
|
+
risk: dangerous ? "increased" : headersChanged ? "unknown" : "unchanged"
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// src/importer/index.ts
|
|
733
|
+
function importHeader(header) {
|
|
734
|
+
if (/[\x00-\x08\x0a-\x1f\x7f,]/.test(header))
|
|
735
|
+
throw new Error(
|
|
736
|
+
"Header contains a control character or multiple policies; import one policy at a time"
|
|
737
|
+
);
|
|
738
|
+
const seen = /* @__PURE__ */ new Set();
|
|
739
|
+
const warnings = [];
|
|
740
|
+
const directives = [];
|
|
741
|
+
for (const segment of header.split(";")) {
|
|
742
|
+
const trimmed = segment.trim();
|
|
743
|
+
if (!trimmed) continue;
|
|
744
|
+
const [rawName, ...values] = trimmed.split(/[ \t]+/);
|
|
745
|
+
if (!rawName || !/^[a-zA-Z][a-zA-Z0-9-]*$/.test(rawName))
|
|
746
|
+
throw new Error(`Invalid directive name: ${rawName ?? ""}`);
|
|
747
|
+
const name = rawName.toLowerCase();
|
|
748
|
+
const ignored = seen.has(name);
|
|
749
|
+
if (ignored)
|
|
750
|
+
warnings.push(`Duplicate ${name} is ignored by CSP; retained for review`);
|
|
751
|
+
seen.add(name);
|
|
752
|
+
directives.push({ name, values, ignored });
|
|
753
|
+
}
|
|
754
|
+
return { directives, warnings };
|
|
755
|
+
}
|
|
756
|
+
function serializeImportedPolicy(policy) {
|
|
757
|
+
return policy.directives.map(({ name, values }) => {
|
|
758
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name) || values.some((value) => !value || /[\x00-\x20\x7f;,]/.test(value))) {
|
|
759
|
+
throw new Error("Invalid imported directive or value");
|
|
760
|
+
}
|
|
761
|
+
return [name, ...values].join(" ");
|
|
762
|
+
}).join("; ");
|
|
763
|
+
}
|
|
764
|
+
function importedPolicyToDraft(policy) {
|
|
765
|
+
if (policy.directives.some((directive) => directive.ignored))
|
|
766
|
+
throw new Error(
|
|
767
|
+
"Duplicate directives require manual review before conversion"
|
|
768
|
+
);
|
|
769
|
+
const base = {};
|
|
770
|
+
const baseRaw = {};
|
|
771
|
+
const ruleRaw = {};
|
|
772
|
+
const baseOnly2 = /* @__PURE__ */ new Set([
|
|
773
|
+
"default-src",
|
|
774
|
+
"object-src",
|
|
775
|
+
"base-uri",
|
|
776
|
+
"frame-ancestors"
|
|
777
|
+
]);
|
|
778
|
+
const supported = /* @__PURE__ */ new Set([
|
|
779
|
+
"default-src",
|
|
780
|
+
"object-src",
|
|
781
|
+
"base-uri",
|
|
782
|
+
"frame-ancestors",
|
|
783
|
+
"script-src",
|
|
784
|
+
"script-src-elem",
|
|
785
|
+
"script-src-attr",
|
|
786
|
+
"style-src",
|
|
787
|
+
"style-src-elem",
|
|
788
|
+
"style-src-attr",
|
|
789
|
+
"img-src",
|
|
790
|
+
"connect-src",
|
|
791
|
+
"font-src",
|
|
792
|
+
"frame-src",
|
|
793
|
+
"child-src",
|
|
794
|
+
"form-action",
|
|
795
|
+
"media-src",
|
|
796
|
+
"worker-src",
|
|
797
|
+
"manifest-src"
|
|
798
|
+
]);
|
|
799
|
+
const risky2 = /* @__PURE__ */ new Set([
|
|
800
|
+
"'unsafe-inline'",
|
|
801
|
+
"'unsafe-eval'",
|
|
802
|
+
"'unsafe-hashes'"
|
|
803
|
+
]);
|
|
804
|
+
for (const directive of policy.directives) {
|
|
805
|
+
if (directive.name === "upgrade-insecure-requests") {
|
|
806
|
+
if (directive.values.length)
|
|
807
|
+
throw new Error(
|
|
808
|
+
"upgrade-insecure-requests with values requires manual review"
|
|
809
|
+
);
|
|
810
|
+
base.upgradeInsecureRequests = true;
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
if (!supported.has(directive.name) || directive.values.some(
|
|
814
|
+
(value) => value.toLowerCase() === "'nonce-{nonce}'"
|
|
815
|
+
)) {
|
|
816
|
+
throw new Error(
|
|
817
|
+
`Directive ${directive.name} contains unsupported syntax; review manually`
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
if (directive.values.length === 0 || ["report-uri", "report-to", "sandbox"].includes(directive.name)) {
|
|
821
|
+
throw new Error(
|
|
822
|
+
`Directive ${directive.name} requires manual review before conversion`
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
const entries = directive.values.map(
|
|
826
|
+
(source) => risky2.has(source.toLowerCase()) ? { source, reason: "Imported legacy policy; review required" } : source
|
|
827
|
+
);
|
|
828
|
+
(baseOnly2.has(directive.name) ? baseRaw : ruleRaw)[directive.name] = entries;
|
|
829
|
+
}
|
|
830
|
+
if (Object.keys(baseRaw).length) base.directives = baseRaw;
|
|
831
|
+
const draft = {
|
|
832
|
+
base,
|
|
833
|
+
ruleSets: Object.keys(ruleRaw).length ? [
|
|
834
|
+
{
|
|
835
|
+
name: "Imported policy",
|
|
836
|
+
purpose: "Imported sources awaiting ownership review",
|
|
837
|
+
rules: { directives: ruleRaw }
|
|
838
|
+
}
|
|
839
|
+
] : []
|
|
840
|
+
};
|
|
841
|
+
const result = policyConfigSchema.safeParse(draft);
|
|
842
|
+
if (!result.success)
|
|
843
|
+
throw new Error(
|
|
844
|
+
`Imported policy contains unsupported sources: ${result.error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join("; ")}`
|
|
845
|
+
);
|
|
846
|
+
return draft;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
export {
|
|
850
|
+
resourceDirectives,
|
|
851
|
+
baseDirectives,
|
|
852
|
+
NONCE_PLACEHOLDER,
|
|
853
|
+
sourceEntrySchema,
|
|
854
|
+
rulesSchema,
|
|
855
|
+
baseSchema,
|
|
856
|
+
ruleSetSchema,
|
|
857
|
+
policyConfigSchema,
|
|
858
|
+
sourceExpressions,
|
|
859
|
+
generateNonce,
|
|
860
|
+
unsafeInline,
|
|
861
|
+
unsafeEval,
|
|
862
|
+
unsafeHashes,
|
|
863
|
+
cspHash,
|
|
864
|
+
matchPolicy,
|
|
865
|
+
parseViolationReports,
|
|
866
|
+
replayReports,
|
|
867
|
+
defineRuleSet,
|
|
868
|
+
Policy,
|
|
869
|
+
definePolicy,
|
|
870
|
+
diffPolicies,
|
|
871
|
+
importHeader,
|
|
872
|
+
serializeImportedPolicy,
|
|
873
|
+
importedPolicyToDraft
|
|
874
|
+
};
|
|
875
|
+
//# sourceMappingURL=chunk-TE3J7Y25.js.map
|