{"record":{"id":"924e9e57e264f63b","repo":"twigphp/Twig","slug":"the-environment-passed-to-s-must-be-dedicated-to-it-pass-a","errorCode":null,"errorMessage":"The environment passed to \"%s\" must be dedicated to it: pass a freshly built environment that has no \"%s\" registered and has not been used yet.","messagePattern":"The environment passed to \"(.+?)\" must be dedicated to it: pass a freshly built environment that has no \"(.+?)\" registered and has not been used yet\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Sandbox/Sandbox.php","lineNumber":43,"sourceCode":" * application environment: the sandbox environment must be dedicated to\n * rendering untrusted templates.\n *\n * @author Fabien Potencier <fabien@symfony.com>\n */\nfinal class Sandbox implements SandboxInterface\n{\n    public function __construct(\n        private Environment $env,\n        SecurityPolicyInterface $policy,\n    ) {\n        if ($policy instanceof SecurityPolicy && !$policy->isStrict()) {\n            throw new \\LogicException('The sandbox requires a strict security policy, call \"setStrict(true)\" on a dedicated policy for this sandbox.');\n        }\n\n        try {\n            $env->addExtension(new SandboxExtension($policy, true));\n        } catch (\\LogicException $e) {\n            throw new \\LogicException(\\sprintf('The environment passed to \"%s\" must be dedicated to it: pass a freshly built environment that has no \"%s\" registered and has not been used yet.', self::class, SandboxExtension::class), 0, $e);\n        }\n    }\n\n    public function render(string $name, array $context = []): string\n    {\n        return $this->env->render($name, $context);\n    }\n\n    public function display(string $name, array $context = []): void\n    {\n        $this->env->display($name, $context);\n    }\n\n    public function stream(string $name, array $context = []): iterable\n    {\n        yield from $this->env->load($name)->stream($context);\n    }\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Sandbox/Sandbox.php#L25-L61","documentation":"Sandbox registers a SandboxExtension into the Environment passed to its constructor. Environments accept each extension only once, so if the Environment already has a SandboxExtension registered or was already used to render/load templates, addExtension() throws a LogicException which Sandbox re-throws with this message (chained as previous exception).","triggerScenarios":"new Sandbox($env, $policy) where $env already has SandboxExtension (e.g. via addExtension or the 'sandbox' extension option), or $env previously rendered/loaded any template (non-fresh environment).","commonSituations":"Sharing one application-wide Environment between normal rendering and a Sandbox instance; constructing two Sandbox objects over the same Environment; reusing an Environment created with sandbox config in twig.yaml.","solutions":["Create a fresh Environment dedicated to the Sandbox: new Sandbox(new Environment($loader, $options), $policy).","Remove any pre-registered SandboxExtension / 'sandbox' option from the Environment passed in.","If sandbox checks in normal templates are needed too, use a separate Environment for full Sandbox rendering."],"exampleFix":"// before\n$env = new Environment($loader);\n$env->addExtension(new SandboxExtension($policy, false));\n$sandbox = new Sandbox($env, $policy); // throws: env not dedicated\n\n// after\n$sandbox = new Sandbox(new Environment($loader), $policy);","handlingStrategy":"validation","validationCode":"// Build a dedicated environment; never reuse an app-wide one\n$sandbox = new \\Twig\\Sandbox\\Sandbox(new \\Twig\\Environment($loader, ['autoescape' => true]), $policy);","typeGuard":null,"tryCatchPattern":"try { $sandbox = new \\Twig\\Sandbox\\Sandbox($env, $policy); } catch (\\LogicException $e) { if (str_contains($e->getMessage(), 'must be dedicated')) { $sandbox = new \\Twig\\Sandbox\\Sandbox(new \\Twig\\Environment($loader), $policy); } else { throw $e; } }","preventionTips":["Never pass a previously used Environment to Sandbox","Do not register SandboxExtension manually in the same environment","Keep sandboxed rendering in its own isolated Environment instance"],"tags":["twig","php","security","sandbox","configuration"],"backgroundTag":"invalid-config-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}