magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Warning: DOMXPath::query(): Recursion limit exceeded

Open matteorossi-thespacesm opened this issue 1 year ago • 3 comments

Preconditions and environment

Magento 2.4.5-p10

Steps to reproduce

Import a large number of products, e.g. 2000 products

Expected result

Import succeds without warnings

Actual result

Exception: Warning: DOMXPath::query(): Recursion limit exceeded in vendor/magento/framework/Validator/HTML/ConfigurableWYSIWYGValidator.php on line 125

Additional information

Magento\Framework\Validator\HTML\ConfigurableWYSIWYGValidator

method validateConfigured(\DOMXPath $xpath) is changed as:

//Validating tags $this->allowedTags = array_merge($this->allowedTags, ["body", "html"]); $found = $xpath->query( '//*[' . implode( ' and ', array_map( function (string $tag): string { return "name() != '$tag'"; }, $this->allowedTags ) ) .']' );

$this->allowedTags is charged with two items "body" and "html" every time validateConfigured is called, so in the end xpath recursion limit is exceeded and such warning appears. Maybe such bug is not triggered in test because recursion limit is quite high. It is MANDATORY to revert method to the previous version:

//Validating tags $this->allowedTags = array_merge($this->allowedTags, ["body", "html"]); $found = $xpath->query( '//*[' . implode( ' and ', array_map( function (string $tag): string { return "name() != '$tag'"; }, $this->allowedTags ) ) .']' );

Release note

No response

Triage and priority

  • [X] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

matteorossi-thespacesm avatar Oct 17 '24 12:10 matteorossi-thespacesm

Hi @matteorossi-thespacesm. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Oct 17 '24 12:10 m2-assistant[bot]

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Oct 17 '24 12:10 m2-assistant[bot]

Hi @matteorossi-thespacesm,

Thanks for your reporting and collaboration.

We have verified the issue in Latest 2.4-develop instance and the issue is not reproducible.Kindly refer the attached video.

https://github.com/user-attachments/assets/6c9a61e3-d5c3-4a67-af4b-c9eab3c9991c

Import was done successfully without warnings.

Kindly recheck the issue in Latest 2.4-develop instance and elaborate the steps to reproduce if the issue is still reproducible.

Thanks.

engcom-Bravo avatar Oct 18 '24 07:10 engcom-Bravo

Hi @matteorossi-thespacesm,

This issue is being closed since it has not been updated in a long time.Please feel free to reopen or raise a new ticket if the issue still exists.

Thanks.

engcom-Bravo avatar Oct 29 '24 04:10 engcom-Bravo

I just updated to 2.4-5-p10 and also have the issue

dsengsqli avatar Oct 29 '24 12:10 dsengsqli

Update: the issue is present on every patched version, including magento 2.4.7-p3.

Such issue is caused by a code fragment as reported above. If anyone is logging $this->allowedTags field content, one will see such field growing due to duplicated and multiplied "body" and "html" array items, at the point that "recursion limit exceeded" warning appears if one has xdebug extension installed and configured. Otherwise, it is possibile to experience SEGFAULT errors.

matteorossi-thespacesm avatar Oct 29 '24 12:10 matteorossi-thespacesm

Can confirm the issue on Adobe Commerce ver. 2.4.6-p8.

@engcom-Bravo To reproduce try to import +5k products.

srepn avatar Oct 30 '24 09:10 srepn

The issue is this piece of code: $this->allowedTags = array_merge($this->allowedTags, ["body", "html"]);

Located in vendor/magento/framework/Validator/HTML/ConfigurableWYSIWYGValidator.php:113

It keeps putting the 'body' and 'html' in the array, which becomes very big.

Replace it with this:

if (!in_array('body', $this->allowedTags)) {
    $this->allowedTags ['body'] = 'body';
}

if (!in_array('html', $this->allowedTags)) {
    $this->allowedTags ['html'] = 'html';
}

That should solve the problem.

ddonselaar avatar Nov 05 '24 14:11 ddonselaar

I can confirm that problem is still there in 2.4.6-p8 - and that small changes from @ddonselaar fixes it

php4umagento avatar Nov 18 '24 14:11 php4umagento

I just can't understand why the approach prior to 2.4.7-p3 was changed. Can't we just revert the ConfigurableWYSIWYGValidator back to the following:

    private function validateConfigured(\DOMXPath $xpath): void
    {
        //Validating tags
        $found = $xpath->query(
            '//*['
                . implode(
                    ' and ',
                    array_map(
                        function (string $tag): string {
                            return "name() != '$tag'";
                        },
                        array_merge($this->allowedTags, ['body', 'html'])
                    )
                )
                .']'
        );
...

It seems to me that array_merge($this->allowedTags, ['body', 'html']) handles this properly, doesn't it?

magnogaspar avatar Nov 28 '24 14:11 magnogaspar

The issue is this piece of code: $this->allowedTags = array_merge($this->allowedTags, ["body", "html"]);

Located in vendor/magento/framework/Validator/HTML/ConfigurableWYSIWYGValidator.php:113

It keeps putting the 'body' and 'html' in the array, which becomes very big.

Replace it with this:

if (!in_array('body', $this->allowedTags)) {
    $this->allowedTags ['body'] = 'body';
}

if (!in_array('html', $this->allowedTags)) {
    $this->allowedTags ['html'] = 'html';
}

That should solve the problem.

Adding a conditional statement would make performance worse to me.

matteorossi-thespacesm avatar Nov 28 '24 14:11 matteorossi-thespacesm

I just can't understand why the approach prior to 2.4.7-p3 was changed. Can't we just revert the ConfigurableWYSIWYGValidator back to the following:

    private function validateConfigured(\DOMXPath $xpath): void
    {
        //Validating tags
        $found = $xpath->query(
            '//*['
                . implode(
                    ' and ',
                    array_map(
                        function (string $tag): string {
                            return "name() != '$tag'";
                        },
                        array_merge($this->allowedTags, ['body', 'html'])
                    )
                )
                .']'
        );
...

It seems to me that array_merge($this->allowedTags, ['body', 'html']) handles this properly, doesn't it?

It does. IMHO method code was modified to improve performance, deleting array_merge call. Unfortunately it has such a side effect, that allowedTags array explodes.

matteorossi-thespacesm avatar Nov 28 '24 14:11 matteorossi-thespacesm

@matteorossi-thespacesm I see, but if the point is performance, I think adding 'body' and 'html' to the class constructor would be a better approach.

Something like:

    public function __construct(
        array $allowedTags,
        array $allowedAttributes = [],
        array $attributesAllowedByTags = [],
        array $attributeValidators = [],
        array $tagValidators = []
    ) {
        if (empty(array_filter($allowedTags))) {
            throw new \InvalidArgumentException('List of allowed HTML tags cannot be empty');
        }
        $this->allowedTags = array_unique(array_merge($allowedTags, ['body', 'html']));
        $this->allowedAttributes = array_unique($allowedAttributes);
...

Anyway: I'm facing this issue and I had to use a patch to fix it. It needs to be reopened @engcom-Bravo

magnogaspar avatar Nov 28 '24 18:11 magnogaspar

@engcom-Bravo, @nathanjosiah, @ihor-sviziev: can we get this issue re-openend? A colleague of mine ran into this problem as well on Magento 2.4.6-p8

It seems like this commit (https://github.com/magento/magento2/commit/42577bc5ab88ce74706d16356825440c9cdce9ba) causes the issue, which was introduced in the latest security patches.

I don't have steps to reproduce yet, if somebody in this thread has clear steps to reproduce and would like to post them, that would be appreciated :)

hostep avatar Dec 20 '24 13:12 hostep

This is fixed within the upcoming February release versions in scope of internal ticket AC-13493 and related backports.

nathanjosiah avatar Dec 20 '24 17:12 nathanjosiah

@nathanjosiah: ah that's good to know! Have those commits already been pushed to github? If yes, can you point to them? So we can patch our projects. My search for "AC-13493" on github results in nothing at the moment.

hostep avatar Dec 23 '24 09:12 hostep

Magento 2.4.7-p4 (and the other security releases) which were released today comes with this change included:

--- vendor_orig/magento/framework/Validator/HTML/ConfigurableWYSIWYGValidator.php	2025-02-11 13:33:15
+++ vendor/magento/framework/Validator/HTML/ConfigurableWYSIWYGValidator.php	2025-01-28 14:24:34
@@ -110,7 +110,8 @@
     private function validateConfigured(\DOMXPath $xpath): void
     {
         //Validating tags
-        $this->allowedTags = array_merge($this->allowedTags, ["body", "html"]);
+        $this->allowedTags['body'] = 'body';
+        $this->allowedTags['html'] = 'html';
         $found = $xpath->query(
             '//*['
             . implode(
diff -ur -N vendor_orig/magento/framework/Test/Unit/Validator/HTML/ConfigurableWYSIWYGValidatorTest.php vendor/magento/framework/Test/Unit/Validator/HTML/ConfigurableWYSIWYGValidatorTest.php
--- vendor_orig/magento/framework/Test/Unit/Validator/HTML/ConfigurableWYSIWYGValidatorTest.php	2025-02-11 13:33:15
+++ vendor/magento/framework/Test/Unit/Validator/HTML/ConfigurableWYSIWYGValidatorTest.php	2025-01-28 14:24:34
@@ -17,6 +17,49 @@
 class ConfigurableWYSIWYGValidatorTest extends TestCase
 {
     /**
+     * @var ConfigurableWYSIWYGValidator
+     */
+    private ConfigurableWYSIWYGValidator $validator;
+
+    protected function setUp(): void
+    {
+        $allowedTags = ['p', 'a', 'div'];
+        $allowedAttributes = ['href', 'title'];
+        $attributesAllowedByTags = ['a' => ['href', 'title']];
+        $attributeValidators = [];
+        $tagValidators = [];
+
+        $this->validator = new ConfigurableWYSIWYGValidator(
+            $allowedTags,
+            $allowedAttributes,
+            $attributesAllowedByTags,
+            $attributeValidators,
+            $tagValidators
+        );
+    }
+
+    /**
+     * Test that the validator error message does not contain duplicated tags body and html.
+     *
+     * @return void
+     * @throws ValidationException
+     */
+    public function testValidateThrowsExceptionForDisallowedTags()
+    {
+        $this->expectException(ValidationException::class);
+        $this->expectExceptionMessageMatches('/^(Allowed HTML tags are: p, a, div, body, html)*$/');
+
+        $validHtml = '<html><body>test1</body></html>';
+        $this->validator->validate($validHtml);
+        $validHtml = '<html><body>test2</body></html>';
+        $this->validator->validate($validHtml);
+        $validHtml = '<html><body>test3</body></html>';
+        $this->validator->validate($validHtml);
+        $invalidHtml = '<html><body><script>alert("XSS")</script></body></html>';
+        $this->validator->validate($invalidHtml);
+    }
+
+    /**
      * Configurations to test.
      *
      * @return array

I'm assuming that will fix the issue discussed here...

hostep avatar Feb 11 '25 13:02 hostep

Here's the merge commit for the people interested, which landed in the 2.4-develop branch today: https://github.com/magento/magento2/commit/87d012e50c28f448ef1b33ddf50e85d9b3d0a72e

hostep avatar Feb 17 '25 14:02 hostep