On limiting serverless functions with Gatsby Cloud #32805
-
Hey everyone! I have gone through the documentation regarding Functions with Gatsby Cloud.
I have a feeling that these concerns might be trivial to the more experienced developer. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is a really great question, thanks for asking!
First off, you should determine if this is in your threat model. If you don't expect this to be an issue then I wouldn't think too much about it. That said, the solution to the problem is a combination of authentication and rate limiting. You essentially need to track the state of valid user connections and ensure that not too many are being processed. A hypothetical scenario could work like this:
All that said, you may see one flaw in the above. What if a user just keeps flooding connections to the function and doesn't care how long they take to process? That's certainly an issue, and one that would require additional infrastructure in place (like a Web Application Firewall) to analyze the traffic and shut it down. It may not be feasible to architect such a solution in your case, but we are also here to help. If you feel like your function in the Gatsby Cloud is being abused, please contact support and let us know.
I don't believe there is a built in way to cache serverless function responses, but you could certainly build something into your application. For example, you could return your serverless response with a timestamp field, then build logic in client-side to not retrieve a response until the cached response has met your threshold. This is a great way to handle accidental client-side calls to a function, like if a user hits a button two times in a row. It won't protect against abuse, but it will help limit valid activity within your application.
You hit the nail on the head; the answer is CORS. You can set Access-Control-Allow-Origin headers within your Gatsby Function to ensure that you are only replying to calls from a valid endpoint. You could do this like so, for example, if you only wanted to accept connections from gatsbyjs.com:
|
Beta Was this translation helpful? Give feedback.
This is a really great question, thanks for asking!
First off, you should determine if this is in your threat model. If you don't expect this to be an issue then I wouldn't think too much about it.
That said, the solution to the problem is a combination of authentication and rate limiting. You essentially need to track the state of valid user connections and ensure that not too many are being processed.
A hypothetical scenario could work like this: