4
0

EntryTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class EntryTest extends \PHPUnit\Framework\TestCase {
  5. #[\Override]
  6. protected function setUp(): void {
  7. if (!FreshRSS_Context::hasSystemConf()) {
  8. FreshRSS_Context::initSystem();
  9. }
  10. }
  11. /**
  12. * Parse a raw RSS payload through the real feed processing pipeline.
  13. * @return list<FreshRSS_Entry>
  14. */
  15. private static function entriesFromRss(string $rss): array {
  16. $feed = new FreshRSS_Feed('http://example.net/feed.xml', validate: false);
  17. $feed->_id(1);
  18. $simplePie = new FreshRSS_SimplePieCustom();
  19. $simplePie->enable_cache(false);
  20. $simplePie->set_raw_data($rss);
  21. self::assertTrue($simplePie->init());
  22. return array_values(iterator_to_array($feed->loadEntries($simplePie)));
  23. }
  24. public function test_content_dropsUnsafeEnclosureAndThumbnailUrls(): void {
  25. $rss = <<<XML
  26. <?xml version="1.0" encoding="UTF-8"?>
  27. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  28. <channel>
  29. <title>Malicious feed</title>
  30. <link>https://example.net/</link>
  31. <description>Feed with malicious enclosure URLs</description>
  32. <item>
  33. <title>Victim Article</title>
  34. <link>https://example.net/article</link>
  35. <guid isPermaLink="false">poc-001</guid>
  36. <pubDate>Tue, 14 Nov 2023 20:13:20 +0000</pubDate>
  37. <description>Hello</description>
  38. <enclosure url="javascript:alert(document.domain)//" length="0" type="application/octet-stream" />
  39. <enclosure url="https://example.com/podcast.mp3" length="1234" type="audio/mpeg" />
  40. <enclosure url="//cdn.example.com/podcast2.mp3" length="1234" type="audio/mpeg" />
  41. <media:content url="https://example.com/pic.jpg" type="image/jpeg">
  42. <media:thumbnail url="javascript:alert(1)" />
  43. <media:thumbnail url="https://example.com/thumb.jpg" />
  44. <media:thumbnail url="//cdn.example.com/thumb2.jpg" />
  45. </media:content>
  46. <media:thumbnail url="javascript:alert(2)" />
  47. </item>
  48. </channel>
  49. </rss>
  50. XML;
  51. $entries = self::entriesFromRss($rss);
  52. self::assertCount(1, $entries);
  53. $entry = $entries[0];
  54. self::assertSame('Victim Article', $entry->title());
  55. // The malicious `<media:thumbnail>` of the item must not be stored as an attribute
  56. self::assertNull($entry->attributeArray('thumbnail'));
  57. // The malicious enclosure must not even be stored as an attribute
  58. $enclosureUrls = array_column($entry->attributeArray('enclosures') ?? [], 'url');
  59. self::assertNotContains('javascript:alert(document.domain)//', $enclosureUrls);
  60. self::assertContains('https://example.com/podcast.mp3', $enclosureUrls);
  61. // SimplePie must absolutise protocol-relative URLs against the feed URL
  62. self::assertContains('https://cdn.example.com/podcast2.mp3', $enclosureUrls);
  63. $html = $entry->content();
  64. self::assertStringNotContainsString('javascript:', $html);
  65. self::assertStringContainsString('href="https://example.com/podcast.mp3"', $html);
  66. self::assertStringContainsString('src="https://example.com/thumb.jpg"', $html);
  67. self::assertStringContainsString('href="https://cdn.example.com/podcast2.mp3"', $html);
  68. self::assertStringContainsString('src="https://cdn.example.com/thumb2.jpg"', $html);
  69. self::assertStringContainsString('Hello', $html);
  70. }
  71. public function test_content_dropsUnsafeUrlsFromLegacyAttributes(): void {
  72. $entry = new FreshRSS_Entry(1, 'poc-003', 'Victim Article', '', 'Hello', 'https://example.net/article');
  73. $entry->_attributes([
  74. 'thumbnail' => ['url' => 'javascript:alert(1)'],
  75. 'enclosures' => [
  76. ['url' => 'javascript:alert(2)', 'title' => 'evil'],
  77. ['url' => 'https://example.com/podcast.mp3', 'thumbnails' => ['javascript:alert(3)', 'https://example.com/thumb.jpg']],
  78. ],
  79. ]);
  80. $html = $entry->content();
  81. self::assertStringNotContainsString('javascript:', $html);
  82. self::assertStringContainsString('href="https://example.com/podcast.mp3"', $html);
  83. self::assertStringContainsString('src="https://example.com/thumb.jpg"', $html);
  84. self::assertStringContainsString('Hello', $html);
  85. }
  86. /** ZIP/JSON export/import */
  87. public function test_toGReaderFreshRssExportRoundTripsEncoding(): void {
  88. $feed = new FreshRSS_Feed('http://example.net/feed.xml', validate: false);
  89. $feed->_id(1);
  90. $feed->_name('Feed &amp; me');
  91. $entry = new FreshRSS_Entry(1, 'tag:example.net,2026:1', 'A &amp; B', 'B &amp; C', 'Hello', 'https://example.net/post-1', 1731312000);
  92. $entry->_id('1731312000000000');
  93. $entry->_feed($feed);
  94. $item = $entry->toGReader('freshrss');
  95. self::assertSame('A &amp; B', $item['title']);
  96. self::assertSame('B &amp; C', $item['author']);
  97. }
  98. /** Above PHP_INT_MAX, an integer literal is a float on 32-bit platforms, so an entry ID is only ever a string */
  99. public function test_idRejectsAFloat(): void {
  100. $entry = new FreshRSS_Entry(1, 'guid-1', 'Title', '', '', 'https://example.net/1', 1700000000);
  101. $this->expectException(\TypeError::class);
  102. // @phpstan-ignore argument.type
  103. $entry->_id(1700000000000001.0);
  104. }
  105. /** @return array<string,array{string,string,string|null}> Content, expected, delimiter (null for the legacy markers) */
  106. public static function provideFullContent(): array {
  107. $start = '<!-- FULLCONTENT start //-->';
  108. $end = '<!-- FULLCONTENT end //-->';
  109. // Original content with copies of the legacy markers inside attribute values, as serialised by libxml2
  110. $original = '<p title="' . $start . '">Original</p> &amp; <b title=\'' . $end . '\'>content</b>';
  111. $cleanOriginal = '<p>Original</p> &amp; <b>content</b>';
  112. $cases = [];
  113. foreach ([null, '0123456789abcdef0123456789abcdef'] as $delimiter) {
  114. $label = $delimiter === null ? 'legacy' : 'delimited';
  115. $separator = $delimiter === null ? '' : " {$delimiter}";
  116. $full = "<!-- FULLCONTENT{$separator} start //--><p>Full</p><!-- FULLCONTENT{$separator} end //-->";
  117. $unpairedStart = "<!-- FULLCONTENT{$separator} start //-->Full";
  118. $cases = $cases + [
  119. "none $label" => [$cleanOriginal, $cleanOriginal, $delimiter],
  120. "prepend $label" => [$full . $original, $original, $delimiter],
  121. "append $label" => [$cleanOriginal . $full, $cleanOriginal, $delimiter],
  122. "replace $label" => [$full, '', $delimiter],
  123. // A lone start marker must not trigger an unsafe cut
  124. "unpaired start $label" => [$unpairedStart, $unpairedStart, $delimiter],
  125. ];
  126. // (Delimited only) A legacy end comment cannot close a delimited start
  127. if ($delimiter !== null) {
  128. $cases["legacy end inside $label span"] = [
  129. "<!-- FULLCONTENT {$delimiter} start //-->Full" . $end . 'x' . "<!-- FULLCONTENT {$delimiter} end //-->",
  130. '',
  131. $delimiter,
  132. ];
  133. }
  134. }
  135. return $cases;
  136. }
  137. #[DataProvider('provideFullContent')]
  138. public function test_originalContent_removesFullContent(string $content, string $expected, ?string $delimiter): void {
  139. $entry = new FreshRSS_Entry(1, 'guid', 'Title', '', $content, 'https://example.net/article');
  140. if ($delimiter !== null) {
  141. $entry->_attribute('full_content_delimiter', $delimiter);
  142. }
  143. self::assertSame($expected, $entry->originalContent());
  144. }
  145. /**
  146. * Legacy full-content of legacy entries (unsafe) already in database.
  147. */
  148. public function test_loadEntries_withMarkerEchoesInAttributesOfFeed(): void {
  149. $rss = <<<'XML'
  150. <?xml version="1.0" encoding="UTF-8"?>
  151. <rss version="2.0">
  152. <channel>
  153. <title>Feed</title>
  154. <link>https://example.net/</link>
  155. <item>
  156. <title>Article</title>
  157. <link>https://example.net/article</link>
  158. <guid isPermaLink="false">fullcontent-001</guid>
  159. <description><![CDATA[<p title="<!-- FULLCONTENT start //-->">Hello</p>
  160. <b title='<!-- FULLCONTENT end //-->" <!--="x"><form><button>x</button></form>-->'>bold</b>]]></description>
  161. </item>
  162. </channel>
  163. </rss>
  164. XML;
  165. $entries = self::entriesFromRss($rss);
  166. self::assertCount(1, $entries);
  167. // The legacy markers echoed in attribute values are still detected and cut at ingest
  168. self::assertStringNotContainsString('<!-- FULLCONTENT', $entries[0]->content(withEnclosures: false));
  169. }
  170. /**
  171. * Test security of full-content markers for new content updates.
  172. */
  173. public function test_originalContent_withForgedMarkersInNewContentUpdate(): void {
  174. $delimiter = bin2hex(random_bytes(16));
  175. // Attacker-forged content echoing the legacy markers inside attribute values, as serialised by libxml2
  176. $forged = '<p title="<!-- FULLCONTENT start //-->">Hello</p> &amp; ' .
  177. '<b title=\'<!-- FULLCONTENT end //-->" <!--="x"><form><button>x</button></form>-->\'>bold</b>';
  178. // Freshly fetched content wrapped by loadCompleteContent(), prepended to the original content
  179. $full = "<!-- FULLCONTENT {$delimiter} start //--><p>Full</p><!-- FULLCONTENT {$delimiter} end //-->";
  180. $entry = new FreshRSS_Entry(1, 'guid', 'Title', '', $full . $forged, 'https://example.net/article');
  181. $entry->_attribute('full_content_delimiter', $delimiter);
  182. // Only the span of the secret unique delimiter is cut; the forged markers are inert
  183. self::assertSame($forged, $entry->originalContent());
  184. // Re-parsing the remaining content must not enable any forged element
  185. $doc = new DOMDocument();
  186. self::assertTrue($doc->loadHTML('<meta charset="UTF-8">' . $forged, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING));
  187. self::assertCount(0, $doc->getElementsByTagName('form'));
  188. self::assertSame('p', $doc->getElementsByTagName('p')->item(0)?->nodeName);
  189. }
  190. }