Security: mandatory Turnstile when configured, admin-only test email, roles on create, safer client IP

- Turnstile is on only when both keys are set (panel or env); then submitForm
  requires a valid token, so a bot can no longer skip the check by omitting it.
  Not configured -> no widget, no check. New getTurnstileConfig() for layouts.
- /api/ipal/test-email now requires the admin role (403 for other users).
- roles field: create access is admin-only, so open sign-up can't self-assign admin.
- clientIp reads X-Forwarded-For from the right, accepts Headers, can be pinned
  via option or IPAL_IP_HEADER; exposed on search/MCP routes.
- Pluggable shared rate-limit store (setRateLimitStore) for multi-instance setups.
- Replace the template int.spec with real unit tests in test/ (44 tests).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-15 23:54:25 +02:00
co-authored by Claude Opus 5.5
parent aa284736c9
commit ce0a390351
25 changed files with 786 additions and 133 deletions
+16 -11
View File
@@ -7,18 +7,25 @@ czyta secret server-side.
## Config
Brak opcji — pola `turnstileSiteKey` / `turnstileSecretKey` są w
SiteIntegrations (tab Turnstile). Edytor wpisuje klucze w panelu.
SiteIntegrations (tab Turnstile). Edytor wpisuje klucze w panelu (fallback z env:
`NEXT_PUBLIC_TURNSTILE_SITE_KEY` / `TURNSTILE_SECRET_KEY`).
**Włączony = oba klucze ustawione.** Wtedy widget się pokazuje, a `submitForm`
**wymaga** poprawnego tokenu: brak tokenu to odrzucenie, więc bot nie ominie
sprawdzenia, po prostu go nie wysyłając. Nieskonfigurowany (lub tylko jeden klucz,
co trafia do logu) → widgetu nie ma, sprawdzenia nie ma, formularze działają.
Stan zwraca `getTurnstileConfig(payload)` → `{ enabled, siteKey }`.
## Front — widget (client)
Import z `@intecion/ipal-kit/client`. `siteKey` pobierz server-side i przekaż jako prop:
```tsx
// server component — pobiera publiczny siteKey z panelu
import { getSiteIntegrations } from '@intecion/ipal-kit'
// server component — publiczny siteKey, null gdy Turnstile wyłączony
import { getTurnstileConfig } from '@intecion/ipal-kit'
const { turnstileSiteKey } = await getSiteIntegrations(payload)
// przekaż do swojego client-formularza → widget
const { siteKey } = await getTurnstileConfig(payload)
// przekaż do swojego client-formularza → widget (siteKey null → widget nic nie renderuje)
```
```tsx
@@ -111,15 +118,13 @@ przekazywania siteKey do każdego formularza.
```tsx
// app/(frontend)/[locale]/layout.tsx (server)
import { getSiteIntegrations } from '@intecion/ipal-kit'
import { getTurnstileConfig } from '@intecion/ipal-kit'
import { TurnstileProvider } from '@intecion/ipal-kit/client'
import { getCachedPayload } from '@/lib/content'
export default async function Layout({ children }) {
// tylko publiczny siteKey — sekret Turnstile nigdy nie trafia do klienta
const { turnstileSiteKey: siteKey } = await getSiteIntegrations<{ turnstileSiteKey?: string }>(
await getCachedPayload(),
)
// tylko publiczny siteKey (null, gdy Turnstile nie jest skonfigurowany) — sekret nigdy nie trafia do klienta
const { siteKey } = await getTurnstileConfig(await getCachedPayload())
return (
<html>
<body>
@@ -157,7 +162,7 @@ function ContactForm() {
```
`token` → do submitForm. `TurnstileWidget` → wstaw gdzie ma być. `reset()` → po
wysłaniu. `enabled` → false gdy brak klucza (dev bez Turnstile).
wysłaniu. `enabled` → false gdy Turnstile nie jest skonfigurowany (widget i weryfikacja wyłączone).
### Dlaczego Turnstile NIE jest w pełni "wstaw i zapomnij" jak CookieBanner