Rewrite Rules: Require a path-segment boundary when stripping the home path in WP::parse_request() - #13373
Conversation
…e path in WP::parse_request()
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
WP::parse_request()strips the site's home path off the front ofPATH_INFO/REQUEST_URI/PHP_SELFusing a regex built withsprintf( '|^%s|i', preg_quote( $home_path, '|' ) ). Because this only anchors on the start of the string and never checks what comes right after the home path, it matches any path that merely begins with the same characters as the home path, not just paths that are actually nested under it.For a site installed in a subdirectory named
wp, a request forwp-json/wc/v1/products(a REST API route) gets the literal substringwpstripped off the front, leaving the mangled path-json/wc/v1/products— breaking REST API routing on such installs. The same root cause affects any endpoint whose name happens to share a prefix with the install's subdirectory (e.g. asitemap.xmlrequest on a site installed under/site).This changes the regex to require a
/or end-of-string right after the home path (!^%s(?:/|$)!i) before stripping it, so a bare prefix match likewp-jsonis left alone, while a real subdirectory match likewp/wp-json/...still haswp/stripped correctly.Adds a regression test,
Tests_WP_ParseRequest::test_pathinfo_prefix_matching_home_path_is_not_stripped, which fails on trunk and passes with this fix.Trac ticket: https://core.trac.wordpress.org/ticket/40339
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.