forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
198 lines (178 loc) · 6.68 KB
/
Copy pathhandler.ts
File metadata and controls
198 lines (178 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { DbClient } from '../db/client'
import type { Static, TSchema } from '@sinclair/typebox'
import { clientIp, originAllowed } from '../auth/security'
import {
RequestBodyTooLargeError,
badRequest,
jsonResponse,
methodNotAllowed,
payloadTooLarge,
readValidatedBody,
} from '../http'
import { createDataRow, getDataTable } from '../repositories/data'
import { getLatestPublishedSiteSnapshot } from '../repositories/publish'
import {
PublicFormChallengeBodySchema,
PublicFormSubmitBodySchema,
derivePageFormSnapshots,
isFormSubmissionTargetTable,
validateFormSubmission,
type PublishedFormSnapshot,
} from '@core/forms'
import {
issuePublicFormChallenge,
verifyPublicFormPageToken,
verifyAndConsumePublicFormChallenge,
} from './challenge'
import {
publicFormChallengePerFormRateLimit,
publicFormChallengePerIpRateLimit,
publicFormPerFormRateLimit,
publicFormPerIpRateLimit,
} from './rateLimit'
type PublicFormRoute = 'challenge' | 'submit'
const PUBLIC_FORM_CHALLENGE_MAX_BODY_BYTES = 8 * 1024
const PUBLIC_FORM_SUBMIT_MAX_BODY_BYTES = 1024 * 1024
export async function handlePublicFormRequest(
req: Request,
db: DbClient,
url: URL,
): Promise<Response | null> {
const route = publicFormRoute(url.pathname)
if (!route) return null
if (req.method !== 'POST') return methodNotAllowed()
if (!publicFormOriginAllowed(req)) {
return jsonResponse({ error: 'Form submissions must come from this site.' }, { status: 403 })
}
if (route === 'challenge') return handleChallenge(req, db)
return handleSubmit(req, db)
}
async function handleChallenge(req: Request, db: DbClient): Promise<Response> {
const parsed = await readPublicFormBody(
req,
PublicFormChallengeBodySchema,
PUBLIC_FORM_CHALLENGE_MAX_BODY_BYTES,
'Invalid form challenge payload',
)
if (parsed instanceof Response) return parsed
const body = parsed
const ipKey = clientIp(req) ?? 'unknown'
const ipDecision = publicFormChallengePerIpRateLimit.consume(ipKey)
if (!ipDecision.ok) return rateLimited(ipDecision.retryAfterMs)
const formDecision = publicFormChallengePerFormRateLimit.consume(`${ipKey}|${body.formId}`)
if (!formDecision.ok) return rateLimited(formDecision.retryAfterMs)
const snapshot = await findPublishedFormSnapshot(db, body.pageId, body.formId)
if (!snapshot) return jsonResponse({ error: 'Form not found' }, { status: 404 })
if (!verifyPublicFormPageToken(body)) {
return jsonResponse({ error: 'Invalid form page token' }, { status: 403 })
}
const challenge = issuePublicFormChallenge({ pageId: snapshot.pageId, formId: snapshot.formId })
return jsonResponse({
token: challenge.token,
challenge: challenge.challenge,
expiresAt: new Date(challenge.expiresAt).toISOString(),
})
}
async function handleSubmit(req: Request, db: DbClient): Promise<Response> {
const parsed = await readPublicFormBody(
req,
PublicFormSubmitBodySchema,
PUBLIC_FORM_SUBMIT_MAX_BODY_BYTES,
'Invalid form submission payload',
)
if (parsed instanceof Response) return parsed
const body = parsed
const ipKey = clientIp(req) ?? 'unknown'
const ipDecision = publicFormPerIpRateLimit.consume(ipKey)
if (!ipDecision.ok) return rateLimited(ipDecision.retryAfterMs)
const formDecision = publicFormPerFormRateLimit.consume(`${ipKey}|${body.formId}`)
if (!formDecision.ok) return rateLimited(formDecision.retryAfterMs)
const challenge = verifyAndConsumePublicFormChallenge({
pageId: body.pageId,
formId: body.formId,
challenge: body.challenge,
token: body.token,
})
if (!challenge) return badRequest('Invalid or expired form challenge')
const snapshot = await findPublishedFormSnapshot(db, body.pageId, body.formId)
if (!snapshot) return jsonResponse({ error: 'Form not found' }, { status: 404 })
const elapsedMs = Date.now() - challenge.issuedAt
if (snapshot.minSubmitSeconds > 0 && elapsedMs < snapshot.minSubmitSeconds * 1000) {
return badRequest('Form submitted too quickly')
}
const values = { ...body.values }
const honeypotValue = values[snapshot.honeypotName]
delete values[snapshot.honeypotName]
if (honeypotValue !== undefined && String(honeypotValue).trim() !== '') {
return badRequest('Invalid form submission')
}
const table = await getDataTable(db, snapshot.targetTableId)
if (!table || !isFormSubmissionTargetTable(table)) {
return jsonResponse({ error: 'Form target not found' }, { status: 404 })
}
const validation = validateFormSubmission({
table,
controls: snapshot.controls,
values,
})
if (!validation.ok) {
return jsonResponse({ error: 'Invalid form values', errors: validation.errors }, { status: 400 })
}
const row = await createDataRow(db, {
tableId: table.id,
cells: validation.cells,
slug: '',
})
return jsonResponse({ ok: true, rowId: row.id })
}
async function readPublicFormBody<T extends TSchema>(
req: Request,
schema: T,
maxBytes: number,
invalidMessage: string,
): Promise<Static<T> | Response> {
try {
const body = await readValidatedBody(req, schema, { maxBytes })
return body ?? badRequest(invalidMessage)
} catch (err) {
if (err instanceof RequestBodyTooLargeError) {
return payloadTooLarge('Form payload is too large.')
}
throw err
}
}
async function findPublishedFormSnapshot(
db: DbClient,
pageId: string,
formId: string,
): Promise<PublishedFormSnapshot | null> {
const snapshot = await getLatestPublishedSiteSnapshot(db)
const page = snapshot?.site.pages.find((candidate) => candidate.id === pageId)
if (!page) return null
return derivePageFormSnapshots(page).find((candidate) => candidate.formId === formId) ?? null
}
function publicFormRoute(pathname: string): PublicFormRoute | null {
if (pathname === '/_instatic/form/challenge') return 'challenge'
if (pathname === '/_instatic/form/submit') return 'submit'
if (pathname.startsWith('/_instatic/form/')) return 'submit'
return null
}
function publicFormOriginAllowed(req: Request): boolean {
// Public form posts always come from a browser, so a missing Origin is
// rejected here (stricter than the admin check, which tolerates curl/SSR).
if (!req.headers.get('origin')) return false
// Same CSRF origin check as the admin/AI handlers — honours the full
// configured public-origin allowlist (platform + custom domain).
if (!originAllowed(req)) return false
const fetchSite = req.headers.get('sec-fetch-site')
return !fetchSite || fetchSite === 'same-origin' || fetchSite === 'none'
}
function rateLimited(retryAfterMs: number): Response {
return jsonResponse(
{ error: 'Too many form submissions. Try again later.' },
{
status: 429,
headers: { 'Retry-After': String(Math.ceil(retryAfterMs / 1000)) },
},
)
}