Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex r4): decode-validate lossless PNG path + accept JPEG MIME aliases (#1763)

[._.]/ Adam Eivy53b2fa633b98
diff --git a/server/lib/imageClean.js b/server/lib/imageClean.js
index 67a37e0a3c..be73be9636 100644
--- a/server/lib/imageClean.js
+++ b/server/lib/imageClean.js
@@ -292,13 +292,21 @@ export async function cleanImageBuffer(buffer, options = {}) {
c2paStripped = hadC2PA;
steps.push({ step: 'metadata', status: 'applied', lossless: false, detail: 'orientation baked + dropped via re-encode (lossy)' });
} else {
- // Lossless PNG path: walk chunks and emit a new buffer minus the
- // metadata chunks. Pixels byte-identical, no decode/re-encode.
+ // Validate the pixel data decodes before returning the (lossless)
+ // original bytes — `.metadata()` only reads headers, so a PNG with a
+ // readable header but corrupt IDAT would otherwise pass through as a
+ // broken "cleaned" download. A raw decode (no re-encode) rejects the
+ // corruption while keeping the output byte-lossless, and yields the
+ // post-decode dims in the same pass.
+ const { info } = await sharp(buffer, { limitInputPixels: MAX_PIXELS })
+ .raw().toBuffer({ resolveWithObject: true });
+ // Lossless strip: walk chunks and emit a new buffer minus the metadata
+ // chunks. Pixels byte-identical, no re-encode.
const stripped = stripPngMetadataChunks(buffer);
outData = stripped.data;
c2paStripped = stripped.droppedTypes.includes('caBX');
- width = meta.width || null;
- height = meta.height || null;
+ width = info.width || null;
+ height = info.height || null;
steps.push({
step: 'metadata',
status: stripped.stripped ? 'applied' : 'noop',
diff --git a/server/lib/imageClean.test.js b/server/lib/imageClean.test.js
index 6a4c36d6d7..b0422a8c8a 100644
--- a/server/lib/imageClean.test.js
+++ b/server/lib/imageClean.test.js
@@ -347,6 +347,17 @@ describe('cleanImageBuffer (composable steps)', () => {
expect(result.height).toBe(32);
});
+ it('rejects a PNG with a readable header but corrupt pixel data on the lossless path', async () => {
+ // Header (IHDR) stays valid so .metadata() succeeds, but the IDAT stream is
+ // corrupted — the lossless path must decode-validate and reject it rather
+ // than pass the broken bytes through as a "cleaned" download.
+ const corrupt = Buffer.from(pngFixture);
+ const start = Math.floor(corrupt.length * 0.5);
+ for (let i = start; i < start + 40 && i < corrupt.length - 12; i += 1) corrupt[i] ^= 0xff;
+ await expect(cleanImageBuffer(corrupt, { metadata: true, denoise: false }))
+ .rejects.toMatchObject({ code: 'INVALID_IMAGE' });
+ });
+
it('reports the unavoidable metadata strip even when only denoise is selected', async () => {
const caBX = makePngChunk('caBX', Buffer.alloc(16, 0xCA));
const polluted = injectChunkBeforeIEND(pngFixture, caBX);
diff --git a/server/routes/imageClean.js b/server/routes/imageClean.js
index db81d2f4f5..df044bcace 100644
--- a/server/routes/imageClean.js
+++ b/server/routes/imageClean.js
@@ -43,7 +43,11 @@ const cleanQuerySchema = z.object({
// that base64 inflation is gone. The decompression-bomb guard (MAX_PIXELS) in
// the lib still protects the process regardless of byte size.
const RAW_LIMIT = '256mb';
-const RAW_TYPES = ['image/png', 'image/jpeg', 'image/webp', 'application/octet-stream'];
+// Include the common JPEG MIME aliases (image/jpg, image/pjpeg) so direct API
+// callers that send them get parsed; the bundled frontend forces
+// application/octet-stream, but other clients shouldn't have to. Format is
+// authoritatively decided by the magic-byte sniff, not the content-type.
+const RAW_TYPES = ['image/png', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/webp', 'application/octet-stream'];
router.post(
'/',