Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#66217 new enhancement

Further hardening of MO file parsing

Reported by: Presskopp Owned by:
Priority: normal Milestone: Awaiting Review
Component: I18N Version:
Severity: minor Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Description

WP_Translation_File_MO::parse_file() already performs some validation when parsing .mo files. See #61794, which added hardening for invalid metadata lines.

There are still other parts of the MO file structure that are not fully validated before being used. In particular, offsets and lengths from the MO header and translation tables are used to calculate table boundaries and read strings without consistently verifying that they describe valid ranges within the file.

Malformed or truncated MO files could therefore result in unexpected parsing behaviour.

Expected behavior

The MO parser should reject structurally invalid files cleanly with an Invalid data error, while continuing to accept valid MO files.

This would extend the existing hardening introduced in #61794 to the remaining structural data of the MO file.

Change History (1)

This ticket was mentioned in ​PR #13850 on ​WordPress/wordpress-develop by ​AbdiTolesa.


7 weeks ago
#1

  • Keywords has-patch has-unit-tests added

Fixes: https://core.trac.wordpress.org/ticket/66217

Summary

WP_Translation_File_MO::parse_file() already validates individual metadata lines (see #61794), but the offsets and lengths taken from the MO header (originals_addr, translations_addr, hash_addr) were not consistently validated before being used to calculate table boundaries and read strings.

Specifically:

  • hash_addr was never checked against the file length (only originals_addr and translations_addr were).
  • There was no check that originals_addr <= translations_addr <= hash_addr, so a malformed header could make the computed table lengths (translations_addr - originals_addr, hash_addr - translations_addr) negative. A negative length passed to substr() doesn't error, but silently slices the wrong region of the file instead of the intended table.
  • Individual originals/translations table entries' pos/length pairs were used directly in substr() without checking they described a range within the file.

A malformed or truncated .mo file could therefore be parsed without error while producing unexpected/garbage data, instead of being cleanly rejected.

Changes

  • In class-wp-translation-file-mo.php, added a bounds/ordering check on the header addresses (originals_addr <= translations_addr <= hash_addr <= file_length), returning the existing 'Invalid data' error, consistent with the neighboring checks.
  • Skip individual originals/translations table entries whose pos/length describe a range outside the file, rather than reading whatever happens to be there.
  • Added test cases to data_invalid_files in wpTranslationsConvert.php covering: originals_addr after translations_addr, translations_addr after hash_addr, and an out-of-bounds hash_addr.

Test plan

  • [x] php -l on both changed files.
  • [x] Reproduced the issue against the pre-fix code with a standalone script constructing malformed headers matching the new test cases — confirmed they parsed with error() === null (i.e. silently treated as valid) before this fix, and now correctly return 'Invalid data'.
  • [x] Verified all existing .mo fixtures under tests/phpunit/data/l10n/ still parse with no errors and unchanged header counts.
  • [ ] Could not run the full PHPUnit suite in this environment (no database available) — recommend running phpunit --filter test_invalid_files tests/phpunit/tests/l10n/wpTranslationsConvert.php before merging.
Note: See TracTickets for help on using tickets.