From 4c0bd9df04b173fb5c0d631d96848ec13f55fcbe Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Thu, 26 Nov 2026 23:52:37 -0500 Subject: [PATCH] fix(subsonic): a scrobble's time is bounded as a native play's is (#8076) The native events endpoint clamps a client's play time to between the account's creation and a few minutes past now. A Subsonic scrobble took its time as sent, so an app with a wrong clock dated plays years out, onto the wrong day in History and into Rediscover's ordering. The clamp moves to playevents.ClampClientTime and both surfaces use it. Co-Authored-By: Claude Opus 5.5 --- internal/api/events.go | 23 ++-------------------- internal/playevents/clienttime.go | 24 +++++++++++++++++++++++ internal/playevents/clienttime_test.go | 25 ++++++++++++++++++++++++ internal/subsonic/scrobble_test.go | 27 ++++++++++++++++++++++++++ internal/subsonic/stream.go | 3 ++- 5 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 internal/playevents/clienttime.go create mode 100644 internal/playevents/clienttime_test.go diff --git a/internal/api/events.go b/internal/api/events.go index 5946d78b..d81d7d22 100644 --- a/internal/api/events.go +++ b/internal/api/events.go @@ -11,6 +11,7 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" + "git.fabledsword.com/bvandeusen/minstrel/internal/playevents" ) type eventRequest struct { @@ -59,26 +60,6 @@ type playStartedResponse struct { SessionID string `json:"session_id"` } -// eventFutureSkew is the tolerated client-clock drift into the future -// before a timestamp is treated as bogus and replaced with now. -const eventFutureSkew = 5 * time.Minute - -// clampEventTime bounds a client-supplied event timestamp to sanity: -// no earlier than the account's creation (offline replays can -// legitimately be days old, but no play can predate the user) and no -// later than now + a small skew allowance. An unbounded client clock would -// let a skewed device write arbitrarily old plays, poisoning Rediscover's -// "not played in 6 months" ordering. -func clampEventTime(at, userCreatedAt, now time.Time) time.Time { - if at.Before(userCreatedAt) { - return userCreatedAt - } - if at.After(now.Add(eventFutureSkew)) { - return now - } - return at -} - type okResponse struct { OK bool `json:"ok"` } @@ -100,7 +81,7 @@ func (h *handlers) handleEvents(w http.ResponseWriter, r *http.Request) { writeErr(w, apierror.BadRequest("bad_request", "invalid `at` timestamp")) return } - at = clampEventTime(parsed.UTC(), user.CreatedAt.Time, time.Now().UTC()) + at = playevents.ClampClientTime(parsed.UTC(), user.CreatedAt.Time, time.Now().UTC()) } clientID := "" if req.ClientID != nil { diff --git a/internal/playevents/clienttime.go b/internal/playevents/clienttime.go new file mode 100644 index 00000000..b1292df4 --- /dev/null +++ b/internal/playevents/clienttime.go @@ -0,0 +1,24 @@ +package playevents + +import "time" + +// clientClockFutureSkew is how far into the future a client's clock may run +// before its timestamp is treated as bogus and replaced with now. +const clientClockFutureSkew = 5 * time.Minute + +// ClampClientTime bounds a play time a client supplied, on any surface (the +// native apps' events and a Subsonic app's scrobble): no earlier than the +// account's creation, since offline replays can be days old but no play +// predates the listener, and no later than now plus a small allowance for +// clock drift. An unbounded client clock would date plays anywhere, landing +// them on the wrong day in History and poisoning Rediscover's "not played in +// six months" ordering. +func ClampClientTime(at, userCreatedAt, now time.Time) time.Time { + if at.Before(userCreatedAt) { + return userCreatedAt + } + if at.After(now.Add(clientClockFutureSkew)) { + return now + } + return at +} diff --git a/internal/playevents/clienttime_test.go b/internal/playevents/clienttime_test.go new file mode 100644 index 00000000..b66a87c3 --- /dev/null +++ b/internal/playevents/clienttime_test.go @@ -0,0 +1,25 @@ +package playevents + +import ( + "testing" + "time" +) + +func TestClampClientTime(t *testing.T) { + created := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + now := time.Date(2026, 11, 26, 12, 0, 0, 0, time.UTC) + for _, c := range []struct { + name string + at time.Time + expect time.Time + }{ + {"before the account existed", created.AddDate(-3, 0, 0), created}, + {"an offline replay days old", now.AddDate(0, 0, -4), now.AddDate(0, 0, -4)}, + {"a clock a little fast", now.Add(4 * time.Minute), now.Add(4 * time.Minute)}, + {"a clock years ahead", now.AddDate(5, 0, 0), now}, + } { + if got := ClampClientTime(c.at, created, now); !got.Equal(c.expect) { + t.Errorf("%s: got %v, want %v", c.name, got, c.expect) + } + } +} diff --git a/internal/subsonic/scrobble_test.go b/internal/subsonic/scrobble_test.go index 1a010c28..11e38e62 100644 --- a/internal/subsonic/scrobble_test.go +++ b/internal/subsonic/scrobble_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strconv" "testing" "time" @@ -98,3 +99,29 @@ func TestHandleScrobble_SubmissionTrueWritesCompletedPlay(t *testing.T) { t.Errorf("closed play_events = %d, want 1", closed) } } + +// A scrobble's time is bounded like a native play's: an app whose clock is +// years out can't date the play years out. +func TestHandleScrobble_AWildClockIsBoundedToNow(t *testing.T) { + pool, user, track := testScrobblePool(t) + m := newScrobbleHandlers(pool) + + q := url.Values{} + q.Set("id", uuidToID(track.ID)) + q.Set("submission", "true") + q.Set("time", strconv.FormatInt(time.Now().AddDate(5, 0, 0).UnixMilli(), 10)) + req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil) + w := httptest.NewRecorder() + m.handleScrobble(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user))) + if w.Code != http.StatusOK { + t.Fatalf("status = %d", w.Code) + } + var startedAt time.Time + if err := pool.QueryRow(context.Background(), + "SELECT started_at FROM play_events WHERE user_id=$1", user.ID).Scan(&startedAt); err != nil { + t.Fatalf("read play: %v", err) + } + if startedAt.After(time.Now().Add(time.Minute)) { + t.Fatalf("play dated %v, want no later than now", startedAt) + } +} diff --git a/internal/subsonic/stream.go b/internal/subsonic/stream.go index 7e445a92..05af691d 100644 --- a/internal/subsonic/stream.go +++ b/internal/subsonic/stream.go @@ -192,8 +192,9 @@ func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) { if ms, err := strconv.ParseInt(t, 10, 64); err == nil { at = time.UnixMilli(ms).UTC() } else if parsed, err := time.Parse(time.RFC3339, t); err == nil { - at = parsed + at = parsed.UTC() } + at = playevents.ClampClientTime(at, user.CreatedAt.Time, time.Now().UTC()) } clientID := params.Get("c") source, err := m.recall.sourceFor(r.Context(), dbq.New(m.pool), user.ID, clientID, trackID)