Skip to content

Twig Sandbox

Secure Sandbox

For security purposes, all message templates are parsed in a secure Twig sandbox with limited functionality. This sandbox can be customized to fit your needs, or disabled entirely in rare cases.

Under the hood, Notifier relies on the nystudio107/craft-twig-sandbox package to provide a secure Twig environment.

To get a good understanding of how the sandbox works, please consult the default blacklist and default whitelist.

Customizing the Twig Sandbox

To configure the sandbox, start by creating a config/notifier-sandbox.php file.

Within the context of that file, you can specify which Twig tags, filters, functions, methods, or properties should be allowed or disallowed. You can choose to add, remove, or replace the default list of Twig specs.

For example, here's how to permit the include tag without making any other changes to the existing default blacklist:

php
// config/notifier-sandbox.php

use doublesecretagency\notifier\models\Sandbox;

return [
    'list' => Sandbox::BLACKLIST, // Use the default blacklist,
    'mode' => Sandbox::REMOVE,    // but remove the `include` tag.
    'twig' => [
        'tags' => ['include']
    ]
];

The example above uses the default blacklist, but removes the include tag from it. In other words, we're marking the include tag as safe, and allowing it to be used in message templates.

Config Parameters

When configuring the sandbox, it's important to keep in mind three things:

  • Are you using the blacklist or the whitelist?
  • Do you want your new Twig specs to be added to, removed from, or completely replace the default list?
  • Which Twig tags, filters, functions, methods, or properties are you specifying?
php
return [
    'list' => $list, // Which list are we using?
    'mode' => $mode, // How are we altering the list?
    'twig' => $twig  // What changes are we making?
];

'list'

string - Defaults to Sandbox::BLACKLIST.

Select which list to enable and modify:

'mode'

string - Defaults to Sandbox::ADD.

Select how the supplied Twig specifications should be handled:

  • Sandbox::ADD - Add the Twig specs to the list.
  • Sandbox::REMOVE - Remove the Twig specs from the list.
  • Sandbox::REPLACE - Replace the entire list with the Twig specs.
  • Sandbox::DISABLE - Completely disable the sandbox. (not recommended)

'twig'

array - Defaults to [].

Specify which Twig tags, filters, functions, methods, or properties will be added, removed, or replaced.

Within the context of the twig parameter, you can specify the following Twig types:

TypesDescription
tagsCraft and native Twig tags.
filtersCraft and native Twig filters.
functionsCraft and native Twig functions.
methodsCraft and native Twig methods.
propertiesCraft and native Twig properties.

Each type takes the form of a nested array:

php
use doublesecretagency\notifier\models\Sandbox;

return [
    'list' => Sandbox::WHITELIST, // Use the default whitelist, and
    'mode' => Sandbox::ADD,       // add these tags, filters, and functions.
    'twig' => [
        'tags' => [
            'extends',
            'include'
        ],
        'filters' => [
            'json_decode', 
            'json_encode'
        ],
        'functions' => [
            'ceil',
            'floor'
        ],
    ]
];

Disable Sandbox Completely

It is also possible to disable the Twig sandbox entirely, and rely on Craft's native Twig functionality.

php
use doublesecretagency\notifier\models\Sandbox;

return [
    'mode' => Sandbox::DISABLE
];

WARNING - Possible Security Risks!

When disabling or reconfiguring the Twig sandbox, be aware of who has permission to edit Notifications. Ensure that Notification editors are trusted system users, otherwise you may be opening up a security loophole for bad actors.

Within the Craft control panel, you can also manage who has access to the Notifier plugin by managing their individual User (or Group) permission settings.