Skip to content

fix(ui): send JSON body as application/json when no file upload is present (Fixes #17777) - #17783

Open
waterWang wants to merge 2 commits into
payloadcms:mainfrom
waterWang:fix/17777-json-formdata-when-no-file
Open

fix(ui): send JSON body as application/json when no file upload is present (Fixes #17777)#17783
waterWang wants to merge 2 commits into
payloadcms:mainfrom
waterWang:fix/17777-json-formdata-when-no-file

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Fixes #17777 — The admin panel unconditionally sends all PATCH/POST requests (save, autosave, publish, save draft) as multipart/form-data, even when the request payload contains only JSON data and no file is being uploaded.

Per HTTP semantics, multipart/form-data is intended for requests that include binary file data. When the payload is purely JSON, the request should use application/json. Sending JSON-only data wrapped in multipart encoding triggers false positives in standard WAF configurations (e.g., Cloudflare Enterprise).

Changes

packages/ui/src/forms/Form/index.tsx:

  • createFormData: When no file is present in the form data, return the JSON body directly as a string instead of wrapping it in FormData with serialize() from object-to-formdata
  • submit: Detect whether the body is FormData (file upload) or a string (JSON), and set Content-Type: application/json header when sending JSON

packages/ui/src/forms/Form/types.ts:

  • Updated CreateFormData return type to FormData | string | Promise<FormData | string>
  • Updated FormProps action callback type to accept FormData | string

Verified behavior

  • Exact same JSON body sent as application/json to the same endpoint: 200 OK
  • Same JSON body wrapped in multipart/form-data to the same endpoint: 403 blocked by WAF (Cloudflare Enterprise rule 6179ae15870a4bb7b2d480d4843b323c)
  • This fix sends the JSON body as application/json when no file is present, bypassing the WAF false positive
  • File uploads continue to use multipart/form-data as before (unaffected)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin panel always sends multipart/form-data even when no file upload is present in the form

1 participant