Add conditional registration for Twig SandboxExtension#18387
Add conditional registration for Twig SandboxExtension#18387MadMikeyB wants to merge 1 commit intocraftcms:5.xfrom
Conversation
|
Yeah sorry for the confusion… we changed our minds a couple times on how to implement sandboxing. As of #18216 / what actually shipped, the only time we register the sandbox extension is when The idea is that when something needs to render an untrusted template (like System Messages), the code can simply call the appropriate Where we landed, System Message templates are the only templates that could be affected by any of the sandbox changes. Craft has never used the SandboxExtension besides that. So it’s hard to see how 5.9 would have affected normal templates / third party plugins, unless those plugins were updated based on a prior 5.9 change that didn’t end up actually shipping. |
|
Thanks @brandonkelly - scuppers my idea as to why the nocache plugin is trying to load the sandbox in it's compiled template then! lol |
|
Sorry, I lied! Looking back at the code, I forgot we are in fact always registering the SandboxExtension, but it’s only actually enabled when a That said, if we were to only register the extension when |
Description
In #18208, the Twig Sandbox was added conditionally to all places it's used, controlled by
enableTwigSandbox(commit).In #18216, the View class was updated (commit), but the conditional check for
enableTwigSandboxwas not applied to the SandboxExtension registration increateTwig(). As I understand it, , this means SandboxExtension is always added to Twig, even whenenableTwigSandboxisfalse.(I've been looking into this in circles for hours, so my apologies if I've missed something here)
Related issues
This seemingly breaks the craft-nocache plugin - ttempleton/craft-nocache#42
When nocache compiles templates, they include
$this->sandbox->ensureToStringAllowed()calls because SandboxExtension is present. However,$this->sandboxis null during rendering because sandbox mode isn't actually enabled (sinceenableTwigSandboxisfalse). This causes fatal errors with no way to resolve other than stripping all$this->sandboxreferences from compiled templates via regex, so I think it better to fix upstream, apologies if this all sounds unrelated.This fix ensures SandboxExtension is only registered when
enableTwigSandboxis explicitly set totrue, resolving the craft-nocache issue. I have tested this by setting->enableTwigSandbox(false)and the issue with nocache is resolved. If I set it to->enableTwigSandbox(true), their bug resurfaces, indicating that sandbox was always on.