Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 6 weeks ago

#61794 closed defect (bug) (fixed)

Bugfix in class-wp-translation-file-mo for "PHP Warning: Undefined array key 1 in"

Reported by: pander Owned by: swissspidy
Priority: low Milestone: 6.7
Component: I18N Version: 6.5
Severity: minor Keywords: has-patch commit has-unit-tests
Cc: Focuses:

Description

The warning "PHP Warning: Undefined array key 1 in" is flooding the error log file of my web service running WordPress.

I think the plugin that is the cause of this warning for me is ​https://github.com/qtranslate/qtranslate-xt/ or ​https://icalevents.com/ but I have seen it being reported for many different plugins.

Line 161 of wp-includes/l10n/class-wp-translation-file-mo.php is triggering this, is

<?php
list( $name, $value ) = array_map( 'trim', explode( ':', $meta_line, 2 ) );

To prevent the warning, I have changed line 157 from

<?php
if ( '' === $meta_line ) {

to

<?php
if ( '' === $meta_line || strpos($meta_line, ':') === false ) {

I know the warning is valid and should be fixed elsewhere, but my knowledge of WordPress is limited and this fix prevents that performance is impeded. Perhaps the warning should be caught in a different way, but this fixed it for now for me.

If there are way to pinpoint better what is causing this, please let me know. I can help to locate it but need some guidance.

Change History (12)

#1 @swissspidy
2 years ago

  • Component General → I18N
  • Focuses performance removed
  • Owner set to swissspidy
  • Status new → assigned

Removing performance focus as this is not related to performance.

#2 @swissspidy
2 years ago

The fix sounds reasonable to me, it adds some hardening.

If there are way to pinpoint better what is causing this, please let me know. I can help to locate it but need some guidance.

One of your plugins has an invalid MO file. You can try a `var_dump( $this->file ) on line 160 to find the culprit.

#3 @swissspidy
2 years ago

  • Keywords has-patch added
  • Milestone Awaiting Review → 6.7
  • Priority normal → low
  • Version 6.6.1 → 6.5

​@mukesh27 commented on ​PR #7116:


2 years ago
#6

@swissspidy Already opened #7115

#7 @swissspidy
2 years ago

  • Keywords commit added

#8 @swissspidy
2 years ago

  • Resolution → fixed
  • Status assigned → closed

In 58922:

I18N: Add hardening for invalid meta lines in MO files.

Props swissspidy, pander.
Fixes #61794.

#10 @pander
2 years ago

Thanks. The culprit is

string(97) "/var/www/DOMAINNAME/wp-content/plugins/amr-ical-events-list/lang/amr-ical-events-list-nl_NL.mo"

and that was repeated 1142 times.

I regenerated the MO file with

cd /var/www/DOMAINNAME/wp-content/plugins/amr-ical-events-list/lang
mv amr-ical-events-list-nl_NL.mo /tmp
msgfmt amr-ical-events-list-nl_NL.po -o amr-ical-events-list-nl_NL.mo

and this fixed it.

#11 @swissspidy
2 years ago

#62097 was marked as a duplicate.

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


6 weeks ago
#12

  • Keywords has-unit-tests added

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.

🤖 Generated with ​Claude Code

Note: See TracTickets for help on using tickets.