Make WordPress Core

Changeset 63823


Ignore:
Timestamp:
09/22/2026 03:27:38 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.4 branch.

Location:
branches/5.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.4

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

    r47198 r63823  
    443443        if ( $pagename ) {
    444444                $pagename_decoded = urldecode( $pagename );
    445                 if ( $pagename_decoded !== $pagename ) {
     445                if ( $pagename_decoded !== $pagename && 0 === validate_file( $pagename_decoded ) ) {
    446446                        $templates[] = "page-{$pagename_decoded}.php";
    447447                }
    … …  
    639639
    640640/**
     641 * Determines whether a template found by locate_template() may be loaded.
     642 *
     643 * @since 7.1.2
     644 * @access private
     645 *
     646 * @param string $path Path to an existing template file.
     647 * @return bool Whether the template may be loaded.
     648 */
     649function _wp_is_template_path_allowed( $path ) {
     650        // A file path that exists and does not contain `..` is allowed.
     651        if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) {
     652                return true;
     653        }
     654
     655        // Resolve the true location of the requested file for later comparison.
     656        $real_path = realpath( $path );
     657
     658        if ( false === $real_path ) {
     659                return false;
     660        }
     661
     662        $real_path = trailingslashit( wp_normalize_path( $real_path ) );
     663
     664        $directories = array(
     665                STYLESHEETPATH,
     666                TEMPLATEPATH,
     667                ABSPATH . WPINC . '/theme-compat',
     668        );
     669
     670        // If a theme is in a subdirectory, accept templates from its direct parent directory.
     671        if ( false !== strpos( get_stylesheet(), '/' ) ) {
     672                $directories[] = dirname( STYLESHEETPATH );
     673        }
     674
     675        // If a parent theme is in a subdirectory, accept templates from its direct parent directory.
     676        if ( false !== strpos( get_template(), '/' ) ) {
     677                $directories[] = dirname( TEMPLATEPATH );
     678        }
     679
     680        foreach ( $directories as $directory ) {
     681                $real_directory = realpath( $directory );
     682
     683                if ( false === $real_directory ) {
     684                        continue;
     685                }
     686
     687                // The true location of the requested file must be inside one of the allowed directories.
     688                if ( str_starts_with( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) {
     689                        return true;
     690                }
     691        }
     692
     693        return false;
     694}
     695
     696/**
    641697 * Retrieve the name of the highest priority template file that exists.
    642698 *
    … …  
    645701 *
    646702 * @since 2.7.0
     703 * @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
    647704 *
    648705 * @param string|array $template_names Template file(s) to search for, in order.
    … …  
    658715                }
    659716                if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
    660                         $located = STYLESHEETPATH . '/' . $template_name;
    661                         break;
     717                        $candidate = STYLESHEETPATH . '/' . $template_name;
    662718                } elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
    663                         $located = TEMPLATEPATH . '/' . $template_name;
    664                         break;
     719                        $candidate = TEMPLATEPATH . '/' . $template_name;
    665720                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
    666                         $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     721                        $candidate = ABSPATH . WPINC . '/theme-compat/' . $template_name;
     722                } else {
     723                        continue;
     724                }
     725
     726                if ( _wp_is_template_path_allowed( $candidate ) ) {
     727                        $located = $candidate;
    667728                        break;
    668729                }
Note: See TracChangeset for help on using the changeset viewer.