fix(uploads): stop a storage hold from being dropped by whoever can name it

A reservation id was never a secret and could not have been one. An upload
token is base64url(payload) followed by its signature, so a client can read
every claim out of its own token, and the two R2 init routes hand their
reservation ids to the client outright. The asset route takes a reservation
id from the request body and deleted it on the strength of that id and the
billed user alone, and every hold an account owns is billed to the same user.

So a caller could start a Bunny upload, read the id out of the token they
were just given, quote it while attaching a one byte image or even a bare
YouTube link, and have the quota handed back while the upload carried on.
Repeat and a trial worth three gigabytes uploads as much as it likes for as
long as Bunny takes to report a figure of its own. Signing the id rather than
handing it over bought nothing, because signing is not hiding.

A hold now records what it was opened for and is only ever consumed by that
flow, so naming one is no longer enough to drop it.

Guests hold against the workspace owner's quota rather than their own and had
no way to give it back: the release was gated on being signed in. Declaring a
size and walking away cost the guest nothing and cost the owner their whole
remaining allowance for two hours. The guest grant now carries the reservation
and the declared size, bound to the Bunny video as well as to ours, so
cancelling gives the quota back and costs them the upload it stood for. What a
guest can hold without cancelling lapses in half an hour rather than two hours.

The in-transaction fallback check counted the account's Bunny storage as zero
on a Bunny upload, because the figure was only prefetched for R2 providers and
that branch was unreachable for Bunny until this PR made it reachable. On an
account whose storage is all Bunny that was a check that could not fail. It is
prefetched for every provider that can reach the fallback now.
This commit is contained in:
2026-08-18 11:07:18 +03:00
parent 4ff801738c
commit 00f1d430b8
22 changed files with 721 additions and 130 deletions
+85 -1
View File
@@ -14,9 +14,11 @@ import {
DELETE as cancelProjectBunnyUpload,
POST as initProjectBunnyUpload,
} from '@/app/api/projects/[projectId]/videos/bunny-init/route';
import { POST as createAsset } from '@/app/api/videos/[videoId]/assets/route';
import { UPLOAD_RESERVATION_PURPOSES } from '@/lib/storage-quota';
import { apiRequest, callRoute, readData, readError } from '../helpers/request';
import { signedInAs } from '../helpers/session';
import { seedProject } from '../factories';
import { createVideo, seedProject } from '../factories';
const GIB = BigInt(1024) * BigInt(1024) * BigInt(1024);
@@ -177,3 +179,85 @@ describe('DELETE /api/projects/[projectId]/videos/bunny-init', () => {
expect(await db.uploadReservation.count()).toBe(2);
});
});
// The reservation id is not a secret and was never going to be one. The upload
// token is `base64url(payload).signature`, so the client can read every claim in
// it, and the two R2 upload routes hand their reservation ids to the client
// outright. What keeps a hold from being dropped by whoever can name it is that
// a reservation records what it was opened for, and every finalize route matches
// on that as well as on the id.
describe('a Bunny hold cannot be consumed by another flow', () => {
/** The claims the client can read out of an upload token without our help. */
function claimsOf(uploadToken: string): Record<string, unknown> {
return JSON.parse(Buffer.from(uploadToken.split('.')[0], 'base64url').toString('utf8'));
}
it('puts the reservation id somewhere the client can read it', async () => {
const scenario = await seedProject();
signedInAs(scenario.owner);
const init = await initUpload(scenario.project.id, BigInt(1) * GIB);
const { uploadToken } = await readData(init);
const reservation = (await db.uploadReservation.findMany())[0];
expect(claimsOf(uploadToken).rid).toBe(reservation.id);
expect(reservation.purpose).toBe(UPLOAD_RESERVATION_PURPOSES.BUNNY);
});
// The attack the purpose column closes. Creating a YouTube asset costs nothing
// and consumes no storage, so quoting a Bunny reservation on one was a way to
// hand back the quota of an upload that was still running and then start
// another. Repeat and a trial worth three gigabytes uploads as much as it
// likes for as long as Bunny takes to report.
it('ignores a Bunny reservation id quoted on a YouTube asset create', async () => {
const scenario = await seedProject();
const video = await createVideo({ projectId: scenario.project.id });
signedInAs(scenario.owner);
const init = await initUpload(scenario.project.id, BigInt(2) * GIB);
const { uploadToken } = await readData(init);
const reservationId = claimsOf(uploadToken).rid as string;
const response = await callRoute(
createAsset,
apiRequest(`/api/videos/${video.id}/assets`, {
body: {
provider: 'YOUTUBE',
sourceUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
reservationId,
},
}),
{ videoId: video.id }
);
expect(response.status).toBe(201);
const reservations = await db.uploadReservation.findMany();
expect(reservations).toHaveLength(1);
expect(reservations[0].id).toBe(reservationId);
});
// And with the hold still standing, the next init has to see it.
it('still refuses the next upload after the quoted release attempt', async () => {
const scenario = await seedProject();
const video = await createVideo({ projectId: scenario.project.id });
signedInAs(scenario.owner);
const init = await initUpload(scenario.project.id, BigInt(2) * GIB);
const { uploadToken } = await readData(init);
await callRoute(
createAsset,
apiRequest(`/api/videos/${video.id}/assets`, {
body: {
provider: 'YOUTUBE',
sourceUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
reservationId: claimsOf(uploadToken).rid,
},
}),
{ videoId: video.id }
);
const second = await initUpload(scenario.project.id, BigInt(2) * GIB);
expect(second.status).toBe(507);
});
});
+2
View File
@@ -11,6 +11,7 @@
import { describe, expect, it } from 'vitest';
import { randomUUID } from 'crypto';
import { db } from '@/lib/db';
import { UPLOAD_RESERVATION_PURPOSES } from '@/lib/storage-quota';
import { cancelR2UploadSession, createR2UploadSession } from '@/lib/r2-upload-session';
import { seedProject } from '../factories';
@@ -101,6 +102,7 @@ describe('createR2UploadSession', () => {
data: {
billedUserId: scenario.owner.id,
sizeBytes: BigInt(4096),
purpose: UPLOAD_RESERVATION_PURPOSES.R2_VIDEO,
expiresAt: new Date(Date.now() + HOUR_MS),
},
});
+2
View File
@@ -16,6 +16,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { randomUUID } from 'crypto';
import { db } from '@/lib/db';
import { UPLOAD_RESERVATION_PURPOSES } from '@/lib/storage-quota';
import { deleteR2Object, deleteVideoObject, headVideoObject, readVideoObjectBytes } from '@/lib/r2';
import { createR2UploadToken } from '@/lib/r2-upload-token';
import { createR2UploadSession } from '@/lib/r2-upload-session';
@@ -515,6 +516,7 @@ describe('finalizeR2VideoUpload success', () => {
data: {
billedUserId: scenario.owner.id,
sizeBytes: BigInt(4096),
purpose: UPLOAD_RESERVATION_PURPOSES.R2_VIDEO,
expiresAt: new Date(Date.now() + 30 * 60 * 1000),
},
});
+79 -17
View File
@@ -15,6 +15,7 @@ import { db } from '@/lib/db';
import { getCachedUserBunnyStorage } from '@/lib/admin-stats';
import {
PLAN_STORAGE_LIMIT_BYTES,
UPLOAD_RESERVATION_PURPOSES,
enforceStorageQuota,
getUserStorageInfo,
getUserTotalStorageBytes,
@@ -83,7 +84,11 @@ describe('the trial ceiling', () => {
const user = await createUser();
await createUploadReservation({ billedUserId: user.id, sizeBytes: BigInt(2) * GIB });
const result = await reserveStorageQuota(user.id, BigInt(2) * GIB);
const result = await reserveStorageQuota(
user.id,
BigInt(2) * GIB,
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('error' in result).toBe(true);
expect((result as { error: Response }).error.status).toBe(507);
@@ -92,7 +97,11 @@ describe('the trial ceiling', () => {
it('still lets a trial account upload inside its own ceiling', async () => {
const user = await createUser();
const result = await reserveStorageQuota(user.id, BigInt(1) * GIB);
const result = await reserveStorageQuota(
user.id,
BigInt(1) * GIB,
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('reservationId' in result).toBe(true);
});
@@ -277,7 +286,11 @@ describe('reserveStorageQuota', () => {
it('writes a reservation row billed to the user with the requested size', async () => {
const user = await createSubscribedUser();
const result = await reserveStorageQuota(user.id, BigInt(4096));
const result = await reserveStorageQuota(
user.id,
BigInt(4096),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('reservationId' in result).toBe(true);
const reservation = await db.uploadReservation.findFirstOrThrow();
@@ -291,7 +304,11 @@ describe('reserveStorageQuota', () => {
vi.stubEnv('OPENFRAME_ENABLE_STRIPE', 'false');
const user = await createSubscribedUser();
const result = await reserveStorageQuota(user.id, BigInt(4096));
const result = await reserveStorageQuota(
user.id,
BigInt(4096),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect(result).toEqual({ reservationId: null });
expect(await db.uploadReservation.count()).toBe(0);
@@ -304,7 +321,11 @@ describe('reserveStorageQuota', () => {
sizeBytes: PLAN_STORAGE_LIMIT_BYTES - BigInt(1024),
});
const result = await reserveStorageQuota(user.id, BigInt(2048));
const result = await reserveStorageQuota(
user.id,
BigInt(2048),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('error' in result).toBe(true);
expect('error' in result && result.error.status).toBe(507);
@@ -321,7 +342,11 @@ describe('reserveStorageQuota', () => {
sizeBytes: PLAN_STORAGE_LIMIT_BYTES - BigInt(100),
});
const result = await reserveStorageQuota(scenario.owner.id, BigInt(200));
const result = await reserveStorageQuota(
scenario.owner.id,
BigInt(200),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('error' in result).toBe(true);
expect(await db.uploadReservation.count()).toBe(0);
@@ -331,7 +356,11 @@ describe('reserveStorageQuota', () => {
const user = await createSubscribedUser();
bunnyStorage({ [user.id]: Number(PLAN_STORAGE_LIMIT_BYTES - BigInt(1024)) });
const result = await reserveStorageQuota(user.id, BigInt(2048));
const result = await reserveStorageQuota(
user.id,
BigInt(2048),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('error' in result).toBe(true);
expect(await db.uploadReservation.count()).toBe(0);
@@ -345,7 +374,11 @@ describe('reserveStorageQuota', () => {
expiresInMs: -60_000,
});
const result = await reserveStorageQuota(user.id, BigInt(2048));
const result = await reserveStorageQuota(
user.id,
BigInt(2048),
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('reservationId' in result).toBe(true);
});
@@ -358,7 +391,11 @@ describe('reserveStorageQuota', () => {
sizeBytes: PLAN_STORAGE_LIMIT_BYTES - BigInt(1024),
});
const result = await reserveStorageQuota(light.id, BigInt(10) * GIB);
const result = await reserveStorageQuota(
light.id,
BigInt(10) * GIB,
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
expect('reservationId' in result).toBe(true);
});
@@ -375,8 +412,8 @@ describe('reserveStorageQuota', () => {
expect(headroom(used)).toBe(BigInt(30) * GIB);
const [first, second] = await Promise.all([
reserveStorageQuota(user.id, request),
reserveStorageQuota(user.id, request),
reserveStorageQuota(user.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
reserveStorageQuota(user.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
]);
const granted = [first, second].filter((result) => 'reservationId' in result);
@@ -401,8 +438,8 @@ describe('reserveStorageQuota', () => {
const request = BigInt(20) * GIB;
const results = await Promise.all([
reserveStorageQuota(user.id, request),
reserveStorageQuota(user.id, request),
reserveStorageQuota(user.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
reserveStorageQuota(user.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
]);
expect(results.every((result) => 'reservationId' in result)).toBe(true);
@@ -417,7 +454,9 @@ describe('reserveStorageQuota', () => {
const request = BigInt(20) * GIB;
const results = await Promise.all(
Array.from({ length: 5 }, () => reserveStorageQuota(user.id, request))
Array.from({ length: 5 }, () =>
reserveStorageQuota(user.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO)
)
);
const granted = results.filter((result) => 'reservationId' in result);
@@ -438,8 +477,8 @@ describe('reserveStorageQuota', () => {
const request = BigInt(150) * GIB;
const results = await Promise.all([
reserveStorageQuota(first.id, request),
reserveStorageQuota(second.id, request),
reserveStorageQuota(first.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
reserveStorageQuota(second.id, request, UPLOAD_RESERVATION_PURPOSES.R2_VIDEO),
]);
expect(results.every((result) => 'reservationId' in result)).toBe(true);
@@ -450,7 +489,11 @@ describe('reserveStorageQuota', () => {
describe('releaseStorageReservation', () => {
it('deletes the reservation and frees the headroom', async () => {
const user = await createSubscribedUser();
const result = await reserveStorageQuota(user.id, BigInt(10) * GIB);
const result = await reserveStorageQuota(
user.id,
BigInt(10) * GIB,
UPLOAD_RESERVATION_PURPOSES.R2_VIDEO
);
const reservationId = 'reservationId' in result ? result.reservationId : null;
expect(reservationId).toBeTruthy();
expect(await getUserTotalStorageBytes(user.id)).toBe(BigInt(10) * GIB);
@@ -484,6 +527,25 @@ describe('releaseStorageReservation', () => {
expect(await db.uploadReservation.count()).toBe(1);
});
// The same scoping in the other direction. Every hold an account owns is
// billed to the same user, so `billedUserId` alone does not separate them: an
// image being attached would release a Bunny upload that was still in flight
// if it could name it, and the ids are readable by the client.
it('refuses to delete a reservation opened for a different flow', async () => {
const owner = await createSubscribedUser();
const reservation = await createUploadReservation({
billedUserId: owner.id,
sizeBytes: BigInt(4096),
purpose: UPLOAD_RESERVATION_PURPOSES.BUNNY,
});
await releaseStorageReservation(reservation.id, owner.id, UPLOAD_RESERVATION_PURPOSES.IMAGE);
expect(await db.uploadReservation.count()).toBe(1);
await releaseStorageReservation(reservation.id, owner.id, UPLOAD_RESERVATION_PURPOSES.BUNNY);
expect(await db.uploadReservation.count()).toBe(0);
});
});
describe('GET /api/settings/storage', () => {
+4
View File
@@ -7,6 +7,7 @@ import {
type VideoVersion,
} from '@prisma/client';
import { db } from '@/lib/db';
import { UPLOAD_RESERVATION_PURPOSES, type UploadReservationPurpose } from '@/lib/storage-quota';
import { nextSeq } from './seq';
export interface CreateVideoInput {
@@ -105,6 +106,8 @@ export interface CreateUploadReservationInput {
sizeBytes: bigint;
/** Milliseconds from now. Negative values produce an already-expired row. */
expiresInMs?: number;
/** Which flow the hold belongs to. Only that flow can consume it. */
purpose?: UploadReservationPurpose;
}
export async function createUploadReservation(
@@ -115,6 +118,7 @@ export async function createUploadReservation(
billedUserId: input.billedUserId,
sizeBytes: input.sizeBytes,
expiresAt: new Date(Date.now() + (input.expiresInMs ?? 30 * 60 * 1000)),
purpose: input.purpose ?? UPLOAD_RESERVATION_PURPOSES.R2_VIDEO,
},
});
}
+1
View File
@@ -67,6 +67,7 @@ const REVIEWED_MIGRATIONS = [
'20260614160000_add_project_allow_downloads',
'20260627140000_add_video_upload_multipart_id',
'20260801120000_add_acquisition_analytics',
'20260818120000_add_upload_reservation_purpose',
];
/** Objects POST_PUSH_SQL must have produced. Verified after it runs. */
+109
View File
@@ -0,0 +1,109 @@
// The grant a share-link visitor gets for a direct upload.
//
// A guest's upload is billed to the workspace owner, not to the guest, so this
// token is the only thing tying what they declared and what they hold to the
// upload it was issued for. Two claims matter here beyond the existing subject
// binding: the provider's own video id, which is what makes releasing the hold
// on the guest's say-so safe, and the declared size, which is what the asset is
// charged until Bunny reports a figure of its own.
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
createGuestUploadToken,
readGuestUploadGrant,
verifyGuestUploadToken,
} from '@/lib/guest-upload-token';
const SUBJECT = {
projectId: 'project-1',
videoId: 'video-1',
intent: 'bunny' as const,
context: '203.0.113.7:public',
};
const BUNNY_VIDEO_ID = 'bunnyvideo-1-abcdefgh';
beforeEach(() => {
vi.stubEnv('GUEST_UPLOAD_TOKEN_SECRET', 'test-guest-upload-token-secret');
});
describe('readGuestUploadGrant', () => {
it('carries back the reservation and the declared size it was signed with', () => {
const token = createGuestUploadToken({
...SUBJECT,
providerVideoId: BUNNY_VIDEO_ID,
reservationId: 'reservation-1',
declaredSizeBytes: BigInt(4096),
});
expect(readGuestUploadGrant(token, SUBJECT, BUNNY_VIDEO_ID)).toEqual({
reservationId: 'reservation-1',
declaredSizeBytes: BigInt(4096),
});
});
// The binding that makes a guest release safe: presenting this token to cancel
// deletes the upload it stands for, so it cannot be used to drop the hold of
// an upload that is still running.
it('refuses a grant presented against a different provider video', () => {
const token = createGuestUploadToken({
...SUBJECT,
providerVideoId: BUNNY_VIDEO_ID,
reservationId: 'reservation-1',
});
expect(readGuestUploadGrant(token, SUBJECT, 'bunnyvideo-2-abcdefgh')).toBeNull();
expect(readGuestUploadGrant(token, SUBJECT, null)).toBeNull();
expect(verifyGuestUploadToken(token, SUBJECT, 'bunnyvideo-2-abcdefgh')).toBe(false);
});
it('still refuses a grant for another subject, bound video or not', () => {
const token = createGuestUploadToken({
...SUBJECT,
providerVideoId: BUNNY_VIDEO_ID,
reservationId: 'reservation-1',
});
expect(
readGuestUploadGrant(token, { ...SUBJECT, videoId: 'video-2' }, BUNNY_VIDEO_ID)
).toBeNull();
expect(readGuestUploadGrant(token, { ...SUBJECT, intent: 'image' }, BUNNY_VIDEO_ID)).toBeNull();
expect(
readGuestUploadGrant(token, { ...SUBJECT, context: '198.51.100.9:public' }, BUNNY_VIDEO_ID)
).toBeNull();
});
// The image and audio grants carry none of this, and a grant issued before the
// claims existed keeps working rather than failing an upload in flight.
it('reads a grant with no claims as holding nothing', () => {
const token = createGuestUploadToken({ ...SUBJECT, intent: 'image' });
const subject = { ...SUBJECT, intent: 'image' as const };
expect(readGuestUploadGrant(token, subject)).toEqual({
reservationId: null,
declaredSizeBytes: null,
});
expect(verifyGuestUploadToken(token, subject, BUNNY_VIDEO_ID)).toBe(true);
});
it('refuses a forged signature', () => {
const token = createGuestUploadToken({
...SUBJECT,
providerVideoId: BUNNY_VIDEO_ID,
reservationId: 'reservation-1',
});
const [payload] = token.split('.');
expect(readGuestUploadGrant(`${payload}.forged`, SUBJECT, BUNNY_VIDEO_ID)).toBeNull();
});
it('reads a non-positive declared size as nothing declared', () => {
const token = createGuestUploadToken({
...SUBJECT,
providerVideoId: BUNNY_VIDEO_ID,
declaredSizeBytes: BigInt(-1),
});
expect(readGuestUploadGrant(token, SUBJECT, BUNNY_VIDEO_ID)?.declaredSizeBytes).toBeNull();
});
});