Prevent plugins that cannot be instantiated from being loaded (#3956)

Credit to @bennothommo
This commit is contained in:
Ben Thomson 2018-12-05 01:22:07 +08:00 committed by Luke Towers
parent caf7cb406f
commit 2002fd6b4b
3 changed files with 47 additions and 8 deletions

View File

@ -5,6 +5,7 @@ use App;
use Str;
use File;
use Lang;
use Log;
use View;
use Config;
use Schema;
@ -121,17 +122,28 @@ class PluginManager
$className = $namespace.'\Plugin';
$classPath = $path.'/Plugin.php';
// Autoloader failed?
if (!class_exists($className)) {
include_once $classPath;
}
try {
// Autoloader failed?
if (!class_exists($className)) {
include_once $classPath;
}
// Not a valid plugin!
if (!class_exists($className)) {
// Not a valid plugin!
if (!class_exists($className)) {
return;
}
$classObj = new $className($this->app);
} catch (\Throwable $e) {
Log::error('Plugin ' . $className . ' could not be instantiated.', [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
]);
return;
}
$classObj = new $className($this->app);
$classId = $this->getIdentifier($classObj);
/*

View File

@ -0,0 +1,16 @@
<?php namespace TestVendor\Goto;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function pluginDetails()
{
return [
'name' => 'Invalid Test Plugin',
'description' => 'Test plugin used by unit tests to detect plugins with invalid namespaces.',
'author' => 'Test Vendor'
];
}
}

View File

@ -58,6 +58,7 @@ class PluginManagerTest extends TestCase
$this->assertArrayHasKey('October.Tester', $result);
$this->assertArrayHasKey('Database.Tester', $result);
$this->assertArrayHasKey('TestVendor.Test', $result);
$this->assertArrayNotHasKey('TestVendor.Goto', $result);
$this->assertInstanceOf('October\NoUpdates\Plugin', $result['October.NoUpdates']);
$this->assertInstanceOf('October\Sample\Plugin', $result['October.Sample']);
@ -66,6 +67,14 @@ class PluginManagerTest extends TestCase
$this->assertInstanceOf('TestVendor\Test\Plugin', $result['TestVendor.Test']);
}
public function testUnloadablePlugin()
{
$manager = PluginManager::instance();
$pluginNamespaces = $manager->getPluginNamespaces();
$result = $manager->loadPlugin('\\testvendor\\goto', $pluginNamespaces['\\testvendor\\goto']);
$this->assertNull($result);
}
public function testGetPluginPath()
{
$manager = PluginManager::instance();
@ -85,6 +94,7 @@ class PluginManagerTest extends TestCase
$this->assertArrayHasKey('October.Tester', $result);
$this->assertArrayHasKey('Database.Tester', $result);
$this->assertArrayHasKey('TestVendor.Test', $result);
$this->assertArrayNotHasKey('TestVendor.Goto', $result);
$this->assertInstanceOf('October\NoUpdates\Plugin', $result['October.NoUpdates']);
$this->assertInstanceOf('October\Sample\Plugin', $result['October.Sample']);
@ -115,12 +125,13 @@ class PluginManagerTest extends TestCase
$manager = PluginManager::instance();
$result = $manager->getPluginNamespaces();
$this->assertCount(5, $result);
$this->assertCount(6, $result);
$this->assertArrayHasKey('\october\noupdates', $result);
$this->assertArrayHasKey('\october\sample', $result);
$this->assertArrayHasKey('\october\tester', $result);
$this->assertArrayHasKey('\database\tester', $result);
$this->assertArrayHasKey('\testvendor\test', $result);
$this->assertArrayHasKey('\testvendor\goto', $result);
}
public function testGetVendorAndPluginNames()