Files
Glimmer/plan.md
T
lucioandClaude Fable 5 82fb5122f3 Harden image decoding, shutdown, and CSRF cookie; add handler tests
- Reject images over 24 MP before full decode (decodeImageBounded) so a
  small PNG declaring huge dimensions cannot OOM the RPi Zero via the
  public thumbnail route or the resize handler
- Graceful shutdown on SIGINT/SIGTERM with a 10s drain so the SQLite DB
  closes cleanly on deploys
- Set the Secure flag on the csrf cookie (same isHTTPS rule as the
  session cookie); csrfToken is now a *Server method
- Add handlers_test.go: auth, login, CSRF, upload serving (traversal +
  forced attachment), and decompression-bomb coverage
- Track remaining work (CSP unsafe-inline removal) in plan.md

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-06-09 19:57:40 -07:00

2.5 KiB
Raw Blame History

Hardening / Robustness Plan

Progress: items 1–4 done (build/vet/tests green, ARMv5 cross-compile verified). Item 5 remains — large template refactor, best done as its own piece of work.

Follow-ups from the 2026-06-09 security review (fixes from that review already landed in 962f5d9). Status legend: ☐ todo · ◐ in progress · ☑ done

1. ☑ Image decompression-bomb guard

generateThumbnail and handleAdminUploadResize call image.Decode with no dimension check. A valid small PNG can declare huge pixel dimensions and OOM the RPi Zero (512 MB) on decode. GET /uploads/thumb/{filename} is public and triggers generation on first request.

  • Add a bounded decode helper: image.DecodeConfig first (header only), reject anything over 24 MP, use int64 math so the width×height product cannot overflow 32-bit int on ARMv5.
  • Use it in both generateThumbnail and the resize handler.

2. ☑ Graceful shutdown

Start() calls ListenAndServe directly; SIGTERM/Ctrl-C kills in-flight requests and gives SQLite no chance to close cleanly.

  • signal.NotifyContext for SIGINT/SIGTERM, then srv.Shutdown with a 10 s timeout.

The session cookie sets Secure via isHTTPS(); the csrf cookie never does. Make csrfToken a *Server method and set Secure: s.isHTTPS(r). (Stays non-HttpOnly on purpose — upload.js reads it.)

4. ☑ Handler test coverage

api_test.go / mcp_test.go are thorough but the web handlers (auth, CSRF, upload serving) have zero tests. Add handlers_test.go covering at least:

  • unauthenticated /admin/* requests redirect to login
  • state-changing POSTs without a CSRF token are rejected (incl. the resize route fixed in 962f5d9)
  • /uploads/{filename}: invalid/traversal names 404; non-image files get Content-Disposition: attachment + octet-stream
  • login: wrong password → 401, correct → session cookie + redirect
  • oversized-image rejection from item 1

5. ☐ Drop 'unsafe-inline' from script-src (CSP)

All template JS is inline, so the CSP doesn't actually block injected scripts. Move inline blocks into embedded static files (the upload.js pattern) and tighten script-src to 'self'. Touches every template — deliberately last, and large enough that it may warrant its own session.


Verification for each item: go build ./..., go vet ./..., go test ./... must pass; CLAUDE.md conventions apply (no new deps, RPi-Zero-friendly memory use).