Add an "editor" extension type
[lhc/web/wiklou.git] / includes / TemplateParser.php
index a1e6d5b..2293dab 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+use MediaWiki\MediaWikiServices;
+
 /**
  * Handles compiling Mustache templates into PHP rendering functions
  *
@@ -36,6 +38,13 @@ class TemplateParser {
         */
        protected $forceRecompile = false;
 
+       /**
+        * @var int Compilation flags passed to LightnCandy
+        */
+       // Do not add more flags here without discussion.
+       // If you do add more flags, be sure to update unit tests as well.
+       protected $compileFlags = LightnCandy::FLAG_ERROR_EXCEPTION;
+
        /**
         * @param string $templateDir
         * @param bool $forceRecompile
@@ -45,6 +54,18 @@ class TemplateParser {
                $this->forceRecompile = $forceRecompile;
        }
 
+       /**
+        * Enable/disable the use of recursive partials.
+        * @param bool $enable
+        */
+       public function enableRecursivePartials( $enable ) {
+               if ( $enable ) {
+                       $this->compileFlags = $this->compileFlags | LightnCandy::FLAG_RUNTIMEPARTIAL;
+               } else {
+                       $this->compileFlags = $this->compileFlags & ~LightnCandy::FLAG_RUNTIMEPARTIAL;
+               }
+       }
+
        /**
         * Constructs the location of the the source Mustache template
         * @param string $templateName The name of the template
@@ -52,18 +73,11 @@ class TemplateParser {
         * @throws UnexpectedValueException If $templateName attempts upwards directory traversal
         */
        protected function getTemplateFilename( $templateName ) {
-               // Prevent upwards directory traversal using same methods as Title::secureAndSplit
+               // Prevent path traversal. Based on Language::isValidCode().
+               // This is for paranoia. The $templateName should never come from
+               // untrusted input.
                if (
-                       strpos( $templateName, '.' ) !== false &&
-                       (
-                               $templateName === '.' || $templateName === '..' ||
-                               strpos( $templateName, './' ) === 0 ||
-                               strpos( $templateName, '../' ) === 0 ||
-                               strpos( $templateName, '/./' ) !== false ||
-                               strpos( $templateName, '/../' ) !== false ||
-                               substr( $templateName, -2 ) === '/.' ||
-                               substr( $templateName, -3 ) === '/..'
-                       )
+                       strcspn( $templateName, ":/\\\000&<>'\"%" ) !== strlen( $templateName )
                ) {
                        throw new UnexpectedValueException( "Malformed \$templateName: $templateName" );
                }
@@ -78,11 +92,13 @@ class TemplateParser {
         * @throws RuntimeException
         */
        protected function getTemplate( $templateName ) {
+               $templateKey = $templateName . '|' . $this->compileFlags;
+
                // If a renderer has already been defined for this template, reuse it
-               if ( isset( $this->renderers[$templateName] ) &&
-                       is_callable( $this->renderers[$templateName] )
+               if ( isset( $this->renderers[$templateKey] ) &&
+                       is_callable( $this->renderers[$templateKey] )
                ) {
-                       return $this->renderers[$templateName];
+                       return $this->renderers[$templateKey];
                }
 
                $filename = $this->getTemplateFilename( $templateName );
@@ -95,10 +111,10 @@ class TemplateParser {
                $fileContents = file_get_contents( $filename );
 
                // Generate a quick hash for cache invalidation
-               $fastHash = md5( $fileContents );
+               $fastHash = md5( $this->compileFlags . '|' . $fileContents );
 
                // Fetch a secret key for building a keyed hash of the PHP code
-               $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
+               $config = MediaWikiServices::getInstance()->getMainConfig();
                $secretKey = $config->get( 'SecretKey' );
 
                if ( $secretKey ) {
@@ -107,20 +123,22 @@ class TemplateParser {
                        $key = $cache->makeKey( 'template', $templateName, $fastHash );
                        $code = $this->forceRecompile ? null : $cache->get( $key );
 
-                       if ( !$code ) {
-                               $code = $this->compileForEval( $fileContents, $filename );
-
-                               // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check
-                               $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code );
-                       } else {
+                       if ( $code ) {
                                // Verify the integrity of the cached PHP code
                                $keyedHash = substr( $code, 0, 64 );
                                $code = substr( $code, 64 );
                                if ( $keyedHash !== hash_hmac( 'sha256', $code, $secretKey ) ) {
-                                       // Generate a notice if integrity check fails
-                                       trigger_error( "Template failed integrity check: {$filename}" );
+                                       // If the integrity check fails, don't use the cached code
+                                       // We'll update the invalid cache below
+                                       $code = null;
                                }
                        }
+                       if ( !$code ) {
+                               $code = $this->compileForEval( $fileContents, $filename );
+
+                               // Prefix the cached code with a keyed hash (64 hex chars) as an integrity check
+                               $cache->set( $key, hash_hmac( 'sha256', $code, $secretKey ) . $code );
+                       }
                // If there is no secret key available, don't use cache
                } else {
                        $code = $this->compileForEval( $fileContents, $filename );
@@ -130,7 +148,7 @@ class TemplateParser {
                if ( !is_callable( $renderer ) ) {
                        throw new RuntimeException( "Requested template, {$templateName}, is not callable" );
                }
-               $this->renderers[$templateName] = $renderer;
+               $this->renderers[$templateKey] = $renderer;
                return $renderer;
        }
 
@@ -171,9 +189,7 @@ class TemplateParser {
                return LightnCandy::compile(
                        $code,
                        [
-                               // Do not add more flags here without discussion.
-                               // If you do add more flags, be sure to update unit tests as well.
-                               'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
+                               'flags' => $this->compileFlags,
                                'basedir' => $this->templateDir,
                                'fileext' => '.mustache',
                        ]