Skip to content

Twig Sandbox

Secure Sandbox

For security purposes, all message templates are parsed in a secure sandbox mode with limited functionality.

Please consult the complete list of default values on Github.

Adjust the Sandbox Configuration

If necessary, you can manually configure the sandbox by editing the PHP Config File.

Add a twigSandbox parameter to the config/notifier.php file:

php
'twigSandbox' => [

    // Add to the default allowed list
    'allow' => [
        ...
    ],
    
    // Remove from the default allowed list
    'disallow' => [
        ...
    ],
    
    // Replace the default allowed list
    'override' => [
        ...
    ],
    
]
ActionBehavior
allowAdds to the default allowed list.
disallowRemoves from the default allowed list.
overrideReplaces the default allowed list.

Within each of those nested arrays, 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.

Defaults

See the complete list of default values on Github.

In this way, you can easily allow additional Twig:

php
// Permit the `do` tag
'allow' => [
    'tags' => ['do']
]

Or disallow Twig from the default list:

php
// Blocks the `macro` tag
'disallow' => [
    'tags' => ['macro']
]

To disallow all values of a given Twig type, override it with an empty array:

php
// No tags allowed
'override' => [
    'tags' => []
]

Omitted types will fall back to their respective default values.

Twig Sandbox Extension

For further information, including how to structure methods and properties, please consult the official Twig docs.

Disable Sandbox Completely

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

php
'twigSandbox' => false

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.

You can always manage who has access to the Notifier plugin by managing their individual User (or Group) permission settings.