Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+152
-56
@@ -30,19 +30,22 @@ import { createHash } from "node:crypto";
|
||||
import {
|
||||
FormSubmissionPayloadSchema,
|
||||
FormDefinitionSchema,
|
||||
filterVisibleFields,
|
||||
isHoneypotTriggered,
|
||||
matchAllowedOrigin,
|
||||
resolveSubmissionTarget,
|
||||
type FormDefinition,
|
||||
} from "@toolctrl/form-widget";
|
||||
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@toolctrl/db/outbox";
|
||||
import { createSlidingWindowRateLimiter } from "@toolctrl/shared-store";
|
||||
} from "@hivegrid/form-widget";
|
||||
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@hivegrid/db/outbox";
|
||||
import { createSlidingWindowRateLimiter } from "@hivegrid/shared-store";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { prepareUploadedFiles, type PreparedFormFile } from "~/modules/formWidget/services/embedFormSubmission.server";
|
||||
import { getSharedStore } from "~/shared-store.server";
|
||||
import { createScopedLogger } from "~/utils/logger.server";
|
||||
import { evaluateFlagForContext } from "~/utils/services/.server/featureFlagEvaluator.server";
|
||||
import { resolveCaptchaConfig, verifyCaptchaToken } from "@toolctrl/captcha";
|
||||
import { getSecret, resolveCaptchaConfig, verifyCaptchaToken } from "@hivegrid/captcha";
|
||||
import { getTrustedClientIp } from "~/utils/server/IpUtils";
|
||||
|
||||
const log = createScopedLogger("form-widget.submit");
|
||||
|
||||
@@ -56,7 +59,8 @@ const RATE_LIMIT_PER_ORIGIN = 100;
|
||||
function shouldFailClosedOnRateLimitInfraFailure(): boolean {
|
||||
return process.env.NODE_ENV === "production";
|
||||
}
|
||||
const PAYLOAD_BYTE_LIMIT = 256 * 1024; // 256 KiB hard cap on the form-encoded body
|
||||
const PAYLOAD_BYTE_LIMIT = 256 * 1024; // 256 KiB hard cap on the JSON payload
|
||||
const REQUEST_BYTE_LIMIT = 30 * 1024 * 1024;
|
||||
|
||||
type EmbedSubmitErrorCode =
|
||||
| "METHOD_NOT_ALLOWED"
|
||||
@@ -138,7 +142,7 @@ function errorResponse(
|
||||
code: EmbedSubmitErrorCode,
|
||||
status: number,
|
||||
params?: Record<string, string | number | boolean>,
|
||||
init?: ResponseInit,
|
||||
init?: ResponseInit
|
||||
): Response {
|
||||
const errorKey = ERROR_KEYS[code];
|
||||
const payload: EmbedSubmitError = {
|
||||
@@ -156,31 +160,59 @@ function hashIp(ip: string | null): string | null {
|
||||
return createHash("sha256").update(ip).digest("hex").slice(0, 32);
|
||||
}
|
||||
|
||||
function clientIp(request: Request): string | null {
|
||||
const fwd = request.headers.get("x-forwarded-for");
|
||||
if (fwd) return fwd.split(",")[0]?.trim() || null;
|
||||
return request.headers.get("x-real-ip");
|
||||
function isUniqueConstraintError(error: unknown): boolean {
|
||||
return typeof error === "object" && error !== null && "code" in error && (error as { code?: unknown }).code === "P2002";
|
||||
}
|
||||
|
||||
function validateAgainstDefinition(values: Record<string, unknown>, def: FormDefinition): { code: EmbedSubmitErrorCode; params: Record<string, string | number | boolean> } | null {
|
||||
function clientIp(request: Request): string | null {
|
||||
// Use a trusted, proxy-set header only — the leftmost X-Forwarded-For value
|
||||
// is client-controlled and would let an attacker reset the per-IP bucket.
|
||||
const ip = getTrustedClientIp(request);
|
||||
return ip === "unknown" ? null : ip;
|
||||
}
|
||||
|
||||
function validateAgainstDefinition(
|
||||
values: Record<string, unknown>,
|
||||
def: FormDefinition
|
||||
): { code: EmbedSubmitErrorCode; params: Record<string, string | number | boolean> } | null {
|
||||
for (const field of def.fields) {
|
||||
const raw = values[field.key];
|
||||
if (field.type === "honeypot") continue;
|
||||
const params = { fieldKey: field.key, fieldLabel: field.label };
|
||||
const filled = raw !== undefined && raw !== null && raw !== "" && raw !== false;
|
||||
const filled = Array.isArray(raw) ? raw.length > 0 : raw !== undefined && raw !== null && raw !== "" && raw !== false;
|
||||
if (field.required && !filled) {
|
||||
return { code: "FIELD_REQUIRED", params };
|
||||
}
|
||||
if (!filled) continue;
|
||||
if (typeof raw === "string") {
|
||||
if (field.maxLength !== undefined && raw.length > field.maxLength) {
|
||||
return { code: "FIELD_TOO_LONG", params: { ...params, maxLength: field.maxLength } };
|
||||
const maxLength = field.validationRules?.maxLength ?? field.maxLength;
|
||||
const minLength = field.validationRules?.minLength ?? field.minLength;
|
||||
if (maxLength !== undefined && raw.length > maxLength) {
|
||||
return { code: "FIELD_TOO_LONG", params: { ...params, maxLength } };
|
||||
}
|
||||
if (field.minLength !== undefined && raw.length < field.minLength && filled) {
|
||||
return { code: "FIELD_TOO_SHORT", params: { ...params, minLength: field.minLength } };
|
||||
if (minLength !== undefined && raw.length < minLength) {
|
||||
return { code: "FIELD_TOO_SHORT", params: { ...params, minLength } };
|
||||
}
|
||||
if (field.type === "email" && filled && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(raw)) {
|
||||
return { code: "FIELD_INVALID_EMAIL", params };
|
||||
}
|
||||
if (field.type === "phone" && !/^[+()\d\s./-]{3,40}$/u.test(raw)) {
|
||||
return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
}
|
||||
if (field.type === "number") {
|
||||
const numberValue = typeof raw === "number" ? raw : typeof raw === "string" ? Number(raw) : Number.NaN;
|
||||
if (!Number.isFinite(numberValue)) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.min !== undefined && numberValue < field.validationRules.min) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.max !== undefined && numberValue > field.validationRules.max) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
if (field.type === "multiselect") {
|
||||
if (!Array.isArray(raw)) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
if (field.validationRules?.minSelected !== undefined && raw.length < field.validationRules.minSelected) return { code: "FIELD_REQUIRED", params };
|
||||
if (field.validationRules?.maxSelected !== undefined && raw.length > field.validationRules.maxSelected) return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
if (field.type === "boolean" && typeof raw !== "boolean") {
|
||||
return { code: "INVALID_PAYLOAD_SHAPE", params };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -195,7 +227,7 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
if (!formKey) return errorResponse(t, "FORM_NOT_FOUND", 404);
|
||||
|
||||
const contentLength = Number(request.headers.get("content-length") ?? "0");
|
||||
if (Number.isFinite(contentLength) && contentLength > PAYLOAD_BYTE_LIMIT) {
|
||||
if (Number.isFinite(contentLength) && contentLength > REQUEST_BYTE_LIMIT) {
|
||||
return errorResponse(t, "PAYLOAD_TOO_LARGE", 413);
|
||||
}
|
||||
|
||||
@@ -228,8 +260,9 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
|
||||
// 1. Parse body
|
||||
let payloadJson: unknown;
|
||||
let formBody: FormData;
|
||||
try {
|
||||
const formBody = await request.formData();
|
||||
formBody = await request.formData();
|
||||
const raw = formBody.get("__payload");
|
||||
if (typeof raw !== "string") throw new Error("payload missing");
|
||||
if (raw.length > PAYLOAD_BYTE_LIMIT) throw new Error("payload too large");
|
||||
@@ -242,7 +275,7 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
if (!parsed.success) {
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
const { values, turnstileToken } = parsed.data;
|
||||
const { values, turnstileToken, captchaToken, gdprAccepted, idempotencyKey } = parsed.data;
|
||||
|
||||
// 3. Honeypot — silent 204 to starve bot signal
|
||||
if (isHoneypotTriggered(values)) {
|
||||
@@ -294,25 +327,33 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
log.error({ op: "form.misconfigured", formId: form.id }, "Stored definition failed schema");
|
||||
return errorResponse(t, "FORM_MISCONFIGURED", 500);
|
||||
}
|
||||
const valueError = validateAgainstDefinition(values, def.data);
|
||||
if (gdprAccepted !== true) {
|
||||
return errorResponse(t, "FIELD_REQUIRED", 400, { fieldKey: "__gdprAccepted", fieldLabel: def.data.gdprConsentLabel });
|
||||
}
|
||||
const visibleFields = filterVisibleFields(def.data.fields, values);
|
||||
const visibleDef: FormDefinition = { ...def.data, fields: visibleFields };
|
||||
const valueError = validateAgainstDefinition(values, visibleDef);
|
||||
if (valueError) {
|
||||
return errorResponse(t, valueError.code, 400, valueError.params);
|
||||
}
|
||||
|
||||
// 6. Optional captcha gate (mCaptcha)
|
||||
// 6. Optional captcha gate (self-hosted provider)
|
||||
const flag = await evaluateFlagForContext("form-widget-turnstile", {
|
||||
flagName: "form-widget-turnstile",
|
||||
tenantId: form.tenantId,
|
||||
userId: null,
|
||||
});
|
||||
const cfg = (form.config ?? {}) as { turnstileEnabled?: boolean };
|
||||
if (flag.enabled && cfg.turnstileEnabled === true) {
|
||||
const cfg = (form.config ?? {}) as { turnstileEnabled?: boolean; captchaEnabled?: boolean };
|
||||
const captchaEnabled = cfg.captchaEnabled === true || cfg.turnstileEnabled === true || def.data.captchaEnabled === true;
|
||||
if (flag.enabled && captchaEnabled) {
|
||||
const captcha = resolveCaptchaConfig(process.env);
|
||||
const secret = getSecret(captcha, "forms");
|
||||
const ok = await verifyCaptchaToken({
|
||||
token: turnstileToken,
|
||||
token: captchaToken ?? turnstileToken,
|
||||
sitekey: captcha.sitekeys.forms,
|
||||
secret: captcha.accountSecret,
|
||||
secret,
|
||||
instanceUrl: captcha.instanceUrl,
|
||||
provider: captcha.provider,
|
||||
failOpen: captcha.failOpen,
|
||||
logger: log,
|
||||
});
|
||||
@@ -334,42 +375,97 @@ export async function action({ params, request }: { params: { formKey?: string }
|
||||
fieldMapping: (form.fieldMapping ?? {}) as Record<string, string>,
|
||||
});
|
||||
|
||||
const submission = await db.$transaction(async (tx) => {
|
||||
const created = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
payload: { values } as object,
|
||||
sourceOrigin: origin ?? null,
|
||||
ipHash: hashIp(ip),
|
||||
userAgent: request.headers.get("user-agent")?.slice(0, 500) ?? null,
|
||||
status: "received",
|
||||
result: { target: target.type } as object,
|
||||
},
|
||||
if (idempotencyKey) {
|
||||
const existing = await db.formSubmission.findUnique({
|
||||
where: { formId_idempotencyKey: { formId: form.id, idempotencyKey } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (existing) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
}
|
||||
|
||||
const outboxPayload = {
|
||||
submissionId: created.id,
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
targetType: target.type,
|
||||
targetEntityId: target.entityId,
|
||||
fieldMapping: { ...target.fieldMapping },
|
||||
webhookUrl: form.webhookUrl ?? null,
|
||||
notifyEmails: form.notifyEmails,
|
||||
values,
|
||||
} as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"];
|
||||
let preparedFiles: PreparedFormFile[];
|
||||
try {
|
||||
preparedFiles = await prepareUploadedFiles({ tenantId: form.tenantId, formBody, fields: visibleFields });
|
||||
} catch (error) {
|
||||
log.warn({ op: "submission.file-rejected", formId: form.id, error: error instanceof Error ? error.message : String(error) }, "Submitted file rejected");
|
||||
return errorResponse(t, "INVALID_PAYLOAD_SHAPE", 400);
|
||||
}
|
||||
|
||||
await publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
|
||||
tenantId: form.tenantId,
|
||||
type: "form.submission.process",
|
||||
payload: outboxPayload,
|
||||
dispatchKey: `form-submission:${created.id}`,
|
||||
let submission: { id: string };
|
||||
try {
|
||||
submission = await db.$transaction(async (tx) => {
|
||||
const created = await tx.formSubmission.create({
|
||||
data: {
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
payload: { values } as object,
|
||||
sourceOrigin: origin ?? null,
|
||||
ipHash: hashIp(ip),
|
||||
userAgent: request.headers.get("user-agent")?.slice(0, 500) ?? null,
|
||||
status: "received",
|
||||
result: { target: target.type } as object,
|
||||
gdprAccepted: true,
|
||||
idempotencyKey: idempotencyKey ?? null,
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
if (preparedFiles.length > 0) {
|
||||
await tx.formFile.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
submissionId: created.id,
|
||||
fieldKey: file.fieldKey,
|
||||
fileId: file.fileId,
|
||||
kind: file.kind,
|
||||
})),
|
||||
});
|
||||
await tx.fileEntityLink.createMany({
|
||||
data: preparedFiles.map((file) => ({
|
||||
tenantId: form.tenantId,
|
||||
createdByUserId: "system:form",
|
||||
fileId: file.fileId,
|
||||
entityType: "form_submission",
|
||||
entityId: created.id,
|
||||
linkRole: file.fieldKey,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
|
||||
const outboxPayload = {
|
||||
submissionId: created.id,
|
||||
formId: form.id,
|
||||
tenantId: form.tenantId,
|
||||
targetType: target.type,
|
||||
targetEntityId: target.entityId,
|
||||
fieldMapping: { ...target.fieldMapping },
|
||||
webhookUrl: form.webhookUrl ?? null,
|
||||
notifyEmails: form.notifyEmails,
|
||||
values,
|
||||
files: preparedFiles,
|
||||
} as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"];
|
||||
|
||||
await publishOutboxEvent(tx as unknown as OutboxPublisherTransaction, {
|
||||
tenantId: form.tenantId,
|
||||
type: "form.submission.process",
|
||||
payload: outboxPayload,
|
||||
dispatchKey: `form-submission:${created.id}`,
|
||||
});
|
||||
|
||||
return created;
|
||||
});
|
||||
|
||||
return created;
|
||||
});
|
||||
} catch (error) {
|
||||
if (idempotencyKey && isUniqueConstraintError(error)) {
|
||||
log.info({ op: "submission.duplicate", formId: form.id, idempotencyKey }, "Duplicate form submission ignored");
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
log.info({ op: "submission.received", formId: form.id, submissionId: submission.id, target: target.type }, "Submission accepted");
|
||||
|
||||
|
||||
Reference in new issue
Block a user