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]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-06-09 19:57:40 -07:00
1 parent 962f5d9e06
commit 82fb5122f3
5 files changed
+496 -20

No files matched your search

+26 -1
View File
@@ -1,13 +1,17 @@
package server
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"glimmer/internal/config"
@@ -175,5 +179,26 @@ func (s *Server) Start() error {
}
log.Printf("Starting server on %s (base URL: %s)", addr, s.cfg.Server.BaseURL)
return srv.ListenAndServe()
// Graceful shutdown: on SIGINT/SIGTERM stop accepting connections, let
// in-flight requests drain (bounded), then return so deferred cleanup
// (e.g. closing the SQLite DB) runs instead of being killed mid-write.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
errCh := make(chan error, 1)
go func() { errCh <- srv.ListenAndServe() }()
select {
case err := <-errCh:
return err
case <-ctx.Done():
log.Printf("Shutting down...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil && !errors.Is(err, context.DeadlineExceeded) {
return err
}
return nil
}
}