#41696 closed defect (bug) (fixed)
Content-Disposition header is blocked by CORS
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.5 | Priority: | low |
Severity: | minor | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch |
Focuses: | Cc: |
Description
The media upload endpoint in the REST API accepts files in two formats: form data (multipart/form-data
) and direct upload (image/png
e.g.). When uploading in direct format, the desired filename is passed in the Content-Disposition
header (e.g. Content-Disposition: atttachment; filename="file.jpg"
).
When sending requests across a cross-site boundary, browser preflight requests only allow a certain subset of headers to be sent. We whitelist Authorization
and Content-Type
in addition to the regular headers, but neither Content-Disposition
nor Content-MD5
are permitted by default or explicitly.
This means that a simple fetch
using a File/Blob object (e.g. from an <input type="file" />
or HTML5 drag-and-drop) for the body will fail:
const url = `http://example.com/wp-json/wp/v2/media`; const opts = { method: 'POST', headers: { 'Content-Disposition': 'attachment; filename="test.txt"', } body: new Blob( [ 'test data' ] ), }; fetch( url, opts ).then( resp => console.log( resp ) );
However, this is allowed by packing the data into a FormData object instead:
const url = `http://example.com/wp-json/wp/v2/media`; const opts = { method: 'POST', }; opts.body = new FormData(); const file = new Blob( [ 'test data' ] ); file.name = 'test.txt'; opts.body.append( 'file', file ); fetch( url, opts ).then( resp => console.log( resp ) );
We should fix this inconsistency to allow for the simpler request format.
Attachments (1)
Change History (6)
This ticket was mentioned in Slack in #core-restapi by dingo_d. View the logs.
6 years ago
This ticket was mentioned in PR #344 on WordPress/wordpress-develop by TimothyBJacobs.
3 years ago
#2
Trac ticket: https://core.trac.wordpress.org/ticket/41696
#3
@
3 years ago
- Milestone changed from Awaiting Review to 5.5
Adding this to 5.5 to accompany #50369. I've added a PR that is the same as 41696.diff but without the short array syntax and I've added the X-WP-Nonce
header. Passing the nonce was already possible through the _wpnonce
query parameter, but I think its worthwhile to have parity. wp.apiFetch
also uses the X-WP-Nonce
header.
#4
@
3 years ago
- Owner set to TimothyBlynJacobs
- Resolution set to fixed
- Status changed from new to closed
In 48452:
TimothyBJacobs commented on PR #344:
3 years ago
#5
This was merged.
Add Content-Disposition and Content-MD5 to allowed headers, and add filter to allow custom headers