Search by

spryker / cms-block

spryker-bot

CmsBlock module

Package info

github.com/spryker/cms-block

pkg:composer/spryker/cms-block

Statistics

Installs: 2 743 162

Dependents: 15

Suggesters: 1

Stars: 0


README

Latest Stable Version Minimum PHP Version

CmsBlock supports management of blocks. Blocks can be viewed as fully localizable partial content snippets that can be embedded in other pages such as Product pages, Category pages, and so on. The CMS Block module supports flexible extension by adding a new connector.

Installation

composer require spryker/cms-block

Rendering placeholder content in a Twig sandbox

The renderCmsBlockAsTwig Twig function, used to embed CMS blocks in e-mails, renders each placeholder of a block as a Twig template. Placeholder content is written in the Back Office. By default it is rendered by the Zed application Twig environment, so it can call every Twig function and filter registered there and read the app global, which holds the application container.

To render placeholder content in a Twig sandbox, override CmsBlockConfig::isPlaceholderTwigSandboxEnabled() at project level:

namespace Pyz\Zed\CmsBlock;

use Spryker\Zed\CmsBlock\CmsBlockConfig as SprykerCmsBlockConfig;

class CmsBlockConfig extends SprykerCmsBlockConfig
{
    public function isPlaceholderTwigSandboxEnabled(): bool
    {
        return true;
    }
}

With the override in place:

  • Placeholder content can use the Twig core tags and the filters, functions and tests of the Zed Twig environment, except the ones that getTwigSandboxRestrictedTags(), getTwigSandboxRestrictedFilters(), getTwigSandboxRestrictedFunctions() and getTwigSandboxRestrictedTests() return. Tags that Twig extensions of the application add are not available.
  • An output expression that uses a restricted filter, function, test or method, or passes a callable that is not an arrow function to a filter such as map, renders ACCESS RESTRICTED instead, and the rest of the content is rendered: Hello {{ constant('PHP_VERSION') }} renders Hello ACCESS RESTRICTED. A restricted name that no Zed extension provides, such as a chart function of a module that is not installed, is restricted as well. A restricted tag, or a restricted call outside of an output expression, such as in a set or if tag or in a block rendered by block(), renders ACCESS RESTRICTED instead of the whole placeholder content. Each restriction is logged as a warning. In debug mode of the Zed Twig environment, ApplicationConstants::ENABLE_APPLICATION_DEBUG by default, a restriction throws a Twig\Sandbox\SecurityError instead, so the e-mail fails to render and the error names the restricted call.
  • By default, the restricted tags are include, embed, extends, import, from and use, the restricted filter is serialize, and the restricted functions are include_only, template_from_string, constant, enum, enum_cases, attribute, getenv, dump, fragment_uri, render, render_*, controller, chart, barChart, pieChart and lineChart, and the restricted test is constant. A * matches any characters, so a name ending with * restricts every name with that prefix. A function that needs the Twig environment or context gets the application environment, so include_only and the chart functions, which render templates or Back Office statistics, are restricted.
  • Methods can only be called on objects of the classes that getTwigSandboxAllowedMethodClasses() returns, by default transfer objects. Every public method is allowed, such as {{ mail.customer.firstName }} or {{ mail.order.toArray() }}, except the ones whose name starts with a prefix of getTwigSandboxRestrictedMethodPrefixes(): by default the writers, such as setName() and fromArray(), so placeholder content cannot change data.
  • include() and source() load only templates whose extension getTwigSandboxAllowedTemplateExtensions() returns, by default twig and html, and whose name does not start with / or contain .., a backslash or a control character, such as {{ include('@MyModule/mail/snippet.twig') }}. The included template is rendered in the sandbox as well, so it cannot use a restricted tag, such as extends, embed or include, and a restriction in it renders ACCESS RESTRICTED in place of the include() expression. Any other name renders ACCESS RESTRICTED.
  • configurationValue() and configurationValues() read the settings that the configuration schema does not mark as secret. In Zed they would return the decrypted value of a secret setting, so configurationValue() of a secret setting renders ACCESS RESTRICTED, and configurationValues() leaves secret settings out. This holds for a key given by position, by name, spread from a mapping or captured by a set tag. A key that is not a string renders ACCESS RESTRICTED. Only the keys that the content reads are looked up, with ConfigurationFacade::getConfigurationSettingCollection(), by the key that configurationValue() resolves, the static_key of a setting included. Without the optional spryker/configuration module, there are no secret settings. CmsBlock conflicts with spryker/configuration versions before 1.11.0, which do not have this method.
  • render() and controller() are restricted by default: a sub-request skips the Back Office firewall and access control, so placeholder content could read and change Back Office data, for example {{ render('/customer/index/table') }} in an e-mail to its editor. A project that removes render and controller from getTwigSandboxRestrictedFunctions() accepts this, as the Spryker Suite does in src/Pyz/CmsBlock/src/Pyz/Zed/CmsBlock/CmsBlockConfig.php, and render() then only renders a relative URL, such as {{ render('/user/index') }} or {{ render('/') }}, or a controller action reference made of /-separated names of letters, digits and hyphens, with the inline strategy, without path segments that start with _, without a script name such as index.php, and without query parameters starting with _, HTML entities such as &_path= included. It renders at most three nested fragments. Any other fragment renders ACCESS RESTRICTED. An e-mail sent without a current request, for example by the OMS, cannot render a fragment: ignore_errors: true does not help there, as on a project without the sandbox.
  • Filters such as map, filter, reduce and sort, and the has some and has every operators, only accept arrow functions, not PHP function names.
  • The app global is not available.

The sandbox needs Twig 3.29 or later and throws a TwigSandboxNotSupportedException on earlier versions. The method returns false by default, so a project keeps the previous behavior until it adds the override. The Spryker Suite ships the override in src/Pyz/CmsBlock/src/Pyz/Zed/CmsBlock/CmsBlockConfig.php. When you add a Twig function or filter to Zed that reads configuration, secrets or files, or that renders other templates or pages, add it to the restricted lists. Never remove the default restricted names, except render and controller as described above.

Documentation

Spryker Documentation