Validate the H5P title as Unicode characters, not bytes#302
Validate the H5P title as Unicode characters, not bytes#302thisismyurl wants to merge 2 commits into
Conversation
The title validation pattern /^.{1,255}$/ counts bytes, so a title made of
multibyte (non-Latin) characters can exceed 255 bytes while being well under
255 characters and is wrongly rejected. Add the u modifier to both title
patterns (content title in h5pRequired and library title in libraryRequired)
so the 255 limit counts Unicode characters. Confirmed against the linked
Moodle report MDL-87358.
There was a problem hiding this comment.
Pull request overview
This PR updates H5P package and library metadata validation so the title length constraint is enforced in Unicode characters (not bytes), preventing multibyte titles (e.g., Japanese/Arabic) from being incorrectly rejected under the 255 limit.
Changes:
- Add the PCRE
umodifier to thetitlevalidation regex in the h5p.json schema ($h5pRequired). - Add the PCRE
umodifier to thetitlevalidation regex in the library.json schema ($libraryRequired).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Schemas used to validate the h5p files | ||
| private $h5pRequired = array( | ||
| 'title' => '/^.{1,255}$/', | ||
| 'title' => '/^.{1,255}$/u', |
With the /u modifier added in the previous commit, preg_match() can return false when the title contains invalid UTF-8 bytes. The prior check `=== 0` treats false as valid (only 0 triggers the error path). Change to `!== 1` so that both 0 (no match) and false (regex error / invalid UTF-8) are treated as invalid, matching the pattern already used in the codebase at h5p.classes.php:4911 (filter_xss()). Addresses the Copilot review on PR h5p#302.
|
Good catch — addressed in the follow-up commit (7672ced). Changed |
Hey folks,
A title made of non-Latin characters can be rejected even when it's well under the length limit. The validation pattern
/^.{1,255}$/counts bytes, not characters, so a multibyte title (Japanese or Arabic, say) hits the 255 cap at around 85 characters. Adding theumodifier makes the quantifier count Unicode characters, which is what the 255 limit is meant to mean. This matches the linked Moodle report (MDL-87358).I applied it to both title patterns: the content title in
$h5pRequiredthat the report points at, and the library title in$libraryRequireda few lines down, which carries the identical byte-counting pattern. One thing worth calling out: with/u,preg_matchalso rejects a title containing an invalid UTF-8 byte sequence, which the byte pattern silently accepted before. For JSON-sourced metadata that's the safer behaviour, but it is a change in behaviour.There's no unit-test harness in the library, so here is the standalone check I used, which anyone can run in under a minute:
Closes #276
(full disclosure: AI helped me identify the issue and verify my work)