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_addrwas never checked against the file length (onlyoriginals_addrandtranslations_addrwere).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 tosubstr()doesn't error, but silently slices the wrong region of the file instead of the intended table.pos/lengthpairs were used directly insubstr()without checking they described a range within the file.A malformed or truncated
.mofile could therefore be parsed without error while producing unexpected/garbage data, instead of being cleanly rejected.Changes
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.pos/lengthdescribe a range outside the file, rather than reading whatever happens to be there.data_invalid_filesinwpTranslationsConvert.phpcovering:originals_addraftertranslations_addr,translations_addrafterhash_addr, and an out-of-boundshash_addr.Test plan
php -lon both changed files.error() === null(i.e. silently treated as valid) before this fix, and now correctly return'Invalid data'..mofixtures undertests/phpunit/data/l10n/still parse with no errors and unchanged header counts.phpunit --filter test_invalid_files tests/phpunit/tests/l10n/wpTranslationsConvert.phpbefore merging.