Просмотр исходного кода

Fix charset Windows-1252 (#9649)

fix https://github.com/FreshRSS/FreshRSS/issues/9648
Alexandre Alapetite 6 часов назад
Родитель
Сommit
ec7c07b17d
2 измененных файлов с 67 добавлено и 2 удалено
  1. 2 2
      app/Utils/httpUtil.php
  2. 65 0
      tests/app/Utils/httpUtilTest.php

+ 2 - 2
app/Utils/httpUtil.php

@@ -211,8 +211,8 @@ final class FreshRSS_http_Util {
 			}
 		}
 		$httpCharsetNormalized = \SimplePie\Misc::encoding($httpCharset);
-		if (in_array($httpCharsetNormalized, ['windows-1252', 'US-ASCII'], true)) {
-			// Default charset for HTTP, do nothing
+		if ($httpCharsetNormalized === 'US-ASCII') {
+			// Do nothing
 			return $html;
 		}
 		if (substr($html, 0, 3) === "\xEF\xBB\xBF" || // UTF-8 BOM

+ 65 - 0
tests/app/Utils/httpUtilTest.php

@@ -113,6 +113,71 @@ final class httpUtilTest extends \PHPUnit\Framework\TestCase {
 		yield 'meta http-equiv' => ['<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>T</title></head>' . $body];
 	}
 
+	/** @return Traversable<string,array{string,string,string}> */
+	public static function provideLegacyCharsets(): Traversable {
+		$isoUtf8 = 'M' . "\xC3\xBC" . 'llerstra' . "\xC3\x9F" . 'e';
+		yield 'iso-8859-1, meta charset' => ['iso-8859-1',
+			'<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head>' .
+			'<body><p>M' . "\xFC" . 'llerstra' . "\xDF" . 'e</p></body></html>', $isoUtf8];
+		// A Windows-1252 body whose quotes differ from the ISO-8859-1 mapping
+		yield 'windows-1252, smart quotes' => ['windows-1252',
+			'<!DOCTYPE html><html><head><meta charset="windows-1252"></head>' .
+			'<body><p>' . "\x93" . 'quoted' . "\x94" . '</p></body></html>', "\xE2\x80\x9C" . 'quoted' . "\xE2\x80\x9D"];
+	}
+
+	// Legacy charsets declared over HTTP must be converted to UTF-8 and not just left
+	// to the HTML parser, because `saveHTML()` serialises extracted fragments according
+	// to the encoding of the document, and downstream steps expecting UTF-8 would
+	// therefore fail on the remaining Windows-1252 bytes (e.g. full-content fetching, issue #9648).
+	#[DataProvider('provideLegacyCharsets')]
+	public function test_enforceHttpEncoding_convertsLegacyCharsetsToUtf8(string $httpCharset, string $html, string $expectedUtf8Text): void {
+		$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
+		$result = $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=' . $httpCharset);
+		self::assertIsString($result);
+		// The HTTP-declared encoding is enforced by prefixing a Unicode byte order mark
+		self::assertStringStartsWith("\xEF\xBB\xBF", $result);
+		self::assertStringContainsString($expectedUtf8Text, $result);
+		// The obsolete charset declaration is removed, so that the now UTF-8 bytes are not re-read with the declared legacy charset
+		self::assertStringNotContainsString('<meta', $result);
+	}
+
+	public function test_enforceHttpEncoding_legacyCharset_fragmentSerialization(): void {
+		$html = '<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head><body><div class="artikel">' .
+			'<p>Gr' . "\xF6" . 'nemeyer, oft w' . "\xFC" . 'tend</p></div></body></html>';
+		$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
+		$html = $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=iso-8859-1');
+		self::assertIsString($html);
+
+		// Extract a fragment the way done when fetching the full content of an article
+		$doc = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
+		$node = $doc->querySelector('div.artikel>p');
+		self::assertNotNull($node);
+		self::assertSame('Gr' . "\xC3\xB6" . 'nemeyer, oft w' . "\xC3\xBC" . 'tend', $node->textContent);
+		$fragment = $doc->saveHTML($node);
+		// The serialised fragment must remain valid UTF-8 and not be corrupted to U+FFFD replacement characters
+		self::assertStringContainsString('Gr' . "\xC3\xB6" . 'nemeyer, oft w' . "\xC3\xBC" . 'tend', $fragment);
+		self::assertStringNotContainsString("\xEF\xBF\xBD", $fragment);
+	}
+
+	public function test_enforceHttpEncoding_usAsciiHttpCharset_unchanged(): void {
+		$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
+		$html = '<p>abcdef</p>';
+		self::assertSame($html, $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=US-ASCII'));
+	}
+
+	public function test_enforceHttpEncoding_noHttpCharsetLeavesLegacyHtmlUnchanged(): void {
+		$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
+		$html = '<!DOCTYPE html><html><head><meta charset="iso-8859-1"></head><body><p>M' . "\xFC" . 'ller</p></body></html>';
+		// Without an HTTP-declared charset, a legacy-declared body is left untouched, for the HTML parser to handle it
+		self::assertSame($html, $enforceHttpEncoding->invoke(null, $html, 'text/html'));
+	}
+
+	public function test_enforceHttpEncoding_utf8HttpCharsetPrefixesByteOrderMark(): void {
+		$enforceHttpEncoding = new ReflectionMethod(FreshRSS_http_Util::class, 'enforceHttpEncoding');
+		$html = '<p>M' . "\xC3\xBC" . 'ller</p>';
+		self::assertSame("\xEF\xBB\xBF" . $html, $enforceHttpEncoding->invoke(null, $html, 'text/html; charset=utf-8'));
+	}
+
 	#[DataProvider('provideCidrRanges')]
 	public function test_checkCIDR(string $ip, string $range, bool $expected): void {
 		$checkCIDR = new ReflectionMethod(FreshRSS_http_Util::class, 'checkCIDR');