From 0f6bdb81a3f388f168f18670b5d20b50b9913661 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:25:00 +0100 Subject: [PATCH] Correct the StringHelper::compliant() docblock The docblock claimed that compliant() accepts five and six octet sequences and is less strict than valid(). PCRE's UTF-8 validation rejects them, as well as overlong forms, surrogates and incomplete sequences. Co-Authored-By: Claude Opus 5.5 --- Tests/StringHelperTest.php | 2 ++ src/StringHelper.php | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/StringHelperTest.php b/Tests/StringHelperTest.php index 8d6fd578..f897f056 100644 --- a/Tests/StringHelperTest.php +++ b/Tests/StringHelperTest.php @@ -436,6 +436,8 @@ public static function seedCompliantStrings(): array ["\xFBa", false], ["\xFDa", false], ["foo\xF7bar", false], + 'five octet sequence' => ["\xF8\x88\x80\x80\x80", false], + 'six octet sequence' => ["\xFC\x84\x80\x80\x80\x80", false], ['george Мога Ž Ψυχοφθόρα ฉันกินกระจกได้ 我能吞下玻璃而不伤身体 ', true], ["\xFF ABC", false], ["0xfffd ABC", true], diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..35953753 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -880,10 +880,8 @@ public static function valid($str) /** * Tests whether a string complies as UTF-8. * - * This will be much faster than StringHelper::valid() but will pass five and six octet UTF-8 sequences, which are not supported by Unicode and - * so cannot be displayed correctly in a browser. In other words it is not as strict as StringHelper::valid() but it's faster. If you use it to - * validate user input, you place yourself at the risk that attackers will be able to inject 5 and 6 byte sequences (which may or may not be a - * significant risk, depending on what you are are doing). + * Uses the UTF-8 validation of PCRE, which rejects five and six octet sequences, overlong forms, surrogates, code points above U+10FFFF + * and incomplete multi-byte sequences. * * @param string $str UTF-8 string to check *