Block Direct Requests

This commit is contained in:
2025-08-01 01:49:54 +01:00
parent dea3c68510
commit df0294c754
+8 -8
View File
@@ -21,27 +21,27 @@ func RateLimiter(rps float64, burst int) func(http.Handler) http.Handler {
} }
} }
func RefererCheck(allowedDomains []string) func(http.Handler) http.Handler { func RefererCheck(allowedOrigins []string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
referer := r.Header.Get("Referer") referer := r.Header.Get("Referer")
// Allow requests with no referer (direct API calls, Postman, etc.) // Block requests with no referer (this blocks direct browser requests)
if referer == "" { if referer == "" {
next.ServeHTTP(w, r) http.Error(w, "Forbidden!", http.StatusForbidden)
return return
} }
// Check if referer matches allowed domains // Check if referer matches allowed origins
for _, domain := range allowedDomains { for _, origin := range allowedOrigins {
if strings.HasPrefix(referer, domain) { if strings.HasPrefix(referer, origin) {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
return return
} }
} }
// Block if referer doesn't match // Block if referer doesn't match any allowed origin
http.Error(w, "Forbidden", http.StatusForbidden) http.Error(w, "Forbidden!", http.StatusForbidden)
}) })
} }
} }