File upload size limit
I'm trying to implement max file upload size to the s3 bucket. I've handled the front-end but I worry that more advanced users could bypass this and spam my s3 bucket.
Here is what I've been trying on the backend (just added the conditions) but it still uploads files greater than MAX_UPLOAD_SIZE (1 * 1024 * 1024):
19 Replies
Hi @David. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain information about setting a maximum file upload size for an S3 bucket in the context of Wasp.
However, you might find it helpful to refer to the AWS S3 documentation or the Prisma documentation for more information on this topic.
For Wasp-related queries, you can refer to the Wasp documentation (https://wasp-lang.dev/docs) or ask the community on the Wasp Discord server (https://discord.com/invite/rzdnErX).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @David, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?
Forgot to mention, I'm trying to avoid the process of
uploading file to my server -> confirming it's size -> uploading to s3 bucket
. I'd like to keep it a single upload to s3.Wohooo @David, you just became a Waspeteer level 1!
I'm not an AWS expert, but I see online people mentioning
createPresignedPost
as a way to use the conditions: https://zaccharles.medium.com/s3-uploads-proxies-vs-presigned-urls-vs-presigned-posts-9661e2b37932
They say getSignedUrl
doesn't really honor the conditions 🤷♂️
Alternatively, you could upload the file to the server and then from the server to S3: https://gist.github.com/infomiho/ec379df4e33f3ae3410a251ba3aa81afMedium
S3 Uploads — Proxies vs Presigned URLs vs Presigned POSTs
What’s the best way to upload a file from a browser to S3? This post compares several options and provides an example codebase.
Gist
Uploading files with Wasp 0.12.3
Uploading files with Wasp 0.12.3. GitHub Gist: instantly share code, notes, and snippets.
Yeah I ended up trying to implement
createPresignedPost
, gonna finish it later today. I'm worried about the security thought. Is the POST just as safe as PUT or perhaps there are some important headers I should include with the presigned post?
actually I followed the exact same blog post you linked 🤣
thank you for your time! @mihoPOST vs PUT, it's just HTTP method names and they don't have any security implications by themselves, so I'm eager to say don't worry about it
Just got back from work and this is what seems to work (including the max file upload limit):
Also had to update my AWS CORS to allow POST:
Also you need to manually install "@aws-sdk/s3-presigned-post" as it doesn't come with Open SaaS by default.
Leaving this here in case someone finds it useful in the future.
P.S. I've been writing C# for past 10 years so pardon my poor typescript 😄
Also how do you go about handling the dev & production enviroments with CORS? Do I just set up 2 aws buckets one for dev that allows localhost:3000 and another one for production that allows the actual domain?
@kapa.ai Also how do you go about handling the dev & production enviroments with CORS? Do I just set up 2 aws buckets one for dev that allows localhost:3000 and another one for production that allows the actual domain?
Let me check the knowledge base for you. One moment! :books:
----------------------
Yes, you're on the right track. For handling different environments with CORS, you can indeed set up two separate AWS buckets, one for development and one for production.
In the development environment, you can allow
localhost:3000
and in the production environment, you can allow your actual domain.
You can manage this by setting the allowed origins in your CORS configuration based on the environment. This can be done by adding environment variables in your .env.server
file and using these variables in your CORS configuration.
Here's an example of how you can customize global middleware to add extra domains to CORS:
In this example, config.frontendUrl
could be your development or production domain, depending on the environment. You can set this value in your .env.server
file.
For more details, you can refer to the Wasp documentation on configuring middleware (https://wasp-lang.dev/docs/advanced/middleware-config#1-customize-global-middleware) and the Wasp documentation on accessing app config (https://wasp-lang.dev/docs/advanced/accessing-app-config#server-configuration-object).
Remember to keep your development and production environments as similar as possible to avoid unexpected behavior when deploying your application.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @miho, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Hm, not really what I was going for 😄
I like this kapa guy, he gets me 😄
Can't you set two domains for one single bucket? 🙂 That would work? But in a lot of places I worked, there were separate buckets for dev and prod just to ensure nothing got deleted by accident when developing stuff
yeah, but couldn't users "hack" the bucket by sending requests from their own localhost:3000 or smth? I guess I'm just paranoid about the security as I'm quite inexperienced with web dev.
by sending requests from their own localhost:3000 or smthThat's why you have presigned URLs that enable them to do a specific action (which you validated they can do on the server)
alright, fair enough.
But, it doesn't hurt to have multilpe layers of security 😄 if something bad happens like your private (dev) key leaks, you are protected to some degree
good point, gonna do this once I get to prod
Wohooo @David, you just became a Waspeteer level 2!
Thanks for the help!