Skip to content

[4.x] Reject truncated multi-byte sequences in StringHelper::valid() - #75

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-valid-truncated
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-valid-truncated

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

StringHelper::valid() is a hand-written UTF-8 state machine. It returned true at the end of the string without checking that the last multi-byte sequence was complete, so a string that ends in a truncated sequence passed:

StringHelper::valid("abc\xC3");          // true, but compliant() and mb_check_encoding() say false
StringHelper::valid("\xDF\xA3\xFB\x88"); // true

A fuzz run over 200,000 random 4-byte high-bit strings found 6,824 disagreements with mb_check_encoding(). Code that uses valid() as a validation gate accepted malformed UTF-8.

valid() now returns mb_check_encoding((string) $str, 'UTF-8'). That function rejects everything the state machine rejected (overlong forms, surrogates, code points above U+10FFFF, five- and six-byte sequences) and incomplete sequences as well. symfony/polyfill-mbstring is already a hard requirement. The same fuzz run finds no disagreements after the change.

Testing Instructions

seedCompliantStrings, which feeds both testValid and testCompliant, gets new cases: truncated 2-, 3-, 4- and 5-byte sequences at the end, an overlong encoding, a surrogate, a code point above U+10FFFF and a complete 4-byte sequence. The four truncated cases fail for valid() before the change and pass after it. compliant() already passed all of them. vendor/bin/phpunit is green, phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on 4.x-dev as well (mb_trim and the other PHP 8.4 polyfill functions are not found), and #66 fixes that.

Documentation Changes Required

docs/overview.md (still "TODO" on 4.x-dev) gets a StringHelper::valid() section that says what is rejected.

The owner's docs PR #72 rewrites docs/overview.md. Whichever of the two is merged second has to fold this section into #72's structure and adjust the valid() paragraph under "Notes on individual methods", which calls it a hand-written state machine. #73 also appends a section to the docs/overview.md stub, so the second of #73 and this PR to be merged gets a small conflict there.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant