3 ms·
I usually write a special middleware in golang for this. It sits in front of the actual router and http handlers and also buffers the response so it can modify
by zaarn 8y ago
I usually write a special middleware in golang for this. It sits in front of the actual router and http handlers and also buffers the response so it can modify it if necessary (this is unrelated to localhost security). Off the top of my head the checks are basically:
1. Who is the remote IP?
2. What does the origin and refer header say?
3. What does it say in the host? (I always enforce a whitelist for localhost applications)
4. Analyze the other headers? Anything out of the ordinary?
5. Enforce a strict CORS policy on ALL requests, no exceptions
6. Minimize contact points if external APIs are called (separate routers, preferably on a separate port)
7. Enforce a strict CSP policy (self only, no insecure eval or anything) on ALL requests
8. If outgoing requests are required, write a portal that they must go through (ie a common http client instance) that enforces where the requests are allowed to go)