EntryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. public function test_content_doesNotRepeatEnclosureTitleForEachThumbnail(): void {
  87. $title = str_repeat('&quot;', 2000);
  88. $thumbnails = implode('', array_map(static fn(int $i): string => "<media:thumbnail url=\"https://example.net/$i.png\" />", range(1, 500)));
  89. $rss = <<<XML
  90. <?xml version="1.0" encoding="UTF-8"?>
  91. <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
  92. <channel>
  93. <title>Feed</title>
  94. <link>https://example.net/</link>
  95. <item>
  96. <title>Gallery</title>
  97. <link>https://example.net/article</link>
  98. <guid isPermaLink="false">gallery-001</guid>
  99. <media:content url="https://example.net/video.mp4" type="video/mp4">
  100. <media:title>{$title}</media:title>
  101. {$thumbnails}
  102. </media:content>
  103. </item>
  104. </channel>
  105. </rss>
  106. XML;
  107. $entries = self::entriesFromRss($rss);
  108. self::assertCount(1, $entries);
  109. $html = $entries[0]->content();
  110. // All the thumbnails are shown, and the title is kept for the enclosure itself
  111. self::assertSame(500, substr_count($html, 'class="enclosure-thumbnail"'));
  112. self::assertSame(1, substr_count($html, 'title="' . $title . '"'));
  113. // But the size of the article is not the length of the title multiplied by the number of thumbnails
  114. self::assertLessThan(5 * strlen($rss), strlen($html));
  115. }
  116. /** ZIP/JSON export/import */
  117. public function test_toGReaderFreshRssExportRoundTripsEncoding(): void {
  118. $feed = new FreshRSS_Feed('http://example.net/feed.xml', validate: false);
  119. $feed->_id(1);
  120. $feed->_name('Feed &amp; me');
  121. $entry = new FreshRSS_Entry(1, 'tag:example.net,2026:1', 'A &amp; B', 'B &amp; C', 'Hello', 'https://example.net/post-1', 1731312000);
  122. $entry->_id('1731312000000000');
  123. $entry->_feed($feed);
  124. $item = $entry->toGReader('freshrss');
  125. self::assertSame('A &amp; B', $item['title']);
  126. self::assertSame('B &amp; C', $item['author']);
  127. }
  128. /** Above PHP_INT_MAX, an integer literal is a float on 32-bit platforms, so an entry ID is only ever a string */
  129. public function test_idRejectsAFloat(): void {
  130. $entry = new FreshRSS_Entry(1, 'guid-1', 'Title', '', '', 'https://example.net/1', 1700000000);
  131. $this->expectException(\TypeError::class);
  132. // @phpstan-ignore argument.type
  133. $entry->_id(1700000000000001.0);
  134. }
  135. /** @return array<string,array{string,string,string|null}> Content, expected, delimiter (null for the legacy markers) */
  136. public static function provideFullContent(): array {
  137. $start = '<!-- FULLCONTENT start //-->';
  138. $end = '<!-- FULLCONTENT end //-->';
  139. // Original content with copies of the legacy markers inside attribute values, as serialised by libxml2
  140. $original = '<p title="' . $start . '">Original</p> &amp; <b title=\'' . $end . '\'>content</b>';
  141. $cleanOriginal = '<p>Original</p> &amp; <b>content</b>';
  142. $cases = [];
  143. foreach ([null, '0123456789abcdef0123456789abcdef'] as $delimiter) {
  144. $label = $delimiter === null ? 'legacy' : 'delimited';
  145. $separator = $delimiter === null ? '' : " {$delimiter}";
  146. $full = "<!-- FULLCONTENT{$separator} start //--><p>Full</p><!-- FULLCONTENT{$separator} end //-->";
  147. $unpairedStart = "<!-- FULLCONTENT{$separator} start //-->Full";
  148. $cases = $cases + [
  149. "none $label" => [$cleanOriginal, $cleanOriginal, $delimiter],
  150. "prepend $label" => [$full . $original, $original, $delimiter],
  151. "append $label" => [$cleanOriginal . $full, $cleanOriginal, $delimiter],
  152. "replace $label" => [$full, '', $delimiter],
  153. // A lone start marker must not trigger an unsafe cut
  154. "unpaired start $label" => [$unpairedStart, $unpairedStart, $delimiter],
  155. ];
  156. // (Delimited only) A legacy end comment cannot close a delimited start
  157. if ($delimiter !== null) {
  158. $cases["legacy end inside $label span"] = [
  159. "<!-- FULLCONTENT {$delimiter} start //-->Full" . $end . 'x' . "<!-- FULLCONTENT {$delimiter} end //-->",
  160. '',
  161. $delimiter,
  162. ];
  163. }
  164. }
  165. return $cases;
  166. }
  167. #[DataProvider('provideFullContent')]
  168. public function test_originalContent_removesFullContent(string $content, string $expected, ?string $delimiter): void {
  169. $entry = new FreshRSS_Entry(1, 'guid', 'Title', '', $content, 'https://example.net/article');
  170. if ($delimiter !== null) {
  171. $entry->_attribute('full_content_delimiter', $delimiter);
  172. }
  173. self::assertSame($expected, $entry->originalContent());
  174. }
  175. /**
  176. * Legacy full-content of legacy entries (unsafe) already in database.
  177. */
  178. public function test_loadEntries_withMarkerEchoesInAttributesOfFeed(): void {
  179. $rss = <<<'XML'
  180. <?xml version="1.0" encoding="UTF-8"?>
  181. <rss version="2.0">
  182. <channel>
  183. <title>Feed</title>
  184. <link>https://example.net/</link>
  185. <item>
  186. <title>Article</title>
  187. <link>https://example.net/article</link>
  188. <guid isPermaLink="false">fullcontent-001</guid>
  189. <description><![CDATA[<p title="<!-- FULLCONTENT start //-->">Hello</p>
  190. <b title='<!-- FULLCONTENT end //-->" <!--="x"><form><button>x</button></form>-->'>bold</b>]]></description>
  191. </item>
  192. </channel>
  193. </rss>
  194. XML;
  195. $entries = self::entriesFromRss($rss);
  196. self::assertCount(1, $entries);
  197. // The legacy markers echoed in attribute values are still detected and cut at ingest
  198. self::assertStringNotContainsString('<!-- FULLCONTENT', $entries[0]->content(withEnclosures: false));
  199. }
  200. /**
  201. * Test security of full-content markers for new content updates.
  202. */
  203. public function test_originalContent_withForgedMarkersInNewContentUpdate(): void {
  204. $delimiter = bin2hex(random_bytes(16));
  205. // Attacker-forged content echoing the legacy markers inside attribute values, as serialised by libxml2
  206. $forged = '<p title="<!-- FULLCONTENT start //-->">Hello</p> &amp; ' .
  207. '<b title=\'<!-- FULLCONTENT end //-->" <!--="x"><form><button>x</button></form>-->\'>bold</b>';
  208. // Freshly fetched content wrapped by loadCompleteContent(), prepended to the original content
  209. $full = "<!-- FULLCONTENT {$delimiter} start //--><p>Full</p><!-- FULLCONTENT {$delimiter} end //-->";
  210. $entry = new FreshRSS_Entry(1, 'guid', 'Title', '', $full . $forged, 'https://example.net/article');
  211. $entry->_attribute('full_content_delimiter', $delimiter);
  212. // Only the span of the secret unique delimiter is cut; the forged markers are inert
  213. self::assertSame($forged, $entry->originalContent());
  214. // Re-parsing the remaining content must not enable any forged element
  215. $doc = new DOMDocument();
  216. self::assertTrue($doc->loadHTML('<meta charset="UTF-8">' . $forged, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING));
  217. self::assertCount(0, $doc->getElementsByTagName('form'));
  218. self::assertSame('p', $doc->getElementsByTagName('p')->item(0)?->nodeName);
  219. }
  220. }