suave icon indicating copy to clipboard operation
suave copied to clipboard

Addition of optional file-part sink to stream multipart uploads without temp files

Open phillidgithub opened this issue 7 months ago • 1 comments

When a client uploads a file via multipart/form-data, Suave unconditionally buffers each file part to a temp file on disk before the WebPart ever runs. See readFilePart in src/Suave/ConnectionFacade.fs (lines 34–105): it calls Path.GetTempFileName(), opens a FileStream, and writes every socket chunk delivered by reader.readUntilPattern into that file. The resulting HttpUpload (src/Suave/Runtime.fs:110) only exposes tempFilePath — no stream, no buffer, no hook. This forces consumers to accept a plaintext copy of the file on disk before they can do anything with it, then re-read the temp file to encrypt / virus-scan / forward / re-chunk it, and delete the temp file on cleanup. For use cases like at-rest encryption, AV scanning, direct streaming to object storage (S3/Azure Blob), or hashing on ingest, the plaintext temp file is unwanted (and could be an issue for high security use cases). The alternative rawForm path has its own problem: it buffers the entire body into a single byte[] via Array.zeroCreate contentLength (src/Suave/ConnectionFacade.fs:229), which trades disk for RAM and doesn't scale beyond ~1 GiB / bounded concurrency.

Proposal

Add an optional file-part sink to SuaveConfig / HttpRuntime. When configured, readFilePart calls the sink factory to obtain a Stream for each incoming file part and writes the socket chunks directly to that stream, bypassing the temp file entirely. When not configured, behavior is byte-identical to today. The shape would be a small record on SuaveConfig (e.g. filePartSink : FilePartSink option) exposing three callbacks: one to open a Stream for a given part (given field name, file name, MIME type, headers), one invoked on successful completion to produce an HttpUpload, and one invoked on failure so the consumer can clean up partial state. In readFilePart (src/Suave/ConnectionFacade.fs:34), the existing temp-file logic becomes the default sink, and the chunk callback passed to reader.readUntilPattern writes into the sink-provided Stream instead of a hard-coded FileStream. Both success and error paths (currently File.Delete tempFilePath on lines 64, 69, 101) delegate to the sink's completion/abort callbacks.

Benefits

True streaming interception — chunks flow socket → user-supplied Stream without an intermediate plaintext file. Enables common patterns out of the box: CryptoStream wrapping a FileStream to write the encrypted file directly; streaming to an S3 / Azure Blob multipart upload as parts arrive; on-the-fly hashing, AV scanning via pipe to ClamAV, or rejecting uploads mid-stream. No disk footprint for deployments that don't want plaintext on local storage (e.g. hardened / ephemeral containers). Backwards compatible — opt-in via SuaveConfig; existing consumers see identical behavior. Fix flows through parseMultipartMixed (src/Suave/ConnectionFacade.fs:107) as well, since it shares readFilePart.

Hope this is something that could be considered for a future update.

Thank you, David

phillidgithub avatar Apr 22 '26 09:04 phillidgithub

Looks great, thank you for adding this feature

phillidgithub avatar Apr 24 '26 07:04 phillidgithub