Make WordPress Core


Ignore:
Timestamp:
10/24/2017 11:14:33 PM (8 years ago)
Author:
johnbillion
Message:

Filesystem API: Add more specificity to the rules for valid files in validate_file().

This now treats files containing ./ as valid, and also treats files containing a trailing ../ as valid due to widespread use of this pattern in theme and plugin zip files.

Adds tests.

Props Ipstenu, borgesbruno, DavidAnderson, philipjohn, birgire
Fixes #42016, #36170

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r42007 r42011  
    42534253 * @return int 0 means nothing is wrong, greater than 0 means something was wrong.
    42544254 */
    4255 function validate_file( $file, $allowed_files = '' ) {
    4256     if ( false !== strpos( $file, '..' ) )
     4255function validate_file( $file, $allowed_files = array() ) {
     4256    // `../` on its own is not allowed:
     4257    if ( '../' === $file ) {
    42574258        return 1;
    4258 
    4259     if ( false !== strpos( $file, './' ) )
     4259    }
     4260
     4261    // More than one occurence of `../` is not allowed:
     4262    if ( preg_match_all( '#\.\./#', $file, $matches, PREG_SET_ORDER ) && ( count( $matches ) > 1 ) ) {
    42604263        return 1;
    4261 
     4264    }
     4265
     4266    // `../` which does not occur at the end of the path is not allowed:
     4267    if ( false !== strpos( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) {
     4268        return 1;
     4269    }
     4270
     4271    // Files not in the allowed file list are not allowed:
    42624272    if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) )
    42634273        return 3;
    42644274
     4275    // Absolute Windows drive paths are not allowed:
    42654276    if (':' == substr( $file, 1, 1 ) )
    42664277        return 2;
Note: See TracChangeset for help on using the changeset viewer.