feat(media): @shade/media — seekable sealed media (SMF1), live channels, media routes and live relay
SMF1 seals a film, video, image or song once into a file any Range-capable store can serve; the reader fetches only segments under the playhead and checks each against a digest table pinned by the grant's root digest. Live channels use a sender-key chain with per-frame Ed25519 signatures, sticky init frames and keyframe snapshots for late joiners, and epoch rotation to remove a viewer. Server side: a disk-backed media store with signed owner uploads and a WebSocket relay with bounded backlog. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
197
packages/shade-media/tests/sealed.test.ts
Normal file
197
packages/shade-media/tests/sealed.test.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import {
|
||||
MediaReader,
|
||||
MediaSealer,
|
||||
sealMediaBytes,
|
||||
sealMediaStream,
|
||||
MIN_SEGMENT_SIZE,
|
||||
FOOTER_BYTES,
|
||||
decodeFooter,
|
||||
} from '../src/sealed.js';
|
||||
import { MemorySource } from '../src/source.js';
|
||||
import { MediaFormatError, MediaIntegrityError } from '../src/errors.js';
|
||||
|
||||
const S = MIN_SEGMENT_SIZE;
|
||||
|
||||
function pattern(n: number, seed = 1): Uint8Array {
|
||||
const out = new Uint8Array(n);
|
||||
let x = seed >>> 0 || 1;
|
||||
for (let i = 0; i < n; i++) {
|
||||
x ^= x << 13;
|
||||
x ^= x >>> 17;
|
||||
x ^= x << 5;
|
||||
out[i] = x & 0xff;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function sealed(plain: Uint8Array, segmentSize = S) {
|
||||
const { bytes, result } = await sealMediaBytes(plain, {
|
||||
segmentSize,
|
||||
meta: { mime: 'video/webm', name: 'test.webm' },
|
||||
});
|
||||
return { bytes, result, keys: { mediaId: result.mediaId, mediaKey: result.mediaKey, rootDigest: result.rootDigest } };
|
||||
}
|
||||
|
||||
describe('SMF1 round trip', () => {
|
||||
for (const n of [0, 1, S - 1, S, S + 1, 3 * S, 3 * S + 17]) {
|
||||
test(`${n} bytes`, async () => {
|
||||
const plain = pattern(n, n + 1);
|
||||
const { bytes, result, keys } = await sealed(plain);
|
||||
expect(result.plaintextSize).toBe(n);
|
||||
expect(result.sealedSize).toBe(bytes.length);
|
||||
expect(result.segmentCount).toBe(Math.max(1, Math.ceil(n / S)));
|
||||
const r = await MediaReader.open(new MemorySource(bytes), keys);
|
||||
expect(r.size).toBe(n);
|
||||
expect(r.meta).toEqual({ mime: 'video/webm', name: 'test.webm' });
|
||||
expect(await r.read(0, n)).toEqual(plain);
|
||||
});
|
||||
}
|
||||
|
||||
test('random access matches the plaintext at every boundary', async () => {
|
||||
const plain = pattern(5 * S + 123, 7);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
const r = await MediaReader.open(new MemorySource(bytes), keys, { cacheSegments: 0 });
|
||||
const points = [0, 1, S - 1, S, S + 1, 2 * S - 3, 4 * S, 5 * S, 5 * S + 122];
|
||||
for (const a of points) {
|
||||
for (const len of [1, 2, S, 2 * S + 5, 10 * S]) {
|
||||
expect(await r.read(a, len)).toEqual(plain.subarray(a, Math.min(plain.length, a + len)));
|
||||
}
|
||||
}
|
||||
expect((await r.read(plain.length, 10)).length).toBe(0);
|
||||
});
|
||||
|
||||
test('stream() yields the requested range', async () => {
|
||||
const plain = pattern(9 * S + 5, 3);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
const r = await MediaReader.open(new MemorySource(bytes), keys, { maxSegmentsPerFetch: 2 });
|
||||
const got: Uint8Array[] = [];
|
||||
for await (const c of r.stream(S + 7, 7 * S + 1)) got.push(c);
|
||||
expect(Buffer.concat(got)).toEqual(Buffer.from(plain.subarray(S + 7, 7 * S + 1)));
|
||||
});
|
||||
|
||||
test('streaming seal in odd chunks produces a readable file of the same size', async () => {
|
||||
const plain = pattern(4 * S + 999, 11);
|
||||
async function* chunks() {
|
||||
let off = 0;
|
||||
let k = 1;
|
||||
while (off < plain.length) {
|
||||
const n = Math.min(plain.length - off, (k++ * 977) % (2 * S) + 1);
|
||||
yield plain.subarray(off, off + n);
|
||||
off += n;
|
||||
}
|
||||
}
|
||||
const { stream, result } = sealMediaStream(chunks(), { segmentSize: S });
|
||||
const parts: Uint8Array[] = [];
|
||||
for await (const p of stream) parts.push(p);
|
||||
const bytes = new Uint8Array(Buffer.concat(parts));
|
||||
const res = await result;
|
||||
expect(res.sealedSize).toBe(bytes.length);
|
||||
const r = await MediaReader.open(new MemorySource(bytes), {
|
||||
mediaId: res.mediaId,
|
||||
mediaKey: res.mediaKey,
|
||||
rootDigest: res.rootDigest,
|
||||
});
|
||||
expect(await r.read(0, plain.length)).toEqual(plain);
|
||||
});
|
||||
|
||||
test('every seal draws a fresh key and id', async () => {
|
||||
const a = new MediaSealer({ segmentSize: S });
|
||||
const b = new MediaSealer({ segmentSize: S });
|
||||
expect(a.mediaKey).not.toEqual(b.mediaKey);
|
||||
expect(a.mediaId).not.toEqual(b.mediaId);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SMF1 tamper resistance', () => {
|
||||
test('a flipped ciphertext byte fails that segment', async () => {
|
||||
const plain = pattern(3 * S, 5);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
const bad = bytes.slice();
|
||||
bad[S + 100]! ^= 1; // inside segment 1
|
||||
const r = await MediaReader.open(new MemorySource(bad), keys);
|
||||
expect(await r.read(0, 10)).toEqual(plain.subarray(0, 10));
|
||||
await expect(r.read(S + 50, 10)).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
});
|
||||
|
||||
test('swapping two segments is caught', async () => {
|
||||
const plain = pattern(3 * S, 9);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
const seg = S + 16;
|
||||
const bad = bytes.slice();
|
||||
bad.set(bytes.subarray(0, seg), seg);
|
||||
bad.set(bytes.subarray(seg, 2 * seg), 0);
|
||||
const r = await MediaReader.open(new MemorySource(bad), keys);
|
||||
await expect(r.read(0, 1)).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
});
|
||||
|
||||
test('a key holder who rewrites the table is caught by the root digest', async () => {
|
||||
// Without the root digest a forged table could vouch for forged segments;
|
||||
// the grant pins the trailer, so even a holder of the key cannot.
|
||||
const plain = pattern(2 * S, 2);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
const bad = bytes.slice();
|
||||
bad[bad.length - FOOTER_BYTES - 1]! ^= 1; // last byte of the table
|
||||
await expect(MediaReader.open(new MemorySource(bad), keys)).rejects.toBeInstanceOf(
|
||||
MediaIntegrityError,
|
||||
);
|
||||
// …and without a root it still fails, at the segment.
|
||||
const r = await MediaReader.open(new MemorySource(bad), { mediaId: keys.mediaId, mediaKey: keys.mediaKey });
|
||||
await expect(r.read(S, 1)).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
});
|
||||
|
||||
test('truncation and a shrunk footer both fail closed', async () => {
|
||||
const plain = pattern(3 * S, 4);
|
||||
const { bytes, keys } = await sealed(plain);
|
||||
await expect(
|
||||
MediaReader.open(new MemorySource(bytes.subarray(0, bytes.length - 1)), keys),
|
||||
).rejects.toThrow();
|
||||
// Rewrite the footer to claim two segments: the second becomes "last",
|
||||
// which its AAD says it is not.
|
||||
const f = decodeFooter(bytes.subarray(bytes.length - FOOTER_BYTES));
|
||||
expect(f.plaintextSize).toBe(3 * S);
|
||||
const footer = bytes.slice(bytes.length - FOOTER_BYTES);
|
||||
new DataView(footer.buffer).setBigUint64(12, BigInt(2 * S), false);
|
||||
const r = await MediaReader.open(
|
||||
new MemorySource(
|
||||
new Uint8Array(
|
||||
Buffer.concat([
|
||||
bytes.subarray(0, 2 * (S + 16)),
|
||||
bytes.subarray(3 * (S + 16), 3 * (S + 16) + 16 + 32 * 2), // meta + 2 digests
|
||||
footer,
|
||||
]),
|
||||
),
|
||||
),
|
||||
{ mediaId: keys.mediaId, mediaKey: keys.mediaKey },
|
||||
).catch((e) => e);
|
||||
if (r instanceof Error) {
|
||||
expect(r).toBeInstanceOf(Error);
|
||||
} else {
|
||||
await expect(r.read(S, 1)).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
}
|
||||
});
|
||||
|
||||
test('wrong key, wrong id, bad magic', async () => {
|
||||
const { bytes, keys } = await sealed(pattern(100));
|
||||
await expect(
|
||||
MediaReader.open(new MemorySource(bytes), { ...keys, mediaKey: new Uint8Array(32) }),
|
||||
).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
await expect(
|
||||
MediaReader.open(new MemorySource(bytes), { ...keys, mediaId: new Uint8Array(16) }),
|
||||
).rejects.toBeInstanceOf(MediaIntegrityError);
|
||||
const bad = bytes.slice();
|
||||
bad[bad.length - 1] = 0;
|
||||
await expect(MediaReader.open(new MemorySource(bad), keys)).rejects.toBeInstanceOf(
|
||||
MediaFormatError,
|
||||
);
|
||||
});
|
||||
|
||||
test('invalid metadata is refused at seal time', async () => {
|
||||
await expect(sealMediaBytes(new Uint8Array(1), { meta: { mime: 'not a mime' } })).rejects.toBeInstanceOf(
|
||||
MediaFormatError,
|
||||
);
|
||||
await expect(sealMediaBytes(new Uint8Array(1), { meta: { name: 'a\nb' } })).rejects.toBeInstanceOf(
|
||||
MediaFormatError,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user