From f80173c8d0c22917af62d44470094df72e1c810c Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:11:47 +0100 Subject: [PATCH] Reject truncated multi-byte sequences in StringHelper::valid() The hand-written state machine returned true at the end of the string without checking that the last multi-byte sequence was complete, so valid("abc\xC3") was true while compliant() and mb_check_encoding() returned false. Code that uses valid() as a validation gate accepted malformed UTF-8. mb_check_encoding() performs the same checks (overlong forms, surrogates, code points above U+10FFFF) and rejects incomplete sequences, and symfony/polyfill-mbstring is already required. Co-Authored-By: Claude Opus 5.5 --- Tests/StringHelperTest.php | 8 +++ docs/overview.md | 7 +++ src/StringHelper.php | 118 +------------------------------------ 3 files changed, 17 insertions(+), 116 deletions(-) diff --git a/Tests/StringHelperTest.php b/Tests/StringHelperTest.php index 8d6fd578..787c4d68 100644 --- a/Tests/StringHelperTest.php +++ b/Tests/StringHelperTest.php @@ -440,6 +440,14 @@ public static function seedCompliantStrings(): array ["\xFF ABC", false], ["0xfffd ABC", true], ['', true], + 'truncated 2-byte sequence at the end' => ["abc\xC3", false], + 'truncated 3-byte sequence at the end' => ["abc\xE2\x82", false], + 'truncated 4-byte sequence at the end' => ["abc\xF0\x9F\x98", false], + 'truncated 5-byte sequence at the end' => ["\xDF\xA3\xFB\x88", false], + 'overlong encoding' => ["\xC0\xAF", false], + 'surrogate' => ["\xED\xA0\x80", false], + 'code point above U+10FFFF' => ["\xF4\x90\x80\x80", false], + 'complete 4-byte sequence' => ["\xF0\x9F\x98\x80", true], ]; } diff --git a/docs/overview.md b/docs/overview.md index 5c220497..c6fa8b38 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -1,3 +1,10 @@ # String package TODO + +## `StringHelper::valid()` + +`StringHelper::valid($str)` returns whether `$str` is valid UTF-8, using `mb_check_encoding($str, 'UTF-8')`. +Overlong forms, UTF-16 surrogates, code points above U+10FFFF, five- and six-byte sequences and +strings that end in an incomplete multi-byte sequence are rejected. Earlier releases reported a string +that ends in an incomplete sequence, such as `"abc\xC3"`, as valid. diff --git a/src/StringHelper.php b/src/StringHelper.php index 98f73e7d..7af0b59d 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -749,132 +749,18 @@ public static function transcode($source, $fromEncoding, $toEncoding) /** * Tests a string as to whether it's valid UTF-8 and supported by the Unicode standard. * - * Note: this function has been modified to simple return true or false. + * Overlong forms, surrogates, code points above U+10FFFF and incomplete multi-byte sequences are rejected. * * @param string $str UTF-8 encoded string. * * @return boolean true if valid * - * @author - * @link https://hsivonen.fi/php-utf8/ * @see compliant * @since 1.3.0 */ public static function valid($str) { - $mState = 0; // cached expected number of octets after the current octet - // until the beginning of the next UTF8 character sequence - $mUcs4 = 0; // cached Unicode character - $mBytes = 1; // cached expected number of octets in the current sequence - - $len = strlen($str); - - for ($i = 0; $i < $len; $i++) { - /* - * Joomla modification - As of PHP 7.4, curly brace access has been deprecated. As a result the line below has - * been modified to use square brace syntax - * See https://github.com/php/php-src/commit/d574df63dc375f5fc9202ce5afde23f866b6450a - * for additional references - */ - $in = ord($str[$i]); - - if ($mState == 0) { - // When mState is zero we expect either a US-ASCII character or a - // multi-octet sequence. - if (0 == (0x80 & ($in))) { - // US-ASCII, pass straight through. - $mBytes = 1; - } elseif (0xC0 == (0xE0 & ($in))) { - // First octet of 2 octet sequence - $mUcs4 = ($in); - $mUcs4 = ($mUcs4 & 0x1F) << 6; - $mState = 1; - $mBytes = 2; - } elseif (0xE0 == (0xF0 & ($in))) { - // First octet of 3 octet sequence - $mUcs4 = ($in); - $mUcs4 = ($mUcs4 & 0x0F) << 12; - $mState = 2; - $mBytes = 3; - } elseif (0xF0 == (0xF8 & ($in))) { - // First octet of 4 octet sequence - $mUcs4 = ($in); - $mUcs4 = ($mUcs4 & 0x07) << 18; - $mState = 3; - $mBytes = 4; - } elseif (0xF8 == (0xFC & ($in))) { - /* First octet of 5 octet sequence. - * - * This is illegal because the encoded codepoint must be either - * (a) not the shortest form or - * (b) outside the Unicode range of 0-0x10FFFF. - * Rather than trying to resynchronize, we will carry on until the end - * of the sequence and let the later error handling code catch it. - */ - $mUcs4 = ($in); - $mUcs4 = ($mUcs4 & 0x03) << 24; - $mState = 4; - $mBytes = 5; - } elseif (0xFC == (0xFE & ($in))) { - // First octet of 6 octet sequence, see comments for 5 octet sequence. - $mUcs4 = ($in); - $mUcs4 = ($mUcs4 & 1) << 30; - $mState = 5; - $mBytes = 6; - } else { - /* Current octet is neither in the US-ASCII range nor a legal first - * octet of a multi-octet sequence. - */ - return false; - } - } else { - // When mState is non-zero, we expect a continuation of the multi-octet - // sequence - if (0x80 == (0xC0 & ($in))) { - // Legal continuation. - $shift = ($mState - 1) * 6; - $tmp = $in; - $tmp = ($tmp & 0x0000003F) << $shift; - $mUcs4 |= $tmp; - - /** - * End of the multi-octet sequence. mUcs4 now contains the final - * Unicode codepoint to be output - */ - if (0 == --$mState) { - /* - * Check for illegal sequences and codepoints. - */ - // From Unicode 3.1, non-shortest form is illegal - if ( - ((2 == $mBytes) && ($mUcs4 < 0x0080)) || - ((3 == $mBytes) && ($mUcs4 < 0x0800)) || - ((4 == $mBytes) && ($mUcs4 < 0x10000)) || - (4 < $mBytes) || - // From Unicode 3.2, surrogate characters are illegal - (($mUcs4 & 0xFFFFF800) == 0xD800) || - // Codepoints outside the Unicode range are illegal - ($mUcs4 > 0x10FFFF) - ) { - return false; - } - - //initialize UTF8 cache - $mState = 0; - $mUcs4 = 0; - $mBytes = 1; - } - } else { - /** - *((0xC0 & (*in) != 0x80) && (mState != 0)) - * Incomplete multi-octet sequence. - */ - - return false; - } - } - } - return true; + return mb_check_encoding((string) $str, 'UTF-8'); } /**