Files
OpenFrame/tests/api/lib-video-assets.test.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

569 lines
20 KiB
TypeScript

// Exercises lib/video-assets.ts directly.
//
// Two API suites (assets-authz, download-authz) already drive
// getVideoAssetAccessContext() through routes, but only ever at the granularity
// of a status code. That leaves the flags it computes indistinguishable from
// one another: a context that set all four booleans to `hasViewAccess` would
// pass every one of those tests. This file asserts on the flags themselves, and
// on the pure helpers around them that nothing else covers at all.
import { describe, expect, it } from 'vitest';
import { createShareSessionValue, getShareSessionCookieName } from '@/lib/share-session';
import {
canDeleteAssetForViewer,
extractAudioFileNameFromProxyUrl,
extractAudioKeyFromProxyUrl,
extractImageFileNameFromProxyUrl,
extractImageKeyFromProxyUrl,
extractVideoFileNameFromProxyUrl,
extractVideoKeyFromProxyUrl,
getVideoAssetAccessContext,
sanitizeAssetDisplayName,
SAFE_BUNNY_VIDEO_ID,
} from '@/lib/video-assets';
import { apiRequest } from '../helpers/request';
import { signedInAs, signedOut } from '../helpers/session';
import {
addProjectMember,
addWorkspaceMember,
createExpiredUser,
createShareLink,
createUser,
createVideo,
seedProject,
} from '../factories';
const IMAGE_URL = '/api/upload/image/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png';
const AUDIO_URL = '/api/upload/audio/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee2.webm';
const VIDEO_URL = '/api/upload/video/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee3.mp4';
describe('sanitizeAssetDisplayName', () => {
it('keeps an ordinary name unchanged', () => {
expect(sanitizeAssetDisplayName('B-roll take 2.mp4', 'fallback')).toBe('B-roll take 2.mp4');
});
// Brackets and parentheses are stripped because the name is interpolated into
// Markdown-ish notification bodies, where they would change the rendering.
it('strips brackets and parentheses', () => {
expect(sanitizeAssetDisplayName('[click](http://evil.test) shot', 'fallback')).toBe(
'clickhttp://evil.test shot'
);
});
// Control characters are stripped before whitespace is collapsed, so a
// newline leaves no gap behind where it used to be.
it('strips control characters', () => {
expect(sanitizeAssetDisplayName('take\u00001\u007Fsecond\nthird', 'fallback')).toBe(
'take1secondthird'
);
});
it('collapses runs of whitespace and trims the ends', () => {
expect(sanitizeAssetDisplayName(' take two \t three ', 'fallback')).toBe(
'take two three'
);
});
it.each<[string | null | undefined, string]>([
[null, 'a null value'],
[undefined, 'an undefined value'],
['', 'an empty string'],
[' ', 'only whitespace'],
['[]()', 'only stripped characters'],
[42 as unknown as string, 'a non-string value'],
])('falls back for %s (%s)', (value) => {
expect(sanitizeAssetDisplayName(value, 'Comment Image')).toBe('Comment Image');
});
it('truncates at 200 characters', () => {
const result = sanitizeAssetDisplayName('x'.repeat(500), 'fallback');
expect(result).toHaveLength(200);
expect(result).toBe('x'.repeat(200));
});
});
describe('proxy URL extraction', () => {
it('derives the image key and file name from a canonical image URL', () => {
expect(extractImageKeyFromProxyUrl(IMAGE_URL)).toBe(
'images/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png'
);
expect(extractImageFileNameFromProxyUrl(IMAGE_URL)).toBe(
'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png'
);
});
it('derives the audio key and file name from a canonical audio URL', () => {
expect(extractAudioKeyFromProxyUrl(AUDIO_URL)).toBe(
'voice/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee2.webm'
);
expect(extractAudioFileNameFromProxyUrl(AUDIO_URL)).toBe(
'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee2.webm'
);
});
it('derives the video key and file name from a canonical video URL', () => {
expect(extractVideoKeyFromProxyUrl(VIDEO_URL)).toBe(
'videos/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee3.mp4'
);
expect(extractVideoFileNameFromProxyUrl(VIDEO_URL)).toBe(
'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee3.mp4'
);
});
// Each extractor is anchored on its own prefix, so a URL of one media type
// must not resolve through another type's extractor.
it('refuses a URL from a different media prefix', () => {
expect(extractImageKeyFromProxyUrl(AUDIO_URL)).toBeNull();
expect(extractAudioKeyFromProxyUrl(VIDEO_URL)).toBeNull();
expect(extractVideoKeyFromProxyUrl(IMAGE_URL)).toBeNull();
});
it.each([
['/api/upload/image/../../videos/live.mp4', 'a traversal segment'],
['https://evil.test/api/upload/image/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png', 'a host'],
['/api/upload/image/not-a-uuid.png', 'a non-uuid basename'],
['/api/upload/image/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png/extra', 'a trailing segment'],
['', 'an empty string'],
])('refuses to derive an image key from %s (%s)', (url) => {
expect(extractImageKeyFromProxyUrl(url)).toBeNull();
expect(extractImageFileNameFromProxyUrl(url)).toBeNull();
});
});
// mediaUrlToR2Key used to live here. It matched the proxy prefix as a substring with no
// shape check, so `https://evil.test/api/upload/image/../../videos/live.mp4` produced the
// key `images/../../videos/live.mp4`. It had no callers, so it was deleted rather than
// reimplemented; the anchored extract* helpers below are what a caller should use.
describe('the anchored extractors refuse a substring match', () => {
it('rejects a hostile url that only contains the proxy prefix', () => {
const hostile = 'https://evil.test/api/upload/image/../../videos/live.mp4';
expect(extractImageKeyFromProxyUrl(hostile)).toBeNull();
expect(extractImageFileNameFromProxyUrl(hostile)).toBeNull();
});
it('still derives keys from canonical urls', () => {
expect(extractImageKeyFromProxyUrl(IMAGE_URL)).toBe(
'images/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee1.png'
);
expect(extractAudioKeyFromProxyUrl(AUDIO_URL)).toBe(
'voice/eeeeeeee-eeee-4eee-8eee-eeeeeeeeeee2.webm'
);
});
});
describe('SAFE_BUNNY_VIDEO_ID', () => {
it.each([
['abcd1234', true],
['a-b_c-d1', true],
['abcd123', false],
['abcd 1234', false],
['abcd/1234', false],
['../secret', false],
['a'.repeat(129), false],
['a'.repeat(128), true],
])('matches %s: %s', (value, expected) => {
expect(SAFE_BUNNY_VIDEO_ID.test(value)).toBe(expected);
});
});
describe('canDeleteAssetForViewer', () => {
const managed = {
canManageAssets: true,
viewerUserId: null,
viewerGuestIdentityId: null,
};
const signedIn = {
canManageAssets: false,
viewerUserId: 'user-1',
viewerGuestIdentityId: null,
};
const guest = {
canManageAssets: false,
viewerUserId: null,
viewerGuestIdentityId: 'guest-1',
};
it('lets a manager delete an asset they did not upload', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: 'someone-else', uploadedByGuestIdentityId: null },
managed
)
).toBe(true);
});
it('lets a signed-in uploader delete their own asset', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: 'user-1', uploadedByGuestIdentityId: null },
signedIn
)
).toBe(true);
});
it('refuses a signed-in non-manager somebody else asset', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: 'user-2', uploadedByGuestIdentityId: null },
signedIn
)
).toBe(false);
});
it('lets a guest delete the asset their own guest identity uploaded', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: null, uploadedByGuestIdentityId: 'guest-1' },
guest
)
).toBe(true);
});
it('refuses a guest another guest asset', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: null, uploadedByGuestIdentityId: 'guest-2' },
guest
)
).toBe(false);
});
// The `!viewer.viewerUserId` guard: a signed-in caller is judged on their user
// id alone, so a stale guest cookie carried alongside a session cannot widen
// what they may delete.
it('ignores a matching guest identity when the viewer is signed in', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: null, uploadedByGuestIdentityId: 'guest-1' },
{ canManageAssets: false, viewerUserId: 'user-1', viewerGuestIdentityId: 'guest-1' }
)
).toBe(false);
});
// Two nulls are not a match. Without the truthiness checks an anonymous
// viewer would be able to delete every anonymously uploaded asset.
it('refuses when both sides have no identity at all', () => {
expect(
canDeleteAssetForViewer(
{ uploadedByUserId: null, uploadedByGuestIdentityId: null },
{ canManageAssets: false, viewerUserId: null, viewerGuestIdentityId: null }
)
).toBe(false);
});
it('refuses when the asset has no uploader and the viewer is an identified guest', () => {
expect(
canDeleteAssetForViewer({ uploadedByUserId: null, uploadedByGuestIdentityId: null }, guest)
).toBe(false);
});
});
describe('getVideoAssetAccessContext', () => {
function shareCookies(videoId: string, token: string, passwordVerified = false) {
return {
[getShareSessionCookieName(videoId)]: createShareSessionValue(
token,
videoId,
passwordVerified
),
};
}
it('returns null for a video that does not exist', async () => {
signedOut();
expect(
await getVideoAssetAccessContext(apiRequest('/api/videos/nope/assets'), 'nope')
).toBeNull();
});
it('denies everything to an anonymous caller on a private project', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context).not.toBeNull();
expect(context?.hasViewAccess).toBe(false);
expect(context?.canUploadAssets).toBe(false);
expect(context?.canDownloadAssets).toBe(false);
expect(context?.canManageAssets).toBe(false);
expect(context?.viewerUserId).toBeNull();
});
it('grants everything to the project owner and echoes the project shape back', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE', allowDownloads: false });
const video = await createVideo({ projectId: scenario.project.id, title: 'Cut 3' });
signedInAs(scenario.owner);
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.hasViewAccess).toBe(true);
expect(context?.canUploadAssets).toBe(true);
// An editor may always download, whatever allowDownloads says.
expect(context?.canDownloadAssets).toBe(true);
expect(context?.canManageAssets).toBe(true);
expect(context?.viewerUserId).toBe(scenario.owner.id);
expect(context?.viewerGuestIdentityId).toBeNull();
expect(context?.video.id).toBe(video.id);
expect(context?.video.title).toBe('Cut 3');
expect(context?.video.projectId).toBe(scenario.project.id);
expect(context?.video.project.workspace.id).toBe(scenario.workspace.id);
expect(context?.video.project.workspace.ownerId).toBe(scenario.owner.id);
});
it('denies everything to a signed-in stranger', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
signedInAs(await createUser());
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.hasViewAccess).toBe(false);
expect(context?.canUploadAssets).toBe(false);
expect(context?.canDownloadAssets).toBe(false);
});
// canManageAssets is `access.canEdit`, which a COMMENTATOR does not have; the
// other three flags are separate computations and must not collapse onto it.
it('gives a project COMMENTATOR view and upload but not manage', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE', allowDownloads: false });
const video = await createVideo({ projectId: scenario.project.id });
const commentator = await createUser();
await addProjectMember({
projectId: scenario.project.id,
userId: commentator.id,
role: 'COMMENTATOR',
});
signedInAs(commentator);
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.hasViewAccess).toBe(true);
expect(context?.canUploadAssets).toBe(true);
expect(context?.canManageAssets).toBe(false);
// allowDownloads is off and the viewer cannot edit, so no export.
expect(context?.canDownloadAssets).toBe(false);
});
it('lets a non-editing member download once the project allows downloads', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE', allowDownloads: true });
const video = await createVideo({ projectId: scenario.project.id });
const commentator = await createUser();
await addProjectMember({
projectId: scenario.project.id,
userId: commentator.id,
role: 'COMMENTATOR',
});
signedInAs(commentator);
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.canDownloadAssets).toBe(true);
});
it('denies a workspace member once the workspace owner loses billing access', async () => {
const expiredOwner = await createExpiredUser();
const scenario = await seedProject({ visibility: 'PRIVATE', ownerUser: expiredOwner });
const video = await createVideo({ projectId: scenario.project.id });
const member = await createUser();
await addWorkspaceMember({
workspaceId: scenario.workspace.id,
userId: member.id,
role: 'COMMENTATOR',
});
signedInAs(member);
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.hasViewAccess).toBe(false);
expect(context?.canUploadAssets).toBe(false);
});
it('grants view but not upload to an anonymous holder of a VIEW share link', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
const link = await createShareLink({
projectId: scenario.project.id,
videoId: video.id,
permission: 'VIEW',
allowGuests: true,
});
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, {
cookies: shareCookies(video.id, link.token),
}),
video.id
);
expect(context?.hasViewAccess).toBe(true);
expect(context?.canUploadAssets).toBe(false);
expect(context?.canDownloadAssets).toBe(false);
expect(context?.canManageAssets).toBe(false);
});
it('grants upload to an anonymous holder of a COMMENT share link that allows guests', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
const link = await createShareLink({
projectId: scenario.project.id,
videoId: video.id,
permission: 'COMMENT',
allowGuests: true,
});
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, {
cookies: shareCookies(video.id, link.token),
}),
video.id,
'COMMENT'
);
expect(context?.hasViewAccess).toBe(true);
expect(context?.canUploadAssets).toBe(true);
expect(context?.canManageAssets).toBe(false);
});
// allowGuests off means the link only works for someone with an account, and
// the upload flag is where that shows up for an anonymous caller.
it('refuses upload to an anonymous COMMENT share when guests are not allowed', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
const link = await createShareLink({
projectId: scenario.project.id,
videoId: video.id,
permission: 'COMMENT',
allowGuests: false,
});
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, {
cookies: shareCookies(video.id, link.token),
}),
video.id,
'COMMENT'
);
expect(context?.canUploadAssets).toBe(false);
});
it('grants download through a share link that allows downloads', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE', allowDownloads: false });
const video = await createVideo({ projectId: scenario.project.id });
const link = await createShareLink({
projectId: scenario.project.id,
videoId: video.id,
permission: 'VIEW',
allowGuests: true,
allowDownloads: true,
});
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, {
cookies: shareCookies(video.id, link.token),
}),
video.id
);
expect(context?.canDownloadAssets).toBe(true);
});
// The cookie is bound to a video id and HMAC-signed, so a session minted for
// one video must not carry over to another.
it('ignores a share cookie minted for a different video', async () => {
const scenario = await seedProject({ visibility: 'PRIVATE' });
const video = await createVideo({ projectId: scenario.project.id });
const otherVideo = await createVideo({ projectId: scenario.project.id });
const link = await createShareLink({
projectId: scenario.project.id,
videoId: otherVideo.id,
permission: 'COMMENT',
allowGuests: true,
});
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, {
cookies: shareCookies(otherVideo.id, link.token),
}),
video.id,
'COMMENT'
);
expect(context?.hasViewAccess).toBe(false);
expect(context?.canUploadAssets).toBe(false);
});
it('reads the guest identity for an anonymous caller and drops it for a signed-in one', async () => {
const scenario = await seedProject({ visibility: 'PUBLIC' });
const video = await createVideo({ projectId: scenario.project.id });
// A signed cookie is the only shape getGuestIdentityFromRequest accepts, so
// it is minted the same way the comment routes mint it.
const { NextResponse } = await import('next/server');
const { setGuestIdentityCookie } = await import('@/lib/guest-identity');
const carrier = NextResponse.json({});
setGuestIdentityCookie(carrier, 'guest-identity-under-test');
const cookieValue = carrier.cookies.get('openframe_guest_identity')?.value ?? '';
const cookies = { openframe_guest_identity: cookieValue };
signedOut();
const anonymous = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, { cookies }),
video.id
);
expect(anonymous?.viewerGuestIdentityId).toBe('guest-identity-under-test');
expect(anonymous?.viewerUserId).toBeNull();
signedInAs(scenario.owner);
const authenticated = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`, { cookies }),
video.id
);
expect(authenticated?.viewerUserId).toBe(scenario.owner.id);
expect(authenticated?.viewerGuestIdentityId).toBeNull();
});
it('gives an anonymous caller view access to a PUBLIC project but no upload or manage', async () => {
const scenario = await seedProject({ visibility: 'PUBLIC' });
const video = await createVideo({ projectId: scenario.project.id });
signedOut();
const context = await getVideoAssetAccessContext(
apiRequest(`/api/videos/${video.id}/assets`),
video.id
);
expect(context?.hasViewAccess).toBe(true);
expect(context?.canUploadAssets).toBe(false);
expect(context?.canManageAssets).toBe(false);
expect(context?.canDownloadAssets).toBe(false);
});
});