Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent crash from invalid media URLs #54834

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/block-library/src/audio/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.' ) );
Expand Down
42 changes: 28 additions & 14 deletions packages/block-library/src/audio/test/edit.native.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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();
} );
} );
2 changes: 1 addition & 1 deletion packages/block-library/src/video/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
29 changes: 29 additions & 0 deletions packages/block-library/src/video/test/edit.native.js
Original file line number Diff line number Diff line change
@@ -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();
} );
} );
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading