Skip to content

Commit 95aea18

Browse files
committed
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 6.4 branch. git-svn-id: https://develop.svn.wordpress.org/branches/6.4@63813 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b8bfa48 commit 95aea18

1 file changed

Lines changed: 70 additions & 6 deletions

File tree

‎src/wp-includes/template.php‎

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ function get_page_template() {
486486
}
487487
if ( $pagename ) {
488488
$pagename_decoded = urldecode( $pagename );
489-
if ( $pagename_decoded !== $pagename ) {
489+
if ( $pagename_decoded !== $pagename && 0 === validate_file( $pagename_decoded ) ) {
490490
$templates[] = "page-{$pagename_decoded}.php";
491491
}
492492
$templates[] = "page-{$pagename}.php";
@@ -681,6 +681,65 @@ function get_attachment_template() {
681681
return get_query_template( 'attachment', $templates );
682682
}
683683

684+
/**
685+
* Determines whether a template found by locate_template() may be loaded.
686+
*
687+
* @since 7.1.2
688+
* @access private
689+
*
690+
* @param string $path Path to an existing template file.
691+
* @return bool Whether the template may be loaded.
692+
*/
693+
function _wp_is_template_path_allowed( $path ) {
694+
// A file path that exists and does not contain `..` is allowed.
695+
if ( 0 === preg_match( '#(?:^|/)\.\.[. ]*(?:/|$)#', wp_normalize_path( $path ) ) ) {
696+
return true;
697+
}
698+
699+
// Resolve the true location of the requested file for later comparison.
700+
$real_path = realpath( $path );
701+
702+
if ( false === $real_path ) {
703+
return false;
704+
}
705+
706+
$real_path = trailingslashit( wp_normalize_path( $real_path ) );
707+
708+
$stylesheet_path = get_stylesheet_directory();
709+
$template_path = get_template_directory();
710+
711+
$directories = array(
712+
$stylesheet_path,
713+
$template_path,
714+
ABSPATH . WPINC . '/theme-compat',
715+
);
716+
717+
// If a theme is in a subdirectory, accept templates from its direct parent directory.
718+
if ( str_contains( get_stylesheet(), '/' ) ) {
719+
$directories[] = dirname( $stylesheet_path );
720+
}
721+
722+
// If a parent theme is in a subdirectory, accept templates from its direct parent directory.
723+
if ( str_contains( get_template(), '/' ) ) {
724+
$directories[] = dirname( $template_path );
725+
}
726+
727+
foreach ( $directories as $directory ) {
728+
$real_directory = realpath( $directory );
729+
730+
if ( false === $real_directory ) {
731+
continue;
732+
}
733+
734+
// The true location of the requested file must be inside one of the allowed directories.
735+
if ( str_starts_with( $real_path, trailingslashit( wp_normalize_path( $real_directory ) ) ) ) {
736+
return true;
737+
}
738+
}
739+
740+
return false;
741+
}
742+
684743
/**
685744
* Retrieves the name of the highest priority template file that exists.
686745
*
@@ -690,6 +749,7 @@ function get_attachment_template() {
690749
*
691750
* @since 2.7.0
692751
* @since 5.5.0 The `$args` parameter was added.
752+
* @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
693753
*
694754
* @param string|array $template_names Template file(s) to search for, in order.
695755
* @param bool $load If true the template file will be loaded if it is found.
@@ -710,13 +770,17 @@ function locate_template( $template_names, $load = false, $load_once = true, $ar
710770
continue;
711771
}
712772
if ( file_exists( $stylesheet_path . '/' . $template_name ) ) {
713-
$located = $stylesheet_path . '/' . $template_name;
714-
break;
773+
$candidate = $stylesheet_path . '/' . $template_name;
715774
} elseif ( $is_child_theme && file_exists( $template_path . '/' . $template_name ) ) {
716-
$located = $template_path . '/' . $template_name;
717-
break;
775+
$candidate = $template_path . '/' . $template_name;
718776
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
719-
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
777+
$candidate = ABSPATH . WPINC . '/theme-compat/' . $template_name;
778+
} else {
779+
continue;
780+
}
781+
782+
if ( _wp_is_template_path_allowed( $candidate ) ) {
783+
$located = $candidate;
720784
break;
721785
}
722786
}

0 commit comments

Comments
 (0)