Skip to content

Commit 72d3d13

Browse files
Matej Ukmarclaude
andcommitted
fix(server): read at most a track header's needed bytes
A malformed MP4 could declare a huge tkhd box and make issuing its asset URL read and allocate that much. Only a height-capped style without a known size no longer reaches the video, so its loaded size still applies. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 527289c commit 72d3d13

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎apps/server/src/assets/videoDimensions.test.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ const box = (type: string, ...children: ReadonlyArray<Uint8Array>) => {
2323
const IDENTITY = [0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000];
2424
const QUARTER_TURN = [0, 0x10000, 0, -0x10000, 0, 0, 0, 0, 0x40000000];
2525

26-
function tkhd(width: number, height: number, options: { version?: 0 | 1; matrix?: number[] } = {}) {
26+
function tkhd(
27+
width: number,
28+
height: number,
29+
options: { version?: 0 | 1; matrix?: number[]; padding?: number } = {},
30+
) {
2731
const matrixOffset = options.version === 1 ? 52 : 40;
28-
const body = new Uint8Array(matrixOffset + 44);
32+
const body = new Uint8Array(matrixOffset + 44 + (options.padding ?? 0));
2933
const data = new DataView(body.buffer);
3034
body[0] = options.version ?? 0;
3135
(options.matrix ?? IDENTITY).forEach((value, index) =>
@@ -61,10 +65,11 @@ const liveSegment = (...children: ReadonlyArray<Uint8Array>) =>
6165
concat(idBytes(0x18538067), UNKNOWN_SIZE, ...children);
6266

6367
async function dimensionsOf(bytes: Uint8Array) {
64-
return readVideoDimensions(
65-
async (offset, length) => bytes.subarray(offset, offset + length),
66-
bytes.length,
67-
);
68+
return readVideoDimensions(async (offset, length) => {
69+
// A box's declared size must never decide how much the parser reads.
70+
expect(length).toBeLessThanOrEqual(96);
71+
return bytes.subarray(offset, offset + length);
72+
}, bytes.length);
6873
}
6974

7075
describe("readVideoDimensions", () => {
@@ -92,6 +97,11 @@ describe("readVideoDimensions", () => {
9297
bytes: concat(ftyp, box("moov", box("trak", tkhd(640, 480, { version: 1 })))),
9398
expected: { width: 640, height: 480 },
9499
},
100+
{
101+
name: "a track header that declares a large body",
102+
bytes: concat(ftyp, box("moov", box("trak", tkhd(1080, 1920, { padding: 1 << 20 })))),
103+
expected: { width: 1080, height: 1920 },
104+
},
95105
{
96106
name: "a phone recording rotated a quarter turn",
97107
bytes: concat(ftyp, box("moov", box("trak", tkhd(1920, 1080, { matrix: QUARTER_TURN })))),

‎apps/server/src/assets/videoDimensions.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function readIsoBmff(read: ReadAt, fileSize: number): Promise<ImageDimensi
6868
for await (const child of isoBoxes(read, track.bodyStart, track.end)) {
6969
if (child.type !== "tkhd") continue;
7070
const dimensions = readTrackHeader(
71-
await read(child.bodyStart, child.end - child.bodyStart),
71+
await read(child.bodyStart, Math.min(TRACK_HEADER_BYTES, child.end - child.bodyStart)),
7272
);
7373
if (dimensions) return dimensions;
7474
break;
@@ -79,6 +79,9 @@ async function readIsoBmff(read: ReadAt, fileSize: number): Promise<ImageDimensi
7979
return null;
8080
}
8181

82+
// A version 1 `tkhd` ends its width and height 96 bytes into the body.
83+
const TRACK_HEADER_BYTES = 96;
84+
8285
function readTrackHeader(body: Uint8Array): ImageDimensions | null {
8386
const matrixOffset = body[0] === 1 ? 52 : 40;
8487
if (body.length < matrixOffset + 44) return null;

‎apps/web/src/components/chat/ChatMarkdownMedia.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,13 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
380380
: fallbackSrc + (props.srcFragment ?? "");
381381
const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
382382
const maxHeightRem = props.maxHeightRem ?? 30;
383-
const style =
383+
const sizedStyle =
384384
props.style ??
385385
(knownSize
386386
? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
387-
: maxHeightRem !== 30
388-
? { maxHeight: `${maxHeightRem}rem` }
389-
: undefined);
387+
: undefined);
388+
const style =
389+
sizedStyle ?? (maxHeightRem !== 30 ? { maxHeight: `${maxHeightRem}rem` } : undefined);
390390
const actionsSource: MediaActionSource = {
391391
kind: props.kind ?? "image",
392392
name: props.alt || (props.kind ?? "image"),
@@ -416,7 +416,7 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
416416
alt={props.alt}
417417
copyMarkdown={props.copyMarkdown}
418418
originalUrl={props.originalUrl}
419-
style={style}
419+
style={sizedStyle}
420420
mediaIdentity={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
421421
onRetry={refreshAssetUrl}
422422
actionsSource={actionsSource}

0 commit comments

Comments
 (0)