feat(media): albums — one link for a season or a photo album; fix path traversal in media get
An album is a sealed file (shade.album/v1) listing grants, so the relay cannot tell it from a film. `shade media put dir/` uploads every file and the album (subdirectories nest, natural order); `media get` brings an album back as a directory; /watch/ shows a grid with decrypted previews and Back returns to the album without putting grants in history. Kotlin reads albums (Albums.read) against a new shared vector. Fixed: `media get` used the sealer-chosen meta.name as the output path. Fixed a relay test race (flush() is sent, not received); the relay now reports framesIn. Verified: 64 TS tests x3, Kotlin 81/0, a 6-file album with a nested album uploaded, browsed, played and downloaded back bit-identical. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
112
packages/shade-media/tests/album.test.ts
Normal file
112
packages/shade-media/tests/album.test.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { createMediaRoutes, MemoryMediaStore } from '../src/server.js';
|
||||
import {
|
||||
ALBUM_MIME,
|
||||
MAX_ALBUM_ITEMS,
|
||||
MediaClient,
|
||||
MediaFormatError,
|
||||
OwnerKey,
|
||||
LivePublisher,
|
||||
encodeAlbum,
|
||||
isAlbum,
|
||||
openAlbum,
|
||||
openGrant,
|
||||
parseAlbum,
|
||||
} from '../src/index.js';
|
||||
import type { Grant, MediaGrant } from '../src/index.js';
|
||||
import { safeName } from '../src/cli.js';
|
||||
|
||||
const enc = new TextEncoder();
|
||||
|
||||
function serve() {
|
||||
const app = new Hono();
|
||||
app.route('/', createMediaRoutes(new MemoryMediaStore()));
|
||||
const server = Bun.serve({ port: 0, fetch: (req) => app.fetch(req) });
|
||||
return { server, base: `http://localhost:${server.port}` };
|
||||
}
|
||||
|
||||
describe('album format', () => {
|
||||
const live = new LivePublisher({ meta: { name: 'Kamera 1', mime: 'video/webm' } }).grant({ from: 'now' });
|
||||
|
||||
test('round trip keeps every grant, media and live, byte for byte', async () => {
|
||||
const { server, base } = serve();
|
||||
try {
|
||||
const client = new MediaClient(base, OwnerKey.generate());
|
||||
const a = await client.uploadBytes(enc.encode('one'), { meta: { name: 'E01.mkv', mime: 'video/x-matroska' } });
|
||||
const b = await client.uploadBytes(enc.encode('two'), { meta: { name: 'cover.jpg', mime: 'image/jpeg' } });
|
||||
const items: Grant[] = [a, b, live];
|
||||
const back = parseAlbum(encodeAlbum({ title: 'Sesong 1 — «Vinter»', items }));
|
||||
expect(back.title).toBe('Sesong 1 — «Vinter»');
|
||||
expect(back.items).toEqual(items);
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
|
||||
test('strict: wrong kind, bad items, too many items, junk', () => {
|
||||
const wrap = (o: unknown) => enc.encode(JSON.stringify(o));
|
||||
expect(() => parseAlbum(wrap({ kind: 'shade.album/v2', title: '', items: [] }))).toThrow(MediaFormatError);
|
||||
expect(() => parseAlbum(wrap({ kind: 'shade.album/v1', title: 1, items: [] }))).toThrow(/title/);
|
||||
expect(() => parseAlbum(wrap({ kind: 'shade.album/v1', title: '', items: [{ kind: 'shade.media-grant/v1' }] }))).toThrow(/item 0/);
|
||||
expect(() => parseAlbum(wrap({ kind: 'shade.album/v1', title: '', items: ['x'] }))).toThrow(/item 0/);
|
||||
expect(() => parseAlbum(wrap({ kind: 'shade.album/v1', title: '', items: new Array(MAX_ALBUM_ITEMS + 1).fill({}) }))).toThrow(/more than/);
|
||||
expect(() => parseAlbum(enc.encode('{"kind":'))).toThrow(MediaFormatError);
|
||||
expect(() => parseAlbum(new Uint8Array([0xff, 0xfe]))).toThrow(MediaFormatError);
|
||||
expect(() => encodeAlbum({ title: 'x'.repeat(513), items: [] })).toThrow(/title/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('albums over the media routes', () => {
|
||||
test('upload items + album; one grant opens all; the relay sees one more opaque object', async () => {
|
||||
const { server, base } = serve();
|
||||
try {
|
||||
const client = new MediaClient(base, OwnerKey.generate());
|
||||
const files = ['E01', 'E02', 'E10'].map((n, i) => ({ name: `${n}.webm`, bytes: enc.encode(`episode ${i}`.repeat(1000)) }));
|
||||
const grants: MediaGrant[] = [];
|
||||
for (const f of files) grants.push(await client.uploadBytes(f.bytes, { meta: { name: f.name, mime: 'video/webm' } }));
|
||||
const inner = await client.uploadAlbum({ title: 'Extras', items: [grants[0]!] });
|
||||
const album = await client.uploadAlbum({ title: 'Sesong 1', items: [...grants, inner] });
|
||||
|
||||
expect(isAlbum(album)).toBe(true);
|
||||
expect(isAlbum(grants[0])).toBe(false);
|
||||
// The grant alone is enough: open the album, then every item in it.
|
||||
const opened = await openAlbum(album);
|
||||
expect(opened.title).toBe('Sesong 1');
|
||||
expect(opened.items.map((g) => g.meta?.name)).toEqual(['E01.webm', 'E02.webm', 'E10.webm', 'Extras']);
|
||||
for (const [i, f] of files.entries()) {
|
||||
const r = await openGrant(opened.items[i] as MediaGrant);
|
||||
expect(Buffer.from(await r.read(0, r.size)).equals(Buffer.from(f.bytes))).toBe(true);
|
||||
}
|
||||
expect((await openAlbum(opened.items[3] as MediaGrant)).items[0]).toEqual(grants[0]!);
|
||||
// A film is not an album.
|
||||
await expect(openAlbum(grants[0]!)).rejects.toThrow(/not an album/);
|
||||
|
||||
// What the relay holds: five sealed objects of plain sizes, no names, no
|
||||
// types, nothing linking the album to its items.
|
||||
const list = await client.list();
|
||||
expect(list.items.length).toBe(5);
|
||||
const raw = await (await fetch(album.url!, { headers: { 'x-shade-read': Buffer.from(album.readToken!).toString('base64url') } })).arrayBuffer();
|
||||
const text = Buffer.from(raw).toString('latin1');
|
||||
for (const leak of ['Sesong', 'E01', ALBUM_MIME, 'shade.album', 'shade.media-grant']) expect(text.includes(leak)).toBe(false);
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeName: names from a sealed file never become paths', () => {
|
||||
test.each([
|
||||
['film.mkv', 'film.mkv'],
|
||||
['../../.bashrc', 'bashrc'],
|
||||
['/etc/passwd', 'passwd'],
|
||||
['C:\\Windows\\evil.dll', 'evil.dll'],
|
||||
['..', 'fallback'],
|
||||
['.hidden', 'hidden'],
|
||||
['a\u0000b\u001fc\u007f.txt', 'abc.txt'],
|
||||
[' ', 'fallback'],
|
||||
[undefined, 'fallback'],
|
||||
] as const)('%p → %p', (input, want) => {
|
||||
expect(safeName(input, 'fallback')).toBe(want);
|
||||
});
|
||||
});
|
||||
@@ -191,7 +191,9 @@ describe('live relay over real WebSockets', () => {
|
||||
for (let i = 0; i < 30; i++) {
|
||||
await client.send({ type: 'chunk', bytes: new Uint8Array([i]) }, { keyframe: i % 10 === 0 });
|
||||
}
|
||||
await client.flush();
|
||||
// flush() means sent, not received: wait until the relay holds all 31,
|
||||
// or a fast viewer joins mid-burst and (correctly) gets an older backlog.
|
||||
await until(() => relay.stats().framesIn === 31);
|
||||
|
||||
const early: ReceivedFrame[] = [];
|
||||
const grant = parseGrant(encodeGrant(client.grant('oldest'))) as LiveGrant;
|
||||
|
||||
Reference in New Issue
Block a user