Dezio/docs/tests/S007-website-security.md
2026-12-02 16:25:57 -06:00

56 lines
7.8 KiB
Markdown

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# S007 — Website and dashboard security: test file
Written by the worker that built it, run by the test phase. Probes below were run once against the local `--web-dev`
copy (`DASHBOARD_URL=http://127.0.0.1:8099/dezio`); a Result of `pass` means it was seen there, `—` means not run yet.
## Built
- `src/web/mod.rs` · `RateLimit` + `SIGN_INS_PER_MINUTE` (20/min, all users) on `/callback`; `HOSTED_CSP` (sandbox) on `/f/`; bare base `/dezio` redirects to `/dezio/`; `/dezio/` serves the site; ServeDir folders no longer redirect; `start_dev` keeps DASHBOARD_URL's path.
- `src/web/activity.rs` · same sign-in limit on `/activity/api/token` (429); `dev_mode` also needs a local Host and no `X-Forwarded-For`.
- `web/dashboard/static/app.js` · text for `?error=busy`.
## Review checklist (findings)
| Item | Finding | Severity / state |
|---|---|---|
| Auth on every API route | All `/api/*` except `/api/me` call `need_session`; Activity routes need a Bearer session (`library`, `rom`, `save`, `state`, `legacy`); `art` is public (cover pictures only). Probed: forged cookie → 401. | fine |
| Guild permission checks | `guild_for`: guild in cache, user is a member, Manage Server or Admin, live from the cache. Probed: unknown guild → 404. Role menus can list an existing non-power role by name, so a Manage Server user can let members self-assign a cosmetic role ranked above his own (same as the Discord editor). | LOW, Later |
| CSRF on writes | `check_write` on every PATCH/POST (user, guild, rolemenus, quote preview, logout): Origin (or Referer) must match, `x-csrf-token` constant-time compare, JSON content type. `web-session` checks Origin. Probed: no token, evil Origin, no Origin, text/plain all refused. Host+Origin both `evil.test` passes the origin check, but the browser sends no cookie for that host, so no session: fine. | fine |
| OAuth state | 24 random bytes in an HttpOnly cookie, compared on callback, cleared after success. `next` only from a whitelist (`/dashboard`, `/activity/`). Probed: wrong state → `error=state`; `next=//evil.test` not stored; forged `dezio_next=//evil.test` cookie ignored. | fine |
| Cookie flags | `HttpOnly; SameSite=Lax; Path=/dezio`, `Secure` on https. Probed. | fine |
| Session expiry | Dashboard 7 days, Activity 24 h, both checked on every use; expired ones pruned on sign-in. Every `web-session` call writes a new Activity session (only signed-in users). | LOW, Later |
| Input size limits | 64 KiB body limit (probed: 200 KB → 413), text fields capped, serde depth limit (probed: 5000-deep JSON refused), saves/states capped (unit test). | fine |
| Path traversal in static files | ServeDir refuses `..` (probed `/assets/../../.env`, `/f/../../.env`, `/dashboard/static/../../../Cargo.toml` → 404); ROM ids are looked up in the library map, never joined to a path. | fine |
| Folder redirects (base path) | ServeDir answered `/dezio/assets/fonts` with `Location: /fonts/` (base and prefix lost; not attacker-controlled). Fixed: folders now 404. | LOW, fixed |
| Open redirect / path tricks | Every redirect is base + fixed path; `/activity?x=…` keeps the query after `?` only. Probed `//evil.test/…`, `/dezio/dezio/…`, `/dezioX/…`, `/dezio/activity/../dashboard` → 404. | fine |
| Bare base path | `/dezio/` gave 404 while `/dezio` served the page with relative links that escape the base. Fixed: `/dezio` → 308 `/dezio/`, `/dezio/` serves the site. | MED (broken page), fixed |
| Hosted files `/f/` | Files from `/download` (internet content) were served on the site's origin with the normal CSP: an HTML/SVG plus a hosted `.js` could run script as the site (`script-src 'self'`). Fixed: `/f/` gets `sandbox; default-src 'none'` CSP. | MED, fixed |
| Security headers | nosniff, HSTS, CSP, X-Frame-Options DENY (site), frameable CSP for Activity only, Referrer-Policy. No Permissions-Policy (nothing to restrict). | fine |
| Error leaks | API errors are fixed texts. Bad JSON returns axum's parser message (position only, no secrets). | fine |
| Rate limits | No limit on `/callback` and `/activity/api/token`, each costs a Discord OAuth call; hammering could get the bot's address limited by Discord. Fixed: 20 per minute together. Writes need a session + CSRF; quote preview has 2 slots; `/api/places` (signed in) calls the geocoder with no limit. | MED fixed; places LOW, Later |
| Activity input | Uploads capped, slot 0–3 only, ROM id from the library map. Dev sessions: `DEZIO_ACTIVITY_DEV=1` + loopback was enough, and behind `tailscale serve` every request is loopback. Fixed: also needs a local Host and no `X-Forwarded-For`. No WebSocket. | LOW, fixed |
## Checks
| # | Risk | Do this | Expect | How | Result |
|---|---|---|---|---|---|
| 1 | HIGH | `cargo test web::` | all pass except `the_game_boy_activity_…` in a worktree without `assets/roms` | unit | pass (10/11, ROM test needs assets/roms) |
| 2 | HIGH | `sign_ins_are_limited_per_minute` | 3rd call in a minute refused, next minute allowed | unit | pass |
| 3 | HIGH | `base_path_routes_and_redirects` (base `/dezio`) | `/dezio/`, `/`, `/dezio/dashboard`, assets 200; `/dezio?x=1` → `/dezio/?x=1`; folder 404; cookie `Path=/dezio`; callback error stays under base | unit | pass |
| 4 | HIGH | GET a `/f/<token>/<file>` link | header `content-security-policy` starts with `sandbox;`; video still plays inline in Firefox and Chrome | unit + by hand | unit pass; browser not run (needs a browser, S011) |
| 5 | HIGH | PATCH `/dezio/api/user` with cookie `dezio_session=devtoken` and: no `x-csrf-token`; `Origin: http://evil.test`; no Origin; `Content-Type: text/plain` | 403, 403, 403, 415 | integration (curl) | pass |
| 6 | HIGH | `curl --path-as-is` `/dezio/assets/../../.env`, `/dezio/f/../../.env` | 404 | integration | pass |
| 7 | MED | 21 POSTs to `/dezio/activity/api/token` with `{"code":"x"}` in one minute (use a stub: do NOT let these reach Discord, e.g. run without network) | 21st → 429 "Too many sign-ins" | integration | pass (no network: 20 x 401, 21st and 22nd 429) |
| 8 | MED | `DEZIO_ACTIVITY_DEV=1`, POST `/dezio/activity/api/dev-session` plain; with `X-Forwarded-For: 1.2.3.4`; with `Host: astar76.com` | session; 404; 404 | integration | pass |
| 9 | MED | GET `/dezio` and `/dezio?a=//evil.test` | 308 to `/dezio/` and `/dezio/?a=//evil.test` (relative, same host) | integration | pass |
| 10 | MED | Open the site at `https://…/dezio` on the live server after deploy | lands on `/dezio/`, CSS and images load | by hand 🧑 | not run: live site, owner |
| 11 | MED | Dashboard with `?error=busy` | shows "Too many sign-ins right now…" | screen | pass (text present in app.js; page not looked at) |
| 12 | LOW | GET `/dezio/assets/fonts`, `/dezio/f/`, `/dezio/activity/static` | 404, no Location header | integration | pass |
## Needs
- `cargo build`, then `DASHBOARD_URL=http://127.0.0.1:8099/dezio DISCORD_BOT_TOKEN=dev-placeholder target/debug/dezio --web-dev`; cookie `dezio_session=devtoken`, CSRF `devcsrf`.
- Never probe the live site or Discord. `/callback` and `/activity/api/token` with a valid-looking state/code DO call discord.com: block the network or stop before that point.
## Doubts
- `HOSTED_CSP` sandbox: not checked in a browser that video/GIF/PNG still show inline (check 4). If a browser refuses media under `sandbox`, drop `sandbox` and keep `default-src 'none'`.
- The `/dezio` → `/dezio/` redirect assumes `tailscale serve` passes `/dezio` unchanged; if it strips the prefix the redirect never fires (harmless).
- `dev_mode` assumes `tailscale serve` sets `X-Forwarded-For` or keeps the public Host; the Host check alone should cover it.
- The limit is global: during an attack real users also get "busy" for up to a minute. Accepted for a small bot.
- One probe in this session sent a bogus code to Discord's token endpoint (forged state cookie on `/callback`), before the rule was noticed. One request, refused.