Files
remark42/backend/vendor/github.com/go-pkgz/rest/blackwords.go
T
Umputun 307e69e5c1 Bump dependencies
- chroma/v2: v2.20.0 → v2.21.1
- go-pkgz/auth/v2: v2.1.0 → v2.1.1
- go-pkgz/rest: v1.20.4 → v1.20.6
- golang.org/x/* packages to latest

Also exclude "meaningless package names" revive warning in linter config.
2025-12-24 01:48:14 -06:00

39 lines
970 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)) {
_ = EncodeJSON(w, http.StatusForbidden, 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()...)
}