rest.CORS refuses "*" together with credentials since go-pkgz/rest#52, so the bump and the option have to land together: the option does not exist in v1.22.0 and the panic fires at construction, inside routes(), which makes it a startup failure rather than a request-time one. The wildcard stays. The comment widget is embedded on arbitrary third-party sites, so the set of origins is not knowable, which is why the escape hatch was asked for upstream instead of accepting the panic. What it costs is unchanged and now written next to the call: any site a signed-in user visits can read authenticated responses, so state-changing requests have to keep being protected by something other than the origin, X-XSRF-Token today. The example module is tidied in the same commit, as it reaches go-pkgz/rest through the replace directive and its indirect graph would otherwise keep the old pin and fail the readonly module check in CI. The bump also carries testify to v1.12.0, which drops go-spew and go-difflib from the module graph.
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
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) {
|
|
|
|
content, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
// the body can't be inspected, refuse rather than pass a partially consumed one through
|
|
_ = EncodeJSON(w, http.StatusBadRequest, JSON{"error": "can't read request body"})
|
|
return
|
|
}
|
|
r.Body = io.NopCloser(bytes.NewReader(content))
|
|
|
|
body := strings.ToLower(string(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()...)
|
|
}
|