prunelle 0.1.1 → 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/llms.txt +12 -12
- package/package.json +1 -1
- package/src/build/client-entries.ts +3 -2
- package/src/build/compile.ts +2 -1
- package/src/build/index.ts +68 -29
- package/src/build/manifest.ts +18 -1
- package/src/build/output.ts +2 -2
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +2 -2
- package/src/build/routes.ts +171 -10
- package/src/build/static.ts +71 -17
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +37 -4
- package/src/cli/commands.ts +5 -3
- package/src/cli/inspect.ts +21 -2
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +119 -220
- package/src/compiler/ssr.ts +121 -24
- package/src/dom/action.ts +4 -1
- package/src/dom/island.ts +5 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/types.ts +13 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action.ts +33 -10
- package/src/server/errors.ts +291 -62
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +84 -17
- package/src/server/index.ts +162 -91
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +40 -14
- package/src/server/public.ts +3 -3
- package/src/server/route-table.ts +5 -2
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +95 -57
- package/src/server/security.ts +171 -22
- package/src/server/serve-handlers.ts +21 -7
- package/src/ssr/context.ts +49 -0
- package/src/ssr/index.ts +120 -1
- package/src/test/browser.ts +6 -0
- package/src/test/index.ts +17 -3
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
package/src/plugins/types.ts
CHANGED
|
@@ -4,12 +4,15 @@ import type { BunPlugin } from 'bun'
|
|
|
4
4
|
import type { RouteManifestEntry } from '../build/index'
|
|
5
5
|
import type { ComplianceConfig } from '../compiler/compliance'
|
|
6
6
|
import type { ConsentConfig } from '../server/consent'
|
|
7
|
+
import type { HealthCheckConfig, HealthCheckFn, HealthCheckResult } from '../server/health'
|
|
7
8
|
import type { Middleware, RequestContext } from '../server/index'
|
|
8
9
|
import type { RateLimit, RateLimiter } from '../server/rate-limit'
|
|
9
10
|
import type { SecurityHeadersOptions, TrustProxy } from '../server/security'
|
|
10
11
|
import type { NavigationConfig } from '../ssr/navigation'
|
|
11
12
|
import type { ServiceKey } from './services'
|
|
12
13
|
|
|
14
|
+
export type { HealthCheckConfig, HealthCheckFn, HealthCheckResult }
|
|
15
|
+
|
|
13
16
|
export type BuildPluginTarget = 'client' | 'server' | 'compile'
|
|
14
17
|
|
|
15
18
|
export interface BuildPluginContext {
|
|
@@ -89,6 +92,11 @@ export interface PluginRouteOptions {
|
|
|
89
92
|
trustProxy?: TrustProxy | undefined
|
|
90
93
|
/** How often one client may call the route: past `max` per `windowMs`, it answers 429 with `Retry-After` before the handler runs. */
|
|
91
94
|
rateLimit?: RateLimit<RequestContext> | undefined
|
|
95
|
+
/**
|
|
96
|
+
* Let the route's catch-all take a segment with an encoded `/`, `\` or NUL (`%2F`, `%5C`, `%00`), which every route refuses by default.
|
|
97
|
+
* Such a segment reaches the param still encoded, never as a separator: for a route that reads it as text (a QR code of a URL), never one that joins it into a file path.
|
|
98
|
+
*/
|
|
99
|
+
encodedSlashes?: boolean | undefined
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
export interface MiddlewareOptions {
|
|
@@ -126,6 +134,8 @@ export interface ServerPluginAPI {
|
|
|
126
134
|
inject<T>(key: ServiceKey<T>): T | undefined
|
|
127
135
|
/** Read persisted build data from `manifest.pluginData` (defaults to the current plugin's name). */
|
|
128
136
|
getBuildData<T = Record<string, unknown>>(pluginName?: string): T | undefined
|
|
137
|
+
/** Register a named health check evaluated on `GET ${assetPrefix}/health`. */
|
|
138
|
+
healthCheck(name: string, fn: HealthCheckFn): void
|
|
129
139
|
/** Register a shutdown cleanup callback run when `app.dispose()` is called (dev reloads, tests, graceful exit). */
|
|
130
140
|
onShutdown(fn: () => void | Promise<void>): void
|
|
131
141
|
}
|
|
@@ -134,6 +144,7 @@ export interface ServerErrorInfo {
|
|
|
134
144
|
error: unknown
|
|
135
145
|
request: Request
|
|
136
146
|
source: 'route' | 'action' | 'plugin'
|
|
147
|
+
requestId?: string | undefined
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
export interface DocumentHeadContext {
|
|
@@ -215,6 +226,8 @@ export interface ServerRuntimeConfig {
|
|
|
215
226
|
/** CNIL/RGPD consent categories, cookie name, retention, and POST endpoint. */
|
|
216
227
|
consent?: ConsentConfig | undefined
|
|
217
228
|
compress?: boolean | undefined
|
|
229
|
+
/** Health check function or named check map evaluated on `GET ${assetPrefix}/health`. */
|
|
230
|
+
healthCheck?: HealthCheckConfig | undefined
|
|
218
231
|
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
219
232
|
plugins?: PrunellePlugin[] | undefined
|
|
220
233
|
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// Built-in spam & abuse protection for public server actions: honeypot field + HMAC-signed render timestamp.
|
|
2
|
+
|
|
3
|
+
import type { ActionContext } from './action'
|
|
4
|
+
import type { RateLimit } from './rate-limit'
|
|
5
|
+
import { assertRateLimit } from './rate-limit'
|
|
6
|
+
|
|
7
|
+
function constantTimeEqual(a: string, b: string): boolean {
|
|
8
|
+
const x = new Bun.CryptoHasher('sha256').update(a).digest()
|
|
9
|
+
const y = new Bun.CryptoHasher('sha256').update(b).digest()
|
|
10
|
+
let diff = 0
|
|
11
|
+
for (let i = 0; i < x.length; i++)
|
|
12
|
+
diff |= x[i]! ^ y[i]!
|
|
13
|
+
return diff === 0
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_HONEYPOT_FIELD = '_pru_hp'
|
|
17
|
+
export const FORM_TIMESTAMP_FIELD = '_pru_ts'
|
|
18
|
+
export const MAX_FORM_AGE_MS = 24 * 60 * 60 * 1000
|
|
19
|
+
|
|
20
|
+
export interface ActionProtectOptions {
|
|
21
|
+
/** Sliding-window rate limit for submissions to this action. */
|
|
22
|
+
rateLimit?: RateLimit<ActionContext> | undefined
|
|
23
|
+
/** Hidden trap input name (`true` uses `_pru_hp`, or pass a custom field name). Non-empty values are rejected. */
|
|
24
|
+
honeypot?: boolean | string | undefined
|
|
25
|
+
/** Minimum elapsed milliseconds between SSR render and submit; enforced with an HMAC-signed `_pru_ts` token. */
|
|
26
|
+
minTime?: number | undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const EPHEMERAL_SECRET_KEY = Symbol.for('pru.form.secret')
|
|
30
|
+
const DIGITS_RE = /^\d+$/
|
|
31
|
+
|
|
32
|
+
function formSecret(): string {
|
|
33
|
+
const configured = process.env.PRUNELLE_FORM_SECRET || process.env.AUTH_SECRET
|
|
34
|
+
if (configured)
|
|
35
|
+
return configured
|
|
36
|
+
const g = globalThis as Record<symbol, string | undefined>
|
|
37
|
+
let secret = g[EPHEMERAL_SECRET_KEY]
|
|
38
|
+
if (secret === undefined) {
|
|
39
|
+
const bytes = new Uint8Array(32)
|
|
40
|
+
crypto.getRandomValues(bytes)
|
|
41
|
+
secret = Buffer.from(bytes).toString('hex')
|
|
42
|
+
g[EPHEMERAL_SECRET_KEY] = secret
|
|
43
|
+
}
|
|
44
|
+
return secret
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function escapeAttrValue(value: string): string {
|
|
48
|
+
return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Returns the configured honeypot field name, or `undefined` when honeypot is disabled. */
|
|
52
|
+
export function honeypotFieldName(protect: ActionProtectOptions | undefined): string | undefined {
|
|
53
|
+
if (protect?.honeypot === true)
|
|
54
|
+
return DEFAULT_HONEYPOT_FIELD
|
|
55
|
+
if (typeof protect?.honeypot === 'string') {
|
|
56
|
+
const trimmed = protect.honeypot.trim()
|
|
57
|
+
return trimmed !== '' ? trimmed : undefined
|
|
58
|
+
}
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Whether `protect` requires reading the request body (honeypot or minTime). */
|
|
63
|
+
export function needsProtectBody(protect: ActionProtectOptions | undefined): boolean {
|
|
64
|
+
return honeypotFieldName(protect) !== undefined || protect?.minTime !== undefined
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Validates `ActionProtectOptions` eagerly when `action()` is defined. */
|
|
68
|
+
export function assertActionProtect(protect: ActionProtectOptions): void {
|
|
69
|
+
if (protect.rateLimit !== undefined)
|
|
70
|
+
assertRateLimit(protect.rateLimit, 'an action')
|
|
71
|
+
if (protect.minTime !== undefined && (!Number.isFinite(protect.minTime) || protect.minTime < 0))
|
|
72
|
+
throw new Error(`prunelle: protect.minTime of an action is ${String(protect.minTime)}; give a non-negative number of milliseconds.`)
|
|
73
|
+
if (typeof protect.honeypot === 'string' && protect.honeypot.trim() === '')
|
|
74
|
+
throw new Error('prunelle: protect.honeypot of an action must be a non-empty field name.')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Creates an HMAC-SHA256 signed timestamp token (`<timestampMs>.<signatureHex>`). */
|
|
78
|
+
export function signFormTimestamp(timestampMs = Date.now()): string {
|
|
79
|
+
const ts = String(Math.trunc(timestampMs))
|
|
80
|
+
const sig = new Bun.CryptoHasher('sha256', formSecret()).update(ts).digest('hex')
|
|
81
|
+
return `${ts}.${sig}`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Verifies an HMAC-signed `_pru_ts` token against `minTimeMs` and the 24-hour expiry window. */
|
|
85
|
+
export function verifyFormTimestamp(
|
|
86
|
+
token: unknown,
|
|
87
|
+
minTimeMs: number,
|
|
88
|
+
now = Date.now(),
|
|
89
|
+
): { ok: true } | { ok: false, reason: string } {
|
|
90
|
+
if (typeof token !== 'string')
|
|
91
|
+
return { ok: false, reason: 'Missing form timestamp' }
|
|
92
|
+
const dot = token.indexOf('.')
|
|
93
|
+
if (dot <= 0 || dot === token.length - 1)
|
|
94
|
+
return { ok: false, reason: 'Invalid form timestamp' }
|
|
95
|
+
const tsStr = token.slice(0, dot)
|
|
96
|
+
const sig = token.slice(dot + 1)
|
|
97
|
+
if (!DIGITS_RE.test(tsStr))
|
|
98
|
+
return { ok: false, reason: 'Invalid form timestamp' }
|
|
99
|
+
const expected = new Bun.CryptoHasher('sha256', formSecret()).update(tsStr).digest('hex')
|
|
100
|
+
if (!constantTimeEqual(sig, expected))
|
|
101
|
+
return { ok: false, reason: 'Invalid form timestamp' }
|
|
102
|
+
const ts = Number(tsStr)
|
|
103
|
+
if (!Number.isFinite(ts))
|
|
104
|
+
return { ok: false, reason: 'Invalid form timestamp' }
|
|
105
|
+
const elapsed = now - ts
|
|
106
|
+
if (elapsed < minTimeMs)
|
|
107
|
+
return { ok: false, reason: 'Form submitted too quickly' }
|
|
108
|
+
if (elapsed > MAX_FORM_AGE_MS)
|
|
109
|
+
return { ok: false, reason: 'Form submission expired' }
|
|
110
|
+
return { ok: true }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function isNonEmptyFieldValue(value: unknown): boolean {
|
|
114
|
+
if (Array.isArray(value))
|
|
115
|
+
return value.some(v => v !== '' && v != null)
|
|
116
|
+
return value !== '' && value != null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Returns a shallow copy of `rawBody` with internal Prunelle protection fields removed. */
|
|
120
|
+
export function stripProtectFields(protect: ActionProtectOptions | undefined, rawBody: unknown): unknown {
|
|
121
|
+
if (protect === undefined || rawBody === null || typeof rawBody !== 'object' || Array.isArray(rawBody))
|
|
122
|
+
return rawBody
|
|
123
|
+
const hpField = honeypotFieldName(protect)
|
|
124
|
+
const hasMinTime = protect.minTime !== undefined
|
|
125
|
+
if (hpField === undefined && !hasMinTime)
|
|
126
|
+
return rawBody
|
|
127
|
+
const obj = rawBody as Record<string, unknown>
|
|
128
|
+
const dropHp = hpField !== undefined && Object.hasOwn(obj, hpField)
|
|
129
|
+
const dropTs = hasMinTime && Object.hasOwn(obj, FORM_TIMESTAMP_FIELD)
|
|
130
|
+
if (!dropHp && !dropTs)
|
|
131
|
+
return rawBody
|
|
132
|
+
const entries: [string, unknown][] = []
|
|
133
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
134
|
+
if ((dropHp && k === hpField) || (dropTs && k === FORM_TIMESTAMP_FIELD))
|
|
135
|
+
continue
|
|
136
|
+
entries.push([k, v])
|
|
137
|
+
}
|
|
138
|
+
return Object.fromEntries(entries)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Verifies honeypot and signed timestamp protections and returns the body with internal fields stripped. */
|
|
142
|
+
export function verifyActionProtection(
|
|
143
|
+
protect: ActionProtectOptions | undefined,
|
|
144
|
+
rawBody: unknown,
|
|
145
|
+
now = Date.now(),
|
|
146
|
+
): { ok: true, cleanedBody: unknown } | { ok: false, error: string, cleanedBody: unknown } {
|
|
147
|
+
if (protect === undefined)
|
|
148
|
+
return { ok: true, cleanedBody: rawBody }
|
|
149
|
+
const hpField = honeypotFieldName(protect)
|
|
150
|
+
const minTime = protect.minTime
|
|
151
|
+
if (hpField === undefined && minTime === undefined)
|
|
152
|
+
return { ok: true, cleanedBody: rawBody }
|
|
153
|
+
|
|
154
|
+
const cleanedBody = stripProtectFields(protect, rawBody)
|
|
155
|
+
const obj = rawBody !== null && typeof rawBody === 'object' && !Array.isArray(rawBody)
|
|
156
|
+
? rawBody as Record<string, unknown>
|
|
157
|
+
: undefined
|
|
158
|
+
|
|
159
|
+
if (hpField !== undefined && obj !== undefined && Object.hasOwn(obj, hpField)) {
|
|
160
|
+
if (isNonEmptyFieldValue(obj[hpField]))
|
|
161
|
+
return { ok: false, error: 'Submission rejected', cleanedBody }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (minTime !== undefined) {
|
|
165
|
+
const rawTs = obj !== undefined && Object.hasOwn(obj, FORM_TIMESTAMP_FIELD) ? obj[FORM_TIMESTAMP_FIELD] : undefined
|
|
166
|
+
const token = Array.isArray(rawTs) ? rawTs[0] : rawTs
|
|
167
|
+
const tsResult = verifyFormTimestamp(token, minTime, now)
|
|
168
|
+
if (!tsResult.ok)
|
|
169
|
+
return { ok: false, error: tsResult.reason, cleanedBody }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { ok: true, cleanedBody }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Renders the SSR hidden protection inputs (visually hidden honeypot + signed timestamp) for `<form action={fn}>`. */
|
|
176
|
+
export function renderProtectFields(protect: ActionProtectOptions | undefined): string {
|
|
177
|
+
if (protect === undefined)
|
|
178
|
+
return ''
|
|
179
|
+
let html = ''
|
|
180
|
+
const hp = honeypotFieldName(protect)
|
|
181
|
+
if (hp !== undefined)
|
|
182
|
+
html += `<input type="text" name="${escapeAttrValue(hp)}" value="" tabindex="-1" autocomplete="off" aria-hidden="true" style="position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden">`
|
|
183
|
+
if (protect.minTime !== undefined)
|
|
184
|
+
html += `<input type="hidden" name="${FORM_TIMESTAMP_FIELD}" value="${signFormTimestamp()}">`
|
|
185
|
+
return html
|
|
186
|
+
}
|
package/src/server/action.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
// Unified server actions: works as both progressive <form action={fn} method="post"> and typed island RPC.
|
|
2
2
|
|
|
3
|
+
import type { ActionProtectOptions } from './action-protect'
|
|
3
4
|
import type { Cookies } from './cookies'
|
|
4
5
|
import type { PrunelleLocals } from './index'
|
|
5
6
|
import type { RateLimit, RateLimiter } from './rate-limit'
|
|
6
7
|
import type { TrustProxy } from './security'
|
|
7
8
|
import type { InferInput, InferOutput, StandardSchemaIssue, StandardSchemaV1, ValidationFailure, ValidationResult } from './standard-schema'
|
|
9
|
+
import { assertActionProtect, needsProtectBody, verifyActionProtection } from './action-protect'
|
|
8
10
|
import { wantsJson } from './http'
|
|
9
11
|
import { assertRateLimit, hitFor, tooManyRequests } from './rate-limit'
|
|
10
12
|
import { currentRequest } from './request-scope'
|
|
11
13
|
import { isTrustedOrigin } from './security'
|
|
12
14
|
import { validateSchema } from './standard-schema'
|
|
13
15
|
|
|
16
|
+
export type { ActionProtectOptions } from './action-protect'
|
|
17
|
+
|
|
14
18
|
declare module './index' {
|
|
15
19
|
interface PrunelleLocals {
|
|
16
20
|
actionError?: ActionErrorFlash | undefined
|
|
@@ -69,6 +73,8 @@ export interface ActionOptions {
|
|
|
69
73
|
flashFields?: readonly string[] | undefined
|
|
70
74
|
/** How often one client may submit it: past `max` per `windowMs`, a call from script gets a 429 with `Retry-After` and a form comes back with the error. */
|
|
71
75
|
rateLimit?: RateLimit<ActionContext> | undefined
|
|
76
|
+
/** Public form spam & abuse protection: rate limit, hidden honeypot field, and minimum render-to-submit time. */
|
|
77
|
+
protect?: ActionProtectOptions | undefined
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
export interface ActionErrorFlash {
|
|
@@ -103,6 +109,7 @@ export interface ServerAction<Input, Output> {
|
|
|
103
109
|
readonly schema: StandardSchemaV1 | undefined
|
|
104
110
|
readonly flashFields: readonly string[] | undefined
|
|
105
111
|
readonly rateLimit: RateLimit<ActionContext> | undefined
|
|
112
|
+
readonly protect: ActionProtectOptions | undefined
|
|
106
113
|
readonly handler: (input: never, ctx: ActionContext) => Output | Response | Promise<Output | Response>
|
|
107
114
|
toString(): string
|
|
108
115
|
}
|
|
@@ -132,8 +139,11 @@ export function action(
|
|
|
132
139
|
? (maybeHandlerOrOpts as (input: never, ctx: ActionContext) => unknown)
|
|
133
140
|
: ((_input: never, ctx: ActionContext) => (schemaOrHandler as (c: ActionContext) => unknown)(ctx))
|
|
134
141
|
const options = hasSchema ? maybeOpts : (maybeHandlerOrOpts as ActionOptions | undefined)
|
|
135
|
-
if (options?.
|
|
136
|
-
|
|
142
|
+
if (options?.protect !== undefined)
|
|
143
|
+
assertActionProtect(options.protect)
|
|
144
|
+
const effectiveRateLimit = options?.protect?.rateLimit ?? options?.rateLimit
|
|
145
|
+
if (effectiveRateLimit !== undefined && options?.protect?.rateLimit === undefined)
|
|
146
|
+
assertRateLimit(effectiveRateLimit, 'an action')
|
|
137
147
|
|
|
138
148
|
const fn = (async (input: unknown) => {
|
|
139
149
|
const scope = currentRequest()
|
|
@@ -148,7 +158,8 @@ export function action(
|
|
|
148
158
|
__pruAction: { value: true, enumerable: false },
|
|
149
159
|
schema: { value: schema, enumerable: false },
|
|
150
160
|
flashFields: { value: options?.flashFields, enumerable: false },
|
|
151
|
-
rateLimit: { value:
|
|
161
|
+
rateLimit: { value: effectiveRateLimit, enumerable: false },
|
|
162
|
+
protect: { value: options?.protect, enumerable: false },
|
|
152
163
|
handler: { value: handler, enumerable: false },
|
|
153
164
|
url: { value: '', writable: true, enumerable: true },
|
|
154
165
|
toString: {
|
|
@@ -409,20 +420,32 @@ export async function executeAction(
|
|
|
409
420
|
|
|
410
421
|
let rawBody: unknown
|
|
411
422
|
let input: unknown
|
|
412
|
-
|
|
423
|
+
const wantsBody = act.schema !== undefined || needsProtectBody(act.protect)
|
|
424
|
+
if (wantsBody) {
|
|
413
425
|
try {
|
|
414
426
|
rawBody = await parseActionBody(request)
|
|
415
427
|
}
|
|
416
428
|
catch {
|
|
417
429
|
return Response.json({ ok: false, status: 400, error: 'Invalid request body' }, { status: 400 })
|
|
418
430
|
}
|
|
431
|
+
}
|
|
419
432
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
433
|
+
const protectedBody = verifyActionProtection(act.protect, rawBody)
|
|
434
|
+
const cleanBody = protectedBody.cleanedBody
|
|
435
|
+
if (!protectedBody.ok) {
|
|
436
|
+
const redirected = flashAndRedirect(protectedBody.error, undefined, cleanBody)
|
|
437
|
+
if (redirected !== undefined)
|
|
438
|
+
return redirected
|
|
439
|
+
return Response.json({ ok: false, status: 400, error: protectedBody.error }, { status: 400 })
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (act.schema !== undefined) {
|
|
443
|
+
let checked = await validateSchema(act.schema, cleanBody)
|
|
444
|
+
if (!checked.ok && !isJsonBody(request) && cleanBody !== null && typeof cleanBody === 'object' && !Array.isArray(cleanBody))
|
|
445
|
+
checked = await retryWithFormArrays(act.schema, cleanBody as Record<string, unknown>, checked)
|
|
423
446
|
if (!checked.ok) {
|
|
424
447
|
// Zero-JS progressive HTML <form> submit: flash fieldErrors + safe submitted values (<4KB) and redirect 303 back to trusted Referer
|
|
425
|
-
const redirected = flashAndRedirect('Validation failed', checked.fieldErrors,
|
|
448
|
+
const redirected = flashAndRedirect('Validation failed', checked.fieldErrors, cleanBody)
|
|
426
449
|
if (redirected !== undefined)
|
|
427
450
|
return redirected
|
|
428
451
|
return Response.json(
|
|
@@ -436,7 +459,7 @@ export async function executeAction(
|
|
|
436
459
|
// Counted once the body is read, so a refused form comes back with what was typed in it.
|
|
437
460
|
const retryAfter = limiter !== undefined && act.rateLimit !== undefined ? hitFor(limiter, act.rateLimit, ctx) : 0
|
|
438
461
|
if (retryAfter > 0) {
|
|
439
|
-
const redirected = flashAndRedirect('Too many requests', undefined,
|
|
462
|
+
const redirected = flashAndRedirect('Too many requests', undefined, cleanBody)
|
|
440
463
|
if (redirected === undefined)
|
|
441
464
|
return tooManyRequests(retryAfter)
|
|
442
465
|
redirected.headers.set('retry-after', String(retryAfter))
|
|
@@ -460,7 +483,7 @@ export async function executeAction(
|
|
|
460
483
|
if (result instanceof Response) {
|
|
461
484
|
const denied = deniedResponses?.get(result)
|
|
462
485
|
if (denied !== undefined) {
|
|
463
|
-
const redirected = flashAndRedirect(denied.message, denied.fieldErrors,
|
|
486
|
+
const redirected = flashAndRedirect(denied.message, denied.fieldErrors, cleanBody)
|
|
464
487
|
if (redirected !== undefined)
|
|
465
488
|
return redirected
|
|
466
489
|
}
|