mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-28 02:40:53 +08:00
fix(server): cap insert_assets request bodies
POST /api/assets decoded with json.NewDecoder directly. A multipart Content-Type skips the JSON body middleware, so that request was uncapped. Decode through the shared limit first and answer 413 before touching the database.
This commit is contained in:
+11
-6
@@ -477,11 +477,6 @@ func (s *Server) deleteAssets(w http.ResponseWriter, r *http.Request) {
|
||||
// =====================================================================
|
||||
|
||||
func (s *Server) insertAssets(w http.ResponseWriter, r *http.Request) {
|
||||
as := s.assetStore()
|
||||
if as == nil {
|
||||
writeErr(w, 503, "database unavailable")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
TaskID int64 `json:"task_id"`
|
||||
Assets []struct {
|
||||
@@ -518,10 +513,20 @@ func (s *Server) insertAssets(w http.ResponseWriter, r *http.Request) {
|
||||
Params []map[string]any `json:"params"`
|
||||
} `json:"assets"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
if err := decode(w, r, &req); err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
writeErr(w, http.StatusRequestEntityTooLarge, "Request body too large")
|
||||
return
|
||||
}
|
||||
writeErr(w, 400, "invalid JSON: "+err.Error())
|
||||
return
|
||||
}
|
||||
as := s.assetStore()
|
||||
if as == nil {
|
||||
writeErr(w, 503, "database unavailable")
|
||||
return
|
||||
}
|
||||
|
||||
type result struct {
|
||||
Index int `json:"index"`
|
||||
|
||||
@@ -52,6 +52,27 @@ func TestRawJSONBodyOverTheCapIs413(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAssetsCapsMultipartBody(t *testing.T) {
|
||||
s := &Server{m: &Manager{}}
|
||||
h := limitRequestBody(http.HandlerFunc(s.insertAssets))
|
||||
|
||||
large := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/assets", strings.NewReader(`{"assets":[{"type":"`+strings.Repeat("a", decodeMaxBytes)+`"}]}`))
|
||||
req.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
||||
h.ServeHTTP(large, req)
|
||||
if large.Code != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("status=%d body=%s, want 413", large.Code, large.Body.String())
|
||||
}
|
||||
|
||||
small := httptest.NewRecorder()
|
||||
ok := httptest.NewRequest(http.MethodPost, "/api/assets", strings.NewReader(`{"assets":[]}`))
|
||||
ok.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
||||
h.ServeHTTP(small, ok)
|
||||
if small.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status=%d body=%s, want 503 without a store", small.Code, small.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipartBodyIsNotGivenTheJSONCap(t *testing.T) {
|
||||
h := limitRequestBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := r.Body.(*limitedBody); ok {
|
||||
|
||||
Reference in New Issue
Block a user