Fix security issues, bugs, and convoluted code across the stack

Security:
- Token revocation. User gains a TokenVersion counter embedded in both
  tokens as a "tv" claim; AuthenticateToken rejects a token whose version
  no longer matches the row. Logout bumps it, so logging out now kills
  outstanding tokens rather than only clearing cookies. Costs one
  primary-key SELECT per authenticated request, which is the price of
  having revocation at all.
- Spotify OAuth CSRF. The fixed SPOTIFY_AUTH_STATE constant is replaced by
  a per-request one-shot nonce, verified and consumed on callback.
- Private chat attachments. Attachments on private messages are written to
  /backend/uploads/private/ and served at /uploads/private/, gated by the
  same nginx auth_request admin check used for /hasura/ and /notes/.
  Private uploads are rejected from non-admin callers.
- The icecast admin UI (/radio/admin/) is behind the admin gate; listener
  traffic is untouched.
- Store caches are mutex-guarded, so -race is clean.

Bugs:
- The email pipeline no longer drops emails permanently on transient
  failure. Failures record an attempt counter and are retried up to 3
  times; the fetch window rewinds to the oldest retryable email's
  ReceivedAt, without which a retry could never re-fetch it.
- Rowing duplicate detection distinguishes ErrRecordNotFound from a real
  DB error, so a transient failure no longer causes a duplicate insert
  plus a paid Claude call.
- DeletePost propagates its delete error instead of reporting success.
- RecentlyPlayed populates the cache it reads.
- main.go exits non-zero if r.Run fails, so a port conflict is visible.
- Python: the scan truncation flag is set on every deadline path;
  timed_out is reported by the worker rather than inferred from the clock;
  date validation uses Europe/London rather than server-local time.
- Vue: Time.vue and Radio.vue intervals are cleared on unmount; Feed.vue
  guards a missing author; Timer.vue gets an explicit state machine and
  compares total elapsed seconds; Elle.vue's comma-operator call and
  per-frame console.log are fixed.

Simplification:
- One TTLCache replaces three near-identical cache implementations, with
  the differing eviction rules as explicit parameters.
- run_scan's transport index no longer uses one object as both ordering
  record and live output; its three phases are separate functions.
- _price_hotels submits all work to a single pool, so the nested-executor
  deadlock hazard is gone by construction.
- The NDJSON event generators share one implementation.
- IMAP FETCH responses pair each line with its literal in the type, so
  callers no longer hand-skip alternate entries.
- Shared helpers for slice-of-pointers, markdown fences and auth cookies.
- Vue: useHotelSearch, createMirrorStore, useRotation, useCreateForm,
  useLinkAttrs, ListWidget and LinkTableItems replace duplicated blocks.
- nginx dev config's duplicated :80/:443 bodies share one partial.

Infra: certbot passes --deploy-hook touching a sentinel that nginx's
watcher reloads on, so renewed certs are served without a restart, and the
watcher also removes the manual restart after first issuance. db is gated
on service_healthy, autoheal is pinned, and the unused envsubst entries
are dropped.

Verified: go build/vet, npx vite build, py_compile, docker compose config
for both overlays, bash -n on all scripts, and an envsubst render of every
nginx template (no leftover placeholders, runtime vars preserved, braces
balanced).

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-09-16 16:12:21 +01:00
co-authored by Claude Opus 5
parent 64df70e5d2
commit 25ac1cde1c
69 changed files with 2381 additions and 1607 deletions
+34 -36
View File
@@ -136,27 +136,35 @@ func (c *imapClient) readLiteral(size int) (string, error) {
return string(buf), nil
}
// fetchPart is one FETCH response line plus the literal payload that followed
// it, if any. Pairing them in the type removes the need for callers to walk an
// interleaved slice and manually skip every second entry — the previous shape,
// where a literal was simply the next element and a caller that forgot to
// advance would parse message bytes as a protocol line.
type fetchPart struct {
Line string
// Literal is the payload that followed Line, valid only when HasLiteral.
Literal string
HasLiteral bool
}
// sendFetch sends a FETCH and collects its response, handling literals.
//
// The returned slice interleaves protocol lines with literal payloads: when a
// line ends in "{N}" the next entry is the N bytes that followed it. The
// caller (fetchEmailsIMAP) relies on that pairing.
//
// The literal is detected with LastIndex("{") plus a "}" suffix rather than a
// real parse, so a line whose text merely ends in braces could be misread —
// tolerable because only FETCH responses reach this code.
func (c *imapClient) sendFetch(cmd string) ([]string, error) {
func (c *imapClient) sendFetch(cmd string) ([]fetchPart, error) {
tag := c.nextTag()
_, err := fmt.Fprintf(c.conn, "%s %s\r\n", tag, cmd)
if err != nil {
return nil, err
}
var lines []string
var parts []fetchPart
for {
line, err := c.readLine()
if err != nil {
return lines, err
return parts, err
}
// Check for literal marker {N} at end of line
@@ -166,23 +174,22 @@ func (c *imapClient) sendFetch(cmd string) ([]string, error) {
if _, err := fmt.Sscanf(sizeStr, "%d", &size); err == nil && size > 0 {
literal, err := c.readLiteral(size)
if err != nil {
return lines, fmt.Errorf("reading literal of %d bytes: %w", size, err)
return parts, fmt.Errorf("reading literal of %d bytes: %w", size, err)
}
lines = append(lines, line)
lines = append(lines, literal)
parts = append(parts, fetchPart{Line: line, Literal: literal, HasLiteral: true})
continue
}
}
if strings.HasPrefix(line, tag+" ") {
// Check for OK
parts := strings.SplitN(line, " ", 3)
if len(parts) >= 2 && parts[1] != "OK" {
return lines, fmt.Errorf("FETCH failed: %s", line)
fields := strings.SplitN(line, " ", 3)
if len(fields) >= 2 && fields[1] != "OK" {
return parts, fmt.Errorf("FETCH failed: %s", line)
}
return lines, nil
return parts, nil
}
lines = append(lines, line)
parts = append(parts, fetchPart{Line: line})
}
}
@@ -245,33 +252,24 @@ func (s *EmailSyncService) fetchEmailsIMAP(since time.Time) ([]graphMessage, err
// sequence set, and each full body is held in memory. A mailbox with a
// very busy day could make this large — there is no batching.
seqSet := strings.Join(seqNums, ",")
fetchLines, err := c.sendFetch(fmt.Sprintf("FETCH %s (BODY[])", seqSet))
fetchParts, err := c.sendFetch(fmt.Sprintf("FETCH %s (BODY[])", seqSet))
if err != nil {
return nil, fmt.Errorf("IMAP fetch: %w", err)
}
// Pair each "... BODY[] {N}" header line with the literal that sendFetch
// stored immediately after it. A message that fails to parse is logged and
// skipped rather than failing the whole sync.
// Each body arrives as a "... BODY[] {N}" line carrying its literal, so
// the pairing is now part of the value rather than something this loop has
// to reconstruct by skipping entries. A message that fails to parse is
// logged and skipped rather than failing the whole sync.
var messages []graphMessage
for i := 0; i < len(fetchLines); i++ {
line := fetchLines[i]
// Look for a literal following this line
if strings.Contains(line, "BODY[]") && strings.HasSuffix(line, "}") {
if i+1 < len(fetchLines) {
raw := fetchLines[i+1]
// Advance past the literal so its contents are never
// examined as a protocol line. Note this is a C-style loop
// precisely so i can be advanced here; a range loop could
// not do it.
i++
gm, err := parseRawEmail(raw)
if err != nil {
log.Printf("[EmailSync/IMAP] Error parsing email: %v", err)
continue
}
messages = append(messages, gm)
for _, part := range fetchParts {
if part.HasLiteral && strings.Contains(part.Line, "BODY[]") {
gm, err := parseRawEmail(part.Literal)
if err != nil {
log.Printf("[EmailSync/IMAP] Error parsing email: %v", err)
continue
}
messages = append(messages, gm)
}
}