Make WordPress Core


Ignore:
Timestamp:
02/03/2023 12:46:18 PM (20 months ago)
Author:
audrasjb
Message:

Media: Replace consecutive periods in sanitize_file_name().

On some servers, consecutive periods in a filename can cause a 403 Forbidden response.
This changeset replaces consecutive periods with a single period, and adds related unit tests.

Props ArtZ91, costdev, SergeyBiryukov, arthurshlain, mukesh27.
Fixes #57242.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/formatting/sanitizeFileName.php

    r53562 r55209  
    9696        );
    9797    }
     98
     99    /**
     100     * Tests that sanitize_file_name() replaces consecutive periods
     101     * with a single period.
     102     *
     103     * @ticket 57242
     104     *
     105     * @dataProvider data_sanitize_file_name_should_replace_consecutive_periods_with_a_single_period
     106     *
     107     * @param string $filename A filename with consecutive periods.
     108     * @param string $expected The expected filename after sanitization.
     109     */
     110    public function test_sanitize_file_name_should_replace_consecutive_periods_with_a_single_period( $filename, $expected ) {
     111        $this->assertSame( $expected, sanitize_file_name( $filename ) );
     112    }
     113
     114    /**
     115     * Data provider for test_sanitize_file_name_should_replace_consecutive_periods_with_a_single_period().
     116     *
     117     * @return array[]
     118     */
     119    public function data_sanitize_file_name_should_replace_consecutive_periods_with_a_single_period() {
     120        return array(
     121            'consecutive periods at the start'         => array(
     122                'filename' => '...filename.png',
     123                'expected' => 'filename.png',
     124            ),
     125            'consecutive periods in the middle'        => array(
     126                'filename' => 'file.......name.png',
     127                'expected' => 'file.name_.png',
     128            ),
     129            'consecutive periods before the extension' => array(
     130                'filename' => 'filename....png',
     131                'expected' => 'filename.png',
     132            ),
     133            'consecutive periods after the extension'  => array(
     134                'filename' => 'filename.png...',
     135                'expected' => 'filename.png',
     136            ),
     137            'consecutive periods at the start, middle, before, after the extension' => array(
     138                'filename' => '.....file....name...png......',
     139                'expected' => 'file.name_.png',
     140            ),
     141            'consecutive periods and no extension'     => array(
     142                'filename' => 'filename...',
     143                'expected' => 'filename',
     144            ),
     145        );
     146    }
    98147}
Note: See TracChangeset for help on using the changeset viewer.