Make WordPress Core

Changeset 63830


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

Location:
branches/4.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

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

    r38951 r63830  
    416416        if ( $pagename ) {
    417417                $pagename_decoded = urldecode( $pagename );
    418                 if ( $pagename_decoded !== $pagename ) {
     418                if ( $pagename_decoded !== $pagename && 0 === validate_file( $pagename_decoded ) ) {
    419419                        $templates[] = "page-{$pagename_decoded}.php";
    420420                }
    … …  
    611611
    612612/**
     613 * Determines whether a template found by locate_template() may be loaded.
     614 *
     615 * @since 7.1.2
     616 * @access private
     617 *
     618 * @param string $path Path to an existing template file.
     619 * @return bool Whether the template may be loaded.
     620 */
     621function _wp_is_template_path_allowed( $path ) {
     622        // A file path that exists and does not contain `..` is allowed.
     623        if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) {
     624                return true;
     625        }
     626
     627        // Resolve the true location of the requested file for later comparison.
     628        $real_path = realpath( $path );
     629
     630        if ( false === $real_path ) {
     631                return false;
     632        }
     633
     634        $real_path = trailingslashit( wp_normalize_path( $real_path ) );
     635
     636        $directories = array(
     637                STYLESHEETPATH,
     638                TEMPLATEPATH,
     639                ABSPATH . WPINC . '/theme-compat',
     640        );
     641
     642        // If a theme is in a subdirectory, accept templates from its direct parent directory.
     643        if ( false !== strpos( get_stylesheet(), '/' ) ) {
     644                $directories[] = dirname( STYLESHEETPATH );
     645        }
     646
     647        // If a parent theme is in a subdirectory, accept templates from its direct parent directory.
     648        if ( false !== strpos( get_template(), '/' ) ) {
     649                $directories[] = dirname( TEMPLATEPATH );
     650        }
     651
     652        foreach ( $directories as $directory ) {
     653                $real_directory = realpath( $directory );
     654
     655                if ( false === $real_directory ) {
     656                        continue;
     657                }
     658
     659                // The true location of the requested file must be inside one of the allowed directories.
     660                if ( 0 === strpos( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) {
     661                        return true;
     662                }
     663        }
     664
     665        return false;
     666}
     667
     668/**
    613669 * Retrieve the name of the highest priority template file that exists.
    614670 *
    … …  
    617673 *
    618674 * @since 2.7.0
     675 * @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
    619676 *
    620677 * @param string|array $template_names Template file(s) to search for, in order.
    … …  
    629686                        continue;
    630687                if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
    631                         $located = STYLESHEETPATH . '/' . $template_name;
    632                         break;
     688                        $candidate = STYLESHEETPATH . '/' . $template_name;
    633689                } elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
    634                         $located = TEMPLATEPATH . '/' . $template_name;
    635                         break;
     690                        $candidate = TEMPLATEPATH . '/' . $template_name;
    636691                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
    637                         $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     692                        $candidate = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     693                } else {
     694                        continue;
     695                }
     696
     697                if ( _wp_is_template_path_allowed( $candidate ) ) {
     698                        $located = $candidate;
    638699                        break;
    639700                }
Note: See TracChangeset for help on using the changeset viewer.