prunelle 0.0.0-stage → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +245 -0
- package/package.json +62 -4
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +43 -0
- package/src/build/bundle.ts +661 -0
- package/src/build/byte-size.ts +11 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +175 -0
- package/src/build/client-prune.ts +168 -0
- package/src/build/compile.ts +130 -0
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +171 -0
- package/src/build/first-load.ts +235 -0
- package/src/build/graph.ts +237 -0
- package/src/build/index.ts +678 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +452 -0
- package/src/build/manifest.ts +204 -0
- package/src/build/output.ts +81 -0
- package/src/build/plugin-host.ts +294 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +288 -0
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +290 -0
- package/src/build/resolve.ts +52 -0
- package/src/build/routes.ts +342 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +209 -0
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +409 -0
- package/src/cli/commands.ts +372 -0
- package/src/cli/docs.ts +403 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +406 -0
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +52 -0
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +367 -0
- package/src/compiler/analyze.ts +651 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +250 -0
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +653 -0
- package/src/compiler/data-keys.ts +302 -0
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +452 -0
- package/src/compiler/dom.ts +641 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +587 -0
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +520 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +511 -0
- package/src/compiler/scope.ts +635 -0
- package/src/compiler/server-only.ts +365 -0
- package/src/compiler/shared.ts +397 -0
- package/src/compiler/signal-reads.ts +246 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +69 -0
- package/src/compiler/ssr.ts +525 -0
- package/src/context.ts +199 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +268 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +24 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +159 -0
- package/src/dev/supervisor.ts +212 -0
- package/src/dev/watchers.ts +276 -0
- package/src/dom/action.ts +187 -0
- package/src/dom/attrs.ts +84 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +579 -0
- package/src/dom/index.ts +329 -0
- package/src/dom/island.ts +318 -0
- package/src/dom/loop.ts +484 -0
- package/src/dom/revive.ts +36 -0
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +92 -0
- package/src/plugins/context.ts +109 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +90 -0
- package/src/plugins/order.ts +148 -0
- package/src/plugins/public.ts +15 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +119 -0
- package/src/plugins/types.ts +278 -0
- package/src/reactivity/graph.ts +259 -0
- package/src/reactivity/index.ts +541 -0
- package/src/reactivity/selector.ts +78 -0
- package/src/reactivity/store.ts +252 -0
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +511 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +168 -0
- package/src/server/compress.ts +646 -0
- package/src/server/consent.ts +290 -0
- package/src/server/cookies.ts +153 -0
- package/src/server/document.ts +286 -0
- package/src/server/env.ts +47 -0
- package/src/server/errors.ts +396 -0
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +588 -0
- package/src/server/index.ts +615 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +131 -0
- package/src/server/page.ts +122 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +267 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +20 -0
- package/src/server/rate-limit.ts +199 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +424 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +388 -0
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +550 -0
- package/src/server/serve-handlers.ts +93 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +104 -0
- package/src/server/start.ts +143 -0
- package/src/server/static-dir.ts +246 -0
- package/src/server/stream.ts +233 -0
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +399 -0
- package/src/ssr/index.ts +624 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +192 -0
- package/src/style.ts +96 -0
- package/src/test/browser.ts +201 -0
- package/src/test/index.ts +262 -0
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +8 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +22 -0
- package/templates/_shared/tsconfig.json +25 -0
- package/templates/app/.env.example +8 -0
- package/templates/app/package.json +22 -0
- package/templates/app/prunelle.config.ts +13 -0
- package/templates/app/src/auth.ts +12 -0
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +40 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +47 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +53 -0
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +14 -0
- package/templates/app/tests/app.test.ts +73 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +25 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +15 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +28 -0
- package/templates/fullstack/src/routes/_layout.tsx +18 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +39 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +18 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +32 -0
- package/ts-plugin.cjs +4277 -0
- package/README.md +0 -3
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// CNIL/RGPD one-declaration consent: cookie parsing/serialization, RequestConsent view, and zero-JS POST /_pru/consent endpoint.
|
|
2
|
+
|
|
3
|
+
import type { PluginRouteOptions } from '../plugins/types'
|
|
4
|
+
import type { RequestContext } from './http'
|
|
5
|
+
import type { TrustProxy } from './security'
|
|
6
|
+
import { wantsJson } from './http'
|
|
7
|
+
import { isTrustedOrigin, safeRedirect } from './security'
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_CONSENT_COOKIE = 'pru_consent'
|
|
10
|
+
/** 180 days in seconds (CNIL 6-month guideline for consent retention). */
|
|
11
|
+
export const DEFAULT_CONSENT_MAX_AGE = 15_552_000
|
|
12
|
+
export const DEFAULT_CONSENT_ENDPOINT = '/_pru/consent'
|
|
13
|
+
// v1 held the granted categories alone; v2 adds when the visitor chose and the policy version they chose under.
|
|
14
|
+
const LEGACY_COOKIE_PREFIX = 'v1:'
|
|
15
|
+
const COOKIE_PREFIX = 'v2:'
|
|
16
|
+
const POLICY_VERSION_RE = /^[\w.-]{1,32}$/
|
|
17
|
+
|
|
18
|
+
export interface ConsentConfig {
|
|
19
|
+
/** Non-essential consent categories (e.g. `['analytics', 'media']` or `{ analytics: 'Audience measurement' }`). */
|
|
20
|
+
categories: readonly string[] | Readonly<Record<string, string>>
|
|
21
|
+
/** Cookie name storing consent choices (default: `'pru_consent'`). */
|
|
22
|
+
cookieName?: string | undefined
|
|
23
|
+
/** Max-Age in seconds (default: `15552000` = 180 days, CNIL 6-month guideline). */
|
|
24
|
+
maxAge?: number | undefined
|
|
25
|
+
/** POST endpoint path handling consent form submissions (default: `'/_pru/consent'`). */
|
|
26
|
+
endpoint?: string | undefined
|
|
27
|
+
/** Version of the cookie policy (letters, digits, `.`, `-`, `_`): changing it asks every visitor again. */
|
|
28
|
+
version?: string | undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RequestConsent {
|
|
32
|
+
readonly decided: boolean
|
|
33
|
+
/** When the visitor made the choice in force, kept in the consent cookie as proof. */
|
|
34
|
+
readonly decidedAt: Date | undefined
|
|
35
|
+
readonly categories: readonly string[]
|
|
36
|
+
/** Each category's label, from `consent.categories`, the name itself when given as a list. */
|
|
37
|
+
readonly labels: Readonly<Record<string, string>>
|
|
38
|
+
/** Path the consent form posts to. */
|
|
39
|
+
readonly endpoint: string
|
|
40
|
+
has(category: string): boolean
|
|
41
|
+
all(): Record<string, boolean>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface NormalizedConsentConfig {
|
|
45
|
+
readonly categories: readonly string[]
|
|
46
|
+
readonly categorySet: ReadonlySet<string>
|
|
47
|
+
readonly labels: Readonly<Record<string, string>>
|
|
48
|
+
readonly cookieName: string
|
|
49
|
+
readonly maxAge: number
|
|
50
|
+
readonly endpoint: string
|
|
51
|
+
readonly version: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const EMPTY_CONSENT: RequestConsent = {
|
|
55
|
+
decided: false,
|
|
56
|
+
decidedAt: undefined,
|
|
57
|
+
categories: [],
|
|
58
|
+
labels: {},
|
|
59
|
+
endpoint: DEFAULT_CONSENT_ENDPOINT,
|
|
60
|
+
has: category => category === 'essential',
|
|
61
|
+
all: () => ({}),
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function normalizeConsentConfig(config: ConsentConfig | undefined): NormalizedConsentConfig | undefined {
|
|
65
|
+
if (config === undefined)
|
|
66
|
+
return undefined
|
|
67
|
+
const rawList = Array.isArray(config.categories) ? config.categories : Object.keys(config.categories)
|
|
68
|
+
const given: Readonly<Record<string, string>> = Array.isArray(config.categories) ? {} : config.categories as Readonly<Record<string, string>>
|
|
69
|
+
const categories: string[] = []
|
|
70
|
+
const categorySet = new Set<string>()
|
|
71
|
+
const labels: Record<string, string> = {}
|
|
72
|
+
for (const item of rawList) {
|
|
73
|
+
const name = item.trim()
|
|
74
|
+
if (name === '' || name === 'essential' || categorySet.has(name))
|
|
75
|
+
continue
|
|
76
|
+
if (name.includes(',') || name.includes(':'))
|
|
77
|
+
throw new Error(`prunelle: consent category "${name}" cannot contain "," or ":"`)
|
|
78
|
+
categorySet.add(name)
|
|
79
|
+
categories.push(name)
|
|
80
|
+
labels[name] = given[item]?.trim() || name
|
|
81
|
+
}
|
|
82
|
+
const version = config.version?.trim() ?? ''
|
|
83
|
+
if (version !== '' && !POLICY_VERSION_RE.test(version))
|
|
84
|
+
throw new Error(`prunelle: consent version "${config.version}" must be 1 to 32 letters, digits, ".", "-" or "_", such as '2026-11' or '3'`)
|
|
85
|
+
const rawEndpoint = (config.endpoint ?? DEFAULT_CONSENT_ENDPOINT).trim()
|
|
86
|
+
const endpoint = rawEndpoint.startsWith('/') ? rawEndpoint : `/${rawEndpoint}`
|
|
87
|
+
return {
|
|
88
|
+
categories,
|
|
89
|
+
categorySet,
|
|
90
|
+
labels,
|
|
91
|
+
cookieName: config.cookieName ?? DEFAULT_CONSENT_COOKIE,
|
|
92
|
+
maxAge: config.maxAge ?? DEFAULT_CONSENT_MAX_AGE,
|
|
93
|
+
endpoint,
|
|
94
|
+
version,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface ParsedConsent { decided: boolean, decidedAt: Date | undefined, granted: Set<string> }
|
|
99
|
+
|
|
100
|
+
function grantedOf(list: string, declared: ReadonlySet<string>): Set<string> {
|
|
101
|
+
const granted = new Set<string>()
|
|
102
|
+
for (const part of list.split(',')) {
|
|
103
|
+
const cat = part.trim()
|
|
104
|
+
if (declared.has(cat))
|
|
105
|
+
granted.add(cat)
|
|
106
|
+
}
|
|
107
|
+
return granted
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Reads `v2:<seconds>:<policy version>:<categories>`; a choice made under another policy version counts as no choice. */
|
|
111
|
+
export function parseConsentCookie(raw: string | undefined, declared: ReadonlySet<string>, version = ''): ParsedConsent {
|
|
112
|
+
const none: ParsedConsent = { decided: false, decidedAt: undefined, granted: new Set() }
|
|
113
|
+
if (raw === undefined)
|
|
114
|
+
return none
|
|
115
|
+
if (raw.startsWith(LEGACY_COOKIE_PREFIX))
|
|
116
|
+
return version === '' ? { decided: true, decidedAt: undefined, granted: grantedOf(raw.slice(LEGACY_COOKIE_PREFIX.length), declared) } : none
|
|
117
|
+
if (!raw.startsWith(COOKIE_PREFIX))
|
|
118
|
+
return none
|
|
119
|
+
const [seconds, storedVersion, list] = raw.slice(COOKIE_PREFIX.length).split(':')
|
|
120
|
+
const at = Number(seconds)
|
|
121
|
+
if (list === undefined || storedVersion !== version || !Number.isSafeInteger(at) || at <= 0)
|
|
122
|
+
return none
|
|
123
|
+
return { decided: true, decidedAt: new Date(at * 1000), granted: grantedOf(list, declared) }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function serializeConsentCookie(granted: ReadonlySet<string>, declared: readonly string[], version = '', decidedAt = new Date()): string {
|
|
127
|
+
const ordered = declared.filter(cat => granted.has(cat))
|
|
128
|
+
return `${COOKIE_PREFIX}${Math.floor(decidedAt.getTime() / 1000)}:${version}:${ordered.join(',')}`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function createRequestConsent(rawCookie: string | undefined, config: NormalizedConsentConfig | undefined): RequestConsent {
|
|
132
|
+
if (config === undefined)
|
|
133
|
+
return EMPTY_CONSENT
|
|
134
|
+
const { decided, decidedAt, granted } = parseConsentCookie(rawCookie, config.categorySet, config.version)
|
|
135
|
+
return {
|
|
136
|
+
decided,
|
|
137
|
+
decidedAt,
|
|
138
|
+
categories: config.categories,
|
|
139
|
+
labels: config.labels,
|
|
140
|
+
endpoint: config.endpoint,
|
|
141
|
+
has(category: string): boolean {
|
|
142
|
+
return category === 'essential' || granted.has(category)
|
|
143
|
+
},
|
|
144
|
+
all(): Record<string, boolean> {
|
|
145
|
+
const out: Record<string, boolean> = {}
|
|
146
|
+
for (const cat of config.categories)
|
|
147
|
+
out[cat] = granted.has(cat)
|
|
148
|
+
return out
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function isTruthyFormValue(value: FormDataEntryValue | null): boolean {
|
|
154
|
+
if (typeof value !== 'string')
|
|
155
|
+
return false
|
|
156
|
+
const norm = value.trim().toLowerCase()
|
|
157
|
+
return norm !== '' && norm !== 'false' && norm !== '0' && norm !== 'off' && norm !== 'no'
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function resolveConsentRedirect(
|
|
161
|
+
request: Request,
|
|
162
|
+
url: URL,
|
|
163
|
+
formRedirect: string | undefined,
|
|
164
|
+
trustedOrigins: ReadonlySet<string> | undefined,
|
|
165
|
+
trustProxy: TrustProxy,
|
|
166
|
+
): string {
|
|
167
|
+
const fromForm = safeRedirect(formRedirect, '')
|
|
168
|
+
if (fromForm !== '')
|
|
169
|
+
return fromForm
|
|
170
|
+
const referer = request.headers.get('referer')
|
|
171
|
+
if (referer !== null) {
|
|
172
|
+
try {
|
|
173
|
+
const refUrl = new URL(referer)
|
|
174
|
+
if (isTrustedOrigin(refUrl.origin, request, trustedOrigins, trustProxy, url))
|
|
175
|
+
return safeRedirect(`${refUrl.pathname}${refUrl.search}`)
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return '/'
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return '/'
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function parseConsentSubmission(
|
|
185
|
+
request: Request,
|
|
186
|
+
config: NormalizedConsentConfig,
|
|
187
|
+
): Promise<{ ok: true, granted: Set<string>, redirect?: string | undefined } | { ok: false, error: string }> {
|
|
188
|
+
const ct = (request.headers.get('content-type') ?? '').toLowerCase()
|
|
189
|
+
const granted = new Set<string>()
|
|
190
|
+
|
|
191
|
+
if (ct.includes('application/json')) {
|
|
192
|
+
let body: unknown
|
|
193
|
+
try {
|
|
194
|
+
body = await request.json()
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return { ok: false, error: 'Invalid JSON body' }
|
|
198
|
+
}
|
|
199
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body))
|
|
200
|
+
return { ok: false, error: 'Consent payload must be a JSON object' }
|
|
201
|
+
const obj = body as Record<string, unknown>
|
|
202
|
+
if (obj.acceptAll === true || obj.action === 'accept-all') {
|
|
203
|
+
for (const cat of config.categories)
|
|
204
|
+
granted.add(cat)
|
|
205
|
+
return { ok: true, granted }
|
|
206
|
+
}
|
|
207
|
+
if (obj.rejectAll === true || obj.action === 'reject-all')
|
|
208
|
+
return { ok: true, granted }
|
|
209
|
+
if (Array.isArray(obj.categories)) {
|
|
210
|
+
for (const item of obj.categories) {
|
|
211
|
+
if (typeof item === 'string' && config.categorySet.has(item))
|
|
212
|
+
granted.add(item)
|
|
213
|
+
}
|
|
214
|
+
return { ok: true, granted }
|
|
215
|
+
}
|
|
216
|
+
if (obj.categories !== null && typeof obj.categories === 'object') {
|
|
217
|
+
for (const [cat, enabled] of Object.entries(obj.categories as Record<string, unknown>)) {
|
|
218
|
+
if (config.categorySet.has(cat) && enabled === true)
|
|
219
|
+
granted.add(cat)
|
|
220
|
+
}
|
|
221
|
+
return { ok: true, granted }
|
|
222
|
+
}
|
|
223
|
+
return { ok: true, granted }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let form: FormData
|
|
227
|
+
try {
|
|
228
|
+
form = await request.formData()
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return { ok: false, error: 'Invalid form submission' }
|
|
232
|
+
}
|
|
233
|
+
const rawRedirect = form.get('redirect')
|
|
234
|
+
const redirect = typeof rawRedirect === 'string' ? rawRedirect : undefined
|
|
235
|
+
const action = form.get('action')
|
|
236
|
+
if (action === 'accept-all') {
|
|
237
|
+
for (const cat of config.categories)
|
|
238
|
+
granted.add(cat)
|
|
239
|
+
return { ok: true, granted, redirect }
|
|
240
|
+
}
|
|
241
|
+
if (action === 'reject-all')
|
|
242
|
+
return { ok: true, granted, redirect }
|
|
243
|
+
|
|
244
|
+
const listed = new Set(form.getAll('categories').filter((v): v is string => typeof v === 'string'))
|
|
245
|
+
for (const cat of config.categories) {
|
|
246
|
+
if (listed.has(cat) || isTruthyFormValue(form.get(cat)))
|
|
247
|
+
granted.add(cat)
|
|
248
|
+
}
|
|
249
|
+
return { ok: true, granted, redirect }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export type ConsentRouteRegistrar = (
|
|
253
|
+
method: string,
|
|
254
|
+
path: string,
|
|
255
|
+
handler: (ctx: RequestContext) => Response | Promise<Response>,
|
|
256
|
+
options?: PluginRouteOptions,
|
|
257
|
+
) => void
|
|
258
|
+
|
|
259
|
+
/** Registers the automatic CSRF-protected `POST /_pru/consent` endpoint when `consent` is configured. */
|
|
260
|
+
export function registerConsentRoute(
|
|
261
|
+
register: ConsentRouteRegistrar,
|
|
262
|
+
config: NormalizedConsentConfig,
|
|
263
|
+
trustedOrigins: ReadonlySet<string> | undefined,
|
|
264
|
+
trustProxy: TrustProxy,
|
|
265
|
+
): void {
|
|
266
|
+
register('POST', config.endpoint, async (ctx) => {
|
|
267
|
+
const parsed = await parseConsentSubmission(ctx.request, config)
|
|
268
|
+
if (!parsed.ok)
|
|
269
|
+
return ctx.deny(400, parsed.error)
|
|
270
|
+
|
|
271
|
+
const cookieVal = serializeConsentCookie(parsed.granted, config.categories, config.version)
|
|
272
|
+
ctx.cookies.set(config.cookieName, cookieVal, {
|
|
273
|
+
httpOnly: true,
|
|
274
|
+
sameSite: 'lax',
|
|
275
|
+
path: '/',
|
|
276
|
+
maxAge: config.maxAge,
|
|
277
|
+
category: 'essential',
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
if (wantsJson(ctx.request)) {
|
|
281
|
+
const consent: Record<string, boolean> = {}
|
|
282
|
+
for (const cat of config.categories)
|
|
283
|
+
consent[cat] = parsed.granted.has(cat)
|
|
284
|
+
return ctx.json({ ok: true, decided: true, consent })
|
|
285
|
+
}
|
|
286
|
+
const target = resolveConsentRedirect(ctx.request, ctx.url, parsed.redirect, trustedOrigins, trustProxy)
|
|
287
|
+
// resolveConsentRedirect only returns this site or a trusted origin.
|
|
288
|
+
return ctx.redirect(target, 303, { external: true })
|
|
289
|
+
}, { csrf: true })
|
|
290
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Typed cookie jar for RequestContext (lazy Cookie header parsing, validated & deduplicated Set-Cookie serialization).
|
|
2
|
+
|
|
3
|
+
import { getAppContext } from '../plugins/context'
|
|
4
|
+
|
|
5
|
+
export interface CookieOptions {
|
|
6
|
+
path?: string | undefined
|
|
7
|
+
domain?: string | undefined
|
|
8
|
+
maxAge?: number | undefined
|
|
9
|
+
expires?: Date | undefined
|
|
10
|
+
httpOnly?: boolean | undefined
|
|
11
|
+
secure?: boolean | undefined
|
|
12
|
+
sameSite?: 'strict' | 'lax' | 'none' | undefined
|
|
13
|
+
/** Consent category required before setting this cookie, or `'essential'` for a cookie the site cannot work without; omitted, it counts as essential and `prunelle dev` warns. */
|
|
14
|
+
category?: string | undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const warnedUncategorized = new Set<string>()
|
|
18
|
+
|
|
19
|
+
// Once per cookie name and process, in dev only: production has no one to read it.
|
|
20
|
+
function warnUncategorized(name: string): void {
|
|
21
|
+
if (warnedUncategorized.has(name) || getAppContext()?.dev !== true)
|
|
22
|
+
return
|
|
23
|
+
warnedUncategorized.add(name)
|
|
24
|
+
console.warn(`prunelle: cookie "${name}" was set without a category, so it counts as essential and is set without consent: pass { category: 'essential' } when the site cannot work without it, or its consent category (such as 'analytics') so it waits for the visitor's consent.`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Cookies {
|
|
28
|
+
get(name: string): string | undefined
|
|
29
|
+
set(name: string, value: string, options?: CookieOptions): void
|
|
30
|
+
delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'secure'>): void
|
|
31
|
+
all(): Record<string, string>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const VALID_COOKIE_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/
|
|
35
|
+
const UNSAFE_ATTR_VAL = /[;\r\n\u0000-\u001F\u007F]/
|
|
36
|
+
|
|
37
|
+
function parseCookieHeader(header: string | null): Map<string, string> {
|
|
38
|
+
const map = new Map<string, string>()
|
|
39
|
+
if (!header)
|
|
40
|
+
return map
|
|
41
|
+
const parts = header.split(';')
|
|
42
|
+
for (let i = 0; i < parts.length; i++) {
|
|
43
|
+
const part = parts[i]!
|
|
44
|
+
const eq = part.indexOf('=')
|
|
45
|
+
if (eq <= 0)
|
|
46
|
+
continue
|
|
47
|
+
const name = part.slice(0, eq).trim()
|
|
48
|
+
// RFC 6265 §5.4: more specific path cookies come first, so first wins.
|
|
49
|
+
if (name === '' || map.has(name))
|
|
50
|
+
continue
|
|
51
|
+
const raw = part.slice(eq + 1).trim()
|
|
52
|
+
try {
|
|
53
|
+
map.set(name, decodeURIComponent(raw))
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
map.set(name, raw)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return map
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function serializeCookie(name: string, value: string, options: CookieOptions = {}, defaultSecure = false): string {
|
|
63
|
+
if (!VALID_COOKIE_NAME.test(name))
|
|
64
|
+
throw new TypeError(`prunelle: invalid cookie name "${name}"`)
|
|
65
|
+
const path = options.path ?? '/'
|
|
66
|
+
if (UNSAFE_ATTR_VAL.test(path))
|
|
67
|
+
throw new TypeError(`prunelle: invalid cookie path "${path}"`)
|
|
68
|
+
if (options.domain !== undefined && UNSAFE_ATTR_VAL.test(options.domain))
|
|
69
|
+
throw new TypeError(`prunelle: invalid cookie domain "${options.domain}"`)
|
|
70
|
+
|
|
71
|
+
const secure = options.secure ?? defaultSecure
|
|
72
|
+
const sameSite = options.sameSite ?? 'lax'
|
|
73
|
+
if (sameSite === 'none' && !secure)
|
|
74
|
+
throw new Error(`prunelle: Cookie "${name}" with sameSite: "none" requires secure: true`)
|
|
75
|
+
|
|
76
|
+
let out = `${name}=${encodeURIComponent(value)}`
|
|
77
|
+
out += `; Path=${path}`
|
|
78
|
+
if (options.domain !== undefined)
|
|
79
|
+
out += `; Domain=${options.domain}`
|
|
80
|
+
if (options.maxAge !== undefined) {
|
|
81
|
+
if (!Number.isFinite(options.maxAge))
|
|
82
|
+
throw new TypeError(`prunelle: invalid cookie maxAge "${options.maxAge}"`)
|
|
83
|
+
out += `; Max-Age=${Math.floor(options.maxAge)}`
|
|
84
|
+
}
|
|
85
|
+
if (options.expires !== undefined)
|
|
86
|
+
out += `; Expires=${options.expires.toUTCString()}`
|
|
87
|
+
if (options.httpOnly ?? true)
|
|
88
|
+
out += '; HttpOnly'
|
|
89
|
+
if (secure)
|
|
90
|
+
out += '; Secure'
|
|
91
|
+
out += `; SameSite=${sameSite[0]!.toUpperCase()}${sameSite.slice(1)}`
|
|
92
|
+
return out
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function cookieKey(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): string {
|
|
96
|
+
return `${name}\0${options?.path ?? '/'}\0${options?.domain ?? ''}`
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class CookieStore implements Cookies {
|
|
100
|
+
#rawHeader: string | null
|
|
101
|
+
#values: Map<string, string> | undefined
|
|
102
|
+
readonly #headers = new Map<string, string>()
|
|
103
|
+
readonly #isHttps: boolean
|
|
104
|
+
readonly #isConsentGranted: ((category: string) => boolean) | undefined
|
|
105
|
+
|
|
106
|
+
constructor(cookieHeader: string | null, isHttps = false, isConsentGranted?: (category: string) => boolean) {
|
|
107
|
+
this.#rawHeader = cookieHeader
|
|
108
|
+
this.#isHttps = isHttps
|
|
109
|
+
this.#isConsentGranted = isConsentGranted
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#ensureValues(): Map<string, string> {
|
|
113
|
+
if (this.#values === undefined) {
|
|
114
|
+
this.#values = parseCookieHeader(this.#rawHeader)
|
|
115
|
+
this.#rawHeader = null
|
|
116
|
+
}
|
|
117
|
+
return this.#values
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get(name: string): string | undefined {
|
|
121
|
+
return this.#ensureValues().get(name)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
set(name: string, value: string, options?: CookieOptions): void {
|
|
125
|
+
if (options?.category === undefined)
|
|
126
|
+
warnUncategorized(name)
|
|
127
|
+
else if (options.category !== 'essential') {
|
|
128
|
+
if (this.#isConsentGranted === undefined || !this.#isConsentGranted(options.category))
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
const serialized = serializeCookie(name, value, options, this.#isHttps)
|
|
132
|
+
this.#ensureValues().set(name, value)
|
|
133
|
+
this.#headers.set(cookieKey(name, options), serialized)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'secure'>): void {
|
|
137
|
+
this.#ensureValues().delete(name)
|
|
138
|
+
this.#headers.set(cookieKey(name, options), serializeCookie(name, '', { ...options, maxAge: 0, expires: new Date(0) }, this.#isHttps))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
all(): Record<string, string> {
|
|
142
|
+
return Object.fromEntries(this.#ensureValues())
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
get hasChanges(): boolean {
|
|
146
|
+
return this.#headers.size > 0
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
applyTo(headers: Headers): void {
|
|
150
|
+
for (const serialized of this.#headers.values())
|
|
151
|
+
headers.append('set-cookie', serialized)
|
|
152
|
+
}
|
|
153
|
+
}
|