Skip to content

Commit 498aa80

Browse files
committed
fix: read TIFF dimensions from raw IFD tags for pre-decode validation
UTIF.decode populates raw tag entries (t256/t257) but .width/.height are only set after decodeImage. Read from raw tags so the pixel limit guard works before the expensive decode step without rejecting valid files.
1 parent 1c061d6 commit 498aa80

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

packages/super-editor/src/core/super-converter/v3/handlers/wp/helpers/tiff-converter.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ export function convertTiffToPng(data) {
9090
const ifds = UTIF.decode(buffer);
9191
if (!ifds || ifds.length === 0) return null;
9292

93-
// Validate dimensions from IFD metadata before decoding pixel data
94-
const { width, height } = ifds[0];
95-
if (!width || !height || width * height > MAX_PIXEL_COUNT) return null;
93+
// Validate dimensions from raw IFD tags before decoding pixel data.
94+
// UTIF.decode populates tag entries (t256=ImageWidth, t257=ImageLength)
95+
// but .width/.height are only set after decodeImage.
96+
const ifdWidth = ifds[0].t256?.[0];
97+
const ifdHeight = ifds[0].t257?.[0];
98+
if (!ifdWidth || !ifdHeight || ifdWidth * ifdHeight > MAX_PIXEL_COUNT) return null;
9699

97100
// Decode pixel data for the first page
98101
UTIF.decodeImage(buffer, ifds[0]);
99102
const rgba = UTIF.toRGBA8(ifds[0]);
100103
if (!rgba || rgba.length === 0) return null;
101104

105+
const { width, height } = ifds[0];
106+
102107
// Render to canvas and export as PNG
103108
const canvas = createCanvas();
104109
if (!canvas) {

packages/super-editor/src/core/super-converter/v3/handlers/wp/helpers/tiff-converter.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ describe('tiff-converter', () => {
6161
});
6262

6363
it('returns null for TIFF with dimensions exceeding pixel limit', () => {
64-
// Mock utif2 to return oversized dimensions (100k × 10k = 1 billion pixels)
64+
// Mock utif2 to return oversized dimensions via raw IFD tags
65+
// (t256=ImageWidth, t257=ImageLength — 100k × 10k = 1 billion pixels)
6566
vi.doMock('utif2', () => ({
66-
decode: () => [{ width: 100_000, height: 10_000 }],
67+
decode: () => [{ t256: [100_000], t257: [10_000] }],
6768
decodeImage: () => undefined,
6869
toRGBA8: () => new Uint8Array(4),
6970
}));

0 commit comments

Comments
 (0)