This commit is contained in:
Eric Espie
2022-11-29 14:39:16 +01:00
parent 084f3ec52b
commit ded2f8af9a
5 changed files with 102 additions and 96 deletions

View File

@@ -31,7 +31,6 @@ use iPopupMenuExtension;
use IssueLog;
use JSButtonItem;
use MetaModel;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

View File

@@ -23,9 +23,12 @@ use AttributeDate;
use AttributeDateTime;
use AttributeText;
use Dict;
use Exception;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
use utils;
/**
@@ -39,14 +42,14 @@ use utils;
class AppExtension extends AbstractExtension
{
/**
* @return array|\Twig\TwigFilter[]|\Twig_SimpleFilter[]
* @return array|\Twig\TwigFilter[]|\Twig\TwigFilter[]
*/
public function getFilters()
{
$filters = array();
// Filter to translate a string via the Dict::S function
// Usage in twig: {{ 'String:ToTranslate'|dict_s }}
$filters[] = new Twig_SimpleFilter('dict_s',
$filters[] = new TwigFilter('dict_s',
function ($sStringCode, $sDefault = null, $bUserLanguageOnly = false) {
return Dict::S($sStringCode, $sDefault, $bUserLanguageOnly);
}
@@ -54,7 +57,7 @@ class AppExtension extends AbstractExtension
// Filter to format a string via the Dict::Format function
// Usage in twig: {{ 'String:ToTranslate'|dict_format() }}
$filters[] = new Twig_SimpleFilter('dict_format',
$filters[] = new TwigFilter('dict_format',
function ($sStringCode, $sParam01 = null, $sParam02 = null, $sParam03 = null, $sParam04 = null) {
return Dict::Format($sStringCode, $sParam01, $sParam02, $sParam03, $sParam04);
}
@@ -67,7 +70,7 @@ class AppExtension extends AbstractExtension
*
* @since 3.0.0
*/
$filters[] = new Twig_SimpleFilter('date_format',
$filters[] = new TwigFilter('date_format',
function ($sDate) {
try
{
@@ -95,7 +98,7 @@ class AppExtension extends AbstractExtension
*
* @since 3.0.0
*/
$filters[] = new Twig_SimpleFilter('size_format',
$filters[] = new TwigFilter('size_format',
function ($sSize) {
return utils::BytesToFriendlyFormat($sSize);
}
@@ -103,12 +106,12 @@ class AppExtension extends AbstractExtension
// Filter to enable base64 encode/decode
// Usage in twig: {{ 'String to encode'|base64_encode }}
$filters[] = new Twig_SimpleFilter('base64_encode', 'base64_encode');
$filters[] = new Twig_SimpleFilter('base64_decode', 'base64_decode');
$filters[] = new TwigFilter('base64_encode', 'base64_encode');
$filters[] = new TwigFilter('base64_decode', 'base64_decode');
// Filter to enable json decode (encode already exists)
// Usage in twig: {{ aSomeArray|json_decode }}
$filters[] = new Twig_SimpleFilter('json_decode', function ($sJsonString, $bAssoc = false) {
$filters[] = new TwigFilter('json_decode', function ($sJsonString, $bAssoc = false) {
return json_decode($sJsonString, $bAssoc);
}
);
@@ -120,7 +123,7 @@ class AppExtension extends AbstractExtension
* @uses \utils::Sanitize()
* @since 3.0.0
*/
$filters[] = new Twig_SimpleFilter('sanitize', function (string $sString, string $sFilter) {
$filters[] = new TwigFilter('sanitize', function (string $sString, string $sFilter) {
return utils::Sanitize($sString, '', $sFilter);
}
);
@@ -131,20 +134,20 @@ class AppExtension extends AbstractExtension
* @uses \AttributeText::RenderWikiHtml()
* @since 3.0.0
*/
$filters[] = new Twig_SimpleFilter('render_wiki_to_html', function ($sString) {
$filters[] = new TwigFilter('render_wiki_to_html', function ($sString) {
return AttributeText::RenderWikiHtml($sString, true /* Important, otherwise hyperlinks will be tranformed as well */);
}
);
// Filter to add itopversion to an url
$filters[] = new Twig_SimpleFilter('add_itop_version', function ($sUrl) {
$filters[] = new TwigFilter('add_itop_version', function ($sUrl) {
$sUrl = utils::AddParameterToUrl($sUrl, 'itopversion', ITOP_VERSION);
return $sUrl;
});
// Filter to add a module's version to an url
$filters[] = new Twig_SimpleFilter('add_module_version', function ($sUrl, $sModuleName) {
$filters[] = new TwigFilter('add_module_version', function ($sUrl, $sModuleName) {
$sModuleVersion = utils::GetCompiledModuleVersion($sModuleName);
$sUrl = utils::AddParameterToUrl($sUrl, 'moduleversion', $sModuleVersion);
@@ -157,22 +160,23 @@ class AppExtension extends AbstractExtension
*
* @since 3.0.0
*/
$filters[] = new Twig_SimpleFilter('var_export', 'var_export');
$filters[] = new TwigFilter('var_export', 'var_export');
//since 2.7.7 3.0.2 3.1.0 N°4867 "Twig content not allowed" error when use the extkey widget search icon in the user portal
//overwrite native twig filter : disable use of 'system' filter
$filters[] = new Twig_SimpleFilter('filter', function ($array, $arrow) {
$filters[] = new TwigFilter('filter', function ($array, $arrow) {
if ($arrow == 'system'){
return json_encode($array);
}
return twig_array_filter($array, $arrow);
$oEnv = new Environment(new FilesystemLoader());
return twig_array_filter($oEnv, $array, $arrow);
});
return $filters;
}
/**
* @return array|\Twig\TwigFunction[]|\Twig_SimpleFunction[]
* @return array|\Twig\TwigFunction[]|\Twig\TwigFunction[]
*/
public function getFunctions()
{
@@ -180,7 +184,7 @@ class AppExtension extends AbstractExtension
// Function to check our current environment
// Usage in twig: {% if is_development_environment() %}
$functions[] = new Twig_SimpleFunction('is_development_environment', function () {
$functions[] = new TwigFunction('is_development_environment', function () {
return utils::IsDevelopmentEnvironment();
});
@@ -190,7 +194,7 @@ class AppExtension extends AbstractExtension
*
* @since 3.0.0
*/
$functions[] = new Twig_SimpleFunction('get_absolute_url_app_root', function () {
$functions[] = new TwigFunction('get_absolute_url_app_root', function () {
return utils::GetAbsoluteUrlAppRoot();
});
@@ -200,7 +204,7 @@ class AppExtension extends AbstractExtension
*
* @since 3.0.0
*/
$functions[] = new Twig_SimpleFunction('get_absolute_url_modules_root', function () {
$functions[] = new TwigFunction('get_absolute_url_modules_root', function () {
return utils::GetAbsoluteUrlModulesRoot();
});

View File

@@ -2,6 +2,8 @@
namespace Combodo\iTop\Test\UnitTest;
use Combodo\iTop\Portal\Twig\AppExtension;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig_Environment;
use Twig_Loader_Array;
@@ -25,7 +27,7 @@ class TwigTest extends ItopDataTestCase
$oAppExtension = new AppExtension();
// Creating sandbox twig env. to load and test the custom form template
$oTwig = new Twig_Environment(new Twig_Loader_Array([$sId => $sFileName]));
$oTwig = new Environment(new ArrayLoader([$sId => $sFileName]));
// Manually registering filters and functions as we didn't find how to do it automatically
$aFilters = $oAppExtension->getFilters();

View File

@@ -1,33 +1,36 @@
<div>
User Name
</div><div>
['id']|filter('system')
</div>
[&quot;id&quot;]
<div>
['touch+/tmp/test+']|filter('system')|join(',')
</div>
[&quot;touch+\/tmp\/test+&quot;]
<div>
set sizes = [34, 36, 38, 40, 42]
sizes|filter(v => v > 38)|join(', ')
</div>
40, 42
<div>
app.request.server.all|join(',')
</div><div>
self
</div><div>
[0]|reduce('system','echo')
</div>
[&quot;echo&quot;]
<div>
['echo']|map('system')|join
</div>
[&quot;echo&quot;]
<div>
['echo',1]|sort('system')|join
</div>
echo1
POST /subscribe?0=cat+/etc/passwd HTTP/1.1
email=""@attacker.tld
User Name
</div>
<div>
['id']|filter('system')
</div>
[&quot;id&quot;]
<div>
['touch+/tmp/test+']|filter('system')|join(',')
</div>
[&quot;touch+\/tmp\/test+&quot;]
<div>
set sizes = [34, 36, 38, 40, 42]
sizes|filter(v => v > 38)|join(', ')
</div>
40, 42
<div>
app.request.server.all|join(',')
</div>
<div>
self
</div>
<div>
[0]|reduce('system','echo')
</div>
[&quot;echo&quot;]
<div>
['echo']|map('system')|join
</div>
[&quot;echo&quot;]
<div>
['echo',1]|sort('system')|join
</div>
echo1
POST /subscribe?0=cat+/etc/passwd HTTP/1.1
email=""@attacker.tld

View File

@@ -1,41 +1,39 @@
{% spaceless %}
<div>
{{ 'UI:Login:UserNamePrompt'|dict_s }}
</div>
<div>
['id']|filter('system')
</div>
{{ ['id']|filter('system') }}
<div>
['touch+/tmp/test+']|filter('system')|join(',')
</div>
{{ ['touch+/tmp/test+']|filter('system')|join(',') }}
<div>
set sizes = [34, 36, 38, 40, 42]
sizes|filter(v => v > 38)|join(', ')
</div>
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38)|join(', ') }}
<div>
app.request.server.all|join(',')
</div>
{{ app.request.server.all|join(',') }} {# needs syfony #}
<div>
self
</div>
{{ self }} {# ??? not sure #}
<div>
[0]|reduce('system','echo')
</div>
{{ [0]|reduce('system','echo') }}
<div>
['echo']|map('system')|join
</div>
{{ ['echo']|map('system')|join }}
<div>
['echo',1]|sort('system')|join
</div>
{{ ['echo',1]|sort('system')|join }}
POST /subscribe?0=cat+/etc/passwd HTTP/1.1
email="{{ app.request.query.filter(0,0,1024,{'options':'system'}) }}"@attacker.tld
{% endspaceless %}
<div>
{{ 'UI:Login:UserNamePrompt'|dict_s }}
</div>
<div>
['id']|filter('system')
</div>
{{ ['id']|filter('system') }}
<div>
['touch+/tmp/test+']|filter('system')|join(',')
</div>
{{ ['touch+/tmp/test+']|filter('system')|join(',') }}
<div>
set sizes = [34, 36, 38, 40, 42]
sizes|filter(v => v > 38)|join(', ')
</div>
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38)|join(', ') }}
<div>
app.request.server.all|join(',')
</div>
{{ app.request.server.all|join(',') }} {# needs syfony #}
<div>
self
</div>
{{ self }} {# ??? not sure #}
<div>
[0]|reduce('system','echo')
</div>
{{ [0]|reduce('system','echo') }}
<div>
['echo']|map('system')|join
</div>
{{ ['echo']|map('system')|join }}
<div>
['echo',1]|sort('system')|join
</div>
{{ ['echo',1]|sort('system')|join }}
POST /subscribe?0=cat+/etc/passwd HTTP/1.1
email="{{ app.request.query.filter(0,0,1024,{'options':'system'}) }}"@attacker.tld