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.
410 lines
13 KiB
TypeScript
410 lines
13 KiB
TypeScript
import { db } from '@/lib/db';
|
|
import { auth, checkProjectAccess } from '@/lib/auth';
|
|
import { apiErrors, successResponse, withCacheControl } from '@/lib/api-response';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
import { validateShareLinkAccess } from '@/lib/share-links';
|
|
import { getShareSessionFromRequest } from '@/lib/share-session';
|
|
import { resolveServerBunnyCdnHostname } from '@/lib/bunny-cdn';
|
|
import { NextRequest } from 'next/server';
|
|
import { DownloadEgressSource } from '@prisma/client';
|
|
import { logError } from '@/lib/logger';
|
|
import { canDownloadProjectMedia } from '@/lib/project-download';
|
|
|
|
type RouteParams = { params: Promise<{ versionId: string }> };
|
|
type BunnyDownloadSourcePreference = 'auto' | 'original' | 'compressed';
|
|
type BunnyDownloadSource = {
|
|
sourceType: 'original' | 'compressed';
|
|
quality: number | null;
|
|
url: string;
|
|
};
|
|
|
|
const BUNNY_DOWNLOAD_FALLBACK_HEIGHTS = [2160, 1440, 1080, 720, 480, 360, 240];
|
|
const BUNNY_ALLOWED_QUALITIES = new Set(BUNNY_DOWNLOAD_FALLBACK_HEIGHTS);
|
|
const BUNNY_SOURCE_RESOLUTION_CACHE_TTL_MS = 60 * 1000;
|
|
const BUNNY_REMOTE_FETCH_TIMEOUT_MS = 8 * 1000;
|
|
|
|
type BunnyDownloadSourceCacheRecord = {
|
|
source: BunnyDownloadSource | null;
|
|
expiresAt: number;
|
|
};
|
|
|
|
const BUNNY_SOURCE_CACHE_MAX_ENTRIES = 500;
|
|
const bunnyDownloadSourceCache = new Map<string, BunnyDownloadSourceCacheRecord>();
|
|
|
|
function resolveBunnyCdnHostname(): string | null {
|
|
return resolveServerBunnyCdnHostname();
|
|
}
|
|
|
|
function buildBunnyOriginalUrl(videoId: string): string {
|
|
const hostname = resolveBunnyCdnHostname();
|
|
if (!hostname) return '';
|
|
return `https://${hostname}/${videoId}/original`;
|
|
}
|
|
|
|
function buildBunnySourceCacheKey(
|
|
videoId: string,
|
|
requestedQuality: number | null,
|
|
sourcePreference: BunnyDownloadSourcePreference
|
|
): string {
|
|
return `${videoId}:${requestedQuality ?? 'none'}:${sourcePreference}`;
|
|
}
|
|
|
|
function getCachedBunnyDownloadSource(
|
|
cacheKey: string,
|
|
now: number
|
|
): BunnyDownloadSource | null | undefined {
|
|
const cached = bunnyDownloadSourceCache.get(cacheKey);
|
|
if (!cached) return undefined;
|
|
|
|
if (cached.expiresAt <= now) {
|
|
bunnyDownloadSourceCache.delete(cacheKey);
|
|
return undefined;
|
|
}
|
|
|
|
return cached.source;
|
|
}
|
|
|
|
function setCachedBunnyDownloadSource(
|
|
cacheKey: string,
|
|
source: BunnyDownloadSource | null,
|
|
now: number
|
|
): void {
|
|
if (bunnyDownloadSourceCache.size >= BUNNY_SOURCE_CACHE_MAX_ENTRIES) {
|
|
// Evict the oldest entry (Maps preserve insertion order)
|
|
const firstKey = bunnyDownloadSourceCache.keys().next().value;
|
|
if (firstKey !== undefined) bunnyDownloadSourceCache.delete(firstKey);
|
|
}
|
|
bunnyDownloadSourceCache.set(cacheKey, {
|
|
source,
|
|
expiresAt: now + BUNNY_SOURCE_RESOLUTION_CACHE_TTL_MS,
|
|
});
|
|
}
|
|
|
|
async function fetchWithTimeout(url: string, init: RequestInit): Promise<Response> {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), BUNNY_REMOTE_FETCH_TIMEOUT_MS);
|
|
try {
|
|
return await fetch(url, { ...init, signal: controller.signal });
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|
|
|
|
async function isRemoteFileAvailable(url: string): Promise<boolean> {
|
|
try {
|
|
const headRes = await fetchWithTimeout(url, { method: 'HEAD', cache: 'no-store' });
|
|
if (headRes.ok) return true;
|
|
|
|
if (headRes.status === 405) {
|
|
const rangeRes = await fetchWithTimeout(url, {
|
|
method: 'GET',
|
|
headers: { Range: 'bytes=0-0' },
|
|
cache: 'no-store',
|
|
});
|
|
return rangeRes.ok || rangeRes.status === 206;
|
|
}
|
|
|
|
return false;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function resolveHighestBunnyMp4Url(videoId: string): Promise<string> {
|
|
const hostname = resolveBunnyCdnHostname();
|
|
if (!hostname) return '';
|
|
const playlistUrl = `https://${hostname}/${videoId}/playlist.m3u8`;
|
|
|
|
let playlistHeights: number[] = [];
|
|
try {
|
|
const playlistRes = await fetchWithTimeout(playlistUrl, { cache: 'no-store' });
|
|
if (playlistRes.ok) {
|
|
const playlist = await playlistRes.text();
|
|
const matches = [...playlist.matchAll(/RESOLUTION=\d+x(\d+)/g)];
|
|
playlistHeights = matches
|
|
.map((match) => Number(match[1]))
|
|
.filter((height) => Number.isFinite(height) && BUNNY_ALLOWED_QUALITIES.has(height))
|
|
.sort((a, b) => b - a);
|
|
}
|
|
} catch {
|
|
// Continue with static fallback list below.
|
|
}
|
|
|
|
const candidateHeights = [...new Set([...playlistHeights, ...BUNNY_DOWNLOAD_FALLBACK_HEIGHTS])];
|
|
|
|
for (const height of candidateHeights) {
|
|
const candidateUrl = `https://${hostname}/${videoId}/play_${height}p.mp4`;
|
|
if (await isRemoteFileAvailable(candidateUrl)) return candidateUrl;
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
async function resolveBunnyOriginalSource(videoId: string): Promise<BunnyDownloadSource | null> {
|
|
const originalUrl = buildBunnyOriginalUrl(videoId);
|
|
if (!originalUrl) return null;
|
|
if (await isRemoteFileAvailable(originalUrl)) {
|
|
return {
|
|
sourceType: 'original',
|
|
quality: null,
|
|
url: originalUrl,
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
async function resolveBunnyCompressedSource(
|
|
videoId: string,
|
|
requestedQuality: number | null
|
|
): Promise<BunnyDownloadSource> {
|
|
const hostname = resolveBunnyCdnHostname();
|
|
if (!hostname) {
|
|
return {
|
|
sourceType: 'compressed',
|
|
quality: null,
|
|
url: '',
|
|
};
|
|
}
|
|
|
|
if (
|
|
typeof requestedQuality === 'number' &&
|
|
Number.isFinite(requestedQuality) &&
|
|
requestedQuality > 0
|
|
) {
|
|
const requestedUrl = `https://${hostname}/${videoId}/play_${requestedQuality}p.mp4`;
|
|
if (await isRemoteFileAvailable(requestedUrl)) {
|
|
return {
|
|
sourceType: 'compressed',
|
|
quality: extractHeightFromBunnyMp4Url(requestedUrl),
|
|
url: requestedUrl,
|
|
};
|
|
}
|
|
}
|
|
|
|
const fallbackUrl = await resolveHighestBunnyMp4Url(videoId);
|
|
if (!fallbackUrl) {
|
|
return {
|
|
sourceType: 'compressed',
|
|
quality: null,
|
|
url: '',
|
|
};
|
|
}
|
|
|
|
return {
|
|
sourceType: 'compressed',
|
|
quality: extractHeightFromBunnyMp4Url(fallbackUrl),
|
|
url: fallbackUrl,
|
|
};
|
|
}
|
|
|
|
async function resolveBunnyDownloadSource(
|
|
videoId: string,
|
|
requestedQuality: number | null,
|
|
sourcePreference: BunnyDownloadSourcePreference
|
|
): Promise<BunnyDownloadSource | null> {
|
|
if (!resolveBunnyCdnHostname()) return null;
|
|
|
|
const now = Date.now();
|
|
const cacheKey = buildBunnySourceCacheKey(videoId, requestedQuality, sourcePreference);
|
|
const cached = getCachedBunnyDownloadSource(cacheKey, now);
|
|
if (cached !== undefined) {
|
|
return cached;
|
|
}
|
|
|
|
let resolvedSource: BunnyDownloadSource | null;
|
|
if (sourcePreference === 'original') {
|
|
resolvedSource = await resolveBunnyOriginalSource(videoId);
|
|
setCachedBunnyDownloadSource(cacheKey, resolvedSource, now);
|
|
return resolvedSource;
|
|
}
|
|
|
|
if (sourcePreference === 'compressed') {
|
|
resolvedSource = await resolveBunnyCompressedSource(videoId, requestedQuality);
|
|
setCachedBunnyDownloadSource(cacheKey, resolvedSource, now);
|
|
return resolvedSource;
|
|
}
|
|
|
|
const originalSource = await resolveBunnyOriginalSource(videoId);
|
|
if (originalSource) {
|
|
setCachedBunnyDownloadSource(cacheKey, originalSource, now);
|
|
return originalSource;
|
|
}
|
|
|
|
resolvedSource = await resolveBunnyCompressedSource(videoId, requestedQuality);
|
|
setCachedBunnyDownloadSource(cacheKey, resolvedSource, now);
|
|
return resolvedSource;
|
|
}
|
|
|
|
function extractHeightFromBunnyMp4Url(url: string): number | null {
|
|
const match = url.match(/\/play_(\d+)p\.mp4$/);
|
|
if (!match?.[1]) return null;
|
|
const parsed = Number(match[1]);
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
}
|
|
|
|
// GET /api/versions/[versionId]/download
|
|
export async function GET(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const isPrepareOnly = searchParams.get('prepare') === '1';
|
|
const rateLimitAction = isPrepareOnly ? 'video-download-prepare' : 'video-download';
|
|
const limited = await rateLimit(request, rateLimitAction);
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
const { versionId } = await params;
|
|
const requestedQuality = Number(searchParams.get('quality'));
|
|
const rawQuality = searchParams.get('quality');
|
|
const sourceParam = searchParams.get('source');
|
|
const sourcePreference: BunnyDownloadSourcePreference =
|
|
sourceParam === null
|
|
? 'auto'
|
|
: sourceParam === 'original' || sourceParam === 'compressed'
|
|
? sourceParam
|
|
: 'auto';
|
|
|
|
const version = await db.videoVersion.findUnique({
|
|
where: { id: versionId },
|
|
include: {
|
|
video: {
|
|
include: {
|
|
project: {
|
|
include: {
|
|
workspace: {
|
|
select: {
|
|
id: true,
|
|
ownerId: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!version) {
|
|
return apiErrors.notFound('Version');
|
|
}
|
|
|
|
const access = await checkProjectAccess(version.video.project, session?.user?.id);
|
|
const shareSession = getShareSessionFromRequest(request, version.video.id);
|
|
const shareAccess = shareSession
|
|
? await validateShareLinkAccess({
|
|
token: shareSession.token,
|
|
projectId: version.video.projectId,
|
|
videoId: version.video.id,
|
|
requiredPermission: 'VIEW',
|
|
passwordVerified: shareSession.passwordVerified,
|
|
})
|
|
: {
|
|
hasAccess: false,
|
|
canComment: false,
|
|
canDownload: false,
|
|
allowGuests: false,
|
|
requiresPassword: false,
|
|
};
|
|
const canDownloadViaShareLink = shareAccess.hasAccess && shareAccess.canDownload;
|
|
const canDownloadViaMembership = canDownloadProjectMedia(version.video.project, access);
|
|
if (!canDownloadViaMembership && !canDownloadViaShareLink) {
|
|
// A caller with no relationship to the project at all is told the version does not
|
|
// exist, matching the comment export route: answering 403 for an id belonging to
|
|
// another tenant confirms that the id exists. Anyone who does have a relationship,
|
|
// including an owner whose billing has lapsed, already knows it exists and gets the
|
|
// more informative 403.
|
|
const belongsToProject = access.isOwner || access.isProjectMember || access.isWorkspaceMember;
|
|
if (!belongsToProject && !shareAccess.hasAccess) {
|
|
return apiErrors.notFound('Version');
|
|
}
|
|
return apiErrors.forbidden('Access denied');
|
|
}
|
|
|
|
if (version.providerId !== 'bunny') {
|
|
return apiErrors.badRequest('Download is currently supported for Bunny versions only');
|
|
}
|
|
|
|
if (
|
|
sourceParam !== null &&
|
|
sourceParam !== 'auto' &&
|
|
sourceParam !== 'original' &&
|
|
sourceParam !== 'compressed'
|
|
) {
|
|
return apiErrors.badRequest('Invalid source. Allowed values: auto, original, compressed');
|
|
}
|
|
|
|
if (
|
|
rawQuality !== null &&
|
|
(!Number.isFinite(requestedQuality) || !BUNNY_ALLOWED_QUALITIES.has(requestedQuality))
|
|
) {
|
|
return apiErrors.badRequest(
|
|
'Invalid quality. Allowed values: 2160, 1440, 1080, 720, 480, 360, 240'
|
|
);
|
|
}
|
|
|
|
if (rawQuality !== null && sourcePreference === 'original') {
|
|
return apiErrors.badRequest('Quality cannot be used when source=original');
|
|
}
|
|
|
|
const source = await resolveBunnyDownloadSource(
|
|
version.videoId,
|
|
Number.isFinite(requestedQuality) ? requestedQuality : null,
|
|
sourcePreference
|
|
);
|
|
|
|
if (!source) {
|
|
if (sourcePreference === 'original') {
|
|
return apiErrors.notFound('Original file');
|
|
}
|
|
return apiErrors.notFound('Download file');
|
|
}
|
|
|
|
if (isPrepareOnly) {
|
|
const response = successResponse({
|
|
quality: source.quality,
|
|
sourceType: source.sourceType,
|
|
});
|
|
return withCacheControl(response, 'private, no-store');
|
|
}
|
|
|
|
// Fetch Content-Length via HEAD so we can record egress bytes without proxying the body.
|
|
let estimatedBytes = BigInt(0);
|
|
try {
|
|
const headRes = await fetchWithTimeout(source.url, { method: 'HEAD', cache: 'no-store' });
|
|
const cl = headRes.headers.get('content-length');
|
|
if (cl && /^\d+$/.test(cl.trim())) {
|
|
const parsed = BigInt(cl.trim());
|
|
if (parsed > BigInt(0)) estimatedBytes = parsed;
|
|
}
|
|
} catch {
|
|
// Best-effort — leave estimatedBytes as 0 if HEAD fails.
|
|
}
|
|
|
|
try {
|
|
await db.downloadEgressEvent.create({
|
|
data: {
|
|
versionId: version.id,
|
|
videoId: version.video.id,
|
|
projectId: version.video.project.id,
|
|
workspaceId: version.video.project.workspace.id,
|
|
billedUserId: version.video.project.workspace.ownerId,
|
|
downloaderUserId: session?.user?.id ?? null,
|
|
source:
|
|
source.sourceType === 'original'
|
|
? DownloadEgressSource.ORIGINAL
|
|
: DownloadEgressSource.COMPRESSED,
|
|
quality: source.quality,
|
|
estimatedBytes,
|
|
},
|
|
});
|
|
} catch (egressError) {
|
|
logError('Failed to record download egress event:', egressError);
|
|
}
|
|
|
|
return Response.redirect(source.url, 302);
|
|
} catch (error) {
|
|
logError('Error downloading version:', error);
|
|
return apiErrors.internalError('Failed to download video');
|
|
}
|
|
}
|