diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f46c990..9c0c3f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **Asset insert applies the JSON body cap**: `POST /api/assets` decodes through the shared JSON limit, including a `multipart/` request that the upload middleware leaves uncapped. A body over the cap is rejected before the database is touched. - **Missing Docker buildx is reported before the build**: without the buildx plugin, Compose only warns and falls back to the legacy builder, which fails on the Dockerfile's first `FROM --platform=$BUILDPLATFORM` with `failed to parse platform`. `install.sh` (all-Docker) and `update.sh` (Docker update) now check `docker buildx version` first and stop with an install hint, and the automatic Docker install on apt-based distros not covered by get.docker.com installs the buildx package as well. The README Docker section names buildx as a requirement. - **A goal URL followed by punctuation is still seeded**: a goal such as `Assess http://juiceshop:3000.` no longer logs `could not parse a target host/IP` and skips the site. Trailing punctuation, including full-width `。` and a closing bracket that has no matching opener, is trimmed from the URL before it is parsed, so `http://[::1]` is left alone. The scheme the goal names is kept and a final period does not end up in the host. - **Task timeout triggers fire for every task**: `on_task_timeout` used the task id as a watermark, but tasks time out in deadline order. Once a newer task timed out first, an older task that timed out later was never fired for. Fired timeouts are now kept in a persisted set, like met goals; on first start the set is seeded with the tasks already timed out, so nothing replays. diff --git a/server/assets.go b/server/assets.go index 2d7a7597..043d33a3 100644 --- a/server/assets.go +++ b/server/assets.go @@ -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"` diff --git a/server/decode_limit_test.go b/server/decode_limit_test.go index 5922673a..41401f09 100644 --- a/server/decode_limit_test.go +++ b/server/decode_limit_test.go @@ -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 {