Răsfoiți Sursa

use realpath and validate tpl directory contents

to ensure only php files inside the tpl dir can get used as templates
El RIDO 10 luni în urmă
părinte
comite
83b5d1fbba
2 a modificat fișierele cu 22 adăugiri și 4 ștergeri
  1. 13 4
      lib/View.php
  2. 9 0
      tst/ViewTest.php

+ 13 - 4
lib/View.php

@@ -12,6 +12,7 @@
 namespace PrivateBin;
 
 use Exception;
+use GlobIterator;
 
 /**
  * View
@@ -49,13 +50,21 @@ class View
      */
     public function draw($template)
     {
+        $dir  = PATH . 'tpl' . DIRECTORY_SEPARATOR;
         $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
-        $path = PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php';
-        if (!file_exists($path)) {
+        $path = realpath($dir . $file . '.php');
+        if ($path === false) {
             throw new Exception('Template ' . $template . ' not found!', 80);
         }
-        extract($this->_variables);
-        include $path;
+        foreach (new GlobIterator($dir . '*.php') as $tplFile) {
+            if ($tplFile->getRealPath() === $path) {
+                $templatesInPath = new GlobIterator(PATH . 'tpl' . DIRECTORY_SEPARATOR . '*.php');
+                extract($this->_variables);
+                include $path;
+                return;
+            }
+        }
+        throw new Exception('Template ' . $file . '.php not found in ' . $dir . '!', 81);
     }
 
     /**

+ 9 - 0
tst/ViewTest.php

@@ -142,4 +142,13 @@ class ViewTest extends TestCase
         $this->expectExceptionCode(80);
         $test->draw('123456789 does not exist!');
     }
+
+    public function testInvalidTemplate()
+    {
+        $test = new View;
+        $this->expectException(Exception::class);
+        $this->expectExceptionCode(81);
+        $test->draw('../index');
+    }
+
 }