Skip to content

Commit

Permalink
Feeds: Validate enclosures prior to generating tags.
Browse files Browse the repository at this point in the history
Prevent possible PHP warnings caused by malformed enclosure meta data. This change ensures the enclosure meta data has at least three lines of text before generating the tag in `rss_enclosure()`.

Props arypneta, johnjamesjacoby, nihar007, rahmohn, rghedin, zahardoc.
Fixes #58798.



git-svn-id: https://develop.svn.wordpress.org/trunk@59241 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Oct 15, 2024
1 parent 660d052 commit 4edc604
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/wp-includes/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ function rss_enclosure() {
foreach ( (array) $val as $enc ) {
$enclosure = explode( "\n", $enc );

if ( count( $enclosure ) < 3 ) {
continue;
}

// Only get the first element, e.g. 'audio/mpeg' from 'audio/mpeg mpga mp2 mp3'.
$t = preg_split( '/[ \t]/', trim( $enclosure[2] ) );
$type = $t[0];
Expand Down
139 changes: 139 additions & 0 deletions tests/phpunit/tests/feed/rssEnclosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* Test the feed.
*
* @group feed
*/
class Tests_rssEnclosure extends WP_UnitTestCase {
/**
* @ticket 58798
*
* @covers ::rss_enclosure
*/
public function test_rss_enclosure_filter() {
$post_id = self::factory()->post->create();
$GLOBALS['post'] = $post_id;

$valid_enclosure_string = "http://example.com/sound2.mp3\n12345\naudio/mpeg\n";

update_post_meta( $post_id, 'enclosure', $valid_enclosure_string );

add_filter(
'rss_enclosure',
function () {
return 'filtered_html_link_tag';
}
);

$this->assertSame( 'filtered_html_link_tag', get_echo( 'rss_enclosure' ), 'The `rss_enclosure` filter could not be applied.' );
}

/**
* @ticket 58798
*
* @covers ::rss_enclosure
*/
public function test_rss_enclosure_when_global_post_is_empty() {
$this->assertEmpty( get_echo( 'rss_enclosure' ), 'It should return empty when the global post is not set.' );
}

/**
* @ticket 58798
*
* @covers ::rss_enclosure
*/
public function test_rss_enclosure_when_enclosure_meta_field_is_empty() {
$post_id = self::factory()->post->create();
$GLOBALS['post'] = $post_id;

$this->assertEmpty( get_echo( 'rss_enclosure' ), 'The global post does not have the `enclosure` meta field and should return empty. ' );
}

/**
* @ticket 58798
*
* @dataProvider data_rss_enclosure_with_multiline_enclosure_string
* @covers ::rss_enclosure
*/
public function test_rss_enclosure_with_multiline_enclosure_string( $enclosure_data, $enclosure_string ) {
$post_id = self::factory()->post->create();
$GLOBALS['post'] = $post_id;

update_post_meta( $post_id, 'enclosure', $enclosure_string );

$expected = '<enclosure url="' . $enclosure_data['url'] . '" length="' . $enclosure_data['length'] . '" type="' . $enclosure_data['type'] . '" />' . "\n";

$this->assertSame( $expected, get_echo( 'rss_enclosure' ), 'It should return a valid enclosure tag. ' );
}

/**
* Data provider for valid enclosure string.
*
* @return array[].
*/
public function data_rss_enclosure_with_multiline_enclosure_string() {
return array(
'two-break-lines' => array(
array(
'url' => 'http://example.com/sound2.mp3',
'length' => 12345,
'type' => 'audio/mpeg',
),
"http://example.com/sound2.mp3\n12345\naudio/mpeg",
),
'three-break-lines' => array(
array(
'url' => 'http://example.com/sound2.mp3',
'length' => 12345,
'type' => 'audio/mpeg',
),
"http://example.com/sound2.mp3\n12345\naudio/mpeg\n",
),
'extra-break-line-at-end' => array(
array(
'url' => 'http://example.com/sound2.mp3',
'length' => 12345,
'type' => 'audio/mpeg',
),
"http://example.com/sound2.mp3\n12345\naudio/mpeg\n\n",
),
'extra-type-elements' => array(
array(
'url' => 'http://example.com/sound2.mp3',
'length' => 12345,
'type' => 'audio/mpeg',
),
"http://example.com/sound2.mp3\n12345\naudio/mpeg mpga mp2 mp3\n",
),
);
}

/**
* @ticket 58798
*
* @dataProvider data_rss_enclosure_with_non_valid_enclosure_string
* @covers ::rss_enclosure
*/
public function test_rss_enclosure_with_non_valid_enclosure_string( $enclosure_string ) {
$post_id = self::factory()->post->create();
$GLOBALS['post'] = $post_id;

update_post_meta( $post_id, 'enclosure', $enclosure_string );

$this->assertEmpty( get_echo( 'rss_enclosure' ), 'It should return empty when the `enclosure` meta field is not saved in a multiline string.' );
}

/**
* Data provider for non-valid enclosure string.
*
* @return array[].
*/
public function data_rss_enclosure_with_non_valid_enclosure_string() {
return array(
'empty' => array( '' ),
'no-break-lines' => array( 'http://example.com/sound2.mp3 12345 audio/mpeg' ),
'one-break-line' => array( "http://example.com/sound2.mp3\n12345 audio/mpeg" ),
);
}
}

0 comments on commit 4edc604

Please sign in to comment.