Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Controller/MailPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use DebugKit\Mailer\AbstractResult;
use DebugKit\Mailer\MailPreview;
use DebugKit\Mailer\PreviewResult;
use DebugKit\Mailer\SentMailResult;

Expand Down Expand Up @@ -255,7 +256,7 @@ protected function findPreferredPart(AbstractResult $email, $partType)
*
* @param string $previewName The Mailer name
* @param string $emailName The mailer preview method
* @param string $plugin The plugin where the mailer preview should be found
* @param null|string $plugin The plugin where the mailer preview should be found
* @return \DebugKit\Mailer\PreviewResult The result of the email preview
* @throws \Cake\Http\Exception\NotFoundException
*/
Expand All @@ -264,9 +265,12 @@ protected function findPreview($previewName, $emailName, $plugin = '')
if ($plugin) {
$plugin = "$plugin.";
}
if (str_contains($previewName, '\\')) {
throw new NotFoundException("Mailer preview $previewName not found");
}

$realClass = App::className($plugin . $previewName, 'Mailer/Preview');
if (!$realClass) {
if (!$realClass || !is_subclass_of($realClass, MailPreview::class, true)) {
throw new NotFoundException("Mailer preview ${previewName} not found");
}
$mailPreview = new $realClass();
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Controller/MailPreviewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ public function testEmailPluginPassedToView()
$this->assertResponseContains('src="?part=html&plugin=DebugkitTestPlugin');
}

/**
* Test that invalid classnames are rejected
*
* @return void
*/
public function testEmailRejectInvalidClassName()
{
$this->get('/debug-kit/mail-preview/preview/Cake\Utility\Inflector/slug');
$this->assertResponseCode(404);

$this->get('/debug-kit/mail-preview/preview/Invalid/hello');
$this->assertResponseCode(404);
}

/** Test email template content
*
* @return void
Expand Down
15 changes: 15 additions & 0 deletions tests/test_app/Mailer/Preview/Invalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace DebugKit\TestApp\Mailer\Preview;

/**
* Stub class that is not a valid mailer preview.
*/
class Invalid
{
public function hello(): string
{
return 'hello';
}
}
Loading