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
22 changes: 22 additions & 0 deletions src/app/api/search/torrents/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ describe('Torrent Search API', () => {
expect(data.error).toBe('Invalid limit. Must be a number.');
});

it('should return 400 for partial, fractional, or unsafe integer limits', async () => {
for (const limit of ['10abc', '1.5', '9007199254740992']) {
const request = createRequest({ q: 'test', limit });
const response = await GET(request);

expect(response.status).toBe(400);
const data = await response.json();
expect(data.error).toBe('Invalid limit. Must be a number.');
}
});

it('should return 400 if limit exceeds maximum', async () => {
const request = createRequest({ q: 'test', limit: '200' });
const response = await GET(request);
Expand Down Expand Up @@ -94,6 +105,17 @@ describe('Torrent Search API', () => {
expect(data.error).toBe('Invalid offset. Must be a number.');
});

it('should return 400 for partial, fractional, or unsafe integer offsets', async () => {
for (const offset of ['10abc', '1.5', '9007199254740992']) {
const request = createRequest({ q: 'test', offset });
const response = await GET(request);

expect(response.status).toBe(400);
const data = await response.json();
expect(data.error).toBe('Invalid offset. Must be a number.');
}
});

it('should return 400 for negative offset', async () => {
const request = createRequest({ q: 'test', offset: '-1' });
const response = await GET(request);
Expand Down
17 changes: 13 additions & 4 deletions src/app/api/search/torrents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
*/
const DEFAULT_LIMIT = 50;

function parseStrictIntegerParam(value: string | null): number | null {
if (value == null || !/^-?\d+$/.test(value)) {
return null;
}

const parsed = Number(value);
return Number.isSafeInteger(parsed) ? parsed : null;
}

/**
* Extended search result with source
*/
Expand Down Expand Up @@ -125,8 +134,8 @@
const limitParam = searchParams.get('limit');
let limit = DEFAULT_LIMIT;
if (limitParam) {
const parsedLimit = parseInt(limitParam, 10);
if (isNaN(parsedLimit)) {
const parsedLimit = parseStrictIntegerParam(limitParam);
if (parsedLimit == null) {
return NextResponse.json(
{ error: 'Invalid limit. Must be a number.' },
{ status: 400 }
Expand All @@ -151,8 +160,8 @@
const offsetParam = searchParams.get('offset');
let offset = 0;
if (offsetParam) {
const parsedOffset = parseInt(offsetParam, 10);
if (isNaN(parsedOffset)) {
const parsedOffset = parseStrictIntegerParam(offsetParam);
if (parsedOffset == null) {
return NextResponse.json(
{ error: 'Invalid offset. Must be a number.' },
{ status: 400 }
Expand Down Expand Up @@ -249,7 +258,7 @@
// Enrich DHT results with IMDB data
const dhtResults = results.filter(r => r.source === 'dht');
if (dhtResults.length > 0) {
const enriched = await batchEnrichDhtWithImdb(dhtResults as any);

Check warning on line 261 in src/app/api/search/torrents/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

Unexpected any. Specify a different type
const enrichedMap = new Map(enriched.map(r => [r.torrent_infohash, r]));
results = results.map(r => {
if (r.source === 'dht') {
Expand Down
Loading