-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
UPDATE
I updated the title and added some more information on the importance of this issue here: #5412 (comment)
Describe the bug
⚠ Disclaimer
First of all I'm not a native english speaker so please forgive my sometimes bad english! ❤
Second of all I'm not entirely sure if this issue belongs to sveltejs/kit
or more to sveltejs/kit/adapter-node
but as they both are on the same repository I'm posting this issue here anyway.
Third of all I know there may be people saying this is more of a feature request than a bug. But in my opinion it's a bug as it's blocking me from using sveltekit in this project.
🐞 The bug
Sveltekit processes any multipart/form-data
that's hitting an POST endpoint completly in RAM before reaching out to the endpoint. This let's me and anyone who wants to enable file uploading run into 2 related but different problems.
- It's impossible to check if a file is acceptable (Filesize, Filetype, etc.) from the endpoint before the entire file is already in memory. Even by utilizing a hook before hitting the real endpoint the entire form-data gets parsed and loaded into memory before the hook is called.
- When transmitting large files (> 1GiB, 5GiB, 10GiB, etc.) the RAM will overflow leading to an out of memory error. This normaly can be avoided by streaming the files while parsing the form-data which leads to way lower ram utilization. Anyway because the entire form-data is directly completly read into memory by hitting the post handler enabled endpoint this can't be done in Sveltekit. As above a hook will not solve the problem as the entire form-data gets parsed and loaded into memory before the hook is called.
🌟A solution
I don't know if this is acceptable or not (maybe not because it will most likely introduce a breaking change) but a very simple solution for this problem would be to let the user decide if the body should be parsed for them. This in my opinion is the "cleanest way" of fixing this. As we already have the request
.formData()
, .text()
, .json()
, etc. functions in the Web APIs Standard.
Little side fact to this: first I thought my code was doing this behaviour because I used a .formData()
which for me was the place to do such a thing but currently it's doing it regardless of the existence of it.
Reproduction
Skeleton Project
A skeleton project which generates a ~1GiB big file inside the browser and sends it to the node server can be found on: https://github.com/UnlimitedBytes/sveltekit-issue-overflow-formdata
Can't recommend
Anyway I do not recommend using this to test the issue a much better way (which won't kill your browser ram too) would be to generate a random file on your os and upload it via a post form!
Better way
To generate a random file with ~1GiB on different operating systems use:
# Create 1 GiB file on windows
fsutil file createnew bigfile.bin 1073741824
# Create 1 GiB file on ubuntu/debian
head -c 1073741824 </dev/urandom >bigfile.bin
# Create 1 GiB file on macos
mkfile -n 1g bigfile.bin
A simple form to upload a file should look like so:
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file" /><br />
<button type="submit">Send File</button>
</form>
EDIT: This was a false assumption it is not needed to do anything with the data you can directly dump the data to the garbage collector! This only will hold the data some time longer for better visibility but the RAM usage will quickly increase anyway until the garbage collector kicks in.
In order to trigger the issue a POST endpoint handler is needed! So create an upload.js
and/or an upload.svelte
file with the following upload.js
content:
// upload.js
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export async function post() {
// Give enough time to check memory
await delay(10000);
return {};
}
Logs

System Info
System:
OS: Windows 10 10.0.19044
CPU: (8) x64 AMD Ryzen™ 7 5800X3D @ 4.50GHz
Memory: 6.05 GB / 32.00 GB
Binaries:
Node: 18.4.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.13.1 - ~\AppData\Roaming\npm\npm.CMD
Browsers:
Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.44)
Internet Explorer: 11.0.19041.1566
npmPackages:
@sveltejs/adapter-node: next => 1.0.0-next.78
@sveltejs/kit: next => 1.0.0-next.360
svelte: ^3.44.0 => 3.49.0
vite: ^2.9.13 => 2.9.13
Severity
blocking all usage of SvelteKit
MAJOR - SECRUITY ISSUE
Additional Information
No response