Files
reproxy/vendor/github.com/go-pkgz/rest/blackwords.go
Umputun 7d8802992a switch to go 1.22, update deps
update version of runners, change goveralls install

update docker build to v1.22

drop useless codeanalysis ci step
2024-03-15 17:14:08 -05:00

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()...)
}