From 83e61975c48d985553fb1f606f7ec19665ce94fa Mon Sep 17 00:00:00 2001 From: splitsec2 <35583321+splitsec2@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:49:40 -0600 Subject: [PATCH] fix(releases): apply the record policy to a provider named alongside a source (#1598) Two small things I found while going back over what has landed since 1.5.0. `/api/releases` takes a `provider` and a `source`, and the policy check only looked at `source`. A user whose policy blocks one release source but not another could pass the blocked one as `provider` with the allowed one as `source`, and get the blocked source's record back. The record route already refuses that with a 403, so the listing route now applies the same refusal when `provider` names a source that is not being listed. The normal case, where provider and source are the same, is already gated and skips the extra check. The second commit is wording only. The Automatic Downloads setting and the Hardcover sync doc said "synced requests", but the pass picks up every pending Hardcover request, including ones a user made themselves with Request Book. The download is still queued as the user who asked (the admin is only the reviewer, same as a manual approval), and admins already see every user's requests. The text now says that, and a new test pins the behaviour for a user's own request. I opened a scratch instance and looked at the Hardcover Sync tab: the Automatic Downloads heading and the checkbox description read as intended and nothing else on the tab moved. Full suite, ruff and basedpyright pass, and the new policy test fails without the fix. --- docs/hardcover-sync.md | 2 +- shelfmark/config/hardcover_sync_settings.py | 5 ++-- shelfmark/main.py | 6 ++++ tests/core/test_auto_download.py | 32 +++++++++++++++++++++ tests/core/test_release_routes_policy.py | 31 ++++++++++++++++++++ 5 files changed, 73 insertions(+), 3 deletions(-) diff --git a/docs/hardcover-sync.md b/docs/hardcover-sync.md index dd2b9af2..5a953d3a 100644 --- a/docs/hardcover-sync.md +++ b/docs/hardcover-sync.md @@ -74,7 +74,7 @@ The check **fails closed** here: if an enabled library cannot be read and has no | `HARDCOVER_SYNC_CONTENT_TYPE` | Request as | Audiobooks | `Audiobooks`, `Ebooks`, or `Ebooks and audiobooks` — the last creates one request of each per shelf book, each checked against its own library | | `HARDCOVER_SYNC_INTERVAL` | Sync interval | 6 | Combined with the unit below | | `HARDCOVER_SYNC_INTERVAL_UNIT` | Interval unit | Hours | Minutes or hours. The effective interval never drops below 60 seconds | -| `AUTO_DOWNLOAD_ENABLED` | Enable automatic downloads | Off | Auto-approve and queue strict matches for Hardcover requests | +| `AUTO_DOWNLOAD_ENABLED` | Enable automatic downloads | Off | Auto-approve and queue strict matches for every pending Hardcover request, synced or made by a user with Request Book. The download belongs to the user who asked, and admins see every user's requests | | `AUTO_DOWNLOAD_SOURCE_PRIORITY` | Audiobook source priority | All usable sources | Drag to order; used for audiobook requests. Sources you disable here, or that are not configured, are skipped | | `AUTO_DOWNLOAD_EBOOK_SOURCE_PRIORITY` | Ebook source priority | All usable sources | Same, for ebook requests | | `AUTO_DOWNLOAD_MIN_SEEDERS` | Minimum seeders (torrents) | 1 | Torrent releases below this are ignored | diff --git a/shelfmark/config/hardcover_sync_settings.py b/shelfmark/config/hardcover_sync_settings.py index 3a3647e7..f1367cb3 100644 --- a/shelfmark/config/hardcover_sync_settings.py +++ b/shelfmark/config/hardcover_sync_settings.py @@ -140,7 +140,8 @@ def hardcover_sync_settings() -> list[SettingsField]: key="auto_download_heading", title="Automatic Downloads", description=( - "When enabled, synced requests are auto-approved and downloaded from the " + "When enabled, pending Hardcover requests (synced from a shelf, or made by a " + "user with Request Book) are auto-approved and downloaded from the " "first source in the matching list below that yields a strict title/author/" "format match for the request's content type (ebook formats for ebook " "requests, audiobook formats for audiobook requests). If no confident match " @@ -150,7 +151,7 @@ def hardcover_sync_settings() -> list[SettingsField]: CheckboxField( key="AUTO_DOWNLOAD_ENABLED", label="Enable automatic downloads", - description="Auto-approve and auto-download matching releases for synced requests.", + description="Auto-approve and auto-download matching releases for Hardcover requests.", default=False, ), OrderableListField( diff --git a/shelfmark/main.py b/shelfmark/main.py index 7b3757e9..099951be 100644 --- a/shelfmark/main.py +++ b/shelfmark/main.py @@ -3357,6 +3357,12 @@ def api_releases() -> Response | tuple[Response, int]: # Source-backed browse flows can reopen the release modal with provider=. # In that flow, treat the source-native record as release-search context instead of # requiring a metadata provider registration. + # A source named in `source` was gated above. When provider names another one, + # its record lookup needs the refusal the record route applies. + if normalize_source(provider) not in {normalize_source(s) for s in sources_to_search}: + refused_record_mode = _release_source_record_refusal(provider) + if refused_record_mode is not None: + return _release_listing_block_response(refused_record_mode) source = get_source(provider) direct_record = source.get_record(book_id) if direct_record is None: diff --git a/tests/core/test_auto_download.py b/tests/core/test_auto_download.py index 93d94dfe..64849f85 100644 --- a/tests/core/test_auto_download.py +++ b/tests/core/test_auto_download.py @@ -366,6 +366,38 @@ class TestAutoDownloadPending: assert summary == {"queued": 0, "no_match": 0, "in_library": 0, "skipped": 0, "error": 0} + def test_picks_up_a_users_own_hardcover_request_and_queues_it_as_that_user( + self, user_db, monkeypatch + ): + """Automatic downloads cover every pending Hardcover request, not only synced ones.""" + monkeypatch.setattr(auto_download, "app_config", _Config(AUTO_DOWNLOAD_ENABLED=True)) + monkeypatch.setattr(auto_download, "_configured_source_priority", lambda _ct: ["first"]) + monkeypatch.setattr( + "shelfmark.core.library_index.is_in_library", lambda *_args, **_kwargs: False + ) + admin = user_db.create_user(username="admin", role="admin") + reader = user_db.create_user(username="reader", role="user") + row = _pending_request(user_db, reader["id"]) + _stub_provider(monkeypatch, _book()) + monkeypatch.setattr( + auto_download, + "search_source_releases", + lambda *_a, **_k: (None, [_release(source="first")], None), + ) + queued = [] + + def _queue(release_data, priority, *, user_id, username): + queued.append((user_id, username)) + return True, None + + summary = auto_download.auto_download_pending( + user_db, queue_release=_queue, admin_user_id=admin["id"] + ) + + assert summary["queued"] == 1 + assert queued == [(reader["id"], "reader")] + assert user_db.get_request(row["id"])["user_id"] == reader["id"] + class TestAutoDownloadRequest: @pytest.fixture(autouse=True) diff --git a/tests/core/test_release_routes_policy.py b/tests/core/test_release_routes_policy.py index 3f34e752..68ab146d 100644 --- a/tests/core/test_release_routes_policy.py +++ b/tests/core/test_release_routes_policy.py @@ -170,6 +170,37 @@ class TestReleaseListPolicy: assert resp.json["code"] == "policy_blocked" assert searched == [] + def test_a_blocked_provider_record_is_not_resolved_through_an_allowed_source( + self, client, main_module + ): + _login(client, main_module) + policy = _policy(rules=[{"source": "prowlarr", "content_type": "*", "mode": "blocked"}]) + searched: list[str] = [] + auth_patch, policy_patch = _with_policy(main_module, policy) + with ( + auth_patch, + policy_patch, + patch( + "shelfmark.release_sources.list_available_sources", + return_value=[{"name": "direct_download", "enabled": True}], + ), + patch("shelfmark.release_sources.get_source", return_value=_Source(searched)), + patch("shelfmark.release_sources.source_results_are_releases", return_value=True), + ): + resp = client.get( + "/api/releases", + query_string={ + "provider": "prowlarr", + "book_id": "rec-1", + "source": "direct_download", + "title": "Dune", + }, + ) + + assert resp.status_code == 403 + assert resp.json["code"] == "policy_blocked" + assert searched == [] + def test_admin_is_not_gated(self, client, main_module): _login(client, main_module, is_admin=True)