Skip to content

DynamicStorage.findProductRewriteByRequestPath() lacks entity_type filtering, causing CMS pages to be treated as products in category URLs #39996

Open
@rbouma

Description

@rbouma

Preconditions and environment

  • Magento version: 2.4.x (affects all versions with DynamicStorage)
  • Environment: Any Magento installation with:
    • CMS pages with URL rewrites (e.g., privacy-policy-cookie-restriction-mode)
    • Categories with URL rewrites (e.g., schroeven)
    • Catalog URL Rewrite module enabled
  • Modules: Default Magento installation (no third-party modules required to reproduce)
  • URL Structure: Category URLs with CMS page identifiers as suffixes

Steps to reproduce

Prerequisites Setup:

  1. Create Category:

    • Create a category with URL key schroeven
    • Note the category entity_id (e.g., ID = 48)
  2. Create CMS Page:

    • Create a CMS page with identifier privacy-policy-cookie-restriction-mode
    • Note the CMS page entity_id (e.g., ID = 4)
  3. Create Product with Matching ID:

    • Create a product with entity_id = 4 (same as CMS page ID)
    • This is crucial - the product ID must match the CMS page ID
    • Assign this product to the schroeven category (ID 48)
    • Ensure the product is enabled and visible
  4. System Maintenance:

    bin/magento indexer:reindex
    bin/magento cache:clean
    bin/magento cache:flush

Reproduction Steps:

  1. Verify URL rewrites exist in database:

    -- Should show CMS page rewrite
    SELECT * FROM url_rewrite WHERE request_path = 'privacy-policy-cookie-restriction-mode';
    -- Should show category rewrite  
    SELECT * FROM url_rewrite WHERE request_path = 'schroeven.html';
  2. Navigate to malformed URL: /schroeven/privacy-policy-cookie-restriction-mode

  3. Observe the response

Why This Setup Is Required:

  • DynamicStorage extracts privacy-policy-cookie-restriction-mode
  • Finds CMS page with entity_id=4 (but treats it as product_id=4)
  • Checks if product_id=4 can be shown in category_id=48 using ProductResource::canBeShowInCategory(4, 48)
  • Since product ID 4 IS assigned to category 48, the check passes
  • DynamicStorage creates: /cms/page/view/page_id/4/category/48

Expected result

The URL /schroeven/privacy-policy-cookie-restriction-mode should return HTTP 404 because:

  • It's not a valid product URL pattern
  • It's not a valid category URL
  • It's not a valid CMS page URL
  • This malformed URL pattern should not resolve to any content

Actual result

The URL /schroeven/privacy-policy-cookie-restriction-mode returns HTTP 200 and serves the CMS page content with category context.

Root Cause Analysis:
The issue occurs in vendor/magento/module-catalog-url-rewrite/Model/Storage/DynamicStorage.php:

  1. Method findProductRewriteByRequestPath() extracts privacy-policy-cookie-restriction-mode from the URL
  2. It queries url_rewrite table WITHOUT filtering by entity_type
  3. Query finds the CMS page URL rewrite (entity_type = 'cms-page')
  4. DynamicStorage treats the CMS page as if it's a product
  5. Creates malformed target path: /cms/page/view/page_id/X/category/Y
  6. UrlRewrite Router serves this as valid content

Problematic Code (line ~147):

$productFromDb = $this->connection->fetchRow($this->prepareSelect($data));

The $data array only contains request_path and store_id - missing entity_type = 'product' filter.

Additional information

Database Evidence:

-- This query shows the CMS page that gets incorrectly matched
SELECT * FROM url_rewrite WHERE request_path = 'privacy-policy-cookie-restriction-mode';
-- Returns: entity_type='cms-page', entity_id=4, target_path='cms/page/view/page_id/4'

Debug Logs:
When processing /schroeven/privacy-policy-cookie-restriction-mode:

  1. UrlRewrite Router calls DynamicStorage
  2. DynamicStorage finds CMS page data (should only find products)
  3. Router creates Forward action to /cms/page/view/page_id/4/category/48

Affected Code Files:

  • vendor/magento/module-catalog-url-rewrite/Model/Storage/DynamicStorage.php (lines 140-200)
  • Method: findProductRewriteByRequestPath()

Suggested Fix:
Add entity_type filtering in the database query:

// Before querying for "product" data, ensure we only get products
$data[UrlRewrite::ENTITY_TYPE] = 'product';
$productFromDb = $this->connection->fetchRow($this->prepareSelect($data));

Security Impact: None - this is a URL routing issue, not a security vulnerability.

Performance Impact: Minimal - affects only specific URL patterns.

Workaround: Plugin on UrlRewrite Router to block malformed URL patterns before they reach DynamicStorage.

Release note

Fixed DynamicStorage URL rewrite query to properly filter by entity_type, preventing CMS pages from being incorrectly processed as products in category URL contexts.

Triage and priority

  • 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”.

Activity

m2-assistant

m2-assistant commented on Jun 13, 2025

@m2-assistant

Hi @rbouma. 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.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 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

m2-assistant commented on Jun 13, 2025

@m2-assistant

Hi @rbouma! 👋
Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

m2-assistant

m2-assistant commented on Jun 13, 2025

@m2-assistant

Hi @rbouma! 👋
Thank you for joining. Please accept team invitation 👉 here 👈 and add your comment one more time.

self-assigned this
on Jun 13, 2025
m2-assistant

m2-assistant commented on Jun 13, 2025

@m2-assistant

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: 👇

  • 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.
engcom-Bravo

engcom-Bravo commented on Jun 16, 2025

@engcom-Bravo
Contributor

Hi @rbouma,

Thanks for your reporting and collaboration.

We have tried to reproduce the issue in Latest 2.4-develop instance and we are not able to reproduce the issue.Kindly refer the screenshots.

Image

The URL /schroeven/privacy-policy-cookie-restriction-mode returns HTTP 404.Could you please let us know if we are missing anything.

Thanks.

moved this from Ready for Confirmation to Needs Update in Issue Confirmation and Triage Boardon Jun 16, 2025

16 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: SEOComponent: UrlIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Reported on 2.4.xIndicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

Type

No type

Projects

Status

Ready for Development

Milestone

No milestone

Relationships

None yet

    Participants

    @rbouma@engcom-Bravo@github-jira-sync-bot

    Issue actions

      DynamicStorage.findProductRewriteByRequestPath() lacks entity_type filtering, causing CMS pages to be treated as products in category URLs · Issue #39996 · magento/magento2