From 9ab8bd8e031a70e7cb4f668173cc320b0b7fd09e Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Sat, 26 Sep 2026 17:06:55 +0100 Subject: [PATCH] Fix: never show now-playing when now_playing_mode is off now_playing_field() read core's now_playing table unconditionally, so a user who never turned federation's now-playing feature on at all would still leak what they're currently listening to on their public actor document, since core's now_playing table is populated for every user regardless of federation settings. Found while designing step 3 (the periodic push task): querying only sharing users with now_playing_mode != "off" made me notice build_document() itself had no equivalent guard, and the existing tests happened not to catch it (they only combined "nothing playing" with "mode off", and "something playing" with "mode on"). Added the missing combination - something playing, mode off - to both the actors.py unit tests and the web-level actor route test. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- src/scrobbler/federation/actors.py | 11 +++++++++-- tests/test_federation_actors.py | 15 ++++++++++++++- tests/test_federation_web.py | 11 +++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/scrobbler/federation/actors.py b/src/scrobbler/federation/actors.py index f7d332d..219006d 100644 --- a/src/scrobbler/federation/actors.py +++ b/src/scrobbler/federation/actors.py @@ -15,7 +15,13 @@ NOW_PLAYING_FIELD = "Now playing" def now_playing_field(user) -> list[tuple[str, str]]: """The actor's now-playing `PropertyValue` field. Empty when nothing's playing - (`get_now_playing` already excludes expired tracks).""" + (`get_now_playing` already excludes expired tracks). + + Doesn't look at the user's now_playing_mode: core's now_playing table is populated + for every user regardless of federation settings, so a caller must gate this itself + (build_document does) rather than leak listening activity for users who never turned + now playing on at all. + """ playing = get_now_playing(user) if playing is None: return [] @@ -23,6 +29,7 @@ def now_playing_field(user) -> list[tuple[str, str]]: def build_document(urls: vocab.ActorUrls, user, settings) -> dict: + fields = now_playing_field(user) if settings.now_playing_mode != "off" else [] return vocab.person( urls, username=user.username, @@ -33,5 +40,5 @@ def build_document(urls: vocab.ActorUrls, user, settings) -> dict: discoverable=settings.discoverable, indexable=settings.indexable, published=settings.created_at.isoformat().replace("+00:00", "Z"), - fields=now_playing_field(user), + fields=fields, ) diff --git a/tests/test_federation_actors.py b/tests/test_federation_actors.py index 013a108..9da04d8 100644 --- a/tests/test_federation_actors.py +++ b/tests/test_federation_actors.py @@ -20,7 +20,10 @@ def test_now_playing_field_shows_the_current_track(fed_ctx, user): def test_build_document_includes_the_now_playing_field(fed_ctx, user): - sharing.update(user.id, {"enabled": True, "display_name": "Alice", "bio": "Hi."}) + sharing.update( + user.id, + {"enabled": True, "display_name": "Alice", "bio": "Hi.", "now_playing_mode": "profile"}, + ) update_now_playing(user, TrackInput(artist="Radiohead", track="Reckoner")) settings = sharing.settings_for(user.id) doc = actors.build_document(URLS, user, settings) @@ -30,7 +33,17 @@ def test_build_document_includes_the_now_playing_field(fed_ctx, user): def test_build_document_has_no_now_playing_field_when_idle(fed_ctx, user): + sharing.update(user.id, {"enabled": True, "now_playing_mode": "profile"}) + settings = sharing.settings_for(user.id) + doc = actors.build_document(URLS, user, settings) + assert doc["attachment"] == [] + + +def test_build_document_never_shows_now_playing_when_the_mode_is_off(fed_ctx, user): + """now_playing_mode defaults to off; core's now_playing table is populated for every + user regardless, so a user who's never turned this on must never leak it.""" sharing.update(user.id, {"enabled": True}) + update_now_playing(user, TrackInput(artist="Radiohead", track="Reckoner")) settings = sharing.settings_for(user.id) doc = actors.build_document(URLS, user, settings) assert doc["attachment"] == [] diff --git a/tests/test_federation_web.py b/tests/test_federation_web.py index 32c6d90..f452174 100644 --- a/tests/test_federation_web.py +++ b/tests/test_federation_web.py @@ -72,6 +72,7 @@ def test_actor_document(fed_client, sharing_user): def test_actor_document_shows_now_playing_live(fed_client, fed_ctx, sharing_user): from scrobbler.services.scrobbles import TrackInput, update_now_playing + sharing.update(sharing_user.id, {"now_playing_mode": "profile"}) update_now_playing(sharing_user, TrackInput(artist="Radiohead", track="Reckoner")) doc = fed_client.get("/users/alice", headers=AP).get_json() assert doc["attachment"] == [ @@ -79,6 +80,16 @@ def test_actor_document_shows_now_playing_live(fed_client, fed_ctx, sharing_user ] +def test_actor_document_never_shows_now_playing_when_the_mode_is_off( + fed_client, fed_ctx, sharing_user +): + from scrobbler.services.scrobbles import TrackInput, update_now_playing + + update_now_playing(sharing_user, TrackInput(artist="Radiohead", track="Reckoner")) + doc = fed_client.get("/users/alice", headers=AP).get_json() + assert doc["attachment"] == [] + + def test_ids_come_from_config_not_the_request(fed_client, sharing_user): doc = fed_client.get("/users/alice", headers={**AP, "Host": "evil.example"}).get_json() assert urlsplit(doc["id"]).netloc == "scrobble.test"