Skip to content

Commit 30758f5

Browse files
committed
fix(backend): validate pagination parameters in views endpoint
1 parent af5c829 commit 30758f5

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

apps/backend/src/__tests__/analytics.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,46 @@ describe(
427427
}
428428
);
429429

430+
it(
431+
'200 — clamps non-numeric page to 1',
432+
async () => {
433+
prismaMock.cardView.count.mockResolvedValue(0);
434+
prismaMock.cardView.findMany.mockResolvedValue([]);
435+
436+
const res = await app.inject({
437+
method: 'GET',
438+
url: '/api/analytics/views?page=abc',
439+
headers: authHeader(),
440+
});
441+
442+
expect(res.statusCode).toBe(200);
443+
expect(
444+
prismaMock.cardView.findMany.mock.calls[0][0]
445+
).toMatchObject({ skip: 0, take: 20 });
446+
expect(res.json().meta.page).toBe(1);
447+
}
448+
);
449+
450+
it(
451+
'200 — clamps negative page to 1',
452+
async () => {
453+
prismaMock.cardView.count.mockResolvedValue(0);
454+
prismaMock.cardView.findMany.mockResolvedValue([]);
455+
456+
const res = await app.inject({
457+
method: 'GET',
458+
url: '/api/analytics/views?page=-5',
459+
headers: authHeader(),
460+
});
461+
462+
expect(res.statusCode).toBe(200);
463+
expect(
464+
prismaMock.cardView.findMany.mock.calls[0][0]
465+
).toMatchObject({ skip: 0, take: 20 });
466+
expect(res.json().meta.page).toBe(1);
467+
}
468+
);
469+
430470
it(
431471
'401 — rejects unauthenticated request',
432472
async () => {

apps/backend/src/routes/analytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export async function analyticsRoutes(
108108
_reply: FastifyReply
109109
) => {
110110
const userId = (request.user as any).id;
111-
const page = parseInt(request.query.page || '1', 10);
111+
const page = Math.max(1, parseInt(request.query.page || '1', 10) || 1);
112112
const limit = 20;
113113
const skip = (page - 1) * limit;
114114

0 commit comments

Comments
 (0)