/** Fully decode all raster pixels with npm-lockfile-pinned sharp; no network. */ import sharp from 'sharp'; import { realpath, readFile } from 'node:fs/promises'; import { resolve, sep } from 'node:path'; import { fileURLToPath } from 'node:url'; sharp.cache(false); sharp.concurrency(1); /** Decode every PNG-backed ICO directory member, not just its largest frame. */ export async function decodeIco(bytes) { if (bytes.length < 6 || bytes.length > 25 * 1024 * 1024 || bytes.readUInt16LE(0) !== 0 || bytes.readUInt16LE(2) !== 1) throw new Error('Invalid ICO header'); const count = bytes.readUInt16LE(4), ranges = [], frames = []; if (count < 1 || count > 64 || bytes.length < 6 + count * 16) throw new Error('Invalid ICO directory'); for (let i = 0; i < count; i++) { const at = 6 + i * 16, width = bytes[at] || 256, height = bytes[at + 1] || 256; const size = bytes.readUInt32LE(at + 8), offset = bytes.readUInt32LE(at + 12), end = offset + size; if (size < 8 || offset < 6 + count * 16 || end > bytes.length || ranges.some(([a, b]) => offset < b && end > a)) throw new Error('Invalid ICO member bounds/overlap'); ranges.push([offset, end]); const png = bytes.subarray(offset, end); if (!png.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) throw new Error('ICO member must be PNG-backed; unsupported DIB is not skipped'); const image = sharp(png, { failOn: 'error', limitInputPixels: 65536 }); const { info } = await image.raw().toBuffer({ resolveWithObject: true }); if (info.width !== width || info.height !== height) throw new Error('ICO directory/pixels dimension mismatch'); frames.push({ width, height, decodedBytes: info.size }); } return { width: Math.max(...frames.map(f => f.width)), height: Math.max(...frames.map(f => f.height)), format: 'ico', pages: count, decodedBytes: frames.reduce((n, f) => n + f.decodedBytes, 0), frames }; } export async function decodeBatch({ root, files }) { if (sharp.versions.sharp !== '0.34.5') { throw new Error(`Expected lockfile sharp0.34.5, found ${sharp.versions.sharp}`); } if (typeof root !== 'string' || !Array.isArray(files) || files.length > 10000) { throw new Error('Expected a public root and a bounded file array'); } const boundary = await realpath(root); const results = []; for (const file of files) { try { const candidate = await realpath(resolve(boundary, file)); if (!candidate.startsWith(boundary + sep)) throw new Error('Raster path outside public root'); if (candidate.toLowerCase().endsWith('.ico')) { const decoded = await decodeIco(await readFile(candidate)); results.push({ file, type: 'raster', valid: true, ...decoded, decoder: `sharp${sharp.versions.sharp}/all-ICO-PNG-frames` }); continue; } const image = sharp(candidate, { failOn: 'error', animated: true, limitInputPixels: 64000000 }); const metadata = await image.metadata(); // metadata() alone is insufficient: force actual decode of every pixel. const { info } = await image.raw().toBuffer({ resolveWithObject: true }); results.push({ file, type: 'raster', valid: true, width: info.width, height: info.height, format: metadata.format, pages: metadata.pages ?? 1, decodedBytes: info.size, decoder: `sharp${sharp.versions.sharp}/libvips${sharp.versions.vips}` }); } catch (error) { results.push({ file, type: 'raster', valid: false, error: String(error.message), decoder: 'sharp0.34.5' }); } } return { decoder: 'sharp', version: sharp.versions.sharp, results }; } if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { try { const chunks = []; let size = 0; for await (const chunk of process.stdin) { size += chunk.length; if (size > 4000000) throw new Error('Raster manifest exceeds 4MB'); chunks.push(chunk); } const report = await decodeBatch(JSON.parse(Buffer.concat(chunks).toString('utf8'))); process.stdout.write(JSON.stringify(report) + '\n'); if (report.results.some(item => !item.valid)) process.exitCode = 1; } catch (error) { process.stderr.write(String(error.stack ?? error) + '\n'); process.exitCode = 2; } }