-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(uploads): bound the HEIF fallback decode by declared pixels, not just bytes #6456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6b215d9
fix(uploads): bound the HEIF fallback decode by declared pixels, not …
waleedlatif1 5ff9d07
fix(uploads): free the HEIF decoder handles after the pixel check
waleedlatif1 df71c97
test(uploads): pin that all() reports dimensions before decoding
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| * | ||
| * The pixel ceiling in `transcodeHeicToJpeg`, tested against a stubbed decoder. | ||
| * | ||
| * Separate from `heic.test.ts` so that file keeps exercising the real WebAssembly | ||
| * decoder — mocking it there would retire the one test proving the dynamic import | ||
| * resolves. Reaching the guard for real would mean hand-building a HEVC-coded HEIF, | ||
| * which needs an encoder this repo does not ship; stubbing the declared dimensions | ||
| * tests the decision the guard actually makes. | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockAll, mockConvert } = vi.hoisted(() => ({ | ||
| mockAll: vi.fn(), | ||
| mockConvert: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('heic-decode', () => ({ all: mockAll, default: Object.assign(vi.fn(), { all: mockAll }) })) | ||
| vi.mock('heic-convert', () => ({ default: mockConvert })) | ||
|
|
||
| import { transcodeHeicToJpeg } from '@/lib/uploads/server/heic' | ||
|
|
||
| /** An ISO-BMFF `ftyp` box declaring a HEVC-coded HEIF still. */ | ||
| function heifHeader(): Buffer { | ||
| const header = Buffer.alloc(16) | ||
| header.writeUInt32BE(16, 0) | ||
| header.write('ftyp', 4, 'ascii') | ||
| header.write('heic', 8, 'ascii') | ||
| return header | ||
| } | ||
|
|
||
| const MAX_TRANSCODE_INPUT_PIXELS = 100_000_000 | ||
|
|
||
| /** `all()` returns live libheif handles plus the `dispose` that frees them. */ | ||
| function handles(sizes: Array<{ width: number; height: number }>) { | ||
| const dispose = vi.fn() | ||
| const list = sizes.map((size) => ({ ...size, decode: vi.fn() })) | ||
| return Object.assign(list, { dispose }) | ||
| } | ||
|
|
||
| describe('transcodeHeicToJpeg pixel ceiling', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockConvert.mockResolvedValue(Buffer.from('jpeg-bytes')) | ||
| }) | ||
|
|
||
| it.each([ | ||
| ['refused', [{ width: 30_000, height: 30_000 }]], | ||
| ['transcoded', [{ width: 8064, height: 6048 }]], | ||
| ])('frees the decoder handles when the image is %s', async (_outcome, sizes) => { | ||
| // `all()` leaves freeing to the caller, so skipping it leaks the libheif | ||
| // context on the WebAssembly heap once per preview. | ||
| const list = handles(sizes) | ||
| mockAll.mockResolvedValue(list) | ||
|
|
||
| await transcodeHeicToJpeg(heifHeader()) | ||
|
|
||
| expect(list.dispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('frees the decoder handles even when reading dimensions throws', async () => { | ||
| const list = handles([{ width: 100, height: 100 }]) | ||
| Object.defineProperty(list[0], 'width', { | ||
| get() { | ||
| throw new Error('handle went away') | ||
| }, | ||
| }) | ||
| mockAll.mockResolvedValue(list) | ||
|
|
||
| await transcodeHeicToJpeg(heifHeader()) | ||
|
|
||
| expect(list.dispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('refuses a container declaring more pixels than the ceiling', async () => { | ||
| // 30000x30000 is ~900MP — the decoder would allocate ~3.4GB before the codec | ||
| // is asked for anything, so the refusal has to happen on the declared size. | ||
| mockAll.mockResolvedValue(handles([{ width: 30_000, height: 30_000 }])) | ||
|
|
||
| expect(await transcodeHeicToJpeg(heifHeader())).toBeNull() | ||
| expect(mockConvert).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('refuses when any image in a multi-image container is oversized', async () => { | ||
| mockAll.mockResolvedValue( | ||
| handles([ | ||
| { width: 100, height: 100 }, | ||
| { width: 30_000, height: 30_000 }, | ||
| ]) | ||
| ) | ||
|
|
||
| expect(await transcodeHeicToJpeg(heifHeader())).toBeNull() | ||
| expect(mockConvert).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('transcodes a container at the ceiling', async () => { | ||
| mockAll.mockResolvedValue( | ||
| handles([{ width: MAX_TRANSCODE_INPUT_PIXELS / 10_000, height: 10_000 }]) | ||
| ) | ||
|
|
||
| expect(await transcodeHeicToJpeg(heifHeader())).toEqual(Buffer.from('jpeg-bytes')) | ||
| expect(mockConvert).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('transcodes an ordinary phone photo', async () => { | ||
| // A 48MP iPhone still, which must stay well inside the ceiling. | ||
| mockAll.mockResolvedValue(handles([{ width: 8064, height: 6048 }])) | ||
|
|
||
| expect(await transcodeHeicToJpeg(heifHeader())).toEqual(Buffer.from('jpeg-bytes')) | ||
| expect(mockConvert).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('never asks the stubbed handle to decode', async () => { | ||
| // The whole point of `all()` over `one()`: the decision is made before the | ||
| // raster is allocated. | ||
| const list = handles([{ width: 30_000, height: 30_000 }]) | ||
| mockAll.mockResolvedValue(list) | ||
|
|
||
| await transcodeHeicToJpeg(heifHeader()) | ||
|
|
||
| expect(list[0].decode).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * `heic-decode` ships no types. Only the surface we use is declared: `all()` | ||
| * reports each image's declared dimensions and defers the decode, which is what | ||
| * lets a caller refuse an oversized one before any raster is allocated. | ||
| */ | ||
| declare module 'heic-decode' { | ||
| interface DecodedHeifImage { | ||
| width: number | ||
| height: number | ||
| data: Uint8ClampedArray | ||
| } | ||
|
|
||
| interface HeifImageHandle { | ||
| width: number | ||
| height: number | ||
| decode: () => Promise<DecodedHeifImage> | ||
| } | ||
|
|
||
| /** | ||
| * `dispose` is non-enumerable on the returned array and is NOT optional: it frees | ||
| * the image handles and the libheif context, which `all()` — unlike the default | ||
| * export — leaves to the caller. Declared required so a caller cannot forget it. | ||
| */ | ||
| interface HeifImageHandles extends Array<HeifImageHandle> { | ||
| dispose: () => void | ||
| } | ||
|
|
||
| function decode(options: { buffer: Buffer }): Promise<DecodedHeifImage> | ||
|
|
||
| namespace decode { | ||
| function all(options: { buffer: Buffer }): Promise<HeifImageHandles> | ||
| } | ||
|
|
||
| export = decode | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.