Skip to content

Commit fb581ee

Browse files
Media: Keep non-image uploads on the classic upload path.
Fix an issue with the client-side media processing pipeline checking file types against the allowed mime types captured when the page rendered, not the uploader's own `filters.mime_types`. File types a plugin allows for a single frame, such as SVG and JSON, were rejected in the browser with "Sorry, you are not allowed to upload this file type." before reaching the server. After this change, only images the pipeline can process (currently JPEG, PNG, GIF, WebP, AVIF and HEIC/HEIF) are routed to it. Everything else (audio, video, PDF, SVG, JSON, archives) uploads through `async-upload.php` as it did before the pipeline was added. An image whose type is missing from the render-time allowed mime types also stays on the classic path, so the server decides whether to accept it. HEIC files are matched by extension too, because browsers without a HEIC codec may give them no type. Follow-up to [64588]. Props desrosj, andrewserong. See #66490. git-svn-id: https://develop.svn.wordpress.org/trunk@65356 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1b1fd1e commit fb581ee

4 files changed

Lines changed: 171 additions & 15 deletions

File tree

‎src/js/_enqueues/admin/media-frame-upload.js‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,12 @@
202202

203203
/*
204204
* Early mime type scanning for images, as wp-plupload.js does,
205-
* extended with the formats the client-side pipeline accepts.
205+
* for the formats the pipeline processes.
206206
*/
207-
const image = /(?:jpe?g|png|gif|webp|avif|heic|heif)$/i.exec(
208-
file.name
209-
);
210-
if ( image ) {
207+
const subtype = pipeline.getImageSubtype( file.name );
208+
if ( subtype ) {
211209
attributes.type = 'image';
212-
// `jpg` is not a valid subtype, so map it to `jpeg`.
213-
const subtype = image[ 0 ].toLowerCase();
214-
attributes.subtype = 'jpg' === subtype ? 'jpeg' : subtype;
210+
attributes.subtype = subtype;
215211
}
216212

217213
const model = wp.media.model.Attachment.create( attributes );

‎src/js/_enqueues/admin/media-upload-pipeline.js‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ window.wp = window.wp || {};
3838

3939
const imageSizeCount = Object.keys( settings.allImageSizes || {} ).length;
4040

41+
/*
42+
* Image subtypes the pipeline processes. Keep in sync with
43+
* CLIENT_SIDE_SUPPORTED_MIME_TYPES and HEIC_MIME_TYPES in the
44+
* upload-media package.
45+
*/
46+
const IMAGE_SUBTYPES = [ 'jpeg', 'png', 'gif', 'webp', 'avif', 'heic', 'heif' ];
47+
4148
/**
4249
* Recursively appends data to a FormData object, supporting nested objects.
4350
*
@@ -412,16 +419,73 @@ window.wp = window.wp || {};
412419
return configured && isStoreConfigured();
413420
}
414421

422+
/**
423+
* Gets the image subtype of a file name the pipeline can process.
424+
*
425+
* @param {string} fileName A file name.
426+
* @return {string|null} The subtype, eg. `jpeg` for `photo.jpg`, or
427+
* null when the pipeline does not process it.
428+
*/
429+
function getImageSubtype( fileName ) {
430+
const match = /\.([a-z0-9]+)$/i.exec( fileName || '' );
431+
if ( ! match ) {
432+
return null;
433+
}
434+
// `jpg` is not a valid subtype, so map it to `jpeg`.
435+
const extension = match[ 1 ].toLowerCase();
436+
const subtype = 'jpg' === extension ? 'jpeg' : extension;
437+
return IMAGE_SUBTYPES.includes( subtype ) ? subtype : null;
438+
}
439+
440+
/**
441+
* Whether a file is an image the client-side pipeline can process.
442+
*
443+
* A file whose type is missing from the allowed types the page was
444+
* rendered with is left to the server, which can allow it for a single
445+
* request (see partitionBatch()). A HEIC file is also matched by its
446+
* extension, since browsers on platforms without a HEIC codec may give
447+
* it no type at all.
448+
*
449+
* @param {plupload.File} file A file added to the plupload queue.
450+
* @return {boolean} True when the pipeline should take the file.
451+
*/
452+
function isPipelineFile( file ) {
453+
const type = ( file.type || '' ).toLowerCase();
454+
const allowedTypes = settings.allowedMimeTypes;
455+
if (
456+
type &&
457+
allowedTypes &&
458+
! Object.values( allowedTypes ).includes( type )
459+
) {
460+
return false;
461+
}
462+
if (
463+
type.startsWith( 'image/' ) &&
464+
IMAGE_SUBTYPES.includes( type.slice( 6 ) )
465+
) {
466+
return true;
467+
}
468+
const subtype = getImageSubtype( file.name );
469+
return 'heic' === subtype || 'heif' === subtype;
470+
}
471+
415472
/**
416473
* Splits a batch of plupload files between the pipeline and classic
417474
* plupload.
418475
*
419-
* Audio files stay classic so they keep the title and description that
420-
* media_handle_upload() derives from their ID3 tags, which the REST
421-
* endpoint does not do. So do JPEG and PNG images while plupload's own
422-
* client-side resize is enabled (the "Scale images" setting, or a
423-
* `resize` passed through `plupload_init`): plupload scales those down
424-
* before uploading them, the pipeline would not.
476+
* Only images the pipeline can process are taken (see isPipelineFile()).
477+
* Everything else, such as audio, video, PDF, SVG or JSON, stays
478+
* classic: the pipeline would do nothing for it but upload it, and
479+
* would check its type against the allowed types the page was rendered
480+
* with instead of the uploader's own filters, which a plugin can extend
481+
* for a single frame. For the same reason an image of a type the page
482+
* was not rendered with stays classic, so the server decides. Audio
483+
* also keeps the title and description that media_handle_upload()
484+
* derives from its ID3 tags, which the REST endpoint does not. JPEG
485+
* and PNG images stay classic too while plupload's own client-side
486+
* resize is enabled (the "Scale images" setting, or a `resize` passed
487+
* through `plupload_init`): plupload scales those down before
488+
* uploading them, the pipeline would not.
425489
*
426490
* The whole batch stays classic when a file cannot be re-queued on
427491
* plupload or is already plupload's: plupload returns no native File
@@ -450,7 +514,7 @@ window.wp = window.wp || {};
450514
}
451515
const type = file.type || '';
452516
if (
453-
/^audio\//i.test( type ) ||
517+
! isPipelineFile( file ) ||
454518
( isResizing && /^image\/(?:jpeg|png)$/i.test( type ) )
455519
) {
456520
batch.classic.push( file );
@@ -735,5 +799,6 @@ window.wp = window.wp || {};
735799
isReady: isReady,
736800
routeBatch: routeBatch,
737801
getErrorText: getErrorText,
802+
getImageSubtype: getImageSubtype,
738803
};
739804
} )( window.wp );

‎tests/e2e/specs/media-library-client-side-upload.test.js‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { test, expect } from '@wordpress/e2e-test-utils-playwright';
99
import {
1010
TEST_IMAGE_PATH,
1111
TEST_AUDIO_PATH,
12+
TEST_SVG_PATH,
1213
SIMULATED_ERROR,
1314
UNDECODABLE_HEIC,
1415
failMediaCreate,
@@ -565,6 +566,96 @@ test.describe( 'Media Library grid client-side uploads', () => {
565566
expect( restUploadCount ).toBe( 0 );
566567
} );
567568

569+
test( 'leaves files that are not processable images to the classic uploader', async ( {
570+
page,
571+
admin,
572+
} ) => {
573+
// A plugin can allow an extra type on its own uploader, as page
574+
// builders do for SVG, without adding it to the allowed mime types
575+
// the page was rendered with.
576+
await overridePluploadDefaults( page, {
577+
filters: { mime_types: [ { extensions: 'svg' } ] },
578+
} );
579+
await stubClassicUpload( page );
580+
await admin.visitAdminPage( 'upload.php', 'mode=grid' );
581+
582+
const isolated = await page.evaluate( () =>
583+
Boolean( window.crossOriginIsolated )
584+
);
585+
test.skip(
586+
! isolated,
587+
'The client-side pipeline requires a cross-origin isolated context'
588+
);
589+
590+
const counts = countUploadRequests( page );
591+
592+
const fileInput = page.locator( FILE_INPUT_SELECTOR ).first();
593+
await expect( fileInput ).toBeAttached( { timeout: 30_000 } );
594+
await fileInput.setInputFiles( TEST_SVG_PATH );
595+
596+
// The uploader's own filter decides, so the file reaches the server
597+
// instead of being rejected in the browser.
598+
await expect
599+
.poll( () => counts.asyncUpload, { timeout: 60_000 } )
600+
.toBeGreaterThanOrEqual( 1 );
601+
expect( counts.rest ).toBe( 0 );
602+
await expect(
603+
page.locator( '.upload-error-message', {
604+
hasText: 'not allowed to upload this file type',
605+
} )
606+
).toHaveCount( 0 );
607+
} );
608+
609+
test( 'leaves images of a type the page was not rendered with to the classic uploader', async ( {
610+
page,
611+
admin,
612+
} ) => {
613+
// A plugin can allow an image type only for its own requests, so
614+
// the allowed mime types the page was rendered with leave it out
615+
// while the uploader's own filter lets it through.
616+
await page.addInitScript( () => {
617+
let settings;
618+
Object.defineProperty( window, '_wpMediaUploadPipelineSettings', {
619+
configurable: true,
620+
get() {
621+
return settings;
622+
},
623+
set( value ) {
624+
settings = value;
625+
const allowed = value && value.allowedMimeTypes;
626+
for ( const extension in allowed || {} ) {
627+
if ( 'image/jpeg' === allowed[ extension ] ) {
628+
delete allowed[ extension ];
629+
}
630+
}
631+
},
632+
} );
633+
} );
634+
await admin.visitAdminPage( 'upload.php', 'mode=grid' );
635+
636+
const isolated = await page.evaluate( () =>
637+
Boolean( window.crossOriginIsolated )
638+
);
639+
test.skip(
640+
! isolated,
641+
'The client-side pipeline requires a cross-origin isolated context'
642+
);
643+
644+
const counts = countUploadRequests( page );
645+
646+
const fileInput = page.locator( FILE_INPUT_SELECTOR ).first();
647+
await expect( fileInput ).toBeAttached( { timeout: 30_000 } );
648+
await fileInput.setInputFiles( TEST_IMAGE_PATH );
649+
650+
// The server decides, as it would without the pipeline.
651+
await expect( tile( page, 'test-image' ) ).toBeVisible( {
652+
timeout: 60_000,
653+
} );
654+
expect( counts.asyncUpload ).toBeGreaterThanOrEqual( 1 );
655+
expect( counts.rest ).toBe( 0 );
656+
await expect( page.locator( '.upload-error' ) ).toHaveCount( 0 );
657+
} );
658+
568659
test( 'splits a batch of an image and audio between the pipeline and the classic uploader', async ( {
569660
page,
570661
admin,

‎tests/e2e/utils/media-pipeline.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const TEST_AUDIO_PATH = path.join(
1818
'../assets/test-audio.wav'
1919
);
2020

21+
// An SVG: like any file that is not an image the pipeline can process, it
22+
// stays on the classic upload path.
23+
export const TEST_SVG_PATH = path.join( __dirname, '../assets/sample.svg' );
24+
2125
// A REST error the pipeline surfaces as-is: its message matches none of the
2226
// transient patterns @wordpress/upload-media retries on.
2327
export const SIMULATED_ERROR = {

0 commit comments

Comments
 (0)