Commit Graph

6190 Commits

Author SHA1 Message Date
Fabien Potencier bc22e38ce6 Fix bad merge 2023-12-20 20:34:40 +01:00
Fabien Potencier e6a98a000f Fix test without any assertions 2023-12-20 20:34:04 +01:00
Fabien Potencier fca89954f1 Simplify tests 2023-12-20 20:34:04 +01:00
Fabien Potencier 204cd6f412 Merge branch '2.x' into 3.x
* 2.x:
  Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source
2023-12-19 08:19:15 +01:00
Fabien Potencier a4974b29a0 feature #3893 Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source (YSaxon)
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source

This is needed to patch some downstream vulnerabilities which I won't describe here.
I wrote `@fabpot` an email about this with more details a few weeks ago.

Generally the Sandbox can be enabled for a given template in either of two ways:
* Globally
* Including the template from another template in which we use the sandbox tag or parameter

This pull request adds a third way
* Using a SourcePolicy to selectively sandbox templates based on their Source object

Commits
-------

a18da161 Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source
2023-12-19 08:18:38 +01:00
Yaakov Saxon a18da1614a Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source 2023-12-19 08:18:35 +01:00
Fabien Potencier d2f493f67c minor #3941 add docs & links (lacpandore)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

add docs & links

Commits
-------

dd24d5bf add docs & links
2023-12-14 08:17:08 +01:00
Louis-Arnaud dd24d5bfd2 add docs & links 2023-12-14 08:17:06 +01:00
Fabien Potencier 804cb1b85b feature #3883 Deprecate internal extension functions in favor of methods on the extension classes (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Deprecate internal extension functions in favor of methods on the extension classes

~~PoC on a simple extension for now.~~

Commits
-------

aa7c454e Fix CS
54d34b96 Move functions for CoreExtension
72071e8e Move functions for EscaperExtension
196e91dd Move functions for StringLoaderExtension
d4d01603 Move functions for DebugExtension
69a89e0e Move functions for MarkdownExtension
e07e9b5b Move functions for InkyExtension
bff189f1 Move functions for CssInlinerExtension
6a18cda5 Move functions for HtmlExtension
16abb69d Deprecate internal extension functions in favor of methods on the extension classes
2023-12-10 20:42:55 +01:00
Fabien Potencier aa7c454e2d Fix CS 2023-12-10 20:34:32 +01:00
Fabien Potencier 54d34b969b Move functions for CoreExtension 2023-12-10 20:13:10 +01:00
Fabien Potencier 72071e8ece Move functions for EscaperExtension 2023-12-10 11:49:57 +01:00
Fabien Potencier 196e91dd72 Move functions for StringLoaderExtension 2023-12-10 11:38:36 +01:00
Fabien Potencier d4d016034c Move functions for DebugExtension 2023-12-10 11:35:00 +01:00
Fabien Potencier 69a89e0e77 Move functions for MarkdownExtension 2023-12-09 18:39:03 +01:00
Fabien Potencier e07e9b5be3 Move functions for InkyExtension 2023-12-09 18:39:03 +01:00
Fabien Potencier bff189f19a Move functions for CssInlinerExtension 2023-12-09 18:39:03 +01:00
Fabien Potencier 6a18cda5aa Move functions for HtmlExtension 2023-12-09 18:39:01 +01:00
Fabien Potencier 16abb69d12 Deprecate internal extension functions in favor of methods on the extension classes 2023-12-09 18:11:17 +01:00
Fabien Potencier d602a55603 Tweak deprecated docs 2023-12-09 17:48:07 +01:00
Fabien Potencier f83b415386 Merge branch '2.x' into 3.x
* 2.x:
  update Blackfire documentation URL
  Prepare the 2.15.6 release
  Update CHANGELOG
2023-12-09 12:45:27 +01:00
Fabien Potencier ec57248c14 minor #3929 update Blackfire documentation URL (Ca-Jou)
This PR was submitted for the 3.x branch but it was merged into the 2.x branch instead.

Discussion
----------

update Blackfire documentation URL

Update Blackfire documentation's URL in README file

Commits
-------

ed2cfbd6 update Blackfire documentation URL
2023-12-09 12:44:06 +01:00
Ca-Jou ed2cfbd6f0 update Blackfire documentation URL 2023-12-09 12:44:01 +01:00
Fabien Potencier aeeec9a5e9 Bump version 2023-11-21 19:55:42 +01:00
Fabien Potencier 9d15f0ac07 Prepare the 3.8.0 release v3.8.0 2023-11-21 19:54:41 +01:00
Fabien Potencier b65ccdfb7d Update CHANGELOG 2023-11-21 19:53:58 +01:00
Fabien Potencier ad637405a8 Prepare the 2.15.6 release v2.15.6 2023-11-21 18:34:48 +01:00
Fabien Potencier a41a0e7fa5 Update CHANGELOG 2023-11-21 18:34:15 +01:00
Fabien Potencier 78e1cdd84c bug #3903 Fix timezone fallback to CoreExtension in IntlExtension (keulinho)
This PR was merged into the 3.x branch.

Discussion
----------

Fix timezone fallback to CoreExtension in IntlExtension

This is probably a regression from #3844

Refer to my comment on the original MR: https://github.com/twigphp/Twig/pull/3844#issuecomment-1792464040

Commits
-------

144c4dac Fix timezone fallback to CoreExtension in IntlExtension
2023-11-21 18:27:48 +01:00
Fabien Potencier 2f73ee35ca minor #3900 Minor rename of Sandbox test: functions->methods (YSaxon)
This PR was merged into the 3.x branch.

Discussion
----------

Minor rename of Sandbox test: functions->methods

Commits
-------

62732646 Minor rename of SandboxTest functions->methods
2023-11-21 17:18:42 +01:00
Fabien Potencier ff98249d0b minor #3911 Add PHP 8.3 to the CI (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Add PHP 8.3 to the CI

Commits
-------

a04cc88a Add PHP 8.3 to the CI
2023-11-21 16:50:45 +01:00
Fabien Potencier a04cc88ad7 Add PHP 8.3 to the CI 2023-11-21 16:47:02 +01:00
Fabien Potencier e6ba716520 minor #3908 Bump dependencies (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Bump dependencies

Commits
-------

fdb9d9ed Bump dependencies
2023-11-21 16:45:18 +01:00
Fabien Potencier fdb9d9eddf Bump dependencies 2023-11-21 15:02:01 +01:00
Fabien Potencier 6f74d5b7d6 minor #3909 Fix tests and CS (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Fix tests and CS

Commits
-------

4be326ae Fix tests and CS
2023-11-21 15:01:23 +01:00
Fabien Potencier 4be326aeeb Fix tests and CS 2023-11-21 14:57:52 +01:00
Jonas Elfering 144c4dac7f Fix timezone fallback to CoreExtension in IntlExtension
This is probably a regression from #3844
2023-11-03 15:14:20 +01:00
Yaakov Saxon 62732646c8 Minor rename of SandboxTest functions->methods 2023-10-27 12:15:56 -04:00
Fabien Potencier 9f42a76306 minor #3835 Removed duplicate sentence in macro scoping (virtualize)
This PR was merged into the 3.x branch.

Discussion
----------

Removed duplicate sentence in macro scoping

Commits
-------

73f5cad8 Remove duplicate sentence in macro scoping
2023-10-26 17:56:31 -07:00
Fabien Potencier b285fccd59 bug #3830 Catch errors thrown during template rendering (richards-square)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Catch errors thrown during template rendering

Some errors, like not providing a function the proper number of arguments or division by zero, extend from `\Error` rather than `\Exception`. This PR catches these types of errors during template rendering and throws a `RuntimeError` in order to provide better debugging information.

Commits
-------

85bf01b4 Catch errors thrown during template rendering
2023-10-26 17:55:51 -07:00
Drew Richards 85bf01b4ab Catch errors thrown during template rendering 2023-10-26 17:55:48 -07:00
Fabien Potencier 9a602c7532 feature #3695 Add @codeCoverageIgnore to untestable compiled methods (markhuot)
This PR was merged into the 3.x branch.

Discussion
----------

Add `@codeCoverageIgnore` to untestable compiled methods

I've been experimenting with adding my compiled templates to my code coverage reports and it works largely as expected. Templates that get executed return percentages accurate to the number of lines the tests actually cover. The only exception are the "meta" methods on the compiled template that aren't necessarily called by the tests.

This PR adds ``@codeCoverageIgnore`` comments to the compiled template for all non-display methods so the coverage report only lists lines from `doDisplay`.

I'm assuming that I will need to update some of the Twig tests to account for these new comments, but am curious if you'd be open to this change.

An example screenshot below. The last line shows line 46 out of the compiled template is never executed. The compiled template's `getDebugInfo` even correctly informs me that line 46 maps to line 4 in my `.twig` so I'll work on updating the line numbering next.

![image](https://user-images.githubusercontent.com/48975/166098890-fbaf99ad-9aa4-4430-9f82-050f482a6777.png)

Commits
-------

1cf610bd Add `@codeCoverageIgnore` to untestable compiled methods
2023-10-26 17:52:38 -07:00
Fabien Potencier 065634f498 bug #3844 Fix IntlExtension::formatDateTime use of date formatter prototype (drjayvee)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Fix IntlExtension::formatDateTime use of date formatter prototype

See https://github.com/twigphp/intl-extra/pull/6 for more details

Commits
-------

c75762c3 Fix IntlExtension::formatDateTime use of date formatter prototype
2023-10-26 17:47:32 -07:00
Jeroen Versteeg c75762c354 Fix IntlExtension::formatDateTime use of date formatter prototype 2023-10-26 17:47:31 -07:00
Fabien Potencier 248855b34b Merge branch '2.x' into 3.x
* 2.x:
  Fix premature loop exit in Security Policy lookup of allowed methods/properties
2023-10-26 17:38:13 -07:00
Fabien Potencier 02262dee80 bug #3873 Fix premature loop exit in Security Policy lookup of allowed methods/properties (YSaxon)
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Fix premature loop exit in Security Policy lookup of allowed methods/properties

The current security policy logic exits too soon when checking permissions for allowed classes and their methods/properties, causing false negatives in situations involving classes related by inheritance.

Consider the following configuration:

```
'methods' => [
  'App\BasicCollection' => ['sortAlphabetically'],
  'App\AdvancedCollection'=> ['sortByTimestamp'],
],
```
where `AdvancedCollection` is a subclass of `BasicCollection`, and `mylist` is an instance of `AdvancedCollection`

If you try to call `{{ mylist.sortByTimestamp() }}`, the current code will first match `mylist` against `App\BasicCollection`. Since `sortByTimestamp` is not an allowed method for `App\BasicCollection`, the code will exit the loop and incorrectly deny access. It will never get to checking `App\AdvancedCollection`.

Note that reordering classes in the config can't solve this issue. If you flipped the order, then it would fail for `{{ mylist.sortAlphabetically() }}` instead.

This pull request fixes the issue by only exiting the loop early when both the class and method/property match.

Commits
-------

5e1838db Fix premature loop exit in Security Policy lookup of allowed methods/properties
2023-10-26 17:37:16 -07:00
Yaakov Saxon 5e1838dbca Fix premature loop exit in Security Policy lookup of allowed methods/properties 2023-10-26 17:37:13 -07:00
Fabien Potencier 415f4cc3ee minor #3896 Compile Elvis operator into Elvis operator ?: (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Compile Elvis operator into Elvis operator `?:`

When using ternary operator without "then" part, the "condition" part is evaluated twice, which is inconsistent with how PHP works.
The Twig template `A ?: B` is currently compiled as `A ? A : B` in PHP. This PR change it to `A ?: B`.

If `A` is a complex expression, it improves performance to only execute the expression once.
If `A` is has a side effect (like updating a variable), the expression being executed twice could result in a bug. ([example in PHP](https://3v4l.org/LWZLR))

Example with ``@WebProfiler`/Collector/form.html.twig` [line 9](https://github.com/symfony/symfony/blob/e6d1ed4edb5ae197ec7d25ddaf64cfa456229504/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig#L9) (see deep [diff](https://www.diffchecker.com/jOm3dE13/))

```twig
{{ collector.data.nb_errors ?: collector.data.forms|length }}
```

Previous compilation:
```php
echo twig_escape_filter($this->env, (((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })())) ? ((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })())) : (twig_get_attribute($this->env, $this->source, (isset($context["collector"]) || array_key_exists("collector", $context) ? $context["collector"] : (function () { throw new RuntimeError('Variable "collector" does not exist.', 9, $this->source); })()), "countDefines", [], "any", false, false, false, 9))), "html", null, true);
```

After this change:
```php
echo twig_escape_filter($this->env, ((isset($context["error_count"]) || array_key_exists("error_count", $context) ? $context["error_count"] : (function () { throw new RuntimeError('Variable "error_count" does not exist.', 9, $this->source); })()) ?: twig_get_attribute($this->env, $this->source, (isset($context["collector"]) || array_key_exists("collector", $context) ? $context["collector"] : (function () { throw new RuntimeError('Variable "collector" does not exist.', 9, $this->source); })()), "countDefines", [], "any", false, false, false, 9)), "html", null, true);
```

Commits
-------

fb0d7495 Compile Elvis operator with Elvis operator
2023-10-22 14:57:13 +02:00
Fabien Potencier 758018b79b minor #3897 Compile starts/ends with using str_starts_with and str_ends_with (GromNaN)
This PR was merged into the 3.x branch.

Discussion
----------

Compile `starts/ends with` using `str_starts_with` and `str_ends_with`

Since we now require PHP 8.0 polyfill #3884, we can use `str_starts_with` and `str_ends_with` to compile `starts with` and `ends with` expressions.

Example with `bootstrap_4_layout.html.twig` [line 6-7](https://github.com/symfony/symfony/blob/e6d1ed4edb5ae197ec7d25ddaf64cfa456229504/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L6-L7) (see deep [diff](https://www.diffchecker.com/18CtPeja/)):

```twig
    {%- set prepend = not (money_pattern starts with '{{') -%}
    {%- set append = not (money_pattern ends with '}}') -%}
```

Previous compilation:
```php
// line 6
$context["prepend"] =  !(is_string($__internal_compile_0 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 6, $this->source); })())) && is_string($__internal_compile_1 = "{{") && ('' === $__internal_compile_1 || 0 === strpos($__internal_compile_0, $__internal_compile_1)));
// line 7
$context["append"] =  !(is_string($__internal_compile_2 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 7, $this->source); })())) && is_string($__internal_compile_3 = "}}") && ('' === $__internal_compile_3 || $__internal_compile_3 === substr($__internal_compile_2, -strlen($__internal_compile_3))));
```

With this change:
```php
// line 6
$context["prepend"] =  !(is_string($__internal_compile_0 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 6, $this->source); })())) && is_string($__internal_compile_1 = "{{") && str_starts_with($__internal_compile_0, $__internal_compile_1));
// line 7
$context["append"] =  !(is_string($__internal_compile_2 = (isset($context["money_pattern"]) || array_key_exists("money_pattern", $context) ? $context["money_pattern"] : (function () { throw new RuntimeError('Variable "money_pattern" does not exist.', 7, $this->source); })())) && is_string($__internal_compile_3 = "}}") && str_ends_with($__internal_compile_2, $__internal_compile_3));
```

Commits
-------

30b5a566 Compile starts/ends with using PHP8 functions str_starts/ends_with
2023-10-22 14:56:22 +02:00
Jérôme Tamarelle 30b5a56625 Compile starts/ends with using PHP8 functions str_starts/ends_with 2023-10-21 19:44:29 +02:00