Fix watch tracking, video unlocking and access-control gaps
Deploy Vault CMS / deploy (push) Failing after 38s
Deploy Vault CMS / deploy (push) Failing after 38s
Watch progress and unlocking: - Player samples the playhead and reports the ranges actually played, so skipped sections earn no credit and early plays are not lost - Server merges ranges into compact segment rows; progress never regresses - Single 80% unlock rule enforced server-side (lib/watch-progress.ts); the Unlock button and automatic unlock now agree - Lock state and progress bar update live from each progress save - Playlist is derived from the video, so unlocks work for shared playlists and for links from watch history and liked videos - First/previous video decided by playlist position, not index numbers Access control: - Video API and HLS/MP4 routes require a signed-in viewer who is enrolled and has the video unlocked; thumbnails require sign-in - Enrolment sync reads course codes from the allow-list, not the request - /api/admin/meta is admin-only - Account status, role and levels are re-checked from the database during a session, so deactivated students lose access within a minute Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import { prisma } from "../../../../lib/prisma";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "../../../../lib/auth-options";
|
||||
import { getVideoUrls } from "../../../../lib/video-urls";
|
||||
import { checkVideoAccess } from "../../../../lib/video-access";
|
||||
|
||||
export async function GET(req: Request, context: any) {
|
||||
try {
|
||||
@@ -74,6 +75,16 @@ export async function GET(req: Request, context: any) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Sequential locks apply to students even when they follow a direct link.
|
||||
const role = (session.user as any).role;
|
||||
const isAdmin = role === "admin" || role === "superadmin";
|
||||
if (!isAdmin) {
|
||||
const access = await checkVideoAccess({ id: user.id, isAdmin: false }, id);
|
||||
if (!access.ok) {
|
||||
return NextResponse.json({ error: access.error }, { status: access.status });
|
||||
}
|
||||
}
|
||||
|
||||
// Find the next video (index = current.index + 1)
|
||||
const next = await prisma.video.findFirst({
|
||||
where: {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { checkVideoAccess, getViewer } from '@/lib/video-access';
|
||||
|
||||
// Path to HLS directory (should match UPLOADS_DIR/hls)
|
||||
const HLS_DIR = path.join(process.env.UPLOADS_DIR || '/uploads', 'hls');
|
||||
@@ -23,6 +24,17 @@ export async function GET(
|
||||
);
|
||||
}
|
||||
|
||||
// Signed-in viewers only, and only for videos they may watch.
|
||||
// The first path segment is the video id.
|
||||
const viewer = await getViewer();
|
||||
if (!viewer) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const access = await checkVideoAccess(viewer, pathSegments[0]);
|
||||
if (!access.ok) {
|
||||
return NextResponse.json({ error: access.error }, { status: access.status });
|
||||
}
|
||||
|
||||
// Construct the file path and validate it
|
||||
const requestedPath = path.join(HLS_DIR, ...pathSegments);
|
||||
|
||||
@@ -44,7 +56,7 @@ export async function GET(
|
||||
}
|
||||
|
||||
// Read the file
|
||||
const fileContent = fs.readFileSync(requestedPath);
|
||||
const fileContent = await fs.promises.readFile(requestedPath);
|
||||
|
||||
// Determine content type based on file extension
|
||||
let contentType = 'application/octet-stream';
|
||||
@@ -62,8 +74,7 @@ export async function GET(
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': 'public, max-age=3600', // Cache for 1 hour
|
||||
'Access-Control-Allow-Origin': '*', // Allow CORS if needed
|
||||
'Cache-Control': 'private, max-age=3600', // Viewer's browser only, 1 hour
|
||||
'Accept-Ranges': 'bytes',
|
||||
},
|
||||
});
|
||||
@@ -89,6 +100,11 @@ export async function HEAD(
|
||||
return new NextResponse(null, { status: 400 });
|
||||
}
|
||||
|
||||
const viewer = await getViewer();
|
||||
if (!viewer) return new NextResponse(null, { status: 401 });
|
||||
const access = await checkVideoAccess(viewer, pathSegments[0]);
|
||||
if (!access.ok) return new NextResponse(null, { status: access.status });
|
||||
|
||||
const requestedPath = path.join(HLS_DIR, ...pathSegments);
|
||||
|
||||
// Security: prevent directory traversal attacks
|
||||
|
||||
Reference in New Issue
Block a user