{"id":18903587,"url":"https://github.com/intaro/twig-sandbox-bundle","last_synced_at":"2025-08-21T10:31:01.331Z","repository":{"id":17646909,"uuid":"20451398","full_name":"intaro/twig-sandbox-bundle","owner":"intaro","description":"Annotation configuration of the allowed methods and properties for Twig\\Sandbox extension","archived":false,"fork":false,"pushed_at":"2024-10-10T19:23:59.000Z","size":70,"stargazers_count":6,"open_issues_count":1,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-10T08:51:22.639Z","etag":null,"topics":["bundle","php","symfony","symfony-bundle","twig","twig-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/intaro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SecurityPolicy/SecurityPolicyRules.php","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-03T16:58:18.000Z","updated_at":"2024-10-10T19:23:24.000Z","dependencies_parsed_at":"2024-06-17T15:11:00.542Z","dependency_job_id":null,"html_url":"https://github.com/intaro/twig-sandbox-bundle","commit_stats":{"total_commits":35,"total_committers":6,"mean_commits":5.833333333333333,"dds":0.7142857142857143,"last_synced_commit":"845ec161775c29526d910bcad093da1890cb8439"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intaro%2Ftwig-sandbox-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intaro%2Ftwig-sandbox-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intaro%2Ftwig-sandbox-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/intaro%2Ftwig-sandbox-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/intaro","download_url":"https://codeload.github.com/intaro/twig-sandbox-bundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230507051,"owners_count":18236944,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundle","php","symfony","symfony-bundle","twig","twig-extension"],"created_at":"2024-11-08T09:05:51.904Z","updated_at":"2025-08-21T10:31:01.298Z","avatar_url":"https://github.com/intaro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TwigSandboxBundle\n\n![CI](https://github.com/intaro/twig-sandbox-bundle/workflows/CI/badge.svg?branch=master)\n\nThere is [Twig](https://twig.symfony.com)-extension [Sandbox](https://twig.symfony.com/doc/2.x/api.html#sandbox-extension) which can be used to evaluate untrusted code and where access to unsafe properties and methods is prohibited. This bundle allows to configure security policy for sandbox.\n\n## Installation\n\nTwigSandboxBundle requires Symfony 6.0 or higher.\n\nInstall the bundle:\n\n```\n$ composer require intaro/twig-sandbox-bundle\n```\n\nRegister the bundle in `config/bundles.php`:\n\n```php\nreturn [\n    // ...\n    Intaro\\TwigSandboxBundle\\IntaroTwigSandboxBundle::class =\u003e ['all' =\u003e true],\n];\n```\n\n## Usage\n\nDefine allowed properties and methods for your entities using attribute `#[Sandbox]`.\nOptionally you can add `type` option for attribute (for example `#[Sandbox(type: 'int')]`).\nThis option defines type of value that property stores or method returns.\n\nIn your application you can use annotation reader to extract value of `type` option and use this value\nto perform additional checks or any other actions, for example, use twig filters according to value of the option.\n\n```php\n\u003c?php\n// Acme/DemoBundle/Entity/Product.php\n\nnamespace Acme\\DemoBundle\\Entity;\n\nuse Doctrine\\ORM\\Mapping as ORM;\nuse Intaro\\TwigSandboxBundle\\Annotation\\Sandbox;\n\n #[ORM\\Table]\n #[ORM\\Entity]\nclass Product\n{\n    #[ORM\\Column(name: 'id', type: 'integer')]\n    #[ORM\\Id]\n    #[ORM\\GeneratedValue(strategy: \"AUTO\")]\n    private ?int $id = null;\n    \n    #[ORM\\Column(name: 'name', type: 'string', length: 255)]\n    #[Sandbox(type: 'string')]\n    private string $name = '';\n\n    #[ORM\\Column(name: 'quantity', type: 'integer', nullable: true)]\n    private ?int $quantity = null;\n\n\n    #[Sandbox(type: 'int')]\n    public function getId(): ?int\n    {\n        return $this-\u003eid;\n    }\n    \n    public function setName(string $name): self\n    {\n        $this-\u003ename = $name;\n\n        return $this;\n    }\n    \n    #[Sandbox]\n    public function getName(): string\n    {\n        return $this-\u003ename;\n    }\n\n    public function setQuantity(?int $quantity): self\n    {\n        $this-\u003equantity = $quantity;\n\n        return $this;\n    }\n    \n    public function getQuantity(): ?int\n    {\n        return $this-\u003equantity;\n    }\n}\n```\n\nAnd use sandbox environment.\n\n```php\n\nuse Acme\\DemoBundle\\Entity\\Product;\nuse Intaro\\TwigSandboxBundle\\Builder\\EnvironmentBuilder;\n\nclass Example {\n\n    private EnvironmentBuilder $environmentBuilder;\n    \n    public function __construct(EnvironmentBuilder $environmentBuilder)\n    {\n        $this-\u003eenvironmentBuilder = $environmentBuilder;\n    }\n    \n    $twig = $this-\u003eenvironmentBuilder-\u003egetSandboxEnvironment();\n    \n    $product = new Product();\n    $product-\u003esetName('Product 1');\n    $product-\u003esetQuantity(5);\n    \n    // successful render\n    $html1 = $twig-\u003erender(\n        'Product {{ product.name }}',\n        ['product' =\u003e $product]\n    );\n    \n    // render with the exception on access to the quantity method\n    $html2 = $twig-\u003erender(\n        'Product {{ product.name }} in the quantity {{ product.quantity }}',\n        ['product' =\u003e $product]\n    );\n    \n}\n```\n\n### Validation\n\nYou can validate entity fields which contain twig templates with TwigSandbox validator.\n\n```php\n// in Entity/Page.php\n\nuse Intaro\\TwigSandboxBundle\\Validator\\Constraints\\TwigSandbox;\n\nclass Page\n{\n    //...\n    \n    public static function loadValidatorMetadata(ClassMetadata $metadata)\n    {\n        $metadata-\u003eaddPropertyConstraint('template', new TwigSandbox());\n    }\n    \n    //...\n}\n\n```\n\n## Configure\n\n### Methods and properties\n\nYou can define allowed methods and properties of entities with attribute `Intaro\\TwigSandboxBundle\\Attribute\\Sandbox`. Example above.\n\n### Tags \n\nDefault list of the allowed tags:\n```yml\n- 'autoescape'\n- 'filter'\n- 'do'\n- 'flush'\n- 'for'\n- 'set'\n- 'verbatium'\n- 'if'\n- 'spaceless'\n```\n\nYou can override list in the parameter `intaro.twig_sandbox.policy_tags`:\n```yml\n# app/config/config.yml\n\nparameters:\n    intaro.twig_sandbox.policy_tags:\n        - 'do'\n        - 'for'\n        - 'if'\n        - 'spaceless'\n```\n\n### Filters\n\nDefault list of the allowed filters:\n```yml\n- 'abs'\n- 'batch'\n- 'capitalize'\n- 'convert_encoding'\n- 'date'\n- 'date_modify'\n- 'default'\n- 'escape'\n- 'first'\n- 'format'\n- 'join'\n- 'json_encode'\n- 'keys'\n- 'last'\n- 'length'\n- 'lower'\n- 'merge'\n- 'nl2br'\n- 'number_format'\n- 'raw'\n- 'replace'\n- 'reverse'\n- 'slice'\n- 'sort'\n- 'split'\n- 'striptags'\n- 'title'\n- 'trim'\n- 'upper'\n- 'url_encode'\n```\n\nYou can override list in the parameter `intaro.twig_sandbox.policy_filters`:\n```yml\n# app/config/config.yml\n\nparameters:\n    intaro.twig_sandbox.policy_filters:\n        - 'sort'\n        - 'upper'\n        - 'sort'\n```\n\n### Functions\n\nDefault list of the allowed functions:\n```yml\n- 'attribute'\n- 'constant'\n- 'cycle'\n- 'date'\n- 'random'\n- 'range'\n```\n\nYou can override list in parameter `intaro.twig_sandbox.policy_functions`:\n```yml\n# app/config/config.yml\n\nparameters:\n    intaro.twig_sandbox.policy_functions:\n        - 'date'\n        - 'range'\n```\n\n### Allowed types\n\nDefault list of allowed return types:\n```yml\n- 'bool'\n- 'collection'\n- 'date'\n- 'float'\n- 'int'\n- 'object'\n- 'string'\n```\n\nYou can override list in parameter `intaro.twig_sandbox.sandbox_annotation.value_types`:\n```yml\n# app/config/config.yml\n\nparameters:\n    intaro.twig_sandbox.sandbox_annotation.value_types:\n        - 'string'\n        - 'date'\n        - 'collection'\n        - 'stdClass'\n```\n\n### Environment\n\nYou can set twig environment parameters:\n```php\n\n$twig = $this-\u003eget(EnvironmentBuilder::class)-\u003egetSandboxEnvironment([\n    'strict_variables' =\u003e true\n]);\n```\n\nAlso, you might want to add extensions to your twig environment. Example how to add:\n```php\n// Acme/DemoBundle/AcmeDemoBundle.php\n\u003c?php\n\nnamespace Acme\\DemoBundle;\n\nuse Symfony\\Component\\HttpKernel\\Bundle\\Bundle;\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\nuse Acme\\DemoBundle\\DependencyInjection\\Compiler\\TwigSandboxPass;\n\nclass AcmeDemoBundle extends Bundle\n{\n    public function build(ContainerBuilder $container)\n    {\n        parent::build($container);\n\n        $container-\u003eaddCompilerPass(new TwigSandboxPass());\n    }\n}\n```\n\n```php\n// Acme/DemoBundle/DependencyInjection/Compiler/TwigSandboxPass.php\n\u003c?php\n\nnamespace Acme\\DemoBundle\\DependencyInjection\\Compiler;\n\nuse Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface;\nuse Symfony\\Component\\DependencyInjection\\ContainerBuilder;\nuse Symfony\\Component\\DependencyInjection\\Reference;\nuse Intaro\\TwigSandboxBundle\\Builder\\EnvironmentBuilder;\n\nclass TwigSandboxPass implements CompilerPassInterface\n{\n    public function process(ContainerBuilder $container)\n    {\n        if (!$container-\u003ehasDefinition(EnvironmentBuilder::class)) {\n            return;\n        }\n\n        $sandbox = $container-\u003egetDefinition(EnvironmentBuilder::class);\n        $sandbox-\u003eaddMethodCall('addExtension', [new Reference('acme_demo.twig_extension')]);\n    }\n}\n```\n\n### Other parameters\n\nYou can specify additional directories for each bundle to scan (using parameter `intaro.twig_sandbox.additional_paths`).\n\nExample:\n```yml\n# app/config/config.yml\n\nparameters:\n    intaro.twig_sandbox.additional_paths:\n        AcmeDemoBundle:\n            - 'Model'\n```\n\n## Development ##\n\n### Run tests ###\n\nInstall vendors:\n```shell\nmake vendor\n```\n\nRun php-cs-fixer, phpstan and phpunit:\n```shell\nmake check\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintaro%2Ftwig-sandbox-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintaro%2Ftwig-sandbox-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintaro%2Ftwig-sandbox-bundle/lists"}