Skip to content

Commit

Permalink
feat: Add console/SandboxErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jun 30, 2024
1 parent 8e5e968 commit 35c8cd5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/console/SandboxErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace nystudio107\crafttwigsandbox\console;

use craft\console\ErrorHandler;
use Twig\Sandbox\SecurityError;

class SandboxErrorHandler extends ErrorHandler
{
// Public Methods
// =========================================================================

/**
* @inheritDoc
*/
public function handleException($exception): void
{
// If this is a Twig Runtime exception, use the previous one instead
if ($exception instanceof SecurityError && ($previousException = $exception->getPrevious()) !== null) {
$exception = $previousException;
}
parent::handleException($exception);
}
}
15 changes: 10 additions & 5 deletions src/web/SandboxView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace nystudio107\crafttwigsandbox\web;

use Craft;
use craft\web\twig\Environment;
use craft\web\View;
use nystudio107\crafttwigsandbox\console\SandboxErrorHandler as ConsoleSandboxErrorHandler;
use nystudio107\crafttwigsandbox\twig\WhitelistSecurityPolicy;
use nystudio107\crafttwigsandbox\web\SandboxErrorHandler as WebSandboxErrorHandler;
use Twig\Extension\SandboxExtension;
use Twig\Sandbox\SecurityPolicyInterface;

Expand All @@ -22,9 +25,9 @@ class SandboxView extends View
// =========================================================================

/**
* @var SandboxErrorHandler|null The error handler to use for the SandboxView
* @var WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null The error handler to use for the SandboxView
*/
protected ?SandboxErrorHandler $sandboxErrorHandler = null;
protected WebSandboxErrorHandler|ConsoleSandboxErrorHandler|null $sandboxErrorHandler = null;

// Public Methods
// =========================================================================
Expand All @@ -35,8 +38,11 @@ class SandboxView extends View
public function init(): void
{
parent::init();
// Use the passed in ErrorHandler, or create a default error handler
$this->sandboxErrorHandler = $this->sandboxErrorHandler ?? new SandboxErrorHandler();
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
$this->sandboxErrorHandler = new ConsoleSandboxErrorHandler();
} else {
$this->sandboxErrorHandler = new WebSandboxErrorHandler();
}
// Use the passed in SecurityPolicy, or create a default security policy
$this->securityPolicy = $this->securityPolicy ?? new WhitelistSecurityPolicy();
}
Expand All @@ -49,7 +55,6 @@ public function createTwig(): Environment
$twig = parent::createTwig();
// Add the SandboxExtension with our SecurityPolicy after Twig is created
$twig->addExtension(new SandboxExtension($this->securityPolicy, true));
$this->sandboxErrorHandler->env = $twig;

return $twig;
}
Expand Down

0 comments on commit 35c8cd5

Please sign in to comment.