# Security

This lists the controls in the code, how they were tested, and the risks that remain. Security is not guaranteed; this is a record of what was done.

## Threat model (short)

| Asset | Threat | Main controls |
|---|---|---|
| Enquiry personal data | public exposure, export abuse | admin-only routes, policies/gates, CSV export logged, no public API |
| CMS accounts | brute force, session theft | throttled login, strong passwords, session regeneration, idle timeout, secure cookies |
| Website content | defacement, stored XSS | fixed section schemas, escaped output, link allowlist, no raw HTML |
| Server | malicious uploads | MIME sniffing, re-encoding to WebP, random names, no SVG/PHP uploads |
| Enquiry form | spam, flooding | CSRF, rate limits, honeypot, minimum fill time |

## Controls in place

**Authentication**
- Email + password only. Passwords hashed by Laravel (bcrypt).
- Password rules: 12+ characters, mixed case, number, symbol.
- Login throttled to 5 per minute per email+IP and 20 per minute per IP.
- Session regenerated on login and invalidated on logout. `auth.session` ends other sessions after a password change.
- Idle timeout: 30 minutes (`CMS_IDLE_TIMEOUT`).
- Deactivated users are signed out on their next request.
- Password reset: Laravel single-use tokens, 60-minute expiry, same reply whether or not the email exists, throttled.
- `SESSION_ENCRYPT=true`. Set `SESSION_SECURE_COOKIE=true` in production (HTTPS).

**Authorization**
- Gates: `manage-content`, `manage-enquiries` (any active user); `manage-users`, `manage-settings`, `view-activity-logs` (Super Admin).
- Applied with route middleware on every admin route, not only in the UI.
- At least one active Super Admin always remains. Users cannot deactivate themselves.
- `role` and `is_active` are not mass assignable.
- Section routes use scoped bindings, so a section can only be edited through its own page (prevents IDOR).

**Input and output**
- Form Requests or `validate()` on every write.
- CMS content is validated against the section schema, and unknown keys are dropped.
- Blade `{{ }}` escaping everywhere. CMS text is never output with `{!! !!}`. The only `{!! !!}` output is fixed SVG files from `public/images/icons` and `nl2br(e(...))`.
- Links: `SafeUrl` blocks `javascript:`, `data:`, protocol-relative and other schemes.
- Markdown emails escape the visitor's message so it cannot inject links.
- CSV export prefixes `= + - @` cells to prevent spreadsheet formula injection.
- Search terms escape SQL `LIKE` wildcards. All queries use Eloquent bindings.

**Uploads**
- Allowed: JPEG, PNG, WebP (8 MB), MP4, WebM (50 MB). The type is read from file content (`finfo`), not the extension.
- Images are decoded and re-encoded to WebP, which removes embedded payloads and metadata. A pixel limit blocks decompression bombs.
- Stored under random UUID names on the `public` disk. Original names are kept only as text.
- SVG uploads are not allowed.
- Files used on a page cannot be deleted.

**Public forms**
- CSRF (web middleware), 3 per minute and 20 per day per IP, honeypot field, encrypted timestamp (submissions under 3 seconds are dropped silently).

**HTTP**
- Headers: `X-Content-Type-Options`, `X-Frame-Options: SAMEORIGIN`, `Referrer-Policy`, `Permissions-Policy`, `Cross-Origin-Opener-Policy`, and HSTS on HTTPS.
- Content-Security-Policy with a per-request nonce: scripts only from this site (no inline scripts without the nonce, no `eval`), frames only from youtube-nocookie.com and player.vimeo.com, `object-src none`, `frame-ancestors self`, `form-action self`. Styles allow inline because the slider library sets inline styles. It can be switched off with `CMS_CSP_ENABLED=false`. It is not sent on the debug error page.
- Admin responses: `X-Robots-Tag: noindex` and `Cache-Control: no-store`.
- `robots.txt` disallows `/admin`.

**Logging** (`activity_logs`)
- Login success and failure (the failed email is stored only as a hash), logout, password reset request and completion, password change, user create and update (role and status changes), page publish, unpublish and discard, section edits, media upload and delete, enquiry received, status change, archive, export and delete, settings changes.
- No passwords, tokens or message bodies are logged.

**Secrets**
- `.env` and `.mcp.json` are git-ignored. `.env.example` has placeholders only.
- No default admin account is seeded. Accounts are created with `php artisan admin:create`, which asks for the password without showing it.

## Tested (automated, see TEST_PLAN.md)
CSP header and nonce, with no CSP console errors in Chromium, Firefox and WebKit, including the YouTube embed. `composer audit` and `npm audit` found 0 known vulnerabilities (2026-11-26). Tracked files were scanned for secrets with none found.
Unauthorized access, Admin trying Super Admin actions, IDOR on sections, XSS payloads in CMS fields, `javascript:` links, unknown fields, file upload bypass (PHP in .jpg, SVG with script, PDF), login brute-force throttle, inactive user session, idle timeout, single-use reset token, CSV formula injection, markdown injection in email, enquiry honeypot, timing and rate limits.

## Not tested / remaining risks
- Real SMTP delivery (needs credentials).
- HTTPS, secure cookies and HSTS on the real server.
- Penetration testing by a third party.
- 2FA is not implemented (optional in the spec).
- Rate limits use the cache store; with `CACHE_STORE=database` they work on a single server.

## Production checklist
- `APP_ENV=production`, `APP_DEBUG=false`, unique `APP_KEY`.
- `SESSION_SECURE_COOKIE=true`, HTTPS only.
- A database user with rights on this database only (no `GRANT`, no other schemas).
- Web root pointing to `public/`.
- **Revoke the Figma API token** that was shared during development and keep `.mcp.json` off the server.
