magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

Scope bug in Catalog URL resource (_getCategories) #38393

Open pmzandbergen opened 9 months ago

pmzandbergen commented 9 months ago

Bug The following condition is obviously invalid: https://github.com/magento/magento2/blob/03621bbcd75cbac4ffa8266a51aa2606980f4830/app/code/Magento/Catalog/Model/ResourceModel/Url.php#L415

Explanation The resulting query is:

SELECT
`main*table\`.`entity*id`,
`main*table\`.`parent*id`,
`main_table\`.`level`,
IF(c.value*id > 0, c.value, c.value) AS `is*active`,
`main_table\`.`path`
FROM
`catalog*category_entity\` AS `main*table`
LEFT JOIN `catalog*category_entity_int\` AS `d` ON d.attribute_id = 46 AND d.store_id = 0 AND d.entity_id = main_table.entity*id
LEFT JOIN `catalog*category_entity_int\` AS `c` ON c.attribute_id = 46 AND c.store_id = 3 AND c.entity_id = main_table.entity*id
WHERE (main*table.entity*id IN(...))

Attribute is_active\ is joined twice on both the store (c\) and default scope (d\). If there is no value available on the store scope (c.value\) the default scope (d.value) should be used instead.

Solution The condition from line 415:

$isActiveExpr = $connection->getCheckSql('c.value_id > 0', 'c.value', 'c.value');

Should be changed to:

$isActiveExpr = $connection->getCheckSql('c.value_id IS NOT NULL', 'c.value', 'd.value');

 

Note: This bug might not be affecting any Magento code (depends if the is_active flag is actually being used somewhere).

m2-assistant[bot] commented 9 months ago

Hi @pmzandbergen. 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. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

andrewbess commented 9 months ago

Hello @sidolov I've checked fixes from this PR and researched history of the fixed logic. This logic is more than 12 years old. It looks like this logic was migrated from Magento1. I am sure this fixes can affect a lot of Magento-projects, 3rd-party extensions, etc, which may be depend on results of it. So, I think we need to discuss it on the guild. Could you please create an internal jira issue to discuss it on the necessary guild.

Thank you in advance.

m2-assistant[bot] commented 9 months ago

Hi @engcom-Hotel. 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:

engcom-Hotel commented 9 months ago

Hello @pmzandbergen,

Thanks for the report and collaboration!

We have verified the issue by looking into the codebase:

https://github.com/magento/magento2/blob/03621bbcd75cbac4ffa8266a51aa2606980f4830/app/code/Magento/Catalog/Model/ResourceModel/Url.php#L415

This statement does not make any sense if in any case, we need to use c.value. Either we need to remove the condition or replace the condition as below:

$isActiveExpr = $connection->getCheckSql('c.value_id > 0', 'c.value', 'd.value');

Hence confirming the issue.

Thanks

github-jira-sync-bot commented 9 months ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-11011 is successfully created for this GitHub issue.

m2-assistant[bot] commented 9 months ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

andrewbess commented 9 months ago

This statement does not make any sense if in any case, we need to use c.value. Either we need to remove the condition or replace the condition as below:

$isActiveExpr = $connection->getCheckSql('c.value_id > 0', 'c.value', 'd.value');

Hence confirming the issue.

Thanks

Hello @engcom-Hotel

I am not sure we need to change results of the SQL query by we-self without discussing with product's owners or platform's architects. This logic is 12 years old and changes can provoke unexpected problem for a lot of projects after platform upgrade. I would propose remove this redundant condition. Anyway, you need to discuss it internally in Adobe (the jira issue already created). Let's do it.

Thank you in advance.

cc @maghamed

pmzandbergen commented 9 months ago

Hello @pmzandbergen,

Thanks for the report and collaboration!

We have verified the issue by looking into the codebase:

https://github.com/magento/magento2/blob/03621bbcd75cbac4ffa8266a51aa2606980f4830/app/code/Magento/Catalog/Model/ResourceModel/Url.php#L415

This statement does not make any sense if in any case, we need to use c.value. Either we need to remove the condition or replace the condition as below:

$isActiveExpr = $connection->getCheckSql('c.value_id > 0', 'c.value', 'd.value');

Hence confirming the issue.

Thanks

Hi @engcom-Hotel,

My PR is using a different condition (IS NULL instead of > 0) with a small but significant difference outcome:

c.value d.value > 0 IS NULL Comment
NULL 0 0 0 OK
NULL 1 1 1 OK
0 0 0 0 OK
0 1 1 0 Unexpected result when using > 0
1 0 1 1 OK
1 1 1 1 OK

Please ensure the correct fix is used (see my PR @ https://github.com/magento/magento2/pull/38394 )