Make WordPress Core

Changeset 63826


Ignore:
Timestamp:
09/22/2026 03:28:22 PM (2 months ago)
Author:
johnbillion
Message:

Themes: Restrict path traversal in locate_template().

Props jeremyfelt, marcs0h, vortfu, jorbin, fiocavallari, xknown, sirlouen, joemcgill, pypwalters, swissspidy, shailu25, oglekler, peterwilsoncc, mukesh27, audrasjb, wildworks, rajinsharwar, martinkrcho, jeffpaul, ressl.

Merges r63792 into the 5.1 branch.

Location:
branches/5.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.1

  • branches/5.1/src/wp-includes/template.php

    r44569 r63826  
    424424        if ( $pagename ) {
    425425                $pagename_decoded = urldecode( $pagename );
    426                 if ( $pagename_decoded !== $pagename ) {
     426                if ( $pagename_decoded !== $pagename && 0 === validate_file( $pagename_decoded ) ) {
    427427                        $templates[] = "page-{$pagename_decoded}.php";
    428428                }
    … …  
    620620
    621621/**
     622 * Determines whether a template found by locate_template() may be loaded.
     623 *
     624 * @since 7.1.2
     625 * @access private
     626 *
     627 * @param string $path Path to an existing template file.
     628 * @return bool Whether the template may be loaded.
     629 */
     630function _wp_is_template_path_allowed( $path ) {
     631        // A file path that exists and does not contain `..` is allowed.
     632        if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) {
     633                return true;
     634        }
     635
     636        // Resolve the true location of the requested file for later comparison.
     637        $real_path = realpath( $path );
     638
     639        if ( false === $real_path ) {
     640                return false;
     641        }
     642
     643        $real_path = trailingslashit( wp_normalize_path( $real_path ) );
     644
     645        $directories = array(
     646                STYLESHEETPATH,
     647                TEMPLATEPATH,
     648                ABSPATH . WPINC . '/theme-compat',
     649        );
     650
     651        // If a theme is in a subdirectory, accept templates from its direct parent directory.
     652        if ( false !== strpos( get_stylesheet(), '/' ) ) {
     653                $directories[] = dirname( STYLESHEETPATH );
     654        }
     655
     656        // If a parent theme is in a subdirectory, accept templates from its direct parent directory.
     657        if ( false !== strpos( get_template(), '/' ) ) {
     658                $directories[] = dirname( TEMPLATEPATH );
     659        }
     660
     661        foreach ( $directories as $directory ) {
     662                $real_directory = realpath( $directory );
     663
     664                if ( false === $real_directory ) {
     665                        continue;
     666                }
     667
     668                // The true location of the requested file must be inside one of the allowed directories.
     669                if ( str_starts_with( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) {
     670                        return true;
     671                }
     672        }
     673
     674        return false;
     675}
     676
     677/**
    622678 * Retrieve the name of the highest priority template file that exists.
    623679 *
    … …  
    626682 *
    627683 * @since 2.7.0
     684 * @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
    628685 *
    629686 * @param string|array $template_names Template file(s) to search for, in order.
    … …  
    639696                }
    640697                if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
    641                         $located = STYLESHEETPATH . '/' . $template_name;
    642                         break;
     698                        $candidate = STYLESHEETPATH . '/' . $template_name;
    643699                } elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
    644                         $located = TEMPLATEPATH . '/' . $template_name;
    645                         break;
     700                        $candidate = TEMPLATEPATH . '/' . $template_name;
    646701                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
    647                         $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     702                        $candidate = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     703                } else {
     704                        continue;
     705                }
     706
     707                if ( _wp_is_template_path_allowed( $candidate ) ) {
     708                        $located = $candidate;
    648709                        break;
    649710                }
Note: See TracChangeset for help on using the changeset viewer.