Files
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

523 lines
14 KiB
TypeScript

import {
AbortMultipartUploadCommand,
CompleteMultipartUploadCommand,
CreateBucketCommand,
CreateMultipartUploadCommand,
DeleteObjectCommand,
GetObjectCommand,
GetBucketCorsCommand,
HeadBucketCommand,
HeadObjectCommand,
PutBucketCorsCommand,
PutObjectCommand,
UploadPartCommand,
S3Client,
type CORSRule,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { VIDEO_OBJECT_KEY_PREFIX } from '@/lib/video-upload-validation';
const IMAGE_OBJECT_KEY_PREFIX = 'images/';
const AUDIO_OBJECT_KEY_PREFIX = 'voice/';
const R2_ACCOUNT_ID = process.env.R2_ACCOUNT_ID;
const R2_ACCESS_KEY_ID = process.env.R2_ACCESS_KEY_ID;
const R2_SECRET_ACCESS_KEY = process.env.R2_SECRET_ACCESS_KEY;
const R2_BUCKET_NAME = process.env.R2_BUCKET_NAME ?? '';
const R2_ENDPOINT = process.env.R2_ENDPOINT;
const R2_PRESIGN_ENDPOINT = process.env.R2_PRESIGN_ENDPOINT;
const R2_PUBLIC_BASE_URL = process.env.R2_PUBLIC_BASE_URL;
let cachedR2Client: S3Client | null = null;
let cachedR2PresignClient: S3Client | null = null;
function trimTrailingSlashes(value: string): string {
return value.replace(/\/+$/, '');
}
function requireStorageValue(name: string, value: string | undefined): string {
if (!value) {
throw new Error(`Missing ${name} for S3-compatible storage`);
}
return value;
}
function getR2Endpoint(): string {
if (R2_ENDPOINT) {
return trimTrailingSlashes(R2_ENDPOINT);
}
if (!R2_ACCOUNT_ID) {
throw new Error('Missing R2_ENDPOINT or R2_ACCOUNT_ID for S3-compatible storage');
}
return `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`;
}
function getR2PresignEndpoint(): string {
if (R2_PRESIGN_ENDPOINT) {
return trimTrailingSlashes(R2_PRESIGN_ENDPOINT);
}
return getR2Endpoint();
}
function getOrCreateR2Client(): S3Client {
if (cachedR2Client) {
return cachedR2Client;
}
cachedR2Client = new S3Client({
region: 'auto',
endpoint: getR2Endpoint(),
forcePathStyle: Boolean(R2_ENDPOINT),
requestChecksumCalculation: 'WHEN_REQUIRED',
responseChecksumValidation: 'WHEN_REQUIRED',
credentials: {
accessKeyId: requireStorageValue('R2_ACCESS_KEY_ID', R2_ACCESS_KEY_ID),
secretAccessKey: requireStorageValue('R2_SECRET_ACCESS_KEY', R2_SECRET_ACCESS_KEY),
},
});
return cachedR2Client;
}
function getOrCreateR2PresignClient(): S3Client {
if (cachedR2PresignClient) {
return cachedR2PresignClient;
}
cachedR2PresignClient = new S3Client({
region: 'auto',
endpoint: getR2PresignEndpoint(),
forcePathStyle: Boolean(R2_PRESIGN_ENDPOINT || R2_ENDPOINT),
requestChecksumCalculation: 'WHEN_REQUIRED',
responseChecksumValidation: 'WHEN_REQUIRED',
credentials: {
accessKeyId: requireStorageValue('R2_ACCESS_KEY_ID', R2_ACCESS_KEY_ID),
secretAccessKey: requireStorageValue('R2_SECRET_ACCESS_KEY', R2_SECRET_ACCESS_KEY),
},
});
return cachedR2PresignClient;
}
export const r2Client = new Proxy({} as S3Client, {
get(_target, prop, receiver) {
if (prop === 'destroy') {
return () => {
// Both are destroyed independently. Returning early when the send client was
// never created leaked the presign client in a process that only ever presigned.
if (cachedR2Client) {
cachedR2Client.destroy();
cachedR2Client = null;
}
if (cachedR2PresignClient) {
cachedR2PresignClient.destroy();
cachedR2PresignClient = null;
}
};
}
const client = getOrCreateR2Client();
const value = Reflect.get(client, prop, receiver);
return typeof value === 'function' ? value.bind(client) : value;
},
});
export function getR2PublicObjectUrl(key: string): string {
const sanitizedKey = key.replace(/^\/+/, '');
if (R2_PUBLIC_BASE_URL) {
return `${trimTrailingSlashes(R2_PUBLIC_BASE_URL)}/${sanitizedKey}`;
}
if (R2_ENDPOINT) {
return `${trimTrailingSlashes(R2_ENDPOINT)}/${R2_BUCKET_NAME}/${sanitizedKey}`;
}
if (!R2_ACCOUNT_ID) {
throw new Error('Missing R2_PUBLIC_BASE_URL or R2_ACCOUNT_ID for public object URLs');
}
return `https://${R2_BUCKET_NAME}.${R2_ACCOUNT_ID}.r2.cloudflarestorage.com/${sanitizedKey}`;
}
export async function ensureR2BucketExists(): Promise<void> {
try {
await r2Client.send(new HeadBucketCommand({ Bucket: R2_BUCKET_NAME }));
return;
} catch (error) {
const statusCode = (error as { $metadata?: { httpStatusCode?: number } })?.$metadata
?.httpStatusCode;
if (statusCode && statusCode !== 404 && statusCode !== 301 && statusCode !== 403) {
throw error;
}
}
await r2Client.send(new CreateBucketCommand({ Bucket: R2_BUCKET_NAME }));
}
export async function uploadAudio(
buffer: Buffer,
filename: string,
contentType: string = 'audio/webm'
): Promise<string> {
// Sanitize: strip any path components, use only the basename
const sanitized = filename.replace(/^.*[\\/]/, '').replace(/\.\.+/g, '');
if (!sanitized) throw new Error('Invalid filename');
const key = `voice/${sanitized}`;
await r2Client.send(
new PutObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
Body: buffer,
ContentType: contentType,
})
);
return getR2PublicObjectUrl(key);
}
const DEFAULT_PRESIGNED_PUT_TTL_SECONDS = 60 * 60;
export function getR2UploadCorsOrigins(extraOrigins: string[] = []): string[] {
const origins = new Set<string>();
for (const raw of [process.env.NEXTAUTH_URL, process.env.NEXT_PUBLIC_APP_URL, ...extraOrigins]) {
if (!raw?.trim()) continue;
try {
origins.add(new URL(trimTrailingSlashes(raw.trim())).origin);
} catch {
// Ignore invalid origin URLs.
}
}
if (process.env.NODE_ENV === 'development') {
origins.add('http://localhost:3000');
origins.add('http://127.0.0.1:3000');
}
return [...origins];
}
function corsRulesMatchOrigins(
existing:
| {
AllowedOrigins?: string[];
AllowedMethods?: string[];
}
| undefined,
requiredOrigins: string[]
): boolean {
if (!existing?.AllowedOrigins?.length || !existing.AllowedMethods?.length) {
return false;
}
const allowedOrigins = new Set(existing.AllowedOrigins);
const methods = new Set(existing.AllowedMethods.map((method) => method.toUpperCase()));
const hasRequiredOrigins = requiredOrigins.every((origin) => allowedOrigins.has(origin));
const hasPut = methods.has('PUT');
const hasGet = methods.has('GET') || methods.has('HEAD');
return hasRequiredOrigins && hasPut && hasGet;
}
export async function ensureR2UploadCors(extraOrigins: string[] = []): Promise<string[]> {
const allowedOrigins = getR2UploadCorsOrigins(extraOrigins);
if (allowedOrigins.length === 0) {
throw new Error(
'No origins configured for R2 upload CORS (set NEXTAUTH_URL or NEXT_PUBLIC_APP_URL)'
);
}
const managedRule = {
AllowedOrigins: allowedOrigins,
AllowedMethods: ['GET', 'PUT', 'HEAD'],
AllowedHeaders: ['*'],
ExposeHeaders: ['ETag'],
MaxAgeSeconds: 3600,
};
// The catch covers the read only. Wrapping the write in it too meant a transient write
// failure was mistaken for "this bucket has no CORS config", and the retry below then
// sent the managed rule on its own, discarding whatever the bucket already had.
let existingRules: CORSRule[] | null = null;
try {
const existing = await r2Client.send(
new GetBucketCorsCommand({
Bucket: R2_BUCKET_NAME,
})
);
existingRules = existing.CORSRules ?? [];
} catch {
// No CORS config yet, or insufficient permissions to read — write the managed rule.
}
if (existingRules) {
if (existingRules.some((rule) => corsRulesMatchOrigins(rule, allowedOrigins))) {
return allowedOrigins;
}
await r2Client.send(
new PutBucketCorsCommand({
Bucket: R2_BUCKET_NAME,
CORSConfiguration: {
CORSRules: [...existingRules, managedRule],
},
})
);
return allowedOrigins;
}
await r2Client.send(
new PutBucketCorsCommand({
Bucket: R2_BUCKET_NAME,
CORSConfiguration: {
CORSRules: [managedRule],
},
})
);
return allowedOrigins;
}
export async function createPresignedVideoPutUrl(
key: string,
contentType: string,
contentLength: bigint,
expiresInSeconds = DEFAULT_PRESIGNED_PUT_TTL_SECONDS
): Promise<string> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
if (contentLength <= BigInt(0) || contentLength > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new Error('Invalid video content length');
}
const command = new PutObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
ContentType: contentType,
ContentLength: Number(contentLength),
});
return getSignedUrl(getOrCreateR2PresignClient(), command, {
expiresIn: expiresInSeconds,
// Passing ContentType to the command is not enough: unless the header is signable the
// grant does not bind it, and whoever holds the url can put any media type at the key.
// The client sends the same value back, so the signature covers what actually lands.
signableHeaders: new Set(['content-type']),
});
}
export async function createMultipartVideoUpload(
key: string,
contentType: string
): Promise<string> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
const result = await r2Client.send(
new CreateMultipartUploadCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
ContentType: contentType,
})
);
if (!result.UploadId) {
throw new Error('Failed to create multipart upload');
}
return result.UploadId;
}
export async function createPresignedUploadPartUrl(
key: string,
uploadId: string,
partNumber: number,
expiresInSeconds = DEFAULT_PRESIGNED_PUT_TTL_SECONDS
): Promise<string> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
if (!Number.isInteger(partNumber) || partNumber < 1 || partNumber > 10000) {
throw new Error('Invalid part number');
}
const command = new UploadPartCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
UploadId: uploadId,
PartNumber: partNumber,
});
return getSignedUrl(getOrCreateR2PresignClient(), command, { expiresIn: expiresInSeconds });
}
export async function completeMultipartVideoUpload(
key: string,
uploadId: string,
parts: Array<{ partNumber: number; etag: string }>
): Promise<void> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
if (parts.length === 0) {
throw new Error('No parts provided for multipart completion');
}
const orderedParts = [...parts]
.sort((a, b) => a.partNumber - b.partNumber)
.map((part) => ({ PartNumber: part.partNumber, ETag: part.etag }));
await r2Client.send(
new CompleteMultipartUploadCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
UploadId: uploadId,
MultipartUpload: { Parts: orderedParts },
})
);
}
export async function abortMultipartVideoUpload(key: string, uploadId: string): Promise<void> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
await r2Client.send(
new AbortMultipartUploadCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
UploadId: uploadId,
})
);
}
export async function createPresignedImagePutUrl(
key: string,
contentType: string,
expiresInSeconds = DEFAULT_PRESIGNED_PUT_TTL_SECONDS
): Promise<string> {
if (!key.startsWith(IMAGE_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid image object key');
}
const command = new PutObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
ContentType: contentType,
});
return getSignedUrl(getOrCreateR2PresignClient(), command, {
expiresIn: expiresInSeconds,
// Without this the grant binds only the host, so an image upload url accepts any
// media type at an `images/` key the app then serves as an image.
signableHeaders: new Set(['content-type']),
});
}
export async function headVideoObject(key: string): Promise<{
contentLength: bigint;
contentType: string | undefined;
} | null> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
return null;
}
try {
const result = await r2Client.send(
new HeadObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
})
);
const contentLength =
typeof result.ContentLength === 'number' && result.ContentLength >= 0
? BigInt(result.ContentLength)
: BigInt(0);
return {
contentLength,
contentType: result.ContentType,
};
} catch (error) {
const statusCode = (error as { $metadata?: { httpStatusCode?: number } })?.$metadata
?.httpStatusCode;
if (statusCode === 404) return null;
throw error;
}
}
export async function readVideoObjectBytes(
key: string,
byteLength: number
): Promise<Uint8Array | null> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX) || byteLength <= 0) {
return null;
}
const rangeEnd = Math.max(0, byteLength - 1);
try {
const result = await r2Client.send(
new GetObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
Range: `bytes=0-${rangeEnd}`,
})
);
if (!result.Body) return null;
const body = result.Body as { transformToByteArray?: () => Promise<Uint8Array> };
if (typeof body.transformToByteArray !== 'function') return null;
return await body.transformToByteArray();
} catch (error) {
const statusCode = (error as { $metadata?: { httpStatusCode?: number } })?.$metadata
?.httpStatusCode;
if (statusCode === 404 || statusCode === 416) return null;
throw error;
}
}
function assertAllowedObjectKey(key: string): void {
// `voice/` belongs here because uploadAudio() writes under it. Leaving it out meant
// deleteR2Object('voice/...') always threw, so a voice note attached to a comment could
// never be removed by the module that stored it and outlived the comment in the bucket.
if (
!key.startsWith(VIDEO_OBJECT_KEY_PREFIX) &&
!key.startsWith(IMAGE_OBJECT_KEY_PREFIX) &&
!key.startsWith(AUDIO_OBJECT_KEY_PREFIX)
) {
throw new Error('Invalid object key');
}
}
export async function deleteVideoObject(key: string): Promise<void> {
if (!key.startsWith(VIDEO_OBJECT_KEY_PREFIX)) {
throw new Error('Invalid video object key');
}
await deleteR2Object(key);
}
export async function deleteR2Object(key: string): Promise<void> {
assertAllowedObjectKey(key);
await r2Client.send(
new DeleteObjectCommand({
Bucket: R2_BUCKET_NAME,
Key: key,
})
);
}
export { R2_BUCKET_NAME };