diff --git a/packages/block-library/src/audio/edit.native.js b/packages/block-library/src/audio/edit.native.js index fbc39ba190d3e2..cbd7f9ff02f8fc 100644 --- a/packages/block-library/src/audio/edit.native.js +++ b/packages/block-library/src/audio/edit.native.js @@ -30,7 +30,7 @@ import { audio as icon, replace } from '@wordpress/icons'; import { useState } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { store as noticesStore } from '@wordpress/notices'; -import { isURL } from '@wordpress/url'; +import { isURL, getProtocol } from '@wordpress/url'; /** * Internal dependencies @@ -73,7 +73,7 @@ function AudioEdit( { function onSelectURL( newSrc ) { if ( newSrc !== src ) { - if ( isURL( newSrc ) ) { + if ( isURL( newSrc ) && /^https?:/.test( getProtocol( newSrc ) ) ) { setAttributes( { src: newSrc, id: undefined } ); } else { createErrorNotice( __( 'Invalid URL. Audio file not found.' ) ); diff --git a/packages/block-library/src/audio/test/edit.native.js b/packages/block-library/src/audio/test/edit.native.js index b59c36db2f66e6..c191fd2fff7989 100644 --- a/packages/block-library/src/audio/test/edit.native.js +++ b/packages/block-library/src/audio/test/edit.native.js @@ -1,13 +1,20 @@ /** * External dependencies */ -import { render } from 'test/helpers'; +import { + addBlock, + dismissModal, + fireEvent, + initializeEditor, + render, + screen, + setupCoreBlocks, +} from 'test/helpers'; /** * WordPress dependencies */ import { BlockEdit } from '@wordpress/block-editor'; -import { registerBlockType, unregisterBlockType } from '@wordpress/blocks'; import { subscribeMediaUpload, sendMediaUpload, @@ -16,7 +23,7 @@ import { /** * Internal dependencies */ -import { metadata, settings, name } from '../index'; +import { name } from '../index'; // react-native-aztec shouldn't be mocked because these tests are based on // snapshot testing where we want to keep the original component. @@ -42,18 +49,9 @@ const getTestComponentWithContent = ( attributes = {} ) => { ); }; -describe( 'Audio block', () => { - beforeAll( () => { - registerBlockType( name, { - ...metadata, - ...settings, - } ); - } ); - - afterAll( () => { - unregisterBlockType( name ); - } ); +setupCoreBlocks( [ 'core/audio' ] ); +describe( 'Audio block', () => { it( 'renders placeholder without crashing', () => { const component = getTestComponentWithContent(); const rendered = component.toJSON(); @@ -86,4 +84,20 @@ describe( 'Audio block', () => { const rendered = component.toJSON(); expect( rendered ).toMatchSnapshot(); } ); + + it( 'should gracefully handle invalid URLs', async () => { + await initializeEditor(); + + await addBlock( screen, 'Audio' ); + fireEvent.press( screen.getByText( 'Insert from URL' ) ); + fireEvent.changeText( + screen.getByPlaceholderText( 'Type a URL' ), + 'h://wordpress.org/audio.mp3' + ); + dismissModal( screen.getByTestId( 'bottom-sheet' ) ); + + expect( + screen.getByText( 'Invalid URL. Audio file not found.' ) + ).toBeVisible(); + } ); } ); diff --git a/packages/block-library/src/video/edit.native.js b/packages/block-library/src/video/edit.native.js index 48b5692aee3d71..6c1798756b40c9 100644 --- a/packages/block-library/src/video/edit.native.js +++ b/packages/block-library/src/video/edit.native.js @@ -162,7 +162,7 @@ class VideoEdit extends Component { onSelectURL( url ) { const { createErrorNotice, onReplace, setAttributes } = this.props; - if ( isURL( url ) ) { + if ( isURL( url ) && /^https?:/.test( getProtocol( url ) ) ) { // Check if there's an embed block that handles this URL. const embedBlock = createUpgradedEmbedBlock( { attributes: { url }, diff --git a/packages/block-library/src/video/test/edit.native.js b/packages/block-library/src/video/test/edit.native.js new file mode 100644 index 00000000000000..9b7f4b0bdffbb4 --- /dev/null +++ b/packages/block-library/src/video/test/edit.native.js @@ -0,0 +1,29 @@ +/** + * External dependencies + */ +import { + addBlock, + dismissModal, + fireEvent, + initializeEditor, + screen, + setupCoreBlocks, +} from 'test/helpers'; + +setupCoreBlocks( [ 'core/video' ] ); + +describe( 'Video block', () => { + it( 'should gracefully handle invalid URLs', async () => { + await initializeEditor(); + + await addBlock( screen, 'Video' ); + fireEvent.press( screen.getByText( 'Insert from URL' ) ); + fireEvent.changeText( + screen.getByPlaceholderText( 'Type a URL' ), + 'h://wordpress.org/video.mp4' + ); + dismissModal( screen.getByTestId( 'bottom-sheet' ) ); + + expect( screen.getByText( 'Invalid URL.' ) ).toBeVisible(); + } ); +} ); diff --git a/packages/react-native-editor/CHANGELOG.md b/packages/react-native-editor/CHANGELOG.md index 70f0605599add7..504bad70c8057c 100644 --- a/packages/react-native-editor/CHANGELOG.md +++ b/packages/react-native-editor/CHANGELOG.md @@ -11,6 +11,7 @@ For each user feature we should also add a importance categorization label to i ## Unreleased - [*] Limit inner blocks nesting depth to avoid call stack size exceeded crash [#54382] +- [*] Prevent crashes when setting an invalid media URL for Video or Audio blocks [#54834] ## 1.104.0 - [*] Fix the obscurred "Insert from URL" input for media blocks when using a device in landscape orientation. [#54096]