fix(backend): IDOR in POST /notifications/:id/read (Modul 26)

markRead() pruefte bisher nicht, ob die Benachrichtigung dem
anfragenden Nutzer gehoert - jeder authentifizierte Nutzer konnte
fremde Benachrichtigungen als gelesen markieren. Jetzt: 403 bei
fremder Benachrichtigung, weiterhin erlaubt fuer eigene und
Broadcast-Benachrichtigungen (userId = null). E2E-Regressionstest
ergaenzt.
This commit is contained in:
Nick Szaunig committed 2026-08-22 22:50:00 +02:00
1 parent a57cde1f9a
commit 75f0d4fd51
4 files changed
+64 -11

No files matched your search

@@ -22,7 +22,7 @@ export class NotificationsController {
}
@Post(':id/read')
markRead(@Param('id') id: string) {
return this.notificationsService.markRead(id);
markRead(@Param('id') id: string, @CurrentUser() actor: AuthenticatedUser) {
return this.notificationsService.markRead(id, actor.id);
}
}
@@ -1,4 +1,4 @@
import { NotFoundException } from '@nestjs/common';
import { ForbiddenException, NotFoundException } from '@nestjs/common';
import { NotificationsService } from './notifications.service';
import { PrismaService } from '../prisma/prisma.service';
@@ -55,19 +55,44 @@ describe('NotificationsService', () => {
it('throws NotFoundException when marking an unknown notification as read', async () => {
prisma.notification.findUnique.mockResolvedValueOnce(null);
await expect(service.markRead('missing')).rejects.toBeInstanceOf(
await expect(service.markRead('missing', 'user-1')).rejects.toBeInstanceOf(
NotFoundException,
);
});
it('marks a notification as read', async () => {
it("throws ForbiddenException when marking another user's notification as read", async () => {
prisma.notification.findUnique.mockResolvedValueOnce({
id: 'n1',
userId: 'owner',
read: false,
});
await expect(service.markRead('n1', 'someone-else')).rejects.toBeInstanceOf(
ForbiddenException,
);
});
it('marks a broadcast notification (no owner) as read for any user', async () => {
prisma.notification.findUnique.mockResolvedValueOnce({
id: 'n1',
userId: null,
read: false,
});
prisma.notification.update.mockResolvedValueOnce({ id: 'n1', read: true });
const result = await service.markRead('n1');
const result = await service.markRead('n1', 'any-user');
expect(result.read).toBe(true);
});
it("marks the owner's own notification as read", async () => {
prisma.notification.findUnique.mockResolvedValueOnce({
id: 'n1',
userId: 'user-1',
read: false,
});
prisma.notification.update.mockResolvedValueOnce({ id: 'n1', read: true });
const result = await service.markRead('n1', 'user-1');
expect(result.read).toBe(true);
});
@@ -1,4 +1,8 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import {
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { Notification, NotificationPriority } from '../../generated/prisma';
@@ -36,13 +40,16 @@ export class NotificationsService {
});
}
async markRead(id: string): Promise<Notification> {
async markRead(id: string, actingUserId: string): Promise<Notification> {
const notification = await this.prisma.notification.findUnique({
where: { id },
});
if (!notification) {
throw new NotFoundException('Notification not found');
}
if (notification.userId !== null && notification.userId !== actingUserId) {
throw new ForbiddenException("Cannot modify another user's notification");
}
return this.prisma.notification.update({
where: { id },
data: { read: true },