[QUESTION] Strategies for limiting upload file size
See original GitHub issueDescription
I’m trying to create an upload endpoint. I want to limit the maximum size that can be uploaded.
My endpoint looks like this:
@app.post('/upload', response_model=UploadedFileDTO)
async def upload_file(file: UploadFile = File(...), db: Session = Depends(get_db_session)):
save_path = local.generate_path(file.filename)
with file.file as f:
local.save(stream=f, save_path=save_path)
u = Upload(filename=file.filename,
path=str(save_path))
db.add(u)
db.commit()
return u
I checked out the source for fastapi.params.File
, but it doesn’t seem to add anything over fastapi.params.Form
.
The only solution that came to my mind is to start saving the uploaded file in chunks, and when the read size exceeds the limit, raise an exception. But I’m wondering if there are any idiomatic ways of handling such scenarios?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Changing the File Upload Size Limit - Jotform
We've made a quick reference list of the upload size limits as stated on Dropbox's and Google Drive's official pages. Note that these...
Read more >How can we go about deciding an appropriate filesize upload ...
I'm merely saying, determine why you need to set a limit first. If the only reason is truly because it's a required setting,...
Read more >Limit the size of a file upload (html input element)
Save this question. Show activity on this post. I would like to simply limit the size of a file that a user can...
Read more >File upload size - MoodleDocs
Upload file size restrictions. Probably the most frequently asked question on moodle.org is "How do I increase the upload file size limit?
Read more >How to Increase the Max Upload Size in WordPress - Kinsta
If you're facing issues with uploading files and file size limits, reaching out to your hosting provider for assistance is one of the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks everyone for the discussion here!
So, here’s the thing, a file is not completely sent to the server and received by your FastAPI app before the code in the path operation starts to execute.
So, you don’t really have an actual way of knowing the actual size of the file before reading it.
You could require the
Content-Length
header and check it and make sure that it’s a valid value. E.g.And then you could re-use that
valid_content_length
dependency in other places if you need to.⚠️ but it probably won’t prevent an attacker from sending a valid
Content-Length
header and a body bigger than what your app can take ⚠️Another option would be to, on top of the header, read the data in chunks. And once it’s bigger than a certain size, throw an error.
E.g.
Ok, I’ve found an acceptable solution. But it relies on
Content-Length
header being present.Edit: I’ve added a check to reject requests without
Content-Length
using it is quite straightforward:
The server sends HTTP 413 response when the upload size is too large, but I’m not sure how to handle if there’s no
Content-Length
header. Edit: Solution: Send 411 response