mirror of
https://github.com/kemko/reproxy.git
synced 2026-01-10 12:15:45 +03:00
update version of runners, change goveralls install update docker build to v1.22 drop useless codeanalysis ci step
40 lines
987 B
Go
40 lines
987 B
Go
package rest
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// BlackWords middleware doesn't allow some words in the request body
|
|
func BlackWords(words ...string) func(http.Handler) http.Handler {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if content, err := io.ReadAll(r.Body); err == nil {
|
|
body := strings.ToLower(string(content))
|
|
r.Body = io.NopCloser(bytes.NewReader(content))
|
|
|
|
if body != "" {
|
|
for _, word := range words {
|
|
if strings.Contains(body, strings.ToLower(word)) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
RenderJSON(w, JSON{"error": "one of blacklisted words detected"})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
}
|
|
|
|
// BlackWordsFn middleware uses func to get the list and doesn't allow some words in the request body
|
|
func BlackWordsFn(fn func() []string) func(http.Handler) http.Handler {
|
|
return BlackWords(fn()...)
|
|
}
|