Opened 3 months ago
Closed 4 days ago
#9338 closed defect (bug) (fixed)
14.5.0: sanitize_utf8_filename() can call transliterator_transliterate() when ext-intl is unavailable
| Reported by: | lverane | Owned by: | espellcaste |
|---|---|---|---|
| Priority: | normal | Milestone: | 14.5.5 |
| Component: | Core | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | lverane |
Description (last modified by )
Reproduction:
PHP 8.2
intl extension disabled
WordPress 7.0.2
BuddyPress 14.5.0
Upload avatar with UTF-8 filename
Cause:
In sanitize_utf8_filename() both instances of function_exists( 'wp_is_valid_utf8' ) ? wp_is_valid_utf8( $retval ) : seems_utf8( $retval ) should be wrapped in parentheses.
File: /wp-content/plugins/buddypress/bp-core/classes/class-bp-attachment.php
Lines 297 - 322
Attachments (1)
Change History (10)
#2
in reply to: ↑ 1
@
3 months ago
- Description modified (diff)
Replying to espellcaste:
@lverane I don't get it. What's exactly the problem? Is there an error you can share?
In sanitize_utf8_filename() both instances of function_exists( 'wp_is_valid_utf8' ) ? wp_is_valid_utf8( $retval ) : seems_utf8( $retval ) should be wrapped in parentheses.
What does that mean exactly?
I added the file path to the ticket description. The issue is that the current if statement
if (
function_exists( 'transliterator_transliterate' )
// wp_is_valid_utf8 is added in WP 6.9.
&& function_exists( 'wp_is_valid_utf8' ) ? wp_is_valid_utf8( $retval ) : seems_utf8( $retval )
) {
Will resolve as
(function_exists( 'transliterator_transliterate' ) && function_exists( 'wp_is_valid_utf8' )) ? wp_is_valid_utf8( $retval ) : seems_utf8( $retval )
Meaning if function_exists( 'transliterator_transliterate' ) is false and seems_utf8( $retval ) is true transliterator_transliterate() is still called. The fix is to add parentheses around the second evaluation:
function_exists( 'transliterator_transliterate' ) && (function_exists( 'wp_is_valid_utf8' ) ? wp_is_valid_utf8( $retval ) : seems_utf8( $retval ))
Same issue/solution for function_exists( 'iconv' ) on the else statement.
#6
@
2 weeks ago
9338.01.patch wraps both ternaries in parentheses, so transliterator_transliterate() and iconv() are only called when they exist. It also adds test_bp_attachment_sanitize_utf8_filename.
Without intl the fatal error is not limited to UTF-8 filenames. ASCII is valid UTF-8, so wp_is_valid_utf8() (or seems_utf8() before WP 6.9) returns true for any name, and BP_Attachment::upload() adds this method to sanitize_file_name around wp_handle_upload(). So on a server without intl, every avatar and cover image upload hits it, avatar.jpg included.
Results with BuddyPress trunk (73c9fc5), WordPress 7.2-beta1, PHP 8.3.32. For the runs without intl, PHP was started with php -n and every extension of the environment except intl. The first four runs use --filter test_bp_attachment.
| Run | Result |
| Trunk, without intl | 1 error: Call to undefined function transliterator_transliterate(), already for mystery-man.jpg
|
| Patch, without intl | OK (20 tests, 40 assertions) |
| Trunk, with intl | OK (20 tests, 40 assertions) |
| Patch, with intl | OK (20 tests, 40 assertions) |
| Patch, with intl, full suite | OK (2574 tests, 7629 assertions, 87 skipped) |
With intl nothing changes: when transliterator_transliterate() exists, both forms check the same function. The new test can only catch the bug on a server without intl, as the existing upload tests fake the upload before the file name is sanitized.
The 14.0 branch has the same two conditions without the phpcs:ignore comments, so the patch needs a small adjustment there.
Thanks @lverane for the report and the analysis.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
@lverane I don't get it. What's exactly the problem? Is there an error you can share?
What does that mean exactly?