diff --git a/internal/api/http/auth_test.go b/internal/api/http/auth_test.go index 4fe65d2..7dc9eda 100644 --- a/internal/api/http/auth_test.go +++ b/internal/api/http/auth_test.go @@ -2,6 +2,7 @@ package httpapi import ( "net/http" + "net/http/httptest" "strconv" "testing" ) @@ -320,3 +321,34 @@ func TestAdminCanStartScan(t *testing.T) { t.Errorf("starts = %d", scan.starts) } } + +// Only "Authorization: Bearer " is accepted. A bare token must not pass. +func TestAuthorizationHeaderMustUseTheBearerScheme(t *testing.T) { + h := newTestAPI(t) + tok := registerAndLogin(t, h) + + for name, header := range map[string]string{ + "bare token": tok, + "basic scheme": "Basic " + tok, + "lowercase": "bearer " + tok, + "token scheme": "Token " + tok, + "empty bearer": "Bearer ", + "bearer no gap": "Bearer" + tok, + } { + req := httptest.NewRequest("GET", "/api/v1/auth/me", nil) + req.Header.Set("Authorization", header) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Errorf("%s: status %d, want 401", name, rec.Code) + } + } + + req := httptest.NewRequest("GET", "/api/v1/auth/me", nil) + req.Header.Set("Authorization", "Bearer "+tok) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Errorf("proper Bearer header: %d", rec.Code) + } +} diff --git a/internal/api/http/middleware.go b/internal/api/http/middleware.go index 05576d1..2175df9 100644 --- a/internal/api/http/middleware.go +++ b/internal/api/http/middleware.go @@ -87,7 +87,12 @@ func requireAuth(tokens tokenParser, allowQuery bool) gin.HandlerFunc { return func(c *gin.Context) { token := "" if header := c.GetHeader("Authorization"); header != "" { - token, _ = strings.CutPrefix(header, "Bearer ") + var ok bool + if token, ok = strings.CutPrefix(header, "Bearer "); !ok { + // Any other scheme, or a bare token, is not accepted. + writeError(c, http.StatusUnauthorized, "unauthorized", "missing bearer token") + return + } } else if allowQuery { token = c.Query("access_token") } diff --git a/internal/api/http/stream_test.go b/internal/api/http/stream_test.go index 91d1626..3999db4 100644 --- a/internal/api/http/stream_test.go +++ b/internal/api/http/stream_test.go @@ -83,6 +83,17 @@ func TestStreamRequiresAuth(t *testing.T) { } } +// A header that is present but not a Bearer token is not rescued by the query. +func TestStreamIgnoresQueryTokenWhenAnyAuthorizationHeaderIsPresent(t *testing.T) { + f := newStreamFixture(t) + for _, header := range []string{f.token, "Basic " + f.token, "Token x"} { + rec := f.get(t, "GET", "/api/v1/tracks/1/stream?access_token="+f.token, map[string]string{"Authorization": header}) + if rec.Code != http.StatusUnauthorized { + t.Errorf("Authorization %.12q... with a valid query token: %d, want 401", header, rec.Code) + } + } +} + func TestStreamAcceptsHeaderAndQueryToken(t *testing.T) { f := newStreamFixture(t) if rec := f.get(t, "GET", "/api/v1/tracks/1/stream", f.auth()); rec.Code != http.StatusOK { diff --git a/internal/config/config.go b/internal/config/config.go index cfd2b5c..4eb758d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "time" "github.com/pelletier/go-toml/v2" @@ -172,6 +173,12 @@ func (c Config) Validate() error { errs = append(errs, errors.New("library.roots needs at least one directory")) } for _, root := range c.Library.Roots { + if !filepath.IsAbs(root) { + // Roots are stored with every indexed file and used to serve it: + // a relative one would depend on the directory sondad starts in. + errs = append(errs, fmt.Errorf("library.roots: %q must be an absolute path", root)) + continue + } info, err := os.Stat(root) switch { case err != nil: diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 46b00a1..e26c24b 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -127,6 +127,7 @@ func TestValidate(t *testing.T) { {"ok", func(c *Config) {}, ""}, {"no roots", func(c *Config) { c.Library.Roots = nil }, "library.roots"}, {"root missing", func(c *Config) { c.Library.Roots = []string{filepath.Join(good, "nope")} }, "library.roots"}, + {"relative root", func(c *Config) { c.Library.Roots = []string{"music"} }, "absolute"}, {"root is file", func(c *Config) { c.Library.Roots = []string{file} }, "not a directory"}, {"bad env", func(c *Config) { c.Log.Env = "staging" }, "log.env"}, {"empty listen", func(c *Config) { c.Server.Listen = "" }, "server.listen"}, diff --git a/internal/streaming/fifo_unix_test.go b/internal/streaming/fifo_unix_test.go new file mode 100644 index 0000000..8edfb60 --- /dev/null +++ b/internal/streaming/fifo_unix_test.go @@ -0,0 +1,52 @@ +//go:build unix + +package streaming + +import ( + "context" + "errors" + "os" + "path/filepath" + "syscall" + "testing" + "time" +) + +// Opening a named pipe blocks forever until a writer shows up, so a FIFO must +// be refused without ever blocking the request. +func TestOpenRefusesFIFOsWithoutHanging(t *testing.T) { + root := t.TempDir() + if err := syscall.Mkfifo(filepath.Join(root, "pipe.mp3"), 0o644); err != nil { + t.Skip("cannot create a FIFO:", err) + } + if err := os.Symlink("pipe.mp3", filepath.Join(root, "pipelink.mp3")); err != nil { + t.Skip("symlinks not supported:", err) + } + + for _, rel := range []string{"pipe.mp3", "pipelink.mp3"} { + s := service(root, map[int64]File{1: {Root: root, RelPath: rel}}) + + type result struct { + st *Stream + err error + } + done := make(chan result, 1) + go func() { + st, err := s.Open(context.Background(), 1) + done <- result{st, err} + }() + + select { + case r := <-done: + if r.err == nil { + r.st.Close() + t.Fatalf("%s: a FIFO was opened as a stream", rel) + } + if !errors.Is(r.err, ErrUnsafePath) { + t.Errorf("%s: err = %v, want ErrUnsafePath", rel, r.err) + } + case <-time.After(3 * time.Second): + t.Fatalf("%s: Open blocked on a FIFO", rel) + } + } +} diff --git a/internal/streaming/open_other.go b/internal/streaming/open_other.go new file mode 100644 index 0000000..75578a3 --- /dev/null +++ b/internal/streaming/open_other.go @@ -0,0 +1,5 @@ +//go:build !unix + +package streaming + +const nonBlock = 0 diff --git a/internal/streaming/open_unix.go b/internal/streaming/open_unix.go new file mode 100644 index 0000000..ac6f3eb --- /dev/null +++ b/internal/streaming/open_unix.go @@ -0,0 +1,9 @@ +//go:build unix + +package streaming + +import "syscall" + +// nonBlock makes open(2) on a FIFO return immediately instead of waiting for a +// writer. It changes nothing for regular files. +const nonBlock = syscall.O_NONBLOCK diff --git a/internal/streaming/streaming.go b/internal/streaming/streaming.go index 1b2e3ce..26b1567 100644 --- a/internal/streaming/streaming.go +++ b/internal/streaming/streaming.go @@ -1,8 +1,9 @@ // Package streaming opens audio files for playback. // -// The only job here is to turn a track id into an open, verified file. Every -// path is checked against the configured library roots before it is opened, so -// a bad database row or a swapped symlink can never expose files outside them. +// The only job here is to turn a track id into an open, verified file. The +// stored path is treated as untrusted: it must belong to a configured library +// root, and the file is opened through os.Root so that nothing, not even a +// symlink swapped in at the last moment, can lead outside that root. package streaming import ( @@ -51,7 +52,7 @@ func (s *Stream) Close() error { return s.File.Close() } // Service opens tracks for playback. type Service struct { source Source - roots []string // cleaned, absolute + roots []string // cleaned; config validation requires them to be absolute } // NewService builds a Service. Only files under the given roots can be opened. @@ -65,23 +66,42 @@ func NewService(source Source, roots []string) *Service { // Open returns the audio file of a track, after verifying that it is a regular // file inside one of the configured roots. +// +// The file is opened through an os.Root, which refuses to leave the root at +// open time: a symlink swapped in after any earlier check cannot escape, and +// there is no gap between "check the path" and "open the file". func (s *Service) Open(ctx context.Context, trackID int64) (*Stream, error) { f, err := s.source.PlayableFile(ctx, trackID) if err != nil { return nil, err } - path, err := s.resolve(f) + rel, err := s.validate(f) if err != nil { return nil, err } - file, err := os.Open(path) + root, err := os.OpenRoot(filepath.Clean(f.Root)) if errors.Is(err, fs.ErrNotExist) { - return nil, ErrFileMissing + return nil, ErrFileMissing // volume not mounted } if err != nil { + return nil, fmt.Errorf("open library root: %w", err) + } + defer root.Close() // files already opened stay valid + + // O_NONBLOCK so that a FIFO cannot hang the request: opening one would + // otherwise block until a writer appears. + file, err := root.OpenFile(rel, os.O_RDONLY|nonBlock, 0) + switch { + case errors.Is(err, fs.ErrNotExist): + return nil, ErrFileMissing + case err != nil && strings.Contains(err.Error(), "path escapes from parent"): + // os.Root has no exported error for this case. + return nil, fmt.Errorf("%w: symlink leaves the library", ErrUnsafePath) + case err != nil: return nil, fmt.Errorf("open audio file: %w", err) } + // Check what was actually opened, not what the path looked like earlier. info, err := file.Stat() if err != nil { @@ -95,15 +115,15 @@ func (s *Service) Open(ctx context.Context, trackID int64) (*Stream, error) { return &Stream{ File: file, - Name: filepath.Base(path), + Name: filepath.Base(rel), ModTime: info.ModTime(), - ContentType: contentType(path), + ContentType: contentType(rel), }, nil } -// resolve returns the real path of f, or ErrUnsafePath if it is not strictly -// inside a configured root once every symlink has been followed. -func (s *Service) resolve(f File) (string, error) { +// validate checks the stored root and relative path and returns the cleaned +// relative path to open under the root. +func (s *Service) validate(f File) (string, error) { root := filepath.Clean(f.Root) if !filepath.IsAbs(root) || !s.allowed(root) { return "", fmt.Errorf("%w: root is not a configured library root", ErrUnsafePath) @@ -117,27 +137,7 @@ func (s *Service) resolve(f File) (string, error) { if rel == "." || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return "", fmt.Errorf("%w: path escapes the root", ErrUnsafePath) } - - // The root itself may be a symlink (for example /srv/music -> /mnt/nas), - // so compare real paths on both sides. - realRoot, err := filepath.EvalSymlinks(root) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return "", ErrFileMissing // volume not mounted - } - return "", fmt.Errorf("resolve root: %w", err) - } - realFull, err := filepath.EvalSymlinks(filepath.Join(root, rel)) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return "", ErrFileMissing - } - return "", fmt.Errorf("resolve path: %w", err) - } - if !strings.HasPrefix(realFull, realRoot+string(filepath.Separator)) { - return "", fmt.Errorf("%w: symlink leaves the library", ErrUnsafePath) - } - return realFull, nil + return rel, nil } func (s *Service) allowed(root string) bool { diff --git a/internal/streaming/streaming_test.go b/internal/streaming/streaming_test.go index 51c1c3f..18f22be 100644 --- a/internal/streaming/streaming_test.go +++ b/internal/streaming/streaming_test.go @@ -168,7 +168,24 @@ func TestOpenRejectsSymlinkEscapingTheRoot(t *testing.T) { } } -func TestOpenAllowsSymlinkThatStaysInsideTheRoot(t *testing.T) { +func TestOpenAllowsRelativeSymlinkThatStaysInsideTheRoot(t *testing.T) { + root := t.TempDir() + write(t, filepath.Join(root, "real/song.mp3"), "ok") + if err := os.Symlink("real", filepath.Join(root, "alias")); err != nil { + t.Skip("symlinks not supported:", err) + } + s := service(root, map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) + st, err := s.Open(context.Background(), 1) + if err != nil { + t.Fatalf("relative symlink inside the root rejected: %v", err) + } + st.Close() +} + +// os.Root refuses absolute symlink targets even when they point back inside the +// root. The scanner never indexes through a symlink, so this only costs files +// that were linked in after the scan, and it keeps the rule simple and safe. +func TestOpenRefusesAbsoluteSymlinksEvenInsideTheRoot(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "real/song.mp3"), "ok") if err := os.Symlink(filepath.Join(root, "real"), filepath.Join(root, "alias")); err != nil { @@ -176,10 +193,13 @@ func TestOpenAllowsSymlinkThatStaysInsideTheRoot(t *testing.T) { } s := service(root, map[int64]File{1: {Root: root, RelPath: "alias/song.mp3"}}) st, err := s.Open(context.Background(), 1) - if err != nil { - t.Fatalf("symlink inside the root rejected: %v", err) + if err == nil { + st.Close() + t.Fatal("an absolute symlink was followed") + } + if !errors.Is(err, ErrUnsafePath) { + t.Errorf("err = %v, want ErrUnsafePath", err) } - st.Close() } func TestOpenAcceptsASymlinkedRoot(t *testing.T) {