Skip to content

Validate the H5P title as Unicode characters, not bytes#302

Open
thisismyurl wants to merge 2 commits into
h5p:masterfrom
thisismyurl:fix/276-unicode-title-validation
Open

Validate the H5P title as Unicode characters, not bytes#302
thisismyurl wants to merge 2 commits into
h5p:masterfrom
thisismyurl:fix/276-unicode-title-validation

Conversation

@thisismyurl

Copy link
Copy Markdown

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 the u modifier 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 $h5pRequired that the report points at, and the library title in $libraryRequired a few lines down, which carries the identical byte-counting pattern. One thing worth calling out: with /u, preg_match also 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:

$title = str_repeat("\xC3\xA9", 200); // 200 e-acute characters = 400 bytes
var_dump(preg_match('/^.{1,255}$/',  $title)); // 0 - rejected on byte count
var_dump(preg_match('/^.{1,255}$/u', $title)); // 1 - accepted on character count

Closes #276

(full disclosure: AI helped me identify the issue and verify my work)

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.
Copilot AI review requested due to automatic review settings July 7, 2026 14:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 u modifier to the title validation regex in the h5p.json schema ($h5pRequired).
  • Add the PCRE u modifier to the title validation regex in the library.json schema ($libraryRequired).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread h5p.classes.php
// 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.
@thisismyurl

Copy link
Copy Markdown
Author

Good catch — addressed in the follow-up commit (7672ced).

Changed === 0 to !== 1 in isValidRequirement(), consistent with the filter_xss() pattern already in this file at line 4911. This ensures that both a non-match (0) and a regex error on invalid UTF-8 (false) are treated as invalid — so a title with malformed byte sequences will now correctly fail validation rather than passing silently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

Support Unicode expression modifier when validating H5P title attribute

2 participants