# Public and Client Form Security

## Scope

This release hardens public contact forms, client and administrator authentication, registration, password recovery, and authenticated client-panel forms. It is a defense-in-depth implementation; no web application can be guaranteed immune from every future vulnerability, so production deployment should still include independent penetration testing and dependency monitoring.

## Google reCAPTCHA

Google reCAPTCHA v2 is integrated through the existing administrator settings page.

Required whenever the integration is active:

- All public website contact forms, including theme contact pages and Footer Builder mini-contact forms
- Client registration
- Client login
- Administrator login

Selectable coverage remains available for client and administrator password-recovery forms.

Every token is verified server-side. The verification checks the Google success result, expected hostname, configured allowed hosts, and challenge age. A widget rendered in the browser without successful backend verification is never accepted.

Configuration:

```env
RECAPTCHA_VERIFY_HOSTNAME=true
RECAPTCHA_ALLOWED_HOSTS=example.com,www.example.com,staging.example.com
```

Credentials are stored in **Admin → Settings → reCAPTCHA**. The secret key is encrypted by the model and is never returned to the browser.

## Request controls

All normal web forms use Laravel CSRF verification. Public/client unsafe requests also pass through:

- `SanitizeUntrustedInput`: normalizes text and rejects HTML, script tags, event-handler attributes, executable URL schemes, PHP tags, NUL bytes and unsafe control characters. Passwords, tokens, API credentials and reCAPTCHA tokens are not modified.
- `EnforceSameOriginWrites`: rejects browser requests explicitly identified as cross-site by Fetch Metadata headers. CSRF validation remains the primary compatibility control.
- `SecurityHeaders`: adds anti-sniffing, frame restrictions, referrer policy, permissions policy, a conservative CSP, private cache controls for auth/client pages and HSTS on secure production traffic.

## Server validation

Public contact data is accepted only through `StorePublicContactMessageRequest` with strict length, character and plain-text rules. Registration uses `RegisterClientRequest`, including a honeypot, normalized email, referral allow-list, deliverable-domain validation, unique email and a strong password policy.

Client-panel mutations use authenticated ownership checks, allow-listed enumerations and rate limits. MT4 account selection is resolved against the authenticated client before an API call. Funding methods, leverage values, currencies and group identifiers are validated rather than trusted from the browser.

## SQL injection controls

User input is not concatenated into SQL statements. Eloquent/query-builder parameters and bound placeholders are used. The contact email lookup uses a bound placeholder, and client account IDs are resolved through ownership-scoped model queries. Blocking words such as `SELECT` or apostrophes is intentionally not used because that is not a reliable SQL-injection control.

## XSS controls

The application rejects executable markup in public/client plain-text inputs. Persisted public contact data is rendered through escaped Blade syntax. Input filtering is not treated as the only control; output encoding and browser security headers remain active.

## Uploaded KYC documents

KYC uploads are authenticated, rate-limited and stored on private Laravel disks outside the public web root. Accepted MIME types, extensions, dimensions and size are allow-listed. Filenames are generated server-side and downloads require an authenticated client route.

## Session and credential controls

- Session IDs rotate after successful login and client password change.
- Logout invalidates the session and rotates the CSRF token.
- Session payload encryption is enabled by default through `SESSION_ENCRYPT=true`.
- Session cookies are HTTP-only and SameSite `lax` by default.
- Set `SESSION_SECURE_COOKIE=true` on HTTPS production sites.
- MT4 passwords and withdrawal destinations remain encrypted at rest.

## Rate limits

```env
SECURITY_CONTACT_RATE_LIMIT_PER_MINUTE=5
SECURITY_CLIENT_SENSITIVE_RATE_LIMIT_PER_MINUTE=8
SECURITY_CLIENT_FINANCE_RATE_LIMIT_PER_TEN_MINUTES=5
SECURITY_CLIENT_ACCOUNT_CREATE_RATE_LIMIT_PER_HOUR=3
```

## Diagnostic

Run:

```bash
php artisan security:diagnose-forms
```

The command checks mandatory reCAPTCHA coverage, route middleware, CSRF, public/client security middleware, discovered contact forms and rate limits on state-changing client endpoints.

## Production checklist

1. Use HTTPS and set `APP_URL` to the HTTPS origin.
2. Set `SESSION_SECURE_COOKIE=true`.
3. Register all production and staging domains in Google reCAPTCHA.
4. Configure `RECAPTCHA_ALLOWED_HOSTS`.
5. Run `php artisan security:diagnose-forms` after deployment.
6. Keep Composer and npm dependencies patched.
7. Run an independent authenticated and unauthenticated penetration test before live launch.
