diff options
| author | Adam Soltys <[email protected]> | 2026-09-26 22:45:43 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-09-26 22:45:43 -0700 |
| commit | 0912f443cc738af7dd7093670f00420de72a095f (patch) | |
| tree | 8626fdd5f9e66d6c06765659963ac137d59b0015 /lib/image-worker.ts | |
| parent | 56aef551bf72eb306d96b4b81b657c4a05037eb5 (diff) | |
| download | coinos-server-0912f443cc738af7dd7093670f00420de72a095f.tar.gz coinos-server-0912f443cc738af7dd7093670f00420de72a095f.zip | |
upload: decode images in a throwaway child process and tighten input checks
Malformed HEIF uploads were corrupting the heap inside libvips/libheif and
taking the whole app down with them, since sharp ran in-process. Move the
decode into a per-upload child (lib/image-worker.ts, spawned by lib/image.ts
with a hard timeout) so a decoder crash fails one request instead of the
server, and never load sharp in the main process.
Also: only accept jpeg/png/webp/gif as sniffed by file-type instead of any
image/* subtype, decode with failOnError and a pixel cap, refuse uploads
from blacklisted accounts (a freeze previously only blocked sends), and
rate-limit uploads per account.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Diffstat (limited to 'lib/image-worker.ts')
| -rw-r--r-- | lib/image-worker.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/image-worker.ts b/lib/image-worker.ts new file mode 100644 index 00000000..d45927d1 --- /dev/null +++ b/lib/image-worker.ts @@ -0,0 +1,29 @@ +// Image conversion runs in THIS separate process, spawned per upload by +// lib/image.ts. It is the only place sharp/libvips is ever loaded. Reads the +// raw upload on stdin, writes the resized webp to stdout, exits 0. Any +// decoder bug (libheif/libwebp/libpng heap corruption, SIGSEGV, SIGILL) kills +// this process alone — the app keeps serving. 2026-09-26: a stream of corrupt +// HEIF uploads crash-looped the whole server when this ran in-process. +const sharp = require("sharp"); + +const MAX_PIXELS = 80_000_000; + +const width = Number(process.argv[2]); +if (!Number.isInteger(width) || width <= 0 || width > 4096) { + console.error("bad width"); + process.exit(2); +} + +try { + const input = Buffer.from(await new Response(Bun.stdin.stream()).arrayBuffer()); + const out = await sharp(input, { failOnError: true, limitInputPixels: MAX_PIXELS }) + .rotate() + .resize(width) + .webp() + .toBuffer(); + await Bun.write(Bun.stdout, out); + process.exit(0); +} catch (e) { + console.error(e?.message || String(e)); + process.exit(2); +} |
