feat: исправила строгую валидацию JSON и пагинации

This commit is contained in:
2026-10-29 12:41:45 +03:00
parent 9ac1762101
commit 10ca948a53
7 changed files with 316 additions and 6 deletions
+49 -6
View File
@@ -16,6 +16,7 @@ import (
"net/http"
"os"
"os/signal"
"reflect"
"regexp"
"strconv"
"strings"
@@ -237,13 +238,48 @@ func (a *app) decode(w http.ResponseWriter, r *http.Request, target any) bool {
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.DisallowUnknownFields()
var extra any
if !utf8.Valid(data) || decoder.Decode(target) != nil || decoder.Decode(&extra) != io.EOF {
if !utf8.Valid(data) || !strictObjectFields(data, target) || decoder.Decode(target) != nil || decoder.Decode(&extra) != io.EOF {
a.error(w, 400, "invalid_request", "Неверный JSON")
return false
}
return true
}
// encoding/json matches struct fields without regard to case. Validate decoded
// top-level keys against the exact JSON tags before allowing it to fill a DTO.
// Current request DTOs are flat objects (member_ids is an array of strings).
func strictObjectFields(data []byte, target any) bool {
t := reflect.TypeOf(target).Elem()
allowed := make(map[string]bool, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
name := strings.Split(field.Tag.Get("json"), ",")[0]
if field.IsExported() && name != "" && name != "-" {
allowed[name] = true
}
}
decoder := json.NewDecoder(bytes.NewReader(data))
opening, err := decoder.Token()
if err != nil || opening != json.Delim('{') {
return false
}
seen := make(map[string]bool, len(allowed))
for decoder.More() {
token, err := decoder.Token()
key, ok := token.(string)
if err != nil || !ok || !allowed[key] || seen[key] {
return false
}
seen[key] = true
var value json.RawMessage
if decoder.Decode(&value) != nil {
return false
}
}
closing, err := decoder.Token()
return err == nil && closing == json.Delim('}')
}
func (a *app) health(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/health/ready" && a.store.db.PingContext(r.Context()) != nil {
a.unavailable(w)
@@ -529,19 +565,26 @@ func (a *app) chatResource(w http.ResponseWriter, r *http.Request) {
}
func (a *app) position(w http.ResponseWriter, r *http.Request, resource string) (cursor, int, bool) {
query := r.URL.Query()
limit := 50
var err error
if value := r.URL.Query().Get("limit"); value != "" {
limit, err = strconv.Atoi(value)
values, hasLimit := query["limit"]
if hasLimit && len(values) == 1 {
limit, err = strconv.Atoi(values[0])
}
p := cursor{Resource: resource, CreatedAt: time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC), ID: "00000000-0000-0000-0000-000000000000"}
if limit < 1 || limit > 100 || err != nil {
if (hasLimit && len(values) != 1) || limit < 1 || limit > 100 || err != nil {
a.error(w, 400, "invalid_request", "Неверный limit")
return p, 0, false
}
if value := r.URL.Query().Get("cursor"); value != "" {
if values, present := query["cursor"]; present {
if len(values) != 1 || values[0] == "" || len(values[0]) > 1024 {
a.error(w, 400, "invalid_request", "Неверный cursor")
return p, 0, false
}
value := values[0]
data, e := base64.RawURLEncoding.DecodeString(value)
if len(value) > 1024 || e != nil || json.Unmarshal(data, &p) != nil || p.Resource != resource || !isUUID(string(p.ID)) || p.CreatedAt.IsZero() {
if e != nil || json.Unmarshal(data, &p) != nil || p.Resource != resource || !isUUID(string(p.ID)) || p.CreatedAt.IsZero() {
a.error(w, 400, "invalid_request", "Неверный cursor")
return p, 0, false
}