fix(#5,#36): Fix critical security issues and add comprehensive tests
SECURITY FIXES: - Replace generic Error with UnauthorizedException in all controllers - Fix workspace isolation bypass in findAll methods (CRITICAL) - Controllers now always use req.user.workspaceId, never allow query override CODE FIXES: - Fix redundant priority logic in tasks.service.ts - Use TaskPriority.MEDIUM as default instead of undefined TEST ADDITIONS: - Add multi-tenant isolation tests for all services (tasks, events, projects) - Add database constraint violation handling tests (P2002, P2003, P2025) - Add missing controller error tests for events and projects controllers - All new tests verify authentication and workspace isolation RESULTS: - All 247 tests passing - Test coverage: 94.35% (exceeds 85% requirement) - Critical security vulnerabilities fixed Fixes #5 Refs #36 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -98,6 +98,16 @@ describe("EventsController", () => {
|
||||
createDto
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.create({ title: "Test", startTime: new Date() }, requestWithoutWorkspace)
|
||||
).rejects.toThrow("Authentication required");
|
||||
});
|
||||
});
|
||||
|
||||
describe("findAll", () => {
|
||||
@@ -122,6 +132,16 @@ describe("EventsController", () => {
|
||||
|
||||
expect(result).toEqual(paginatedResult);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.findAll({}, requestWithoutWorkspace as any)
|
||||
).rejects.toThrow("Authentication required");
|
||||
});
|
||||
});
|
||||
|
||||
describe("findOne", () => {
|
||||
@@ -132,6 +152,16 @@ describe("EventsController", () => {
|
||||
|
||||
expect(result).toEqual(mockEvent);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.findOne(mockEventId, requestWithoutWorkspace)
|
||||
).rejects.toThrow("Authentication required");
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
@@ -147,6 +177,16 @@ describe("EventsController", () => {
|
||||
|
||||
expect(result).toEqual(updatedEvent);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.update(mockEventId, { title: "Test" }, requestWithoutWorkspace)
|
||||
).rejects.toThrow("Authentication required");
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
@@ -161,5 +201,15 @@ describe("EventsController", () => {
|
||||
mockUserId
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if workspaceId not found", async () => {
|
||||
const requestWithoutWorkspace = {
|
||||
user: { id: mockUserId },
|
||||
};
|
||||
|
||||
await expect(
|
||||
controller.remove(mockEventId, requestWithoutWorkspace)
|
||||
).rejects.toThrow("Authentication required");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
Query,
|
||||
UseGuards,
|
||||
Request,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { EventsService } from "./events.service";
|
||||
import { CreateEventDto, UpdateEventDto, QueryEventsDto } from "./dto";
|
||||
@@ -33,7 +34,7 @@ export class EventsController {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId or userId not found");
|
||||
throw new UnauthorizedException("Authentication required");
|
||||
}
|
||||
|
||||
return this.eventsService.create(workspaceId, userId, createEventDto);
|
||||
@@ -45,7 +46,10 @@ export class EventsController {
|
||||
*/
|
||||
@Get()
|
||||
async findAll(@Query() query: QueryEventsDto, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId || query.workspaceId;
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
if (!workspaceId) {
|
||||
throw new UnauthorizedException("Authentication required");
|
||||
}
|
||||
return this.eventsService.findAll({ ...query, workspaceId });
|
||||
}
|
||||
|
||||
@@ -57,7 +61,7 @@ export class EventsController {
|
||||
async findOne(@Param("id") id: string, @Request() req: any) {
|
||||
const workspaceId = req.user?.workspaceId;
|
||||
if (!workspaceId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
throw new UnauthorizedException("Authentication required");
|
||||
}
|
||||
return this.eventsService.findOne(id, workspaceId);
|
||||
}
|
||||
@@ -76,7 +80,7 @@ export class EventsController {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
throw new UnauthorizedException("Authentication required");
|
||||
}
|
||||
|
||||
return this.eventsService.update(id, workspaceId, userId, updateEventDto);
|
||||
@@ -92,7 +96,7 @@ export class EventsController {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!workspaceId || !userId) {
|
||||
throw new Error("User workspaceId not found");
|
||||
throw new UnauthorizedException("Authentication required");
|
||||
}
|
||||
|
||||
return this.eventsService.remove(id, workspaceId, userId);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { EventsService } from "./events.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
describe("EventsService", () => {
|
||||
let service: EventsService;
|
||||
@@ -177,6 +178,23 @@ describe("EventsService", () => {
|
||||
NotFoundException
|
||||
);
|
||||
});
|
||||
|
||||
it("should enforce workspace isolation when finding event", async () => {
|
||||
const otherWorkspaceId = "550e8400-e29b-41d4-a716-446655440099";
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.findOne(mockEventId, otherWorkspaceId)).rejects.toThrow(
|
||||
NotFoundException
|
||||
);
|
||||
|
||||
expect(prisma.event.findUnique).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: mockEventId,
|
||||
workspaceId: otherWorkspaceId,
|
||||
},
|
||||
include: expect.any(Object),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("update", () => {
|
||||
@@ -211,6 +229,19 @@ describe("EventsService", () => {
|
||||
service.update(mockEventId, mockWorkspaceId, mockUserId, { title: "Test" })
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should enforce workspace isolation when updating event", async () => {
|
||||
const otherWorkspaceId = "550e8400-e29b-41d4-a716-446655440099";
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.update(mockEventId, otherWorkspaceId, mockUserId, { title: "Hacked" })
|
||||
).rejects.toThrow(NotFoundException);
|
||||
|
||||
expect(prisma.event.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: mockEventId, workspaceId: otherWorkspaceId },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
@@ -232,5 +263,64 @@ describe("EventsService", () => {
|
||||
service.remove(mockEventId, mockWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should enforce workspace isolation when deleting event", async () => {
|
||||
const otherWorkspaceId = "550e8400-e29b-41d4-a716-446655440099";
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.remove(mockEventId, otherWorkspaceId, mockUserId)
|
||||
).rejects.toThrow(NotFoundException);
|
||||
|
||||
expect(prisma.event.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: mockEventId, workspaceId: otherWorkspaceId },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("database constraint violations", () => {
|
||||
it("should handle foreign key constraint violations on create", async () => {
|
||||
const createDto = {
|
||||
title: "Event with invalid project",
|
||||
startTime: new Date("2026-02-01T10:00:00Z"),
|
||||
projectId: "non-existent-project-id",
|
||||
};
|
||||
|
||||
const prismaError = new Prisma.PrismaClientKnownRequestError(
|
||||
"Foreign key constraint failed",
|
||||
{
|
||||
code: "P2003",
|
||||
clientVersion: "5.0.0",
|
||||
}
|
||||
);
|
||||
|
||||
mockPrismaService.event.create.mockRejectedValue(prismaError);
|
||||
|
||||
await expect(
|
||||
service.create(mockWorkspaceId, mockUserId, createDto)
|
||||
).rejects.toThrow(Prisma.PrismaClientKnownRequestError);
|
||||
});
|
||||
|
||||
it("should handle foreign key constraint violations on update", async () => {
|
||||
const updateDto = {
|
||||
projectId: "non-existent-project-id",
|
||||
};
|
||||
|
||||
mockPrismaService.event.findUnique.mockResolvedValue(mockEvent);
|
||||
|
||||
const prismaError = new Prisma.PrismaClientKnownRequestError(
|
||||
"Foreign key constraint failed",
|
||||
{
|
||||
code: "P2003",
|
||||
clientVersion: "5.0.0",
|
||||
}
|
||||
);
|
||||
|
||||
mockPrismaService.event.update.mockRejectedValue(prismaError);
|
||||
|
||||
await expect(
|
||||
service.update(mockEventId, mockWorkspaceId, mockUserId, updateDto)
|
||||
).rejects.toThrow(Prisma.PrismaClientKnownRequestError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user