Преглед изворни кода

Fix regression lazyload (#9601)

fix https://github.com/FreshRSS/FreshRSS/pull/7864#issuecomment-7036632538
Follow-up of https://github.com/FreshRSS/FreshRSS/pull/5816
Alexandre Alapetite пре 8 часа
родитељ
комит
71cda6bfe2
2 измењених фајлова са 65 додато и 7 уклоњено
  1. 21 4
      lib/lib_rss.php
  2. 44 3
      tests/lib/LibRssTest.php

+ 21 - 4
lib/lib_rss.php

@@ -344,15 +344,32 @@ function cleanCache(int $hours = 720): void {
 
 /**
  * Add native lazy loading by inserting a `loading="lazy"` attribute right after
- * the tag name of img/image/iframe/track/video elements that do not have one.
+ * the tag name of img/image/iframe elements that do not have one,
+ * and use `data-original` attribute for video/track elements and their poster/src attributes.
  * @param string $content is the text we want to parse
  */
 function lazyimg(string $content): string {
-	return preg_replace(
-		'/<((?:img|image|iframe|track|video)\b)(?![^>]*\b(?:loading|data-original)=)([^>]*+)>/i',
+	$content = preg_replace(
+		'/<((?:img|image|iframe)\b)(?![^>]{0,1000}\b(?:loading|data-original)=)([^>]{0,1000}+)>/i',
 		'<$1 loading="lazy"$2>',
 		$content
-	) ?? '';
+	) ?? $content;
+
+	$placeholder = Minz_Url::display('/themes/icons/grey.gif');
+	return preg_replace(
+		[
+			'/<((?:video)(?![^>]{0,1000}\bdata-original=)[^>]{1,1000}?)(?<![\w\'"-])poster="([^"]+)"([^>]*)>/i',
+			'/<((?:video)(?![^>]{0,1000}\bdata-original=)[^>]{1,1000}?)(?<![\w\'"-])poster=\'([^\']+)\'([^>]*)>/i',
+			'/<((?:track)(?![^>]{0,1000}\bdata-original=)[^>]{1,1000}?)(?<![\w\'"-])src="([^"]+)"([^>]*)>/i',
+			'/<((?:track)(?![^>]{0,1000}\bdata-original=)[^>]{1,1000}?)(?<![\w\'"-])src=\'([^\']+)\'([^>]*)>/i',
+		], [
+			'<$1poster="' . $placeholder . '" data-original="$2"$3>',
+			'<$1poster=\'' . $placeholder . '\' data-original=\'$2\'$3>',
+			'<$1src="' . $placeholder . '" data-original="$2"$3>',
+			'<$1src=\'' . $placeholder . '\' data-original=\'$2\'$3>',
+		],
+		$content
+	) ?? $content;
 }
 
 /** @return numeric-string */

+ 44 - 3
tests/lib/LibRssTest.php

@@ -10,14 +10,55 @@ final class LibRssTest extends \PHPUnit\Framework\TestCase {
 		self::assertSame($expected, lazyimg($content));
 	}
 
+	public function test_lazyimg_regex_failure_preserves_content(): void {
+		// Simulate the failure of PCRE on potentially large documents, e.g. because of backtracking limits
+		$limit = ini_get('pcre.backtrack_limit');
+		self::assertIsString($limit);
+		ini_set('pcre.backtrack_limit', '1');
+		try {
+			$input = '<video poster="p.jpg"><img src="a.png"></video>';
+			self::assertSame($input, lazyimg($input));
+		} finally {
+			ini_set('pcre.backtrack_limit', $limit);
+		}
+	}
+
+	public function test_lazyimg_overlong_tags_are_skipped(): void {
+		// Tags with more than 1000 characters of attributes are expected to be skipped (eager loading)
+		$overlong = str_repeat('a', 1001);
+		self::assertSame('<img alt="' . $overlong . '" src="a.png">', lazyimg('<img alt="' . $overlong . '" src="a.png">'));
+		self::assertSame('<video data-x="' . $overlong . '" poster="p.jpg"></video>',
+			lazyimg('<video data-x="' . $overlong . '" poster="p.jpg"></video>'));
+
+		// Under the limit, the work is still bounded and lazy loading is applied
+		$atLimit = str_repeat('a', 980);	// 6 + 980 + 13 = 999 characters of attributes
+		$placeholder = Minz_Url::display('/themes/icons/grey.gif');
+		self::assertSame('<img loading="lazy" alt="' . $atLimit . '" src="a.png">',
+			lazyimg('<img alt="' . $atLimit . '" src="a.png">'));
+		self::assertSame('<video data-x="' . $atLimit . '" poster="' . $placeholder . '" data-original="p.jpg"></video>',
+			lazyimg('<video data-x="' . $atLimit . '" poster="p.jpg"></video>'));
+	}
+
 	/** @return array<string,array{string,string}> */
 	public static function provideLazyimg(): array {
+		$placeholder = Minz_Url::display('/themes/icons/grey.gif');
 		return [
 			'ordinary image' => ['<img src="a.png">', '<img loading="lazy" src="a.png">'],
 			'multiple tags' => ['<img src="1.png"><iframe src="2.html" width="9"></iframe>',
 				'<img loading="lazy" src="1.png"><iframe loading="lazy" src="2.html" width="9"></iframe>'],
-			'video poster' => ['<video poster="p.jpg"></video>', '<video loading="lazy" poster="p.jpg"></video>'],
-			'track element' => ['<track src="t.vtt">', '<track loading="lazy" src="t.vtt">'],
+			'video poster' => ['<video poster="p.jpg"></video>', '<video poster="' . $placeholder . '" data-original="p.jpg"></video>'],
+			'video poster with other attributes' => ['<video poster="p.jpg" width="9"></video>',
+				'<video poster="' . $placeholder . '" data-original="p.jpg" width="9"></video>'],
+			'video without poster untouched' => ['<video src="v.mp4"></video>', '<video src="v.mp4"></video>'],
+			'track element' => ['<track src="t.vtt">', '<track src="' . $placeholder . '" data-original="t.vtt">'],
+			'video and track elements' => ['<video poster="p.jpg" width="9"><track src="t.vtt" srclang="en"></video>',
+				'<video poster="' . $placeholder . '" data-original="p.jpg" width="9">'
+					. '<track src="' . $placeholder . '" data-original="t.vtt" srclang="en"></video>'],
+			'track data-src is not rewritten' => ['<track data-src="x.vtt">', '<track data-src="x.vtt">'],
+			'poster inside attribute value untouched' => ['<video title="poster=" src="v.mp4"></video>',
+				'<video title="poster=" src="v.mp4"></video>'],
+			'legacy video data-original untouched' => ['<video data-original="p.jpg" poster="p.jpg"></video>',
+				'<video data-original="p.jpg" poster="p.jpg"></video>'],
 			'case insensitive' => ['<IMG SRC="A.PNG">', '<IMG loading="lazy" SRC="A.PNG">'],
 
 			'already lazy' => ['<img loading="lazy" src="a.png">', '<img loading="lazy" src="a.png">'],
@@ -27,7 +68,7 @@ final class LibRssTest extends \PHPUnit\Framework\TestCase {
 			'tag name boundary' => ['<imgish src="a.png">', '<imgish src="a.png">'],
 			'end tag untouched' => ['</img src="a.png">', '</img src="a.png">'],
 
-			'data-src is not rewritten' => ['<iframe data-src="javascript:x()">', '<iframe loading="lazy" data-src="javascript:x()">'],
+			'iframe data-src is not rewritten' => ['<iframe data-src="javascript:x()">', '<iframe loading="lazy" data-src="javascript:x()">'],
 
 			'src inside a title value' => [
 				'<iframe title="src=" data-x=" srcdoc=\'<p>doc</p>\' sandbox=\'allow-scripts allow-same-origin allow-forms\' " src="doc.pdf">',