Make WordPress Core

Changeset 63829


Ignore:
Timestamp:
09/22/2026 03:29:08 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 4.8 branch.

Location:
branches/4.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

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

    r40057 r63829  
    420420        if ( $pagename ) {
    421421                $pagename_decoded = urldecode( $pagename );
    422                 if ( $pagename_decoded !== $pagename ) {
     422                if ( $pagename_decoded !== $pagename && 0 === validate_file( $pagename_decoded ) ) {
    423423                        $templates[] = "page-{$pagename_decoded}.php";
    424424                }
    … …  
    615615
    616616/**
     617 * Determines whether a template found by locate_template() may be loaded.
     618 *
     619 * @since 7.1.2
     620 * @access private
     621 *
     622 * @param string $path Path to an existing template file.
     623 * @return bool Whether the template may be loaded.
     624 */
     625function _wp_is_template_path_allowed( $path ) {
     626        // A file path that exists and does not contain `..` is allowed.
     627        if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) {
     628                return true;
     629        }
     630
     631        // Resolve the true location of the requested file for later comparison.
     632        $real_path = realpath( $path );
     633
     634        if ( false === $real_path ) {
     635                return false;
     636        }
     637
     638        $real_path = trailingslashit( wp_normalize_path( $real_path ) );
     639
     640        $directories = array(
     641                STYLESHEETPATH,
     642                TEMPLATEPATH,
     643                ABSPATH . WPINC . '/theme-compat',
     644        );
     645
     646        // If a theme is in a subdirectory, accept templates from its direct parent directory.
     647        if ( false !== strpos( get_stylesheet(), '/' ) ) {
     648                $directories[] = dirname( STYLESHEETPATH );
     649        }
     650
     651        // If a parent theme is in a subdirectory, accept templates from its direct parent directory.
     652        if ( false !== strpos( get_template(), '/' ) ) {
     653                $directories[] = dirname( TEMPLATEPATH );
     654        }
     655
     656        foreach ( $directories as $directory ) {
     657                $real_directory = realpath( $directory );
     658
     659                if ( false === $real_directory ) {
     660                        continue;
     661                }
     662
     663                // The true location of the requested file must be inside one of the allowed directories.
     664                if ( 0 === strpos( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) {
     665                        return true;
     666                }
     667        }
     668
     669        return false;
     670}
     671
     672/**
    617673 * Retrieve the name of the highest priority template file that exists.
    618674 *
    … …  
    621677 *
    622678 * @since 2.7.0
     679 * @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
    623680 *
    624681 * @param string|array $template_names Template file(s) to search for, in order.
    … …  
    633690                        continue;
    634691                if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
    635                         $located = STYLESHEETPATH . '/' . $template_name;
    636                         break;
     692                        $candidate = STYLESHEETPATH . '/' . $template_name;
    637693                } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
    638                         $located = TEMPLATEPATH . '/' . $template_name;
    639                         break;
     694                        $candidate = TEMPLATEPATH . '/' . $template_name;
    640695                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
    641                         $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     696                        $candidate = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     697                } else {
     698                        continue;
     699                }
     700
     701                if ( _wp_is_template_path_allowed( $candidate ) ) {
     702                        $located = $candidate;
    642703                        break;
    643704                }
Note: See TracChangeset for help on using the changeset viewer.