@consent-kit/audit 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.
@@ -0,0 +1,348 @@
1
+ import { CspDeviation } from '@consent-kit/compiler';
2
+ import { Registry, AppliedPreset, OverrideRecord, Receipts, PresetDefinition } from '@consent-kit/schema';
3
+ import { Browser, Page } from 'playwright';
4
+
5
+ /**
6
+ * The seven checks of spec section 9, the consent-receipts and subject-requests checks, the two coverage
7
+ * classes of 6b that no numbered check owns, and two setup checks: that a guard shipped in the page
8
+ * actually runs, and that the site's own markup and code do not violate its
9
+ * Content-Security-Policy. The last four are derived from what the browser checks' sessions saw.
10
+ */
11
+ declare const CHECK_IDS: readonly ["pre-consent-silence", "reject-means-reject", "grant-activates-declared", "withdrawal-cleans-up", "gpc-honoured", "undeclared-drift", "banner-ux", "consent-receipts", "subject-requests", "attribute-drift", "unexercised", "guard-installed", "csp-violations"];
12
+ type CheckId = (typeof CHECK_IDS)[number];
13
+ declare const CHECK_TITLES: Record<CheckId, string>;
14
+ /**
15
+ * What an observation is about. `script`, `iframe` and `request` are URLs and
16
+ * are matched against the guard manifest's `scripts`, `iframes` and `endpoints`
17
+ * lists; the others are matched by name against the list of the same name.
18
+ */
19
+ type ObservedKind = 'cookie' | 'localStorage' | 'sessionStorage' | 'indexedDB' | 'script' | 'iframe' | 'request';
20
+ interface ObservedCookie {
21
+ domain: string;
22
+ path: string;
23
+ /** Seconds since the epoch, or -1 for a session cookie (Playwright's convention). */
24
+ expires: number;
25
+ sameSite: 'Strict' | 'Lax' | 'None';
26
+ httpOnly: boolean;
27
+ secure: boolean;
28
+ /** Epoch milliseconds at which the audit first saw this cookie with this expiry. */
29
+ firstSeen: number;
30
+ }
31
+ /** One thing the browser did or holds, as seen by the audit. */
32
+ interface Observation {
33
+ kind: ObservedKind;
34
+ /** Cookie name, storage key, database name, or the URL's origin + pathname (never query or hash). */
35
+ key: string;
36
+ /** Where it was seen: a network request, the cookie jar, the page's storage, or a write intercepted in the page. */
37
+ source: 'request' | 'cookie-jar' | 'storage' | 'write';
38
+ /** Pathname of the audited page at the time. */
39
+ page: string;
40
+ /** What the audit had done so far, e.g. "before any decision". */
41
+ phase: string;
42
+ /** Consent categories the audit had granted when this was observed. */
43
+ granted: string[];
44
+ /** Set when the browser refused the request because of the page's Content-Security-Policy. */
45
+ blockedBy?: 'csp';
46
+ /** Playwright resource type, for requests. */
47
+ resourceType?: string;
48
+ cookie?: ObservedCookie;
49
+ }
50
+ /** Whether the consent-kit runtime guard was running on one page load. */
51
+ interface GuardStatus {
52
+ /** Pathname of the audited page. */
53
+ page: string;
54
+ phase: string;
55
+ /** The page's HTML contains the inline guard script. */
56
+ present: boolean;
57
+ /** The guard installed: it set `window.__ck_guard`. */
58
+ installed: boolean;
59
+ /** The browser refused an inline script on this page because of the Content-Security-Policy. */
60
+ inlineScriptBlocked: boolean;
61
+ }
62
+ /**
63
+ * Something in the page itself that its Content-Security-Policy refused and that is not a
64
+ * request to a URL: an inline `<style>` or `style` attribute, an inline script or event
65
+ * handler, `eval`, a `data:` or `blob:` resource. (A refused request is an `Observation`
66
+ * with `blockedBy: 'csp'`.)
67
+ */
68
+ interface PolicyViolation {
69
+ /** Pathname of the audited page. */
70
+ page: string;
71
+ phase: string;
72
+ /** The directive that refused it, e.g. "style-src-attr". */
73
+ directive: string;
74
+ /** What was refused, as the browser names it: "inline", "eval", "data", "blob", … */
75
+ blocked: string;
76
+ }
77
+ type FindingCode =
78
+ /** A declared, consent-bound artefact was used while its category was not granted. */
79
+ 'wrong-purpose'
80
+ /** Observed, but nothing in the registry declares it. */
81
+ | 'undeclared'
82
+ /** Check 3: declared for the granted category, but never observed after the grant. */
83
+ | 'missing'
84
+ /** Check 4: still present after withdrawal. */
85
+ | 'not-cleaned'
86
+ /** Check 4: the artefact never appeared, so its cleanup could not be verified. */
87
+ | 'not-verified'
88
+ /** The banner did what was clicked, but the consent cookie does not say so. */
89
+ | 'consent-not-recorded'
90
+ /** The audit could not operate the banner or the preferences modal. */
91
+ | 'cannot-drive' | 'banner-missing' | 'banner-without-consent' | 'button-missing' | 'unequal-weight' | 'preferences-unreachable' | 'attribute-drift' | 'unexercised'
92
+ /** The page ships the inline guard, but it did not install (typically: its text no longer matches the CSP hash). */
93
+ | 'guard-not-installed'
94
+ /** The page's own markup or code was refused by its Content-Security-Policy (inline style or script, eval, data: or blob:). */
95
+ | 'csp-violation'
96
+ /** The runtime guard (record mode) blocked an attempt. Informational. */
97
+ | 'guard-blocked'
98
+ /** Consent receipts: a decision sent no receipt to the declared endpoint. */
99
+ | 'receipt-missing'
100
+ /** Consent receipts: a decision sent more than one. */
101
+ | 'receipt-duplicate'
102
+ /** Consent receipts: the receipt is not of the documented shape, or says something else than the decision. */
103
+ | 'receipt-malformed'
104
+ /** Consent receipts: the registry opts out. A warning that carries the reason. */
105
+ | 'receipts-opted-out'
106
+ /** Consent receipts: the registry asks for consent but has no `receipts` section. */
107
+ | 'receipts-not-stated'
108
+ /** Subject requests: no audited page has the request form, or links to a page that has it. */
109
+ | 'subject-request-form-missing'
110
+ /** Subject requests: the form posts to another origin, or not with POST. */
111
+ | 'subject-request-form-foreign'
112
+ /** Subject requests: the test submission was not answered 303 by the endpoint. */
113
+ | 'subject-request-not-accepted'
114
+ /** Subject requests: submitting the form made the browser contact a third party. */
115
+ | 'subject-request-third-party'
116
+ /** The check itself crashed. */
117
+ | 'check-error';
118
+ interface Finding {
119
+ severity: 'error' | 'warning';
120
+ code: FindingCode;
121
+ message: string;
122
+ /** Registry artefact id, when the finding is about a declared artefact. */
123
+ artefact?: string;
124
+ kind?: ObservedKind;
125
+ key?: string;
126
+ page?: string;
127
+ phase?: string;
128
+ /** The consent category the finding relates to. */
129
+ category?: string;
130
+ /** For `undeclared`: a ready-to-paste `privacy.registry.yaml` artefact entry. */
131
+ registryEntry?: string;
132
+ }
133
+ interface CheckResult {
134
+ id: CheckId;
135
+ title: string;
136
+ /** `fail`: at least one error finding. `warn`: only warnings. `skipped`: not selected. */
137
+ status: 'pass' | 'warn' | 'fail' | 'skipped';
138
+ /** Why the check had less to do than usual, e.g. "no purpose asks for consent". */
139
+ note?: string;
140
+ findings: Finding[];
141
+ durationMs: number;
142
+ }
143
+ /** The four classes of spec 6b layer 4, merged over every check's browser session. */
144
+ interface Coverage {
145
+ /** Artefact ids that were observed at least once. */
146
+ exercised: string[];
147
+ /** Declared, never observed. */
148
+ unexercised: string[];
149
+ undeclared: Finding[];
150
+ wrongPurpose: Finding[];
151
+ attributeDrift: Finding[];
152
+ }
153
+ interface AuditReport {
154
+ /** True when no check failed. */
155
+ ok: boolean;
156
+ url: string;
157
+ pages: string[];
158
+ strict: boolean;
159
+ startedAt: string;
160
+ durationMs: number;
161
+ checks: CheckResult[];
162
+ coverage: Coverage;
163
+ summary: {
164
+ passed: number;
165
+ warned: number;
166
+ failed: number;
167
+ skipped: number;
168
+ };
169
+ /**
170
+ * Present when the registry adopts presets: which ones, and every local
171
+ * override of a preset default ("local drift from the documented default").
172
+ * Informational; it does not affect `ok`.
173
+ */
174
+ presets?: {
175
+ applied: AppliedPreset[];
176
+ overrides: OverrideRecord[];
177
+ };
178
+ /**
179
+ * How the Content-Security-Policy generated from the registry compares with the strict
180
+ * default: every deviation, with its reason. Informational; it does not affect `ok`.
181
+ * (It describes the generated policy, not the header the audited server actually sent.)
182
+ */
183
+ csp: {
184
+ level: 'strict' | 'loosened';
185
+ deviations: CspDeviation[];
186
+ };
187
+ /**
188
+ * The registry's `receipts` section: the endpoint the consent-receipts check verified, or
189
+ * the opt-out and its reason (also a warning of that check). Null without a section.
190
+ */
191
+ receipts: Receipts | null;
192
+ }
193
+ /** CSS selectors for the banner. The defaults are CookieConsent v3's own markup. */
194
+ interface Selectors {
195
+ /** First layer (the consent modal). */
196
+ banner: string;
197
+ accept: string;
198
+ reject: string;
199
+ /** The first layer's button that opens the preferences modal. */
200
+ managePreferences: string;
201
+ preferencesModal: string;
202
+ /** The link or button that reopens the preferences; check 7 requires one in the footer of every page. */
203
+ preferencesLink: string;
204
+ /** `{category}` is replaced with the consent category id. Must select a checkbox. */
205
+ categoryToggle: string;
206
+ save: string;
207
+ rejectInPreferences: string;
208
+ }
209
+ interface Tolerances {
210
+ /**
211
+ * Check 7. Accept and Reject have equal size when the smaller one's area and
212
+ * height are each at least `1 - size` of the larger one's. Default 0.25.
213
+ */
214
+ size: number;
215
+ /**
216
+ * Check 7. Equal contrast: for both the text-on-button contrast and the
217
+ * button-on-banner contrast (WCAG contrast ratios), the lower value is at
218
+ * least `1 - contrast` of the higher one. Default 0.25.
219
+ */
220
+ contrast: number;
221
+ /**
222
+ * Attribute drift. A cookie's observed lifetime may differ from the declared
223
+ * duration by this share of the declared duration, and always by one hour. Default 0.01.
224
+ */
225
+ expiry: number;
226
+ }
227
+ interface ExerciseContext {
228
+ /** Pathname of the page that was just loaded. */
229
+ path: string;
230
+ phase: string;
231
+ granted: string[];
232
+ }
233
+ interface AuditOptions {
234
+ /** The site under audit, e.g. a preview deploy. Its origin is the first party. */
235
+ url: string;
236
+ registry: Registry;
237
+ /**
238
+ * The `presets` of the `parseRegistry` result the registry came from. Copied
239
+ * into the report so that local overrides of preset defaults are visible there.
240
+ */
241
+ presets?: {
242
+ applied: AppliedPreset[];
243
+ overrides: OverrideRecord[];
244
+ };
245
+ /** Paths (or absolute same-origin URLs) to audit. Default `['/']`. Check 2 needs two to cross a navigation. */
246
+ pages?: string[];
247
+ /** Unexercised declarations fail instead of warn. */
248
+ strict?: boolean;
249
+ /** Run only these checks. Default: all. */
250
+ checks?: readonly CheckId[];
251
+ /** Reuse a browser (must be Chromium). The audit opens and closes its own contexts. */
252
+ browser?: Browser;
253
+ headless?: boolean;
254
+ /** How many browser contexts may be open at once; checks and their sessions run in parallel up to this. Default 6. */
255
+ concurrency?: number;
256
+ /** A page has settled when no request has started or finished for this long. Default 300 ms. */
257
+ settleMs?: number;
258
+ /** Limit for one navigation or one click. Default 8000 ms. */
259
+ timeoutMs?: number;
260
+ /** Name of CookieConsent's consent cookie. Default "cc_cookie". */
261
+ consentCookieName?: string;
262
+ selectors?: Partial<Selectors>;
263
+ tolerances?: Partial<Tolerances>;
264
+ /**
265
+ * Called after every page load, so project-specific interactions (a theme
266
+ * toggle, a form) are exercised and their artefacts count as observed.
267
+ */
268
+ exercise?: (page: Page, context: ExerciseContext) => Promise<void>;
269
+ }
270
+
271
+ /**
272
+ * Audits a running site against its registry: the seven checks of spec
273
+ * section 9, each in its own fresh Chromium context, plus the coverage
274
+ * classification of section 6b over everything those sessions observed, plus
275
+ * the check that a guard shipped in the page is really running.
276
+ *
277
+ * Never throws for a failing site; a check that crashes is reported as failed
278
+ * with a `check-error` finding. Throws only for unusable options.
279
+ */
280
+ declare function runAudit(options: AuditOptions): Promise<AuditReport>;
281
+
282
+ /** The report as plain text lines, one block per check, findings indented under it. */
283
+ declare function renderReport(report: AuditReport): string[];
284
+
285
+ interface PresetFixtureOptions {
286
+ /** Parameter values that replace the fixture's own (e.g. the origin of a local fake vendor). */
287
+ with?: Record<string, string>;
288
+ /** `live` when the vendor's real service is on the other end. Default `offline`. */
289
+ against?: 'live' | 'offline';
290
+ /** Reuse a browser (must be Chromium). */
291
+ browser?: Browser;
292
+ headless?: boolean;
293
+ /** A page has settled when no request has started or finished for this long. Default 500 ms. */
294
+ settleMs?: number;
295
+ /** Default 15000 ms. */
296
+ timeoutMs?: number;
297
+ }
298
+ interface PresetFixtureResult {
299
+ /** Nothing missing, nothing undeclared, no attribute drift. */
300
+ ok: boolean;
301
+ preset: string;
302
+ version: string;
303
+ against: 'live' | 'offline';
304
+ /** Default ids of the declared artefacts that were observed. */
305
+ observed: string[];
306
+ /** Default ids the fixture expects but the page view did not produce: the declaration may be stale. */
307
+ missing: string[];
308
+ /** What the tool did that the preset does not declare: a new cookie, storage key or origin. Each carries a ready-to-paste entry. */
309
+ undeclared: Finding[];
310
+ /** Declared cookies whose observed attributes differ, e.g. a longer lifetime. */
311
+ attributeDrift: Finding[];
312
+ }
313
+ /** Runs `preset.fixture` once. Throws only when the page cannot be served or loaded at all. */
314
+ declare function runPresetFixture(preset: PresetDefinition, options?: PresetFixtureOptions): Promise<PresetFixtureResult>;
315
+
316
+ declare const EXIT_OK = 0;
317
+ /** At least one check failed. */
318
+ declare const EXIT_FAILED = 1;
319
+ /** Bad command line, or a registry that cannot be read or is invalid. */
320
+ declare const EXIT_USAGE = 2;
321
+ /** The help text. `defaultRegistry` makes `--registry` optional. */
322
+ declare function helpText(program?: string, defaultRegistry?: string): string;
323
+ interface CliIo {
324
+ cwd?: string;
325
+ stdout?: {
326
+ write(text: string): unknown;
327
+ };
328
+ stderr?: {
329
+ write(text: string): unknown;
330
+ };
331
+ /** Injected in tests. Default: the real `runAudit`. */
332
+ audit?: (options: AuditOptions) => Promise<AuditReport>;
333
+ /** How the command names itself in help and errors, e.g. "consent-kit audit". Default "consent-kit-audit". */
334
+ programName?: string;
335
+ /** Registry path used when `--registry` is not given. Without it, `--registry` is required. */
336
+ defaultRegistry?: string;
337
+ }
338
+ /** Runs the CLI and returns its exit code. Never calls `process.exit`. */
339
+ declare function run(argv: readonly string[], io?: CliIo): Promise<number>;
340
+
341
+ declare const DEFAULT_SELECTORS: Selectors;
342
+
343
+ /** Pure logic: observations in, findings out. Nothing here touches a browser. */
344
+
345
+ declare const DEFAULT_TOLERANCES: Tolerances;
346
+
347
+ export { CHECK_IDS, CHECK_TITLES, DEFAULT_SELECTORS, DEFAULT_TOLERANCES, EXIT_FAILED, EXIT_OK, EXIT_USAGE, helpText, renderReport, run, runAudit, runPresetFixture };
348
+ export type { AuditOptions, AuditReport, CheckId, CheckResult, CliIo, Coverage, ExerciseContext, Finding, FindingCode, GuardStatus, Observation, ObservedCookie, ObservedKind, PolicyViolation, PresetFixtureOptions, PresetFixtureResult, Selectors, Tolerances };