Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion documentation/phase3/download-clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +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 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 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 <id> 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)
Expand Down
13 changes: 9 additions & 4 deletions src/lib/integrations/torrent-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ 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 */
/**
* 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])];
}
2 changes: 1 addition & 1 deletion src/lib/utils/shared-torrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions tests/integrations/qbittorrent-ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ 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']);
});

it.each([['too short', B32.slice(1)], ['not base32 (0, 1, 8, 9)', '0'.repeat(32)], ['hex', HEX]])('rejects %s', (_label, value) => {
expect(base32ToHex(value)).toBeNull();
expect(normalizeTorrentId(value)).toBe(value);
});

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(), B32]);
});
});

describe('magnetHashes', () => {
Expand Down
12 changes: 12 additions & 0 deletions tests/integrations/torrent-client-ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ describe('Deluge', () => {
expect(fakeD.session.size).toBe(0);
});

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();
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');
Expand Down
8 changes: 6 additions & 2 deletions tests/processors/qbittorrent-hybrid-ownership.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]] },
]);
});
});
71 changes: 71 additions & 0 deletions tests/utils/shared-torrent.test.ts
Original file line number Diff line number Diff line change
@@ -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> = {}): 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([]);
});
});
Loading