Vellum/internal/jellyfin/items.go
mithral ac1bd04df3 Phase 1: one item-access seam, and architecture rules that fail the build
Motivation, from the evidence rather than from taste. Five packages each
implemented "load an item, then check the caller may read it": media, comics,
games, jellyfin (with its own dbItem type and its own SQL), and subsonic. Five
copies of one authorization rule means four can drift, and the failure mode is
not a crash — it is silently serving somebody a library they were never granted.
One implementation is one thing to review.

internal/catalog now owns item and file access, and the access check.

  LoadUnchecked(ctx, db, id)        -- no access check; jobs and tests only
  LoadFor(ctx, db, u, id)           -- load AND enforce the grant, in one call
  Files / FilesFor / FileByID / FirstFile / FileCount

The unchecked variant is deliberately named so that reaching for it by accident
is hard: it is the dangerous call, so it should not have the friendlier name.
This came from the interface-design rule that a good interface makes the right
thing easy and the wrong thing hard.

The security property is tested, not asserted: a forbidden item and an absent
item return byte-identical errors, so no handler can leak the existence of
another user's library through a 403. That is now enforced in one place.

Migrated all five surfaces onto it. media, comics, games and ebooks went from
four duplicate loaders to zero. jellyfin's dbItem and loadDBItem are gone.
subsonic's per-item authorization moved while its album/artist aggregate
projections stayed put, because grouping items into albums is a presentation
concern, not a data-access one. The remaining raw SQL everywhere is browse and
aggregate queries; no per-item load decides access any more.

internal/arch enforces the layering instead of documenting it. Five rules, each
proven able to fail by deliberately breaking it:
  - no upward imports across six explicit layers
  - internal/core may depend only on migrations
  - internal/catalog must not know about HTTP, or it stops being reusable
  - modules must not import each other
  - every package must be placed in a layer, so the map cannot go stale

A test that only ever passes is decoration, so each rule was verified by
introducing the violation and confirming the failure.

Verified: gofmt clean, go build ./... exit 0, go vet exit 0, 20 packages pass,
svelte-check 0/0. End to end against a running server: the vellum API serves
item detail with codec facts, an unknown item 404s, Jellyfin AuthenticateByName
mints a token and /Items browses, and Subsonic ping returns status ok. The
subsonic migration was additionally A/B'd byte-identical against the previous
implementation across ~15 endpoints, and Jellyfin/Subsonic both still return the
same error for a forbidden item as for an absent one.

Known follow-ups, recorded rather than silently skipped:
  - internal/jellyfin has no test files at all. Its wire contract is the most
    client-visible surface in the tree and is now verified only by hand.
  - catalog is item-centric, but Subsonic addresses songs file-first (mf-<id>),
    so it resolves file to item through its own projection. A catalog lookup
    that takes a file id would close that.
  - subsonic loadAlbum now scopes the projection to the item's own library
    rather than all visible audio libraries. Equivalent by construction, since
    an item's kind tracks its library's kind, but a hand-edited mismatch would
    differ.
2026-10-11 15:47:18 -04:00

291 lines
8.1 KiB
Go

package jellyfin
import (
"context"
"database/sql"
"errors"
"net/http"
"strings"
"github.com/mithral/vellum/internal/auth"
"github.com/mithral/vellum/internal/catalog"
"github.com/mithral/vellum/internal/httpx"
"github.com/mithral/vellum/internal/library"
"github.com/mithral/vellum/internal/stream"
)
// handleItems is the browse workhorse for GET /Items. The /Users/{userId}/Items
// form delegates here; the token's user is authoritative for scoping.
func (h *Handlers) handleItems(w http.ResponseWriter, r *http.Request, u auth.User) {
ctx := r.Context()
q := r.URL.Query()
libs, err := auth.VisibleLibraryIDs(ctx, h.db, u)
if err != nil {
h.fail(w, "visible libraries", err)
return
}
if libs == nil {
libs = []int64{}
}
startIndex := atoiDefault(q.Get("StartIndex"), 0)
if startIndex < 0 {
startIndex = 0
}
limit := atoiDefault(q.Get("Limit"), defaultLimit)
if limit <= 0 || limit > maxLimit {
limit = maxLimit
}
parentRaw := strings.TrimSpace(q.Get("ParentId"))
recursive := parseBool(q.Get("Recursive"))
search := strings.TrimSpace(q.Get("SearchTerm"))
// Top level: no parent, not recursive, no search. Real clients expect the
// user's views here, so the visible libraries are returned as
// CollectionFolder items to drill into.
if parentRaw == "" && !recursive && search == "" {
h.writeViews(w, ctx, libs, startIndex, limit)
return
}
// A user with no grants gets an empty page, never an error and never
// content from another library.
if len(libs) == 0 {
writeItems(w, startIndex, 0, nil)
return
}
query := listQuery{
userID: u.ID,
libraryIDs: libs,
sortColumn: sortColumn(q.Get("SortBy")),
descending: isDescending(q.Get("SortOrder")),
limit: limit,
offset: startIndex,
searchTerm: search,
filters: splitCSV(q.Get("Filters")),
kinds: kindsForTypes(splitCSV(q.Get("IncludeItemTypes"))),
includeSources: containsFold(splitCSV(q.Get("Fields")), "MediaSources"),
}
if parentRaw != "" {
pid, isLib, ok := decodeID(parentRaw)
if !ok {
// An unknown ParentId is an empty page, not an error: clients probe
// ids and expect to be told there is nothing there.
writeItems(w, startIndex, 0, nil)
return
}
if isLib {
if !containsInt64(libs, pid) {
writeItems(w, startIndex, 0, nil)
return
}
query.libraryIDs = []int64{pid}
} else {
// Children of an item, and only if that item is visible.
visible, err := h.itemVisible(ctx, u, pid)
if err != nil {
h.fail(w, "check parent", err)
return
}
if !visible {
writeItems(w, startIndex, 0, nil)
return
}
query.parentItemID = pid
}
}
items, total, err := h.queryItems(ctx, query)
if err != nil {
h.fail(w, "list items", err)
return
}
apiKey := tokenFromRequest(r)
dtos := make([]BaseItemDto, 0, len(items))
for _, it := range items {
dtos = append(dtos, toBaseItemDto(it, query.includeSources, apiKey))
}
writeItems(w, startIndex, total, dtos)
}
// handleUserItems serves GET /Users/{userId}/Items. The token's user is
// authoritative; a mismatched path id is a 404 (cross-user reads are not
// implemented — see the package notes).
func (h *Handlers) handleUserItems(w http.ResponseWriter, r *http.Request, u auth.User) {
id, _, ok := decodeID(r.PathValue("userId"))
if !ok || id != u.ID {
httpx.WriteError(w, http.StatusNotFound, "user not found")
return
}
h.handleItems(w, r, u)
}
// handleItem serves one BaseItemDto. A library id is answered as a folder so a
// client that treats views as items still works.
func (h *Handlers) handleItem(w http.ResponseWriter, r *http.Request, u auth.User) {
id, isLib, ok := decodeID(r.PathValue("itemId"))
if !ok {
httpx.WriteError(w, http.StatusNotFound, "item not found")
return
}
if isLib {
lib, err := library.Get(r.Context(), h.db, id)
if errors.Is(err, library.ErrNotFound) {
httpx.WriteError(w, http.StatusNotFound, "item not found")
return
}
if err != nil {
h.fail(w, "load library", err)
return
}
allowed, err := auth.CanAccessLibrary(r.Context(), h.db, u, id)
if err != nil {
h.fail(w, "check access", err)
return
}
if !allowed {
httpx.WriteError(w, http.StatusNotFound, "item not found")
return
}
httpx.WriteJSON(w, http.StatusOK, libraryDTO(lib))
return
}
it, ok := h.authorizedItem(w, r, u, id)
if !ok {
return
}
v := itemView{Item: it}
if err := h.attachFile(r.Context(), &v); err != nil {
h.fail(w, "load file", err)
return
}
if err := h.loadOverlay(r.Context(), u.ID, it.ID, &v); err != nil {
if errors.Is(err, sql.ErrNoRows) {
httpx.WriteError(w, http.StatusNotFound, "item not found")
return
}
h.fail(w, "load item state", err)
return
}
httpx.WriteJSON(w, http.StatusOK, toBaseItemDto(v, true, tokenFromRequest(r)))
}
// handleImagePrimary streams the cover bytes the scanner/web already extracted.
//
// It does not trigger extraction: NewHandlers has no cache directory, and the
// extraction path belongs to the web layer. An item whose cover has not been
// extracted yet simply 404s, which clients treat as "no artwork".
func (h *Handlers) handleImagePrimary(w http.ResponseWriter, r *http.Request, u auth.User) {
id, isLib, ok := decodeID(r.PathValue("itemId"))
if !ok || isLib {
httpx.WriteError(w, http.StatusNotFound, "no cover available")
return
}
if _, ok := h.authorizedItem(w, r, u, id); !ok {
return
}
var path string
err := h.db.QueryRowContext(r.Context(),
`SELECT path FROM assets WHERE item_id = ? AND role = 'cover' LIMIT 1`, id).Scan(&path)
if errors.Is(err, sql.ErrNoRows) {
httpx.WriteError(w, http.StatusNotFound, "no cover available")
return
}
if err != nil {
h.fail(w, "load cover", err)
return
}
w.Header().Set("Cache-Control", "public, max-age=86400")
if err := stream.ServeFile(w, r, path); err != nil {
httpx.WriteError(w, http.StatusNotFound, "no cover available")
return
}
}
// authorizedItem loads an item and confirms access, writing a 404 (never a 403,
// so existence is not disclosed) on failure. Both the load and the grant check
// live in catalog.LoadFor, which returns the same ErrNotFound for a missing
// item and an unreadable one.
func (h *Handlers) authorizedItem(w http.ResponseWriter, r *http.Request, u auth.User, id int64) (catalog.Item, bool) {
it, err := catalog.LoadFor(r.Context(), h.db, u, id)
if errors.Is(err, catalog.ErrNotFound) {
httpx.WriteError(w, http.StatusNotFound, "item not found")
return catalog.Item{}, false
}
if err != nil {
h.fail(w, "load item", err)
return catalog.Item{}, false
}
return it, true
}
// writeViews returns the caller's visible libraries as CollectionFolder items,
// paginated like any other browse result.
func (h *Handlers) writeViews(w http.ResponseWriter, ctx context.Context, libs []int64, startIndex, limit int) {
list, err := library.ListVisible(ctx, h.db, libs)
if err != nil {
h.fail(w, "list libraries", err)
return
}
total := len(list)
if startIndex > total {
startIndex = total
}
end := startIndex + limit
if end > total {
end = total
}
items := make([]BaseItemDto, 0, end-startIndex)
for _, lib := range list[startIndex:end] {
items = append(items, libraryDTO(lib))
}
writeItems(w, startIndex, total, items)
}
// libraryDTO renders a library as a Jellyfin folder.
func libraryDTO(lib library.Library) BaseItemDto {
return BaseItemDto{
Id: libraryID(lib.ID),
Name: lib.Name,
Type: "CollectionFolder",
CollectionType: collectionType(lib.Kind),
IsFolder: true,
SortName: lib.Name,
}
}
// collectionType maps a Vellum kind to the Jellyfin collection type, which
// tells a client how to render the folder.
func collectionType(kind string) string {
switch kind {
case "video":
return "movies"
case "audiobooks", "music":
return "music"
case "photos":
return "homevideos"
case "ebooks", "comics":
return "books"
default:
return ""
}
}
func writeItems(w http.ResponseWriter, startIndex, total int, items []BaseItemDto) {
if items == nil {
items = []BaseItemDto{}
}
httpx.WriteJSON(w, http.StatusOK, itemQueryResult{
Items: items,
TotalRecordCount: total,
StartIndex: startIndex,
})
}