From 361aece639b355a7fa42bcc20d2df772ee016499 Mon Sep 17 00:00:00 2001 From: kikootwo Date: Tue, 27 Oct 2026 00:34:26 -0400 Subject: [PATCH 1/2] fix: Deluge finds torrents stored under an upper-case hex hash (#460) normalizeTorrentId() now lower-cases a valid 40-hex ID as well as converting base32. A row with no client ID falls back to the indexer's infohash, which can be upper case; Deluge looks torrents up by exact lower-case key, so the seeding cleanup marked such a row removed and request delete left the torrent. torrentHashForms() keeps the stored form. Co-Authored-By: Claude Opus 5.5 --- documentation/phase3/download-clients.md | 3 ++- src/lib/integrations/torrent-ids.ts | 4 ++-- tests/integrations/qbittorrent-ids.test.ts | 5 +++++ tests/integrations/torrent-client-ids.test.ts | 12 ++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/documentation/phase3/download-clients.md b/documentation/phase3/download-clients.md index 12df242c3..8c190d016 100644 --- a/documentation/phase3/download-clients.md +++ b/documentation/phase3/download-clients.md @@ -75,7 +75,8 @@ interface DownloadClientConfig { - **Recorded ID** (`AddDownloadResult.id`): the 40-hex ID the add answer reports, lowercased (Transmission `torrent-added`/`torrent-duplicate` `hashString`; Deluge `core.add_torrent_*` result); a non-hex answer → the computed v1. A reported ID that is none of the computed IDs is still recorded (it is the torrent the add created), with a warn log (`addedTorrentId()`) - **`infoHash`** = v1 on every path (as qBittorrent). `DownloadHistory.torrentHash` = Prowlarr's infohash when the release has one (any case), else this v1 - **Deluge add checks** (pre-check, and the re-check after a rejected add): `core.get_torrent_status [id, ['name']]` per candidate ID, first hit wins: truncated v2 (a hybrid `.torrent`'s `meta version` 2, or a magnet's `btmh`), then v1. Non-hybrids: one call, as before. A `.torrent` add sets label + seed limits on the ID Deluge returns (was the parsed v1) -- **Lookups** send a stored base32 ID as hex (`normalizeTorrentId()`; anything else unchanged): `getDownload`, `probeDownload`, pause, resume, delete, `setCategory` on both clients +- **Lookups** send a stored ID as lower-case hex (`normalizeTorrentId()`: 40-hex lower-cased, base32 converted, anything else unchanged): `getDownload`, `probeDownload`, pause, resume, delete, `setCategory` on both clients (and qBittorrent's lookups). `torrentHashForms()` keeps the stored form too, so the shared-torrent check is unchanged +- Fixed #460: a row with no `downloadClientId` falls back to `torrentHash` (the indexer's infohash, often upper case); Deluge's exact-key `core.get_torrent_status` missed it, so the probe marked the row `removed` and request delete left the torrent. Ownership still decides any remove (`resolveOwnership()`) - **Transmission identity check:** `getTorrentByHash` takes only the `torrent-get` entry whose `hashString` is the requested hash (case-insensitive, `sameHash()` in `torrent-probe.ts`); none → `Torrent not found`. Was `torrents[0]`: a server that ignored `ids` could have RMAB act on another torrent - **Rows stored in base32 before the fix:** found again while the request is active. Admin retry-download calls `getDownload` first, so a failed base32 request resumes monitoring on its original `addedByRmab: true` row (RMAB's own torrent); post-import category, seeding cleanup and request delete can then act on it. Legacy `null` base32 rows become reachable under the legacy window (residual risk in [Torrent Ownership](#torrent-ownership-423)). Ownership is still `resolveOwnership()`; a row already `removed` stays `removed` - Cleanup `deletedHashes` stays keyed by the raw stored ID: a base32 row next to a hex row of the same torrent takes the probe → `missing` path (marks only) diff --git a/src/lib/integrations/torrent-ids.ts b/src/lib/integrations/torrent-ids.ts index ceb10750b..db1379874 100644 --- a/src/lib/integrations/torrent-ids.ts +++ b/src/lib/integrations/torrent-ids.ts @@ -111,9 +111,9 @@ export function addedTorrentId(reported: unknown, hashes: TorrentHashes, warn: ( return id; } -/** A stored base32 hash (any case) as hex; any other value unchanged */ +/** A stored hash as lower-case hex: 40-hex (any case) lower-cased, base32 (any case) converted; any other value unchanged */ export function normalizeTorrentId(id: string): string { - return base32ToHex(id) ?? id; + return validTorrentId(id) ?? base32ToHex(id) ?? id; } /** A stored torrent hash in every form a row may hold it: as given, hex, and lower-case base32 */ diff --git a/tests/integrations/qbittorrent-ids.test.ts b/tests/integrations/qbittorrent-ids.test.ts index 02d27c418..de1ca596e 100644 --- a/tests/integrations/qbittorrent-ids.test.ts +++ b/tests/integrations/qbittorrent-ids.test.ts @@ -49,6 +49,11 @@ describe('base32 hashes', () => { expect(base32ToHex(value)).toBeNull(); expect(normalizeTorrentId(value)).toBe(value); }); + + it('lower-cases a stored upper-case hex hash; the row forms keep it as stored (#460)', () => { + expect(normalizeTorrentId(HEX.toUpperCase())).toBe(HEX); + expect(torrentHashForms(HEX.toUpperCase())).toEqual([HEX.toUpperCase(), HEX, B32.toLowerCase()]); + }); }); describe('magnetHashes', () => { diff --git a/tests/integrations/torrent-client-ids.test.ts b/tests/integrations/torrent-client-ids.test.ts index 03bb64969..9381cf3cf 100644 --- a/tests/integrations/torrent-client-ids.test.ts +++ b/tests/integrations/torrent-client-ids.test.ts @@ -165,6 +165,18 @@ describe('Deluge', () => { expect(fakeD.session.size).toBe(0); }); + it('reaches a row with no client ID whose upper-case indexer hash is the fallback: lookups and removal send lower case (#460)', async () => { + fakeD.session.set(HEX, PLAIN.name); + const d = deluge(); + const stored = HEX.toUpperCase(); + await expect(d.probeDownload(stored)).resolves.toMatchObject({ found: true, info: { id: HEX, name: PLAIN.name } }); + await expect(d.getDownload(stored)).resolves.toMatchObject({ id: HEX, name: PLAIN.name }); + await d.deleteDownload(stored, true); + expect(fakeD.statusIds()).toEqual([HEX, HEX]); + expect(fakeD.callsOf('core.remove_torrent')).toEqual([[HEX, true]]); + expect(fakeD.session.size).toBe(0); + }); + it('finds a hybrid .torrent Deluge holds under its truncated v2 on the pre-check', async () => { serveTorrent(HYBRID); fakeD.session.set(HYBRID.id, 'Theirs'); From ad29387899219db1e265129669d0ed52a843b6fa Mon Sep 17 00:00:00 2001 From: kikootwo Date: Tue, 27 Oct 2026 00:55:28 -0400 Subject: [PATCH 2/2] fix: shared-torrent guard matches hashes in either letter case (#460) torrentHashForms() lists the stored hash as hex and base32 in both lower and upper case, so the guard sees another active request's row for the same torrent whatever case either row stores. It only adds matches, so cleanup and request delete only keep more torrents. Co-Authored-By: Claude Opus 5.5 --- documentation/phase3/download-clients.md | 5 +- src/lib/integrations/torrent-ids.ts | 9 ++- src/lib/utils/shared-torrent.ts | 2 +- tests/integrations/qbittorrent-ids.test.ts | 8 +-- tests/integrations/torrent-client-ids.test.ts | 2 +- .../qbittorrent-hybrid-ownership.test.ts | 8 ++- tests/utils/shared-torrent.test.ts | 71 +++++++++++++++++++ 7 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 tests/utils/shared-torrent.test.ts diff --git a/documentation/phase3/download-clients.md b/documentation/phase3/download-clients.md index 8c190d016..e0e317b4c 100644 --- a/documentation/phase3/download-clients.md +++ b/documentation/phase3/download-clients.md @@ -75,8 +75,9 @@ interface DownloadClientConfig { - **Recorded ID** (`AddDownloadResult.id`): the 40-hex ID the add answer reports, lowercased (Transmission `torrent-added`/`torrent-duplicate` `hashString`; Deluge `core.add_torrent_*` result); a non-hex answer → the computed v1. A reported ID that is none of the computed IDs is still recorded (it is the torrent the add created), with a warn log (`addedTorrentId()`) - **`infoHash`** = v1 on every path (as qBittorrent). `DownloadHistory.torrentHash` = Prowlarr's infohash when the release has one (any case), else this v1 - **Deluge add checks** (pre-check, and the re-check after a rejected add): `core.get_torrent_status [id, ['name']]` per candidate ID, first hit wins: truncated v2 (a hybrid `.torrent`'s `meta version` 2, or a magnet's `btmh`), then v1. Non-hybrids: one call, as before. A `.torrent` add sets label + seed limits on the ID Deluge returns (was the parsed v1) -- **Lookups** send a stored ID as lower-case hex (`normalizeTorrentId()`: 40-hex lower-cased, base32 converted, anything else unchanged): `getDownload`, `probeDownload`, pause, resume, delete, `setCategory` on both clients (and qBittorrent's lookups). `torrentHashForms()` keeps the stored form too, so the shared-torrent check is unchanged -- Fixed #460: a row with no `downloadClientId` falls back to `torrentHash` (the indexer's infohash, often upper case); Deluge's exact-key `core.get_torrent_status` missed it, so the probe marked the row `removed` and request delete left the torrent. Ownership still decides any remove (`resolveOwnership()`) +- **Lookups** send a stored ID as lower-case hex (`normalizeTorrentId()`: 40-hex lower-cased, base32 converted, anything else unchanged): `getDownload`, `probeDownload`, pause, resume, delete, `setCategory` on both clients (and qBittorrent's lookups). `torrentHashForms()` keeps the stored form and adds the others (see the guard line below) +- Fixed #460 (hardening: no write path is known to store a Deluge row without its client ID): a row with no `downloadClientId` falls back to `torrentHash` (the indexer's infohash, often upper case); Deluge's exact-key `core.get_torrent_status` would miss it, so the probe would mark the row `removed` and request delete would leave the torrent. Ownership still decides any remove (`resolveOwnership()`) +- Fixed #460, shared-torrent guard (`otherActiveRequestsUsingTorrent()`, `src/lib/utils/shared-torrent.ts`): its Prisma `in` is case-sensitive, so a lower-case row missed another active request's upper-case row for the same torrent and cleanup or request delete could remove a torrent still in use. `torrentHashForms()` (used only by the guard) now lists the stored form, hex and base32, each in lower and upper case; it only adds matches, so only more torrents are kept - **Transmission identity check:** `getTorrentByHash` takes only the `torrent-get` entry whose `hashString` is the requested hash (case-insensitive, `sameHash()` in `torrent-probe.ts`); none → `Torrent not found`. Was `torrents[0]`: a server that ignored `ids` could have RMAB act on another torrent - **Rows stored in base32 before the fix:** found again while the request is active. Admin retry-download calls `getDownload` first, so a failed base32 request resumes monitoring on its original `addedByRmab: true` row (RMAB's own torrent); post-import category, seeding cleanup and request delete can then act on it. Legacy `null` base32 rows become reachable under the legacy window (residual risk in [Torrent Ownership](#torrent-ownership-423)). Ownership is still `resolveOwnership()`; a row already `removed` stays `removed` - Cleanup `deletedHashes` stays keyed by the raw stored ID: a base32 row next to a hex row of the same torrent takes the probe → `missing` path (marks only) diff --git a/src/lib/integrations/torrent-ids.ts b/src/lib/integrations/torrent-ids.ts index db1379874..70002db5d 100644 --- a/src/lib/integrations/torrent-ids.ts +++ b/src/lib/integrations/torrent-ids.ts @@ -116,8 +116,13 @@ export function normalizeTorrentId(id: string): string { return validTorrentId(id) ?? base32ToHex(id) ?? id; } -/** A stored torrent hash in every form a row may hold it: as given, hex, and lower-case base32 */ +/** + * A stored torrent hash in every form a row may hold it: as given, then hex and base32, each in lower and + * upper case (the indexer's infohash is stored in whatever case it came in). Only adds matches. + */ export function torrentHashForms(hash: string): string[] { const hex = validTorrentId(normalizeTorrentId(hash)); - return [...new Set([hash, ...(hex ? [hex, hexToBase32(hex).toLowerCase()] : [])])]; + if (!hex) return [hash]; + const base32 = hexToBase32(hex); + return [...new Set([hash, hex, hex.toUpperCase(), base32.toLowerCase(), base32])]; } diff --git a/src/lib/utils/shared-torrent.ts b/src/lib/utils/shared-torrent.ts index 19f289dd8..0d7e05d1c 100644 --- a/src/lib/utils/shared-torrent.ts +++ b/src/lib/utils/shared-torrent.ts @@ -18,7 +18,7 @@ export interface ActiveRequestRef { /** * Active (not deleted) requests other than `requestId` whose selected - * download is the torrent `torrentHash` (hex and base32 forms match each other). Empty when there is no hash. + * download is the torrent `torrentHash` (hex and base32 forms, in either letter case, match each other). Empty when there is no hash. */ export async function otherActiveRequestsUsingTorrent( requestId: string, diff --git a/tests/integrations/qbittorrent-ids.test.ts b/tests/integrations/qbittorrent-ids.test.ts index de1ca596e..c684476c2 100644 --- a/tests/integrations/qbittorrent-ids.test.ts +++ b/tests/integrations/qbittorrent-ids.test.ts @@ -40,8 +40,8 @@ describe('base32 hashes', () => { it('encodes hex as base32 and lists every form a row may hold', () => { expect(hexToBase32(HEX)).toBe(B32); expect(hexToBase32('b6692ea5df920cad691c20319a6fffd7a4a766b8')).toBe('WZUS5JO7SIGK22I4EAYZU37726SKOZVY'); - expect(torrentHashForms(B32)).toEqual([B32, HEX, B32.toLowerCase()]); - expect(torrentHashForms(HEX)).toEqual([HEX, B32.toLowerCase()]); + expect(torrentHashForms(B32)).toEqual([B32, HEX, HEX.toUpperCase(), B32.toLowerCase()]); + expect(torrentHashForms(HEX)).toEqual([HEX, HEX.toUpperCase(), B32.toLowerCase(), B32]); expect(torrentHashForms('hash-1')).toEqual(['hash-1']); }); @@ -50,9 +50,9 @@ describe('base32 hashes', () => { expect(normalizeTorrentId(value)).toBe(value); }); - it('lower-cases a stored upper-case hex hash; the row forms keep it as stored (#460)', () => { + it('lower-cases a stored upper-case hex hash; the row forms list it in both cases (#460)', () => { expect(normalizeTorrentId(HEX.toUpperCase())).toBe(HEX); - expect(torrentHashForms(HEX.toUpperCase())).toEqual([HEX.toUpperCase(), HEX, B32.toLowerCase()]); + expect(torrentHashForms(HEX.toUpperCase())).toEqual([HEX.toUpperCase(), HEX, B32.toLowerCase(), B32]); }); }); diff --git a/tests/integrations/torrent-client-ids.test.ts b/tests/integrations/torrent-client-ids.test.ts index 9381cf3cf..3204013f9 100644 --- a/tests/integrations/torrent-client-ids.test.ts +++ b/tests/integrations/torrent-client-ids.test.ts @@ -165,7 +165,7 @@ describe('Deluge', () => { expect(fakeD.session.size).toBe(0); }); - it('reaches a row with no client ID whose upper-case indexer hash is the fallback: lookups and removal send lower case (#460)', async () => { + it('finds and removes a torrent by an upper-case stored hash (the fallback a row with no client ID uses), sending lower case (#460)', async () => { fakeD.session.set(HEX, PLAIN.name); const d = deluge(); const stored = HEX.toUpperCase(); diff --git a/tests/processors/qbittorrent-hybrid-ownership.test.ts b/tests/processors/qbittorrent-hybrid-ownership.test.ts index 43d621151..97d850b89 100644 --- a/tests/processors/qbittorrent-hybrid-ownership.test.ts +++ b/tests/processors/qbittorrent-hybrid-ownership.test.ts @@ -167,11 +167,15 @@ describe('a v1.2.11 row for a duplicate of the user\'s hybrid', () => { }); describe('shared-torrent guard', () => { - it('matches a row stored by a base32 or hex hash under both forms', async () => { + it('matches a row stored by a base32 or hex hash under both forms, in either case', async () => { const { otherActiveRequestsUsingTorrent } = await import('@/lib/utils/shared-torrent'); await otherActiveRequestsUsingTorrent('req-1', PLAIN.b32.toLowerCase()); await otherActiveRequestsUsingTorrent('req-1', PLAIN.v1); const filters = prismaMock.request.findMany.mock.calls.map(([args]) => args.where.downloadHistory.some.torrentHash); - expect(filters).toEqual([{ in: [PLAIN.b32.toLowerCase(), PLAIN.v1] }, { in: [PLAIN.v1, PLAIN.b32.toLowerCase()] }]); + const upper = [PLAIN.v1.toUpperCase(), PLAIN.b32.toUpperCase()]; + expect(filters).toEqual([ + { in: [PLAIN.b32.toLowerCase(), PLAIN.v1, upper[0], upper[1]] }, + { in: [PLAIN.v1, upper[0], PLAIN.b32.toLowerCase(), upper[1]] }, + ]); }); }); diff --git a/tests/utils/shared-torrent.test.ts b/tests/utils/shared-torrent.test.ts new file mode 100644 index 000000000..926d07d95 --- /dev/null +++ b/tests/utils/shared-torrent.test.ts @@ -0,0 +1,71 @@ +/** + * Component: Shared Torrent Guard Tests (#460) + * Documentation: documentation/admin-features/request-deletion.md + * + * The guard must see another active request's row for the same torrent whatever letter case + * either row stores the hash in. `findMany` here evaluates the guard's `where` over fixture rows + * with PostgreSQL's default case-sensitive `in`. + */ + +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { createPrismaMock } from '../helpers/prisma'; + +const prismaMock = createPrismaMock(); +vi.mock('@/lib/db', () => ({ prisma: prismaMock })); + +async function otherActiveRequestsUsingTorrent(requestId: string, hash: string | null) { + const guard = await import('@/lib/utils/shared-torrent'); + return guard.otherActiveRequestsUsingTorrent(requestId, hash); +} + +const HEX = '0123456789abcdef0123456789abcdef01234567'; +const B32 = 'AERUKZ4JVPG66AJDIVTYTK6N54ASGRLH'; + +interface FixtureRequest { + id: string; + status: string; + deletedAt: Date | null; + downloadHistory: { torrentHash: string; selected: boolean }[]; +} + +let requests: FixtureRequest[] = []; + +beforeEach(() => { + requests = []; + prismaMock.request.findMany.mockImplementation(async ({ where }) => { + const { torrentHash, selected } = where.downloadHistory.some; + return requests + .filter((r) => r.id !== where.id.not && r.deletedAt === where.deletedAt) + .filter((r) => r.downloadHistory.some((h) => h.selected === selected && torrentHash.in.includes(h.torrentHash))) + .map(({ id, status }) => ({ id, status })); + }); +}); + +function request(id: string, torrentHash: string, fields: Partial = {}): FixtureRequest { + return { id, status: 'downloaded', deletedAt: null, downloadHistory: [{ torrentHash, selected: true }], ...fields }; +} + +describe('otherActiveRequestsUsingTorrent', () => { + it.each([ + ['lower-case hex', 'upper-case hex', HEX, HEX.toUpperCase()], + ['upper-case hex', 'lower-case hex', HEX.toUpperCase(), HEX], + ['lower-case hex', 'upper-case base32', HEX, B32], + ['lower-case hex', 'lower-case base32', HEX, B32.toLowerCase()], + ['upper-case base32', 'lower-case base32', B32, B32.toLowerCase()], + ['lower-case base32', 'upper-case hex', B32.toLowerCase(), HEX.toUpperCase()], + ])('a row stored as %s sees an active request whose row is %s', async (_a, _b, hashA, hashB) => { + requests = [request('req-a', hashA, { deletedAt: new Date() }), request('req-b', hashB)]; + await expect(otherActiveRequestsUsingTorrent('req-a', hashA)).resolves.toEqual([{ id: 'req-b', status: 'downloaded' }]); + }); + + it('still ignores deleted requests, unselected rows, other torrents and the request itself', async () => { + requests = [ + request('req-a', HEX), + request('req-deleted', HEX.toUpperCase(), { deletedAt: new Date() }), + request('req-unselected', HEX.toUpperCase(), { downloadHistory: [{ torrentHash: HEX.toUpperCase(), selected: false }] }), + request('req-other', 'f'.repeat(40)), + ]; + await expect(otherActiveRequestsUsingTorrent('req-a', HEX)).resolves.toEqual([]); + await expect(otherActiveRequestsUsingTorrent('req-a', null)).resolves.toEqual([]); + }); +});