fix(gui): harden multimodal image attachments

This commit is contained in:
Yichen Jiang
2026-07-23 19:38:37 +08:00
parent eea595fcb4
commit 580e05b794
61 changed files with 1700 additions and 214 deletions

View File

@@ -0,0 +1,93 @@
import { describe, expect, it } from 'vitest'
import { detectImage } from '../src/image.ts'
function bytes(text: string): number[] {
return [...Buffer.from(text, 'ascii')]
}
function webp(chunk: string, mutate: (data: Uint8Array) => void): Uint8Array {
const data = new Uint8Array(30)
data.set(bytes('RIFF'), 0)
data.set([22, 0, 0, 0], 4)
data.set(bytes('WEBP'), 8)
data.set(bytes(chunk), 12)
mutate(data)
return data
}
describe('raster header detection', () => {
it('detects PNG dimensions', () => {
const data = Uint8Array.from(Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'base64',
))
expect(detectImage(data)).toEqual({ mediaType: 'image/png', width: 1, height: 1 })
})
it('detects both GIF revisions and rejects zero dimensions', () => {
expect(detectImage(Uint8Array.from([...bytes('GIF87a'), 3, 0, 2, 0])))
.toEqual({ mediaType: 'image/gif', width: 3, height: 2 })
expect(detectImage(Uint8Array.from([...bytes('GIF89a'), 4, 0, 5, 0])))
.toEqual({ mediaType: 'image/gif', width: 4, height: 5 })
expect(() => detectImage(Uint8Array.from([...bytes('GIF89a'), 0, 0, 1, 0])))
.toThrow(/positive/)
expect(() => detectImage(Uint8Array.from([...bytes('GIF89a'), 1, 0, 0, 0])))
.toThrow(/positive/)
})
it('walks JPEG marker forms and reports malformed dimensions', () => {
const sof = [0xff, 0xc0, 0, 7, 8, 0, 2, 0, 3]
expect(detectImage(Uint8Array.from([0xff, 0xd8, ...sof])))
.toEqual({ mediaType: 'image/jpeg', width: 3, height: 2 })
expect(detectImage(Uint8Array.from([
0xff, 0xd8,
0xe0, 0, 2,
0x01,
0xff, ...sof,
]))).toEqual({ mediaType: 'image/jpeg', width: 3, height: 2 })
expect(() => detectImage(Uint8Array.from([0xff, 0xd8, 0xd9, 0, 0, 0])))
.toThrow(/missing/)
expect(() => detectImage(Uint8Array.from([0xff, 0xd8, 0xff, 0xff, 0xff, 0xff])))
.toThrow(/missing/)
expect(() => detectImage(Uint8Array.from([0xff, 0xd8, 0xe0, 0, 1, 0])))
.toThrow(/truncated/)
expect(() => detectImage(Uint8Array.from([0xff, 0xd8, 0xe0, 0, 9, 0])))
.toThrow(/truncated/)
expect(() => detectImage(Uint8Array.from([0xff, 0xd8, 0xc0, 0, 6, 0, 0, 0, 0])))
.toThrow(/dimensions are truncated/)
})
it('detects each WebP header and rejects truncated or unknown chunks', () => {
expect(detectImage(webp('VP8X', (data) => {
data.set([2, 0, 0], 24)
data.set([3, 0, 0], 27)
}))).toEqual({ mediaType: 'image/webp', width: 3, height: 4 })
expect(detectImage(webp('VP8L', (data) => {
data[20] = 0x2f
data.set([2, 0, 1, 0], 21)
}))).toEqual({ mediaType: 'image/webp', width: 3, height: 5 })
expect(detectImage(webp('VP8 ', (data) => {
data.set([0x9d, 0x01, 0x2a], 23)
data.set([6, 0, 7, 0], 26)
}))).toEqual({ mediaType: 'image/webp', width: 6, height: 7 })
const truncated = webp('VP8X', () => {})
truncated[4] = 23
expect(() => detectImage(truncated)).toThrow(/truncated/)
expect(() => detectImage(webp('NOPE', () => {}))).toThrow(/dimensions are missing/)
expect(() => detectImage(webp('VP8L', () => {}))).toThrow(/dimensions are missing/)
expect(() => detectImage(webp('VP8 ', () => {}))).toThrow(/dimensions are missing/)
})
it('rejects unrecognized bytes and near-miss signatures', () => {
expect(() => detectImage(new Uint8Array(0))).toThrow(/Unsupported/)
expect(() => detectImage(Uint8Array.from([...bytes('GIFxxa'), 1, 0, 1, 0])))
.toThrow(/Unsupported/)
const nearWebp = webp('VP8X', () => {})
nearWebp[8] = 0
expect(() => detectImage(nearWebp)).toThrow(/Unsupported/)
})
})

View File

@@ -0,0 +1,39 @@
import { Context } from 'cordis'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import LocalAttachmentStore, {
DEFAULT_MAX_IMAGE_BYTES,
DEFAULT_MAX_IMAGE_PIXELS,
DEFAULT_MAX_IMAGES_PER_MESSAGE,
DEFAULT_MAX_MESSAGE_IMAGE_BYTES,
} from '../src/index.ts'
describe('local attachment service', () => {
it('resolves every omitted admission limit explicitly', () => {
const service = new LocalAttachmentStore(new Context(), {})
expect(service.imageLimits).toEqual({
maxImageBytes: DEFAULT_MAX_IMAGE_BYTES,
maxImagesPerMessage: DEFAULT_MAX_IMAGES_PER_MESSAGE,
maxMessageImageBytes: DEFAULT_MAX_MESSAGE_IMAGE_BYTES,
maxImagePixels: DEFAULT_MAX_IMAGE_PIXELS,
mediaTypes: ['image/png', 'image/jpeg', 'image/webp', 'image/gif'],
})
})
it('saves and reads through the service boundary', async () => {
const dshHome = await mkdtemp(join(tmpdir(), 'dsh-attachment-service-'))
try {
const service = new LocalAttachmentStore(new Context(), { dshHome })
const data = Uint8Array.from(Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
'base64',
))
const ref = await service.saveImage({ data, mediaType: 'image/png' })
await expect(service.readImage(ref)).resolves.toEqual({ ref, data })
} finally {
await rm(dshHome, { recursive: true, force: true })
}
})
})

View File

@@ -54,11 +54,21 @@ describe('local attachment store', () => {
expect(new Uint8Array(await readFile(object))).toEqual(PNG)
expect((await stat(object)).mode & 0o777).toBe(0o600)
expect((await stat(join(storageRoot, 'objects', sha256.slice(0, 2)))).mode & 0o777).toBe(0o700)
await expect(readImageFile(storageRoot, first, LIMITS)).resolves.toEqual({ ref: first, data: PNG })
await expect(readImageFile(storageRoot, first)).resolves.toEqual({ ref: first, data: PNG })
})
it('keeps admitted history readable after deployment limits become stricter', async () => {
const storageRoot = await root()
const ref = await saveImageFile(storageRoot, { data: PNG, mediaType: 'image/png' }, LIMITS)
await expect(readImageFile(storageRoot, ref)).resolves.toEqual({ ref, data: PNG })
})
it('rejects malformed bytes, mismatched declarations, byte limits, and decoded-pixel limits', async () => {
const storageRoot = await root()
await expect(saveImageFile(storageRoot, {
data: new Uint8Array(0), mediaType: 'image/png',
}, LIMITS)).rejects.toMatchObject({ code: 'INVALID_IMAGE' })
await expect(saveImageFile(storageRoot, {
data: Uint8Array.of(1, 2, 3), mediaType: 'image/png',
}, LIMITS)).rejects.toMatchObject({ code: 'INVALID_IMAGE' })
@@ -74,6 +84,10 @@ describe('local attachment store', () => {
await expect(saveImageFile(storageRoot, {
data: wide, mediaType: 'image/png',
}, LIMITS)).rejects.toMatchObject({ code: 'IMAGE_TOO_MANY_PIXELS' })
const unnamed = await saveImageFile(storageRoot, {
data: PNG, mediaType: 'image/png', name: '\u0000',
}, LIMITS)
expect(unnamed).not.toHaveProperty('name')
})
it('fails closed when an object is missing, corrupted, or addressed by an invalid reference', async () => {
@@ -83,14 +97,45 @@ describe('local attachment store', () => {
const object = join(storageRoot, 'objects', sha256.slice(0, 2), sha256)
await chmod(object, 0o600)
await writeFile(object, Uint8Array.of(1, 2, 3))
await expect(readImageFile(storageRoot, ref, LIMITS))
await expect(readImageFile(storageRoot, ref))
.rejects.toMatchObject({ code: 'ATTACHMENT_CORRUPT' })
await expect(readImageFile(storageRoot, { ...ref, attachmentId: 'bad' as never }, LIMITS))
await expect(readImageFile(storageRoot, { ...ref, attachmentId: 'bad' as never }))
.rejects.toMatchObject({ code: 'INVALID_ATTACHMENT_REF' })
const missingRoot = await root()
await mkdir(missingRoot, { recursive: true })
await expect(readImageFile(missingRoot, ref, LIMITS))
await expect(readImageFile(missingRoot, ref))
.rejects.toMatchObject({ code: 'ATTACHMENT_NOT_FOUND' })
const unreadableRoot = await root()
const target = join(unreadableRoot, 'objects', sha256.slice(0, 2), sha256)
await mkdir(target, { recursive: true })
await expect(readImageFile(unreadableRoot, ref))
.rejects.toMatchObject({ code: 'ATTACHMENT_READ_FAILED' })
})
it('rejects conflicting existing objects and reference metadata mismatches', async () => {
const storageRoot = await root()
const sha256 = createHash('sha256').update(PNG).digest('hex')
const target = join(storageRoot, 'objects', sha256.slice(0, 2), sha256)
await mkdir(join(storageRoot, 'objects', sha256.slice(0, 2)), { recursive: true })
await writeFile(target, Uint8Array.of(1, 2, 3))
await expect(saveImageFile(storageRoot, { data: PNG, mediaType: 'image/png' }, LIMITS))
.rejects.toMatchObject({ code: 'ATTACHMENT_CORRUPT' })
await writeFile(target, PNG)
const ref = await saveImageFile(storageRoot, { data: PNG, mediaType: 'image/png' }, LIMITS)
await expect(readImageFile(storageRoot, { ...ref, width: ref.width + 1 }))
.rejects.toMatchObject({ code: 'ATTACHMENT_CORRUPT' })
})
it('maps unexpected publication failures to a stable storage error', async () => {
const storageRoot = await root()
const sha256 = createHash('sha256').update(PNG).digest('hex')
const target = join(storageRoot, 'objects', sha256.slice(0, 2), sha256)
await mkdir(target, { recursive: true })
await expect(saveImageFile(storageRoot, { data: PNG, mediaType: 'image/png' }, LIMITS))
.rejects.toMatchObject({ code: 'ATTACHMENT_WRITE_FAILED' })
})
})