val-town / val-town-docs

Val Town Documentation
https://docs.val.town/
28 stars 25 forks source link

Docs for "securing endpoints" #35

Open stevekrouse opened 9 months ago

stevekrouse commented 9 months ago

We're in the business of letting users expose endpoints that call our to other API that they pay for. The whole point of running code to APIs with API keys on a server is that it's secure. However once you expose that endpoint to the world so that you can call it, what's to prevent the whole world from calling it?

For example, one of our best users is using us to wrap a API thing inside of a form, ie as you fill out the form, based on your zip code, it does some calculation. However he's terrified of a surprise crazy bill from the API because someone discovered his endpoint and abuses it. It's a good point! Right now it's really just security by obfuscation. Anyone could get free access to that API if they find it. What's the point of using Val Town even? He could almost put his API token right on the frontend!

So we could answer those questions:

We could really crush the SEO on this too and make this a wonderful evergreen resource. I bet folks are googling for this sort of thing all the time. We just need to figure out the right keywords.

stevekrouse commented 9 months ago

More context: https://discord.com/channels/1020432421243592714/1020432421243592717/1172197816744026204

stevekrouse commented 9 months ago

From @tmcw

If you have an application with users who authenticate to our application, and you want to drive part of that application with Val Town, the things that come to mind are:

JWT tokens If your app uses JWT tokens, you could theoretically set your JWT secret in your Val Town environment variables, which would allow us to decode JWT tokens that come with requests and verify your users sessions. This would require you pass the JWT tokens explicitly from the form or request - cookies for your application won't be sent with arbitrary requests to other domains.

Host referrer In an ideal world, you could check the referrer of the request and only allow front-end requests with a proper referrer to succeed – so if the request isn't coming from your.app.com, it'll get rejected.

Unfortunately, because of bad actors, referrers are kind of unreliable - I think that Safari gets rid of the path name and censors referrers, and some proxies might drop them entirely. You might get some false negatives if you filter by referrer.

karfau commented 9 months ago

As soon as you find a way to serve the val from your own domain, the whole thing gets way simpler, since you get those cookies, and can block any access without cookies.

I guess that it already possible using cloudflare or some other tools, but didn't I hear somebody say your are working on integrating that into valtown?

stevekrouse commented 9 months ago

How do custom domains help you manage cookies? Wouldn't you still need to manage them in your code manually yourself? Or does Cloudflare somehow do this for you automatically for certain kinds of protection?

karfau commented 9 months ago

I'm aware that cloudflare access set's a cookie which is a jwt token. The identity provider it connects to is able to add some information to the cookie. we use that at work.

And also we were talking about "seccuring endoints", right, so if you have a custom domain that you can reach your val endpoint from, the endpoint would receive any properly configured cookie (*.domain.tldr) of the domain that hosts the frontend (could be the same, if vals also render the frontend)

stevekrouse commented 9 months ago

Yeah I think I understand. That makes sense

stevekrouse commented 8 months ago

Questions from @Xkonti:

The things discussed above protects a resource within a val but not the val user's limit of 10 or 100 runs per minute. Should we also come up with a way to protect that?

Exploiting a user's runs per minute hasn't happened yet and might not happen for a while. We can list it as a potential thing to worry about and assure users that it's unlikely and that if it happens they are not liable for it, and we will work with them to mitigate it if it happens. We also have cloudflare in front of all of our services which should mitigate a lot of similar such attacks

Is this a single large document or a collection of smaller documents?

I imagine it as a main document that links out to other smaller ones. It could start as a single document and grow into a folder. This is a pattern we've already seen in our docs.