Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/form-widget/src/honeypot.ts
T

24 lines
976 B
TypeScript

/**
* Honeypot detection helper. A honeypot field is a hidden input that
* legitimate users never see / fill — bots that scrape form fields and
* submit blindly do.
*
* The renderer emits a field with key {@link HONEYPOT_FIELD_KEY} hidden
* via CSS + aria-hidden + tabindex=-1. The submission endpoint passes the
* raw payload through this helper. When a non-empty value is detected we
* return `true` — the caller should silently accept the request (204) so
* the bot gets no signal that it was rejected.
*/
import { HONEYPOT_FIELD_KEY } from "./schema.js";
export function isHoneypotTriggered(values: Record<string, unknown> | null | undefined): boolean {
if (!values) return false;
const raw = values[HONEYPOT_FIELD_KEY];
if (raw === undefined || raw === null) return false;
if (typeof raw === "string") return raw.trim() !== "";
if (typeof raw === "boolean") return raw === true;
if (typeof raw === "number") return raw !== 0;
return true;
}