Make WordPress Core

Opened 5 weeks ago

Closed 4 weeks ago

#66063 closed defect (bug) (fixed)

Check cache return value in WP_Textdomain_Registry::get_language_files_from_path()

Reported by: josephscott Owned by: jonsurrell
Priority: normal Milestone: 7.2
Component: I18N Version:
Severity: normal Keywords: has-patch commit
Cc: Focuses:

Description

In WP_Textdomain_Registry::get_language_files_from_path() there is a call to the cache to get the files ( ​GH ):

<?php
                $cache_key = md5( $path );
                $files     = wp_cache_get( $cache_key, 'translation_files' );

                if ( false === $files ) {

The checks on the return value in $files are insufficient, only looking to see if it is false. It needs to make sure that it gets an array back and that every entry in that array is a string.

A non-array value from wp_cache_get() will cause a fatal error in PHP. This is another case where WordPress core could use a validating cache function. I've already seen that happen.

I have a PR to address this.

Change History (5)

This ticket was mentioned in ​PR #13429 on ​WordPress/wordpress-develop by ​@josephscott.


5 weeks ago
#1

  • Keywords has-patch added

…s_from_path()

https://core.trac.wordpress.org/ticket/66063

I came across this because I ran into a PHP fatal error from the lack of checking the return value from the cache:

Uncaught TypeError: str_starts_with(): Argument #1 ($haystack) must be of type string, array given

Inside of get_path_from_lang_dir().

AI assistance: Yes
Tool(s): Claude
Model(s): Fable 5.1
Used for: Used for discussing the fatal error and narrowing down why it was happening. The code is my own.

#2 @jonsurrell
5 weeks ago

  • Component General → I18N
  • Milestone Awaiting Review → 7.2
  • Owner set to jonsurrell
  • Status new → accepted

#3 @jonsurrell
5 weeks ago

  • Keywords commit added

​@josephscott commented on ​PR #13429:


5 weeks ago
#4

Thank you. The tests looked fine, I made one small change, replacing assertContainsOnly() with assertIsString(), because assertContainsOnly() is going away in later versions of PHPUnit -

Deprecation: assertContainsOnly() and assertNotContainsOnly() are deprecated

As of PHPUnit 11.5, the assertContainsOnly() and assertNotContainsOnly() methods are hard-deprecated. Using these methods will trigger a deprecation warning. The methods will be removed in PHPUnit 13.

From ​https://docs.phpunit.de/en/12.5/assertions.html

So switching now makes for one less thing to update in the future.

If you are good with that, then I agree, this change is ready.

#5 @jonsurrell
4 weeks ago

  • Resolution → fixed
  • Status accepted → closed

In 63580:

I18N: Validate cached translation file lists.

Prevent possible fatal errors on bad cached values by validating the caches is an array of strings.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13429

Props josephscott, jonsurrell.
Fixes #66063.

Note: See TracTickets for help on using tickets.