fix(watch-progress): validate versionId and enforce video access/version ownership before saving progress

This commit is contained in:
Yusuf İpek
2026-02-24 17:20:45 +03:00
parent 354bc38d42
commit 68c63ce020
+10 -12
View File
@@ -88,16 +88,18 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
return apiErrors.badRequest('Invalid progress value');
}
// Get the video version
let targetVersionId = versionId;
if (versionId !== undefined && typeof versionId !== 'string') {
return apiErrors.badRequest('Invalid versionId');
}
if (!targetVersionId) {
// Always load the requested video and validate access before writing progress.
// If versionId is provided, verify it belongs to this video; otherwise resolve active version.
const video = await db.video.findUnique({
where: { id: videoId },
include: {
project: true,
versions: {
where: { isActive: true },
where: versionId ? { id: versionId } : { isActive: true },
take: 1,
},
},
@@ -107,19 +109,15 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
return apiErrors.notFound('Video');
}
// Check access including workspace membership
const access = await checkProjectAccess(video.project, session?.user?.id);
if (!access.hasAccess) {
return apiErrors.forbidden('Access denied');
}
const activeVersion = video.versions[0];
if (!activeVersion) {
const targetVersion = video.versions[0];
if (!targetVersion) {
return apiErrors.notFound('Video version');
}
targetVersionId = activeVersion.id;
}
// Calculate percentage
const safeDuration = duration || 0;
@@ -130,7 +128,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
where: {
userId_versionId: {
userId: session.user.id,
versionId: targetVersionId,
versionId: targetVersion.id,
},
},
update: {
@@ -140,7 +138,7 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
},
create: {
userId: session.user.id,
versionId: targetVersionId,
versionId: targetVersion.id,
progress,
duration: safeDuration,
percentage,