From 49f1051b3f569dc1ef42f85b5b5b3de1b5a9095a Mon Sep 17 00:00:00 2001 From: tgiachi Date: Wed, 11 Nov 2026 12:56:20 +0100 Subject: [PATCH 1/2] feat(streaming): stream tracks with HTTP Range support - feat(streaming): open a track's file only after checking it against the configured library roots: relative paths only, no "..", symlinks resolved on both sides so none can leave the root, regular files only - feat(api): GET and HEAD /api/v1/tracks/{id}/stream via http.ServeContent (206, 416, Content-Length, conditional requests), MIME type from the extension, nosniff, no-referrer and private caching headers - feat(api): the stream route also accepts the access token as access_token in the query string, because an audio element cannot send headers; no other route does, and the query string is never logged - feat(storage): PlayableFile picks the oldest file of a track that is not flagged missing - fix(api): internal errors set by handlers are now written to the request log - test: range variants, HEAD, conditional requests, auth variants, traversal, symlink escape, symlinked root, missing file, directory instead of file --- cmd/sondad/main.go | 2 + internal/api/http/api_test.go | 23 ++- internal/api/http/handlers_stream.go | 51 ++++++ internal/api/http/middleware.go | 27 ++- internal/api/http/router.go | 15 +- internal/api/http/stream_test.go | 245 +++++++++++++++++++++++++++ internal/storage/library.go | 19 +++ internal/storage/library_test.go | 48 ++++++ internal/streaming/streaming.go | 161 ++++++++++++++++++ internal/streaming/streaming_test.go | 226 ++++++++++++++++++++++++ 10 files changed, 806 insertions(+), 11 deletions(-) create mode 100644 internal/api/http/handlers_stream.go create mode 100644 internal/api/http/stream_test.go create mode 100644 internal/streaming/streaming.go create mode 100644 internal/streaming/streaming_test.go diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index 4ad51a8..ba7c362 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -20,6 +20,7 @@ import ( "codeberg.org/squidd/sonda/internal/library" "codeberg.org/squidd/sonda/internal/listening" "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/migrations" ) @@ -97,6 +98,7 @@ func run() error { Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), Scan: scanControl{ctx: ctx, m: scans}, + Streams: streaming.NewService(libraryRepo, cfg.Library.Roots), }) // No WriteTimeout: audio streaming (later) needs long responses. diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index eac6d34..10100b0 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -17,6 +17,7 @@ import ( "codeberg.org/squidd/sonda/internal/auth" "codeberg.org/squidd/sonda/internal/listening" "codeberg.org/squidd/sonda/internal/storage" + "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/migrations" ) @@ -31,6 +32,12 @@ func newTestAPI(t *testing.T) http.Handler { } func newTestAPIWithScan(t *testing.T, scan scanService) http.Handler { + t.Helper() + return newTestAPIWith(t, func(d *Deps) { d.Scan = scan }) +} + +// newTestAPIWith wires real services and lets a test replace parts of Deps. +func newTestAPIWith(t *testing.T, mutate func(*Deps)) http.Handler { t.Helper() db, err := storage.Open(storage.DriverSQLite, filepath.Join(t.TempDir(), "api.db")) if err != nil { @@ -49,13 +56,23 @@ func newTestAPIWithScan(t *testing.T, scan scanService) http.Handler { } tokens := auth.NewTokenIssuer(secret, time.Hour) - return NewRouter(Deps{ + deps := Deps{ Logger: zap.NewNop(), Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), Tokens: tokens, Listening: listening.NewService(storage.NewListeningRepo(db)), - Scan: scan, - }) + Scan: &fakeScan{}, + Streams: streaming.NewService(noFiles{}, nil), + } + mutate(&deps) + return NewRouter(deps) +} + +// noFiles is a streaming source with nothing to play. +type noFiles struct{} + +func (noFiles) PlayableFile(context.Context, int64) (streaming.File, error) { + return streaming.File{}, streaming.ErrNotFound } type resp struct { diff --git a/internal/api/http/handlers_stream.go b/internal/api/http/handlers_stream.go new file mode 100644 index 0000000..44862bb --- /dev/null +++ b/internal/api/http/handlers_stream.go @@ -0,0 +1,51 @@ +package httpapi + +import ( + "errors" + "net/http" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/streaming" +) + +type streamHandlers struct{ svc streamService } + +// stream serves a track's original file. http.ServeContent does the protocol +// work: Range (206, 416), HEAD, Content-Length, conditional requests, and it +// stops copying when the client goes away. +func (h *streamHandlers) stream(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + st, err := h.svc.Open(c.Request.Context(), id) + switch { + case errors.Is(err, streaming.ErrNotFound): + writeError(c, http.StatusNotFound, "track_not_found", "no playable file for this track") + return + case errors.Is(err, streaming.ErrFileMissing): + writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") + return + case errors.Is(err, streaming.ErrUnsafePath): + // A stored path that leaves the library is a data problem, not the + // client's: log it, but say no more than "not available". + _ = c.Error(err) + writeError(c, http.StatusNotFound, "file_missing", "the audio file is not available") + return + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + return + } + defer st.Close() + + hdr := c.Writer.Header() + hdr.Set("Content-Type", st.ContentType) + hdr.Set("X-Content-Type-Options", "nosniff") + // The URL may carry the access token: never leak it through Referer. + hdr.Set("Referrer-Policy", "no-referrer") + hdr.Set("Cache-Control", "private, no-cache") + + http.ServeContent(c.Writer, c.Request, st.Name, st.ModTime, st.File) +} diff --git a/internal/api/http/middleware.go b/internal/api/http/middleware.go index bf03245..05576d1 100644 --- a/internal/api/http/middleware.go +++ b/internal/api/http/middleware.go @@ -32,12 +32,17 @@ func requestLog(logger *zap.Logger) gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() c.Next() - logger.Info("http request", + fields := []zap.Field{ zap.String("method", c.Request.Method), zap.String("path", c.Request.URL.Path), zap.Int("status", c.Writer.Status()), zap.Duration("latency", time.Since(start)), - ) + } + if last := c.Errors.Last(); last != nil { + // Internal errors are hidden from the client; keep them in the log. + fields = append(fields, zap.String("error", last.Error())) + } + logger.Info("http request", fields...) } } @@ -73,12 +78,20 @@ func requireAdmin(users userLookup) gin.HandlerFunc { } } -// requireAuth accepts only "Authorization: Bearer ". -func requireAuth(tokens tokenParser) gin.HandlerFunc { +// requireAuth checks the access token. It reads "Authorization: Bearer "; +// when allowQuery is true and the header is absent it also accepts the +// access_token query parameter. That is for routes loaded by an