Skip to content

Add a secondary (more strict) timeout for decoding - #16

Merged
bgw merged 3 commits into
mainfrom
bgw/decode-timeout
Nov 16, 2026
Merged

bgw merged 3 commits into
mainfrom
bgw/decode-timeout

Conversation

@bgw

@bgw bgw commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

The default Next.js timeout for image decoding is 7s. Paired with a malicious image that exploits time complexity bugs in a codec, that could still be enough to DoS some applications.

We want to set a generous default limit, because we don't want to abort valid requests, and we don't know how fast or slow the application server is.

We can assume that decoding should be much faster than encoding. We can also assume that it's much harder to exploit time complexity bugs in an encoder than a decoder. By this logic, we should have a tighter timeout on decoding.

We can do this because we don't try to stream decoding. libvips couldn't easily do this because it'll often try to stream the decoder to the encoder (e.g. for non-progressive JPEGs).

How?

We add a new message to the IPC that indicates decoding has finished, and disable the decoder timeout when we get that message.

If you get RCE inside the worker, you could send the message sooner, but you'd still be subject to the overall timeout. If the worst you could get from and RCE is a DoS, I'm happy with that.

Refactoring

This adds more complexity to worker pool management.

Some common logic shared between native and wasm is moved into a base class in packages/nsio/worker-pool.ts.

@bgw
bgw requested review from andrewimm and styfle November 7, 2026 02:28
@bgw
bgw marked this pull request as ready for review November 7, 2026 02:28
@bgw
bgw force-pushed the bgw/decode-timeout branch from 9a6565f to ec473c6 Compare November 9, 2026 20:34
Comment thread packages/nsio/wasm-worker.ts Outdated
/** A message to the pool. Exactly one field is set. */
export interface WasmWorkerMessage {
/** The module decoded a transform's input (`nsio.decoded`), before responding. */
decoded?: true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using type: 'decoded' so you can extend more easily in the future. Also works better for TS discriminated union since decoded looks like it can't error for example.

@styfle styfle left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the doc comment edit in crates/worker/src/main.rs:11 leaves a 134-char line; the rest of the file wraps at 100.

Reviewed via styfle's devbox

@bgw
bgw enabled auto-merge (squash) November 12, 2026 16:42

styfle commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

The two red jobs are unrelated to each other.

Test x86_64-apple-darwin on macos-15 - node@20.9 is this PR. the decode timeout stops once the worker has decoded the input has now failed on both pushes of this branch, each time with ERR_TIMEOUT: decoding took longer than 100ms, but at a different assertion (the slow case on ec473c6, the metadata case on a279897) — so it's timing, not logic.

The decode timer starts in #assign before worker.send(), so the 100ms has to cover spawning the #!/bin/sh worker and getting its first write back. That matrix entry is x64 Node under Rosetta on arm64 hardware, where one spawn-and-respond already costs more than the whole budget:

same test macos-15 arm64 macos-15 x64
responses are passed through 12ms 138ms
a worker that sends an invalid response is replaced 48ms 874ms
workers are replaced after maxImagesPerWorker responses 88ms 1674ms

100ms needs to be much larger (neighbouring tests give the overall timeout 10s), or the test shouldn't race against spawn.

Test x86_64-unknown-linux-gnu on ubuntu-22.04 - node@20.9 is not. many parallel pipelines lives in safety.test.ts, which this PR doesn't touch; it passed on the previous push, and the only commit since is wasm-only. the worker crashed (signal SIGKILL) predates this branch — workers can be reused failed the same way in run 41541470637 on Nov 2.

Reviewed via styfle's devbox

@bgw
bgw merged commit d7afcf4 into main Nov 16, 2026
29 checks passed
@bgw
bgw deleted the bgw/decode-timeout branch November 16, 2026 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants