forked from mtusi2026/mtusi2026lab2
feat: исправила строгую валидацию JSON и пагинации
This commit is contained in:
@@ -15,6 +15,7 @@ jobs:
|
||||
- run: docker compose --env-file .env up -d --build
|
||||
- run: python scripts/wait_ready.py
|
||||
- run: make check test PYTHON=python
|
||||
- run: python student-tests/regression.py
|
||||
- run: chmod +x scripts/restart.sh && make test-persistence PYTHON=python ACTION_SCRIPT=scripts/restart.sh
|
||||
- run: make test-extra research PYTHON=python
|
||||
- uses: actions/upload-artifact@v4
|
||||
|
||||
@@ -170,6 +170,16 @@ Backup: 2 882 994 bytes, создание 1,500 с, восстановление
|
||||
|
||||
## Ограничения
|
||||
|
||||
### Исправления по замечаниям от 29 октября 2026 года
|
||||
|
||||
Перед декодированием HTTP body добавлена проверка точных имён верхнеуровневых полей по JSON-тегам DTO. `encoding/json` по умолчанию сопоставляет имена без учёта регистра даже с DisallowUnknownFields; теперь `{"text":"valid","TEXT":"unexpected-property"}` отклоняется с `400 invalid_request` до заполнения структуры и записи сообщения. Повторяющиеся JSON-ключи также отклоняются. Проверка общая для регистрации, создания чата и отправки сообщения; текущие DTO не содержат вложенных объектов.
|
||||
|
||||
Пагинация различает отсутствие параметра и переданное пустое значение: без limit используется 50, но `?limit=`, `?cursor=` и параметры без `=` возвращают `400 invalid_request`. Повторяющиеся limit/cursor отклоняются как неоднозначные. Исправление действует для истории сообщений и списка чатов.
|
||||
|
||||
Проверено: Docker build с `go test ./...` и `go vet ./...`, `python scripts/check_contract.py`, публичный smoke и `python student-tests/regression.py --base-url http://127.0.0.1:8080` — PASS. В последнем проверяются авторизованные запросы, отсутствие новых сообщений после отклонённых POST, точный error code, пустые параметры и корректное продолжение страниц. Результаты — `evidence/regression.json`. Проверка добавлена в локальный verify.py и workflow Application. Полное исследование производительности при этом исправлении повторно не запускалось; измерения выше относятся к прогону 10 октября.
|
||||
|
||||
### Эксплуатационные ограничения
|
||||
|
||||
Одна API-реплика и одна основная БД: отказ PostgreSQL не скрывается, HA не заявляется. Redis — доверенный внутренний сервис локальной Compose-сети, без TLS/ACL; он не опубликован наружу. Пользователь БД в учебном стенде управляет миграциями; разделение migration/runtime ролей оставлено для последующих лабораторных. Secrets Compose — файлы локального оператора, внешний secret manager не используется.
|
||||
|
||||
Перезапуск с volumes проверяет сохранность контейнерного стенда, а не внезапное отключение питания или повреждение диска. Отдельные request contexts ограничивают время ожидания; 201 может быть потерян на сети после COMMIT, поэтому прозрачный retry POST в lab2 не добавлен. Идемпотентность появляется по контракту lab3.
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
{
|
||||
"cases": [
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/users",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "POST",
|
||||
"path": "/api/v1/chats",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?limit=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?limit",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?cursor",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?limit=1&cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?limit=1&limit=2",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats/725cde91-1e57-41c1-9933-c82e995a8276/messages?cursor=&cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?limit=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?limit",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?cursor",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?limit=1&cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?limit=1&limit=2",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/api/v1/chats?cursor=&cursor=",
|
||||
"status": 400,
|
||||
"code": "invalid_request"
|
||||
}
|
||||
],
|
||||
"rejected_posts_leave_history_unchanged": true,
|
||||
"valid_pagination": "PASS"
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -27,6 +28,12 @@ func TestCursorScopeAndValidation(t *testing.T) {
|
||||
query string
|
||||
valid bool
|
||||
}{
|
||||
{"", true},
|
||||
{"limit=", false}, {"cursor=", false}, {"limit", false}, {"cursor", false},
|
||||
{"limit=&cursor=" + encodeCursor(p), false},
|
||||
{"limit=1&cursor=", false},
|
||||
{"limit=1&limit=2", false},
|
||||
{"cursor=" + encodeCursor(p) + "&cursor=", false},
|
||||
{"limit=1&cursor=" + encodeCursor(p), true},
|
||||
{"limit=100&cursor=" + encodeCursor(p), true},
|
||||
{"limit=0", false}, {"limit=101", false}, {"limit=1.2", false}, {"cursor=garbage", false},
|
||||
@@ -41,6 +48,69 @@ func TestCursorScopeAndValidation(t *testing.T) {
|
||||
if ok != c.valid {
|
||||
t.Errorf("cursor query %s: valid=%v", c.query, ok)
|
||||
}
|
||||
if !c.valid {
|
||||
assertInvalidRequest(t, w)
|
||||
} else if c.query == "" {
|
||||
_, limit, _ := a.position(httptest.NewRecorder(), r, p.Resource)
|
||||
if limit != 50 {
|
||||
t.Fatal("absent limit must default to 50")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertInvalidRequest(t *testing.T, w *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
var body struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if w.Code != 400 || json.Unmarshal(w.Body.Bytes(), &body) != nil || body.Error.Code != "invalid_request" {
|
||||
t.Fatalf("expected 400 invalid_request, received %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExactJSONFieldNames(t *testing.T) {
|
||||
a := &app{limits: limits{maxBodyBytes: 1024}}
|
||||
cases := []struct {
|
||||
name, data string
|
||||
target func() any
|
||||
valid bool
|
||||
}{
|
||||
{"message", `{"text":"valid"}`, func() any { return &createMessage{} }, true},
|
||||
{"escaped exact key", `{"te\u0078t":"valid"}`, func() any { return &createMessage{} }, true},
|
||||
{"case overwrite", `{"text":"valid","TEXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
||||
{"case first", `{"TEXT":"unexpected-property","text":"valid"}`, func() any { return &createMessage{} }, false},
|
||||
{"mixed case", `{"Text":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
||||
{"escaped uppercase", `{"\u0054EXT":"unexpected-property"}`, func() any { return &createMessage{} }, false},
|
||||
{"duplicate", `{"text":"valid","text":"overwrite"}`, func() any { return &createMessage{} }, false},
|
||||
{"escaped duplicate", `{"text":"valid","te\u0078t":"overwrite"}`, func() any { return &createMessage{} }, false},
|
||||
{"null object", `null`, func() any { return &createMessage{} }, false},
|
||||
{"array", `[]`, func() any { return &createMessage{} }, false},
|
||||
{"user", `{"username":"alice","display_name":"Alice","password":"long-password"}`, func() any { return &createUser{} }, true},
|
||||
{"user case", `{"username":"alice","display_name":"Alice","PASSWORD":"long-password"}`, func() any { return &createUser{} }, false},
|
||||
{"Go field name", `{"Username":"alice"}`, func() any { return &createUser{} }, false},
|
||||
{"chat", `{"title":"Chat","member_ids":[]}`, func() any { return &createChat{} }, true},
|
||||
{"chat case", `{"TITLE":"Chat","member_ids":[]}`, func() any { return &createChat{} }, false},
|
||||
{"trailing object", `{"text":"valid"} {}`, func() any { return &createMessage{} }, false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
target := c.target()
|
||||
if got := a.decode(w, httptest.NewRequest("POST", "/", strings.NewReader(c.data)), target); got != c.valid {
|
||||
t.Fatalf("valid=%v, expected %v", got, c.valid)
|
||||
}
|
||||
if !c.valid {
|
||||
assertInvalidRequest(t, w)
|
||||
}
|
||||
if c.name == "message" || c.name == "escaped exact key" {
|
||||
if target.(*createMessage).Text != "valid" {
|
||||
t.Fatal("valid text changed")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""HTTP regressions: exact JSON fields and explicitly empty pagination values."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, arguments, check
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Strict JSON and pagination regression checks')
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = '/api/v1/chats/'+chat['id']+'/messages'
|
||||
cases = []
|
||||
|
||||
def invalid(method, url, **kwargs):
|
||||
response = c.call(method, url, 400, actor=alice, **kwargs)
|
||||
check(response['json']['error']['code']=='invalid_request', 'expected invalid_request')
|
||||
cases.append({'method': method, 'path': url, 'status': 400, 'code': 'invalid_request'})
|
||||
|
||||
c.message(alice, chat, 'confirmed before invalid requests')
|
||||
before = c.call('GET', path, actor=bob, schema='MessagePage')['json']
|
||||
for body in [
|
||||
{'text': 'valid', 'TEXT': 'unexpected-property'},
|
||||
{'TEXT': 'unexpected-property', 'text': 'valid'},
|
||||
{'Text': 'unexpected-property'},
|
||||
]:
|
||||
invalid('POST', path, body=body)
|
||||
invalid('POST', path, raw=b'{"text":"valid","text":"overwrite"}',
|
||||
headers={'Content-Type': 'application/json'})
|
||||
after = c.call('GET', path, actor=bob, schema='MessagePage')['json']
|
||||
check(after==before, 'rejected JSON modified message history')
|
||||
|
||||
invalid('POST', '/api/v1/users', body=dict(alice['registration'], PASSWORD='unexpected-property'))
|
||||
invalid('POST', '/api/v1/chats', body={'title':'valid', 'TITLE':'unexpected-property', 'member_ids':[alice['id']]})
|
||||
for resource in [path, '/api/v1/chats']:
|
||||
for query in ['limit=', 'cursor=', 'limit', 'cursor', 'limit=1&cursor=', 'limit=1&limit=2', 'cursor=&cursor=']:
|
||||
invalid('GET', resource+'?'+query)
|
||||
# Omitting both parameters and setting a valid limit must still work.
|
||||
for query in ['', '?limit=1']:
|
||||
page = c.call('GET', path+query, actor=alice, schema='MessagePage')['json']
|
||||
check(len(page['items'])==1, 'valid pagination lost confirmed message')
|
||||
c.message(alice, chat, 'second confirmed message')
|
||||
first = c.call('GET', path+'?limit=1', actor=alice, schema='MessagePage')['json']
|
||||
check(first['next_cursor'] is not None, 'expected continuation cursor')
|
||||
second = c.call('GET', path+'?limit=1&cursor='+first['next_cursor'], actor=bob, schema='MessagePage')['json']
|
||||
check(len(second['items'])==1 and second['next_cursor'] is None, 'valid continuation failed')
|
||||
check(first['items'][0]['id']!=second['items'][0]['id'], 'duplicate page item')
|
||||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
|
||||
result = {'cases':cases, 'rejected_posts_leave_history_unchanged':True, 'valid_pagination':'PASS'}
|
||||
(ROOT/'evidence'/'regression.json').write_text(json.dumps(result,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
|
||||
print('PASS: exact JSON fields, no rejected writes, empty/repeated pagination rejected, valid pages preserved')
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
@@ -34,6 +34,7 @@ def main():
|
||||
commands = [
|
||||
[sys.executable,'scripts/check_contract.py'],
|
||||
[sys.executable,'tests/smoke.py','--base-url',results['base_url']],
|
||||
[sys.executable,'student-tests/regression.py','--base-url',results['base_url']],
|
||||
[sys.executable,'tests/resilience.py','--mode','persistence','--action-script',hook,'--base-url',results['base_url']],
|
||||
[sys.executable,'student-tests/integration.py'],
|
||||
[sys.executable,'student-tests/research.py'],
|
||||
|
||||
Reference in New Issue
Block a user