Skip to:
Content

BuddyPress.org

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 lverane)

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)

9338.01.patch​ (2.1 KB ) - added by softglaze 2 weeks ago.
Wrap both ternaries in parentheses, with a unit test.

Download all attachments as: .zip

Change History (10)

#1 follow-up: @espellcaste
3 months ago

@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?

#2 in reply to: ↑ 1 @lverane
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.

Last edited 3 months ago by lverane (previous) (diff)

#3 @lverane
3 months ago

  • Description modified (diff)

#4 @espellcaste
5 weeks ago

  • Milestone Awaiting Review → 14.5.3

#5 @espellcaste
2 weeks ago

  • Milestone 14.5.3 → 14.5.4

Milestone renamed

@softglaze
2 weeks ago

Wrap both ternaries in parentheses, with a unit test.

#6 @softglaze
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.

#7 @espellcaste
13 days ago

  • Milestone 14.5.4 → 14.5.5

Milestone renamed

#8 @espellcaste
4 days ago

In 14386:

Core: Prevent filename sanitization from calling unavailable conversion functions.

Check UTF-8 validity only when the conversion function exists, and preserve the filename if conversion fails.

Props lverane and softglaze.
See #9338 (14.0).

#9 @espellcaste
4 days ago

  • Owner set to espellcaste
  • Resolution → fixed
  • Status new → closed

In 14387:

Core: Prevent filename sanitization from calling unavailable conversion functions.

Check UTF-8 validity only when the conversion function exists, and preserve the filename if conversion fails.

Props lverane and softglaze.
Fixes #9338 (trunk).

Note: See TracTickets for help on using tickets.