import { Injectable, InternalServerErrorException, Logger, ServiceUnavailableException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import sharp from 'sharp'; import type { AppConfig } from '../../config/app-config.schema.js'; import { MetricsService } from '../../metrics/metrics.service.js'; import { ImageRejectedError } from './image-inspector.js'; // One libvips worker per decode and no operation cache: decodes are one-off, // and memory must stay predictable under the per-replica limit. sharp.cache(false); sharp.concurrency(1); // Failures of the replica itself, not of the image: they must not turn into a // client 415. libvips spills large images to a temp file unless // VIPS_DISC_THRESHOLD (compose.yaml) keeps them in memory; a full /tmp or an // allocation failure ends up here. export const SERVER_FAULT = /no space left|out of memory|unable to allocate|cannot allocate|write failed|unable to open|tmp/iu; /** * Full decode check. ImageInspector only proves the container structure * (signature, chunk CRCs, markers, dimensions); a PNG with a garbage zlib stream * or a JPEG without scan data passes it. Decoding every scanline with * failOn=warning rejects those, including truncated or corrupted compressed * data. A downscale to a thumbnail is the sink: it decodes all input but keeps * far less in memory than a full-size pipeline (measured in * evidence/lab4/decode-memory.txt). Decodes are bounded by * IMAGE_DECODE_MAX_CONCURRENT; a waiter whose request was aborted leaves the * queue and releases its buffer. */ @Injectable() export class ImageDecoder { private readonly logger = new Logger(ImageDecoder.name); private readonly maxPixels: number; private readonly maxConcurrent: number; private active = 0; private readonly waiting = new Set<() => void>(); constructor( config: ConfigService, private readonly metrics: MetricsService, ) { this.maxPixels = config.getOrThrow('MAX_IMAGE_PIXELS'); this.maxConcurrent = config.getOrThrow('IMAGE_DECODE_MAX_CONCURRENT'); } async verify(data: Buffer, signal?: AbortSignal): Promise { await this.acquire(signal); const started = performance.now(); try { await sharp(data, { failOn: 'warning', sequentialRead: true, limitInputPixels: this.maxPixels, }) .resize(64, 64, { fit: 'inside' }) .toBuffer(); } catch (error) { const message = error instanceof Error ? error.message : String(error); if (SERVER_FAULT.test(message)) { this.metrics.increment('attachments.decode.server_error'); this.logger.error( `Image decode failed on the server: ${message}`, ); throw new InternalServerErrorException(); } this.metrics.increment('attachments.decode.rejected'); throw new ImageRejectedError( 'unsupported_media_type', 'Image data cannot be decoded', ); } finally { this.metrics.add( 'attachments.decode.ms', performance.now() - started, ); this.release(); } } private acquire(signal?: AbortSignal): Promise { const abandoned = new ServiceUnavailableException( 'Image check was abandoned', ); if (signal?.aborted) return Promise.reject(abandoned); if (this.active < this.maxConcurrent) { this.active += 1; return Promise.resolve(); } const { promise, resolve, reject } = Promise.withResolvers(); const cancel = () => { this.waiting.delete(grant); reject(abandoned); }; const grant = () => { signal?.removeEventListener('abort', cancel); resolve(); }; this.waiting.add(grant); signal?.addEventListener('abort', cancel, { once: true }); return promise; } private release(): void { const next = this.waiting.values().next(); if (next.done) { this.active -= 1; return; } this.waiting.delete(next.value); next.value(); } }