# Turnstile registration protection

Registration uses a Cloudflare Turnstile Managed widget and server-side Siteverify validation. The widget sends `cf-turnstile-response`; Nuhet accepts only a successful response whose hostname matches the configured registration hostname and whose action is `register`. Missing, malformed, expired, replayed, mismatched and unreachable verification all fail closed.

Configuration is under `registration.turnstile`: `site_key`, `secret_key`, `hostname`, and `action`. For an uncached environment, set `TURNSTILE_SITE_KEY`, `TURNSTILE_SECRET_KEY` and `TURNSTILE_HOSTNAME`. The existing `registration.captcha_enabled` control and registration rate limits are preserved. Only the public site key appears in HTML.

Provider configuration and network failures use `CaptchaVerificationUnavailable` with a ten-minute reporting throttle. Logs contain fixed messages and safe error identifiers, never tokens, secrets, request bodies or provider exception chains. Captcha reporting runs in a scoped Sentry event processor that removes the request body/query, and one-use tokens are excluded from flashed form input.

## Production configuration and release

Nuhet has cached Laravel configuration. **Do not run `php artisan config:cache`**: it previously broke unrelated runtime `env()` consumers, including SMS.

Before release, create the Free Turnstile widget in the approved Xile Cloudflare account, restrict its hostname to `post.nuhet.se`, and securely stage its key pair. The secret must travel only through protected credential/file delivery, never chat, Git, command arguments or scratch logs.

The narrow installer is `docs/ops/turnstile-config-apply.php`. Independently review it and qualify its write/rollback behavior before it runs on live. Its CLI is fixed to `/var/www/html`; the reviewed script can be staged privately under `/tmp` and run before the application code changes. As root on Nuhet, use `--check` with a protected stdin key payload to obtain the current environment/cache hashes, then `--apply` with those expected hashes to prevent stale configuration replacement. It writes private rollback copies under `storage/app/private/turnstile-3804/BACKUP_ID/`, updates only the three TURNSTILE environment variables and `registration.turnstile` cached section, and checks the fresh saved bytes. Use `--rollback BACKUP_ID` only when no subsequent runtime configuration change occurred; the operation checks both current files against the installed hashes. No keys travel in CLI arguments or output. Record the concrete backup ID and qualified write/rollback results before release. Do not deploy this registration change until that secure installation route is available and verified.

Release through the documented Nuhet git-bundle workflow. Verify the ordinary registration URL and same-site assets, a missing/invalid token rejection with the Swedish error, and a real registration that creates the company/user and sends its password email. Log into the test account so the daily abandoned-account rule excludes it. Check the actual Cloudflare hostname/action verification in the live runtime. Retain the existing rate limit.

Reference: https://developers.cloudflare.com/turnstile/get-started/server-side-validation/

The installer holds an exclusive `storage/framework/turnstile-3804-config.lock` through each check, apply or rollback, including backups and readback. All cooperating configuration operations must use that same lock. Before a live operation, establish exclusive ownership of Nuhet environment/cached-configuration writes and ensure no non-cooperating writer is active; the lock cannot stop an unrelated tool that ignores it. Hash checks detect stale state but do not alone make two-file updates atomic against arbitrary writers. A partial rollback attempts to restore the installed state and records its result privately in `state.json`; any `manual-recovery-required` result is a stop condition. Qualification must cover success, second-write failure, rollback recovery and lock contention before live.
