Repository navigation
Add a secondary (more strict) timeout for decoding - #16
Conversation
9a6565f to
ec473c6
Compare
| /** 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 |
There was a problem hiding this comment.
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.
|
The two red jobs are unrelated to each other.
The decode timer starts in
100ms needs to be much larger (neighbouring tests give the overall timeout 10s), or the test shouldn't race against spawn.
Reviewed via styfle's devbox |
…line wrapping in worker/src/main.rs
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.