mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
The suite that landed in #43/#44 was written against existing behaviour, so a number of tests pinned bugs rather than asserting correct behaviour. This fixes the production code and moves each of those tests onto the fixed behaviour in the same change. Security: - project-download: derive the archive entry extension from the last path segment and restrict it to a short alphanumeric run, so an extensionless allowlisted url can no longer contribute a path separator; validate the r2 branch against the strict proxy-path pattern instead of a `startsWith`, which let `/api/upload/video/clip.mp4/../../etc/passwd` through verbatim. - rate-limit: hash a key or action wider than its column instead of skipping the query. Both the guard and the failing INSERT used to answer "allowed", so the limit stopped applying entirely. Warn at startup when TRUSTED_PROXY_MODE is unset in production. - video uploads: the file name decides the content type; a client-declared video mime no longer makes `payload.exe` acceptable. - email templates: escape in the helpers rather than relying on every caller, with an explicit `rawEmailHtml()` opt-out for the one call site that builds markup. `escapeHtml` now covers the single quote. - CSP: allow loopback object storage outside production only. - route-access: reach the billing redirect only for the workspace owner. Keying it off the owner's billing status alone made the redirect target an oracle for whose subscription had lapsed, and sent members to a page they cannot act on. - search: carry the same billing condition every other read path carries. - logger: check `err.name` as well as `err.constructor.name`, so a re-thrown, deserialised or minified Prisma error is still redacted. - upload tokens: resolve the signing secret outside the try, so a server booted without one fails loudly instead of reporting every grant as a forgery. - invitations: never downgrade an existing membership, and report a scoped invitation that points at nothing as not_found rather than accepted. - auth: resolve the workspace role for every signed-in caller, so checkProjectAccess and computeProjectAccess stop disagreeing about the owner who also owns the workspace. The `intent` option is gone with it. - r2-media-proxy: validate the object key inside the proxy so the guard travels with the function; delete the unused, unanchored `mediaUrlToR2Key`. - r2: sign the content type into presigned PUT grants. Correctness: - frame rate snapping picks the nearest standard, not the first within tolerance, so 24, 30 and 60 fps are reachable at all. - a version upload registers its Bunny cleanup as soon as bunny-init answers, so a failed tus upload no longer leaves a billed video behind. - deleting videos clears storage before the rows, so a refused DELETE leaves a retryable row rather than an orphaned object. - an expired upload session can be cancelled, which is what releases its quota. - `voice/` joins the delete allowlist, so a voice note can be removed by the module that wrote it. - a failed CORS write propagates instead of being mistaken for an empty config and replacing the bucket's rules. - filtering projects by workspace no longer hides projects the unfiltered call returns. - upload retries skip aborts and permanent 4xx; progress no longer divides by zero. - reply edits no longer clear the comment's tag; optimistic resolve rolls back to the state it replaced; the delete snapshot is captured once. - assorted UI fixes: duplicate React keys, double-click guards reading stale closures, the tag list fetched twice per load, a failed member list rendering as an empty one, a stale "Initializing upload..." beside a failure, and a registration banner pointing at an email that never arrives. Consistency and access: - the two download routes answer 404 for an id belonging to another tenant, as the comment export route already did. A caller who does belong still gets 403. - accessible names for the share-link password field, the guest name gates, the version dialog inputs and the comment-tag controls. Repository health: - the runner image installs production dependencies only. - a setup file for the unit project restores stubbed env centrally. - native tsconfig path resolution replaces vite-tsconfig-paths. - `uploadBytesWithProgress` exists once. - admin stats bill Bunny storage to the workspace owner like every other quota, gate on the configured flag, wire up the single-flight guard and count the statuses that belonged to no bucket. - `r2Client.destroy()` releases the presign client too. - `prepare` tolerates a production install, where husky is absent.
222 lines
8.0 KiB
TypeScript
222 lines
8.0 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { auth, checkProjectAccess } from '@/lib/auth';
|
|
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
|
|
import { db } from '@/lib/db';
|
|
import { logError } from '@/lib/logger';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
|
|
type RouteParams = { params: Promise<{ projectId: string }> };
|
|
|
|
const MAX_BULK_MOVE = 50;
|
|
|
|
// Thrown inside the move transaction when the atomic source-ownership re-check
|
|
// fails (a concurrent request relocated a video between check and commit).
|
|
class VideoMoveConflictError extends Error {}
|
|
|
|
// GET /api/projects/[projectId]/videos/move
|
|
// Lists destination projects (same workspace, manageable by the user) the
|
|
// current project's videos can be moved into.
|
|
export async function GET(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const limited = await rateLimit(request, 'api');
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
const { projectId } = await params;
|
|
|
|
if (!session?.user?.id) {
|
|
return apiErrors.unauthorized();
|
|
}
|
|
const userId = session.user.id;
|
|
|
|
const project = await db.project.findUnique({
|
|
where: { id: projectId },
|
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
|
});
|
|
if (!project) {
|
|
return apiErrors.notFound('Project');
|
|
}
|
|
|
|
const access = await checkProjectAccess(project, userId);
|
|
if (!access.canEdit) {
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
|
|
// Workspace owners/admins can manage every project in the workspace; everyone
|
|
// else can only move into projects they own or are an admin member of.
|
|
const [workspace, workspaceMember] = await Promise.all([
|
|
db.workspace.findUnique({
|
|
where: { id: project.workspaceId },
|
|
select: { ownerId: true },
|
|
}),
|
|
db.workspaceMember.findUnique({
|
|
where: { workspaceId_userId: { workspaceId: project.workspaceId, userId } },
|
|
}),
|
|
]);
|
|
const isWorkspaceManager = workspace?.ownerId === userId || workspaceMember?.role === 'ADMIN';
|
|
|
|
const targets = await db.project.findMany({
|
|
where: {
|
|
workspaceId: project.workspaceId,
|
|
id: { not: projectId },
|
|
...(isWorkspaceManager
|
|
? {}
|
|
: {
|
|
OR: [{ ownerId: userId }, { members: { some: { userId, role: 'ADMIN' } } }],
|
|
}),
|
|
},
|
|
orderBy: { name: 'asc' },
|
|
select: { id: true, name: true },
|
|
});
|
|
|
|
const response = successResponse({ projects: targets });
|
|
return withCacheControl(response, 'private, no-store');
|
|
} catch (error) {
|
|
logError('Error listing video move targets:', error);
|
|
return apiErrors.internalError('Failed to load destination projects');
|
|
}
|
|
}
|
|
|
|
// POST /api/projects/[projectId]/videos/move
|
|
// Moves one or more videos from this project into another project in the same
|
|
// workspace. Versions, comments and assets follow the video automatically.
|
|
export async function POST(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const limited = await rateLimit(request, 'mutate');
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
const { projectId } = await params;
|
|
|
|
if (!session?.user?.id) {
|
|
return apiErrors.unauthorized();
|
|
}
|
|
const userId = session.user.id;
|
|
|
|
const body = await request.json();
|
|
const { videoIds, targetProjectId } = body as {
|
|
videoIds?: unknown;
|
|
targetProjectId?: unknown;
|
|
};
|
|
|
|
if (!Array.isArray(videoIds) || videoIds.length === 0) {
|
|
return apiErrors.badRequest('videoIds must be a non-empty array');
|
|
}
|
|
if (videoIds.length > MAX_BULK_MOVE) {
|
|
return apiErrors.badRequest(`You can move at most ${MAX_BULK_MOVE} videos at once`);
|
|
}
|
|
if (!videoIds.every((id) => typeof id === 'string' && id.trim().length > 0)) {
|
|
return apiErrors.badRequest('Each video id must be a non-empty string');
|
|
}
|
|
if (typeof targetProjectId !== 'string' || targetProjectId.trim().length === 0) {
|
|
return apiErrors.badRequest('targetProjectId must be a non-empty string');
|
|
}
|
|
|
|
const normalizedIds = [...new Set(videoIds.map((id) => id.trim()))];
|
|
const targetId = targetProjectId.trim();
|
|
|
|
if (targetId === projectId) {
|
|
return apiErrors.badRequest('Source and destination projects are the same');
|
|
}
|
|
|
|
const [sourceProject, targetProject] = await Promise.all([
|
|
db.project.findUnique({
|
|
where: { id: projectId },
|
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
|
}),
|
|
db.project.findUnique({
|
|
where: { id: targetId },
|
|
select: { id: true, ownerId: true, workspaceId: true, visibility: true },
|
|
}),
|
|
]);
|
|
if (!sourceProject) {
|
|
return apiErrors.notFound('Project');
|
|
}
|
|
if (!targetProject) {
|
|
return apiErrors.badRequest('Destination project not found');
|
|
}
|
|
if (sourceProject.workspaceId !== targetProject.workspaceId) {
|
|
return apiErrors.badRequest('Videos can only be moved within the same workspace');
|
|
}
|
|
|
|
const [sourceAccess, targetAccess] = await Promise.all([
|
|
checkProjectAccess(sourceProject, userId),
|
|
checkProjectAccess(targetProject, userId),
|
|
]);
|
|
if (!sourceAccess.canEdit) {
|
|
return apiErrors.forbidden('You cannot move videos out of this project');
|
|
}
|
|
if (!targetAccess.canEdit) {
|
|
return apiErrors.forbidden('You cannot move videos into the selected project');
|
|
}
|
|
|
|
// Fast, friendly pre-check for the common case (stale UI). The authoritative
|
|
// ownership guard is re-asserted atomically inside the transaction below.
|
|
const videos = await db.video.findMany({
|
|
where: { id: { in: normalizedIds }, projectId },
|
|
select: { id: true },
|
|
});
|
|
if (videos.length !== normalizedIds.length) {
|
|
return apiErrors.badRequest('One or more selected videos do not belong to this project');
|
|
}
|
|
|
|
try {
|
|
await db.$transaction(async (tx) => {
|
|
// Append moved videos after the destination's existing videos so ordering
|
|
// stays stable instead of colliding with the source positions. Read this
|
|
// before the move so the videos being moved aren't counted yet.
|
|
const maxPosition = await tx.video.aggregate({
|
|
where: { projectId: targetId },
|
|
_max: { position: true },
|
|
});
|
|
const basePosition = (maxPosition._max.position ?? -1) + 1;
|
|
|
|
// Re-assert source ownership as part of the write itself: a concurrent
|
|
// move can't slip a video out from under us between check and commit,
|
|
// and the row locks serialize competing moves of the same videos.
|
|
const moved = await tx.video.updateMany({
|
|
where: { id: { in: normalizedIds }, projectId },
|
|
data: { projectId: targetId },
|
|
});
|
|
if (moved.count !== normalizedIds.length) {
|
|
throw new VideoMoveConflictError();
|
|
}
|
|
|
|
// Apply per-video ordering now that the videos live in the destination.
|
|
await Promise.all(
|
|
normalizedIds.map((id, index) =>
|
|
tx.video.update({ where: { id }, data: { position: basePosition + index } })
|
|
)
|
|
);
|
|
|
|
// Keep video-scoped share links pointing at the video's new project.
|
|
await tx.shareLink.updateMany({
|
|
where: { videoId: { in: normalizedIds } },
|
|
data: { projectId: targetId },
|
|
});
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof VideoMoveConflictError) {
|
|
return apiErrors.conflict(
|
|
'One or more selected videos changed while moving. Please refresh and try again.'
|
|
);
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
revalidatePath(`/projects/${projectId}`);
|
|
revalidatePath(`/projects/${targetId}`);
|
|
|
|
const response = successResponse({
|
|
message: `${normalizedIds.length} video${normalizedIds.length === 1 ? '' : 's'} moved`,
|
|
movedCount: normalizedIds.length,
|
|
targetProjectId: targetId,
|
|
});
|
|
return withCacheControl(response, 'private, no-store');
|
|
} catch (error) {
|
|
logError('Error moving videos:', error);
|
|
return apiErrors.internalError('Failed to move videos');
|
|
}
|
|
}
|