Skip to content

Commit 4a0c24b

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 4.7 branch. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@63830 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d95d59e commit 4a0c24b

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

‎src/wp-includes/template.php‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ function get_page_template() {
415415
$templates[] = $template;
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
}
421421
$templates[] = "page-$pagename.php";
@@ -609,13 +609,70 @@ function get_attachment_template() {
609609
return get_query_template( 'attachment', $templates );
610610
}
611611

612+
/**
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+
*/
621+
function _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+
612668
/**
613669
* Retrieve the name of the highest priority template file that exists.
614670
*
615671
* Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
616672
* so that themes which inherit from a parent theme can just overload one file.
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.
621678
* @param bool $load If true the template file will be loaded if it is found.
@@ -628,13 +685,17 @@ function locate_template($template_names, $load = false, $require_once = true )
628685
if ( !$template_name )
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
}
640701
}

0 commit comments

Comments
 (0)