@@ -484,7 +484,7 @@ function get_page_template() {
484484 }
485485 if ( $ pagename ) {
486486 $ pagename_decoded = urldecode ( $ pagename );
487- if ( $ pagename_decoded !== $ pagename ) {
487+ if ( $ pagename_decoded !== $ pagename && 0 === validate_file ( $ pagename_decoded ) ) {
488488 $ templates [] = "page- {$ pagename_decoded }.php " ;
489489 }
490490 $ templates [] = "page- {$ pagename }.php " ;
@@ -679,6 +679,62 @@ function get_attachment_template() {
679679 return get_query_template ( 'attachment ' , $ templates );
680680}
681681
682+ /**
683+ * Determines whether a template found by locate_template() may be loaded.
684+ *
685+ * @since 7.1.2
686+ * @access private
687+ *
688+ * @param string $path Path to an existing template file.
689+ * @return bool Whether the template may be loaded.
690+ */
691+ function _wp_is_template_path_allowed ( $ path ) {
692+ // A file path that exists and does not contain `..` is allowed.
693+ if ( 0 === preg_match ( '#(?:^|/)\.\.[. ]*(?:/|$)# ' , wp_normalize_path ( $ path ) ) ) {
694+ return true ;
695+ }
696+
697+ // Resolve the true location of the requested file for later comparison.
698+ $ real_path = realpath ( $ path );
699+
700+ if ( false === $ real_path ) {
701+ return false ;
702+ }
703+
704+ $ real_path = trailingslashit ( wp_normalize_path ( $ real_path ) );
705+
706+ $ directories = array (
707+ STYLESHEETPATH ,
708+ TEMPLATEPATH ,
709+ ABSPATH . WPINC . '/theme-compat ' ,
710+ );
711+
712+ // If a theme is in a subdirectory, accept templates from its direct parent directory.
713+ if ( str_contains ( get_stylesheet (), '/ ' ) ) {
714+ $ directories [] = dirname ( STYLESHEETPATH );
715+ }
716+
717+ // If a parent theme is in a subdirectory, accept templates from its direct parent directory.
718+ if ( str_contains ( get_template (), '/ ' ) ) {
719+ $ directories [] = dirname ( TEMPLATEPATH );
720+ }
721+
722+ foreach ( $ directories as $ directory ) {
723+ $ real_directory = realpath ( $ directory );
724+
725+ if ( false === $ real_directory ) {
726+ continue ;
727+ }
728+
729+ // The true location of the requested file must be inside one of the allowed directories.
730+ if ( str_starts_with ( $ real_path , trailingslashit ( wp_normalize_path ( $ real_directory ) ) ) ) {
731+ return true ;
732+ }
733+ }
734+
735+ return false ;
736+ }
737+
682738/**
683739 * Retrieves the name of the highest priority template file that exists.
684740 *
@@ -687,6 +743,7 @@ function get_attachment_template() {
687743 *
688744 * @since 2.7.0
689745 * @since 5.5.0 The `$args` parameter was added.
746+ * @since 7.1.2 A template name containing `..` is only located if it resolves inside the theme.
690747 *
691748 * @param string|array $template_names Template file(s) to search for, in order.
692749 * @param bool $load If true the template file will be loaded if it is found.
@@ -703,13 +760,17 @@ function locate_template( $template_names, $load = false, $require_once = true,
703760 continue ;
704761 }
705762 if ( file_exists ( STYLESHEETPATH . '/ ' . $ template_name ) ) {
706- $ located = STYLESHEETPATH . '/ ' . $ template_name ;
707- break ;
763+ $ candidate = STYLESHEETPATH . '/ ' . $ template_name ;
708764 } elseif ( file_exists ( TEMPLATEPATH . '/ ' . $ template_name ) ) {
709- $ located = TEMPLATEPATH . '/ ' . $ template_name ;
710- break ;
765+ $ candidate = TEMPLATEPATH . '/ ' . $ template_name ;
711766 } elseif ( file_exists ( ABSPATH . WPINC . '/theme-compat/ ' . $ template_name ) ) {
712- $ located = ABSPATH . WPINC . '/theme-compat/ ' . $ template_name ;
767+ $ candidate = ABSPATH . WPINC . '/theme-compat/ ' . $ template_name ;
768+ } else {
769+ continue ;
770+ }
771+
772+ if ( _wp_is_template_path_allowed ( $ candidate ) ) {
773+ $ located = $ candidate ;
713774 break ;
714775 }
715776 }
0 commit comments