Skip to content

Commit 6143369

Browse files
Media: Read HEIC brands only from inside the File Type Box
The 64-byte read overshoots a short box into the next one, whose item types are also four ASCII characters, so a word there could pass for a brand. Bound the compatible-brand scan by the box's own size. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VCEHrF9CTU4qb12bHG1z55
1 parent 5b1a83c commit 6143369

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎packages/upload-media/src/test/is-heic-buffer.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ describe( 'isHeicBuffer', () => {
6060
false
6161
);
6262
} );
63+
64+
it( 'reads brands only from inside the File Type Box', () => {
65+
// A short box, then the start of the next one. Item types in the meta
66+
// box are also four ASCII characters, so a word there can spell an
67+
// AVIF brand without the file being one.
68+
const ftyp = new Uint8Array( buildFtyp( 'heic', 'mif1' ) );
69+
const next = [ ...'\0\0\0\x18metaavif' ].map( ( character ) =>
70+
character.charCodeAt( 0 )
71+
);
72+
73+
expect(
74+
isHeicBuffer( new Uint8Array( [ ...ftyp, ...next ] ).buffer )
75+
).toBe( true );
76+
} );
6377
} );
6478

6579
describe( 'isHeicFile', () => {

‎packages/upload-media/src/utils.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ const AVIF_BRANDS = [ 'avif', 'avis' ];
211211
*
212212
* 1. Checks for the `ftyp` marker at offset 4.
213213
* 2. Collects the brands the box declares — the major brand at offset 8, then
214-
* the compatible brands from offset 16 on, four ASCII characters each.
214+
* the compatible brands from offset 16 up to the box size, four ASCII
215+
* characters each.
215216
* 3. Returns true when a HEIC/HEIF brand is present and no AVIF brand is.
216217
*
217218
* The browser derives `File.type` from the file extension alone, so this is the
@@ -241,9 +242,15 @@ export function isHeicBuffer( buffer: ArrayBuffer ): boolean {
241242
return false;
242243
}
243244

245+
// Anything past the box size belongs to the next box, not to the brands.
246+
const boxEnd = Math.min(
247+
view.length,
248+
new DataView( buffer ).getUint32( 0 )
249+
);
250+
244251
// The major brand, then every compatible brand after the minor version.
245252
const brands = [ readBrand( 8 ) ];
246-
for ( let offset = 16; offset + 4 <= view.length; offset += 4 ) {
253+
for ( let offset = 16; offset + 4 <= boxEnd; offset += 4 ) {
247254
brands.push( readBrand( offset ) );
248255
}
249256

0 commit comments

Comments
 (0)