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);
});
});