Files
OpenFrame/lib/client/r2-video-upload.ts
T
yusufipk b51e690062 fix: close the findings the test suite surfaced
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.
2026-07-26 18:53:54 +07:00

333 lines
9.0 KiB
TypeScript

import { captureVideoThumbnail } from '@/lib/client/video-thumbnail';
import {
getMultipartProgressPercent,
getPartByteRange,
getRetryDelayMs,
getUploadProgressPercent,
isRetryableUploadError,
PART_RETRY_DELAYS_MS,
} from '@/lib/client/upload-chunking';
export type R2MultipartPart = { partNumber: number; url: string };
export type R2MultipartInit = {
uploadId: string;
partSizeBytes: number;
parts: R2MultipartPart[];
};
export type R2VideoInitResponse = {
presignedPutUrl: string;
objectKey: string;
proxyUrl: string;
uploadToken: string;
reservationId: string | null;
contentType: string;
thumbnailPresignedPutUrl: string;
thumbnailObjectKey: string;
thumbnailProxyUrl: string;
multipart: R2MultipartInit | null;
};
export type R2VideoUploadResult = R2VideoInitResponse & {
duration: number | null;
thumbnailUrl: string | null;
};
export type UploadProgressHandler = (progress: number) => void;
export function uploadBytesWithProgress(
url: string,
body: Blob | File,
contentType: string,
onProgress?: UploadProgressHandler
): Promise<void> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', url);
xhr.setRequestHeader('Content-Type', contentType);
xhr.upload.onprogress = (event) => {
if (!onProgress || !event.lengthComputable) return;
onProgress(getUploadProgressPercent(event.loaded, event.total));
};
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve();
return;
}
reject(new Error(`Upload failed with status ${xhr.status}`));
};
xhr.onerror = () => {
reject(
new Error(
'Network error during upload. If you use direct S3/R2 uploads, configure bucket CORS to allow PUT from this site origin.'
)
);
};
xhr.onabort = () => reject(new Error('Upload aborted'));
xhr.send(body);
});
}
function uploadPartWithProgress(
url: string,
body: Blob,
onPartProgress?: (loadedBytes: number) => void
): Promise<string> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', url);
// Intentionally no Content-Type header: it is not part of the presigned
// UploadPart signature, and the part body is raw bytes.
xhr.upload.onprogress = (event) => {
if (!onPartProgress || !event.lengthComputable) return;
onPartProgress(event.loaded);
};
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
const etag = xhr.getResponseHeader('ETag');
if (!etag) {
reject(
new Error(
'Upload response missing ETag header. Configure bucket CORS to expose the ETag header.'
)
);
return;
}
resolve(etag);
return;
}
reject(new Error(`Chunk upload failed with status ${xhr.status}`));
};
xhr.onerror = () => {
reject(
new Error(
'Network error during upload. If you use direct S3/R2 uploads, configure bucket CORS to allow PUT from this site origin.'
)
);
};
xhr.onabort = () => reject(new Error('Upload aborted'));
xhr.send(body);
});
}
async function withRetry<T>(fn: () => Promise<T>, delays: number[]): Promise<T> {
let lastError: unknown;
for (let attempt = 0; attempt < delays.length; attempt += 1) {
if (attempt > 0) {
await new Promise((resolve) => setTimeout(resolve, getRetryDelayMs(attempt, delays)));
}
try {
return await fn();
} catch (error) {
lastError = error;
// An abort or a permanent 4xx will fail the same way every time, so repeating it
// only delays the error the caller is waiting for.
if (!isRetryableUploadError(error)) break;
}
}
throw lastError instanceof Error ? lastError : new Error('Upload failed after retries');
}
async function completeMultipartUpload(
projectId: string,
objectKey: string,
uploadToken: string,
parts: Array<{ partNumber: number; etag: string }>
): Promise<void> {
const res = await fetch(`/api/projects/${projectId}/videos/r2-complete`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ objectKey, uploadToken, parts }),
});
if (!res.ok) {
const payload = (await res.json().catch(() => null)) as { error?: string } | null;
throw new Error(payload?.error || 'Failed to complete multipart upload');
}
}
async function uploadVideoMultipart(
projectId: string,
file: File,
multipart: R2MultipartInit,
objectKey: string,
uploadToken: string,
onProgress?: UploadProgressHandler
): Promise<void> {
const totalBytes = file.size;
const partSize = multipart.partSizeBytes;
const loadedPerPart = new Array<number>(multipart.parts.length).fill(0);
const reportProgress = () => {
if (!onProgress) return;
onProgress(getMultipartProgressPercent(loadedPerPart, totalBytes));
};
const completedParts: Array<{ partNumber: number; etag: string }> = [];
for (let index = 0; index < multipart.parts.length; index += 1) {
const part = multipart.parts[index];
const { start, end } = getPartByteRange(part.partNumber, partSize, totalBytes);
const blob = file.slice(start, end);
const etag = await withRetry(
() =>
uploadPartWithProgress(part.url, blob, (loadedBytes) => {
loadedPerPart[index] = loadedBytes;
reportProgress();
}),
PART_RETRY_DELAYS_MS
);
loadedPerPart[index] = end - start;
reportProgress();
completedParts.push({ partNumber: part.partNumber, etag });
}
await completeMultipartUpload(projectId, objectKey, uploadToken, completedParts);
}
async function readVideoDuration(file: File): Promise<number | null> {
return new Promise((resolve) => {
const objectUrl = URL.createObjectURL(file);
const video = document.createElement('video');
video.preload = 'metadata';
const cleanup = () => {
video.removeAttribute('src');
video.load();
URL.revokeObjectURL(objectUrl);
};
video.onloadedmetadata = () => {
const duration =
Number.isFinite(video.duration) && video.duration > 0 ? Math.round(video.duration) : null;
cleanup();
resolve(duration);
};
video.onerror = () => {
cleanup();
resolve(null);
};
video.src = objectUrl;
});
}
export async function initR2VideoUpload(
projectId: string,
file: File
): Promise<R2VideoInitResponse> {
const initRes = await fetch(`/api/projects/${projectId}/videos/r2-init`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
fileName: file.name,
contentType: file.type,
sizeBytes: file.size,
}),
});
const initPayload = (await initRes.json().catch(() => null)) as {
data?: R2VideoInitResponse;
error?: string;
} | null;
if (!initRes.ok || !initPayload?.data) {
throw new Error(initPayload?.error || 'Failed to initialize video upload');
}
return initPayload.data;
}
export async function cleanupPendingR2VideoUpload(
projectId: string,
input: {
objectKey: string;
uploadToken: string;
reservationId: string | null;
thumbnailObjectKey?: string | null;
},
keepalive = false
): Promise<void> {
try {
await fetch(`/api/projects/${projectId}/videos/r2-init`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
objectKey: input.objectKey,
uploadToken: input.uploadToken,
reservationId: input.reservationId,
thumbnailObjectKey: input.thumbnailObjectKey ?? undefined,
}),
keepalive,
});
} catch (error) {
console.error('Failed to cleanup pending R2 video upload:', error);
}
}
export async function uploadVideoToR2(
projectId: string,
file: File,
options?: { onProgress?: UploadProgressHandler }
): Promise<R2VideoUploadResult> {
const init = await initR2VideoUpload(projectId, file);
const cleanupInput = {
objectKey: init.objectKey,
uploadToken: init.uploadToken,
reservationId: init.reservationId,
thumbnailObjectKey: init.thumbnailObjectKey,
};
try {
if (init.multipart) {
await uploadVideoMultipart(
projectId,
file,
init.multipart,
init.objectKey,
init.uploadToken,
options?.onProgress
);
} else {
await uploadBytesWithProgress(
init.presignedPutUrl,
file,
init.contentType,
options?.onProgress
);
}
} catch (error) {
await cleanupPendingR2VideoUpload(projectId, cleanupInput);
throw error;
}
const [duration, thumbnailBlob] = await Promise.all([
readVideoDuration(file),
captureVideoThumbnail(file),
]);
let thumbnailUrl: string | null = null;
if (thumbnailBlob) {
try {
await uploadBytesWithProgress(init.thumbnailPresignedPutUrl, thumbnailBlob, 'image/jpeg');
thumbnailUrl = init.thumbnailProxyUrl;
} catch (error) {
console.warn('Failed to upload video thumbnail:', error);
}
}
return { ...init, duration, thumbnailUrl };
}