Make WordPress Core

Opened 10 years ago

Last modified 5 weeks ago

#40339 new defect (bug)

If $home_path=='wp' then WP::parse_request() will remove 'wp' from 'wp-json/wc/v1/products' in $pathinfo

Reported by: Magenta Cuda Owned by:
Priority: high Milestone: Awaiting Review
Component: Rewrite Rules Version: 4.7.3
Severity: critical Keywords: needs-testing has-patch has-unit-tests
Cc: Focuses:

Description

I have installed WordPress in a subdirectory 'wp' so $home_path is 'wp'. The statement

pathinfo = preg_replace( $home_path_regex, '', $pathinfo );

is called with arguments

$home_path_regex = '|^wp|i'
$pathinfo = 'wp-json/wc/v1/products'

and returns

$pathinfo = '-json/wc/v1/products'

Since, $_SERVER[ 'PATH_INFO' ] is the trailing part of the path wouldn't the leading home path be already removed?

Here is the $_SERVER variable:

$_SERVER=Array
(
    [SERVER_SOFTWARE] => PHP 5.6.30 Development Server
    [REQUEST_URI] => /wp/wp-json/wc/v1/products
    [DOCUMENT_ROOT] => C:\\WWW
    [REMOTE_ADDR] => 192.168.1.113
    [REMOTE_PORT] => 54207
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_NAME] => me.local.com
    [SERVER_PORT] => 80
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /wp/index.php
    [SCRIPT_FILENAME] => C:\\WWW\\wp\\index.php
    [PATH_INFO] => /wp-json/wc/v1/products
    [PHP_SELF] => /wp/index.php/wp-json/wc/v1/products
    [HTTP_HOST] => me.local.com
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8,en-AU;q=0.6
    [HTTP_COOKIE] => wp-settings-1=libraryContent%3Dbrowse%26mfold%3Do%26editor%3Dhtml%26posts_list_mode%3Dexcerpt%26widgets_access%3Don%26post_dfw%3Don%26hidetb%3D1; wp-settings-time-1=1489742056; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_82a92e8b6fd4f1e7bd35e69acfebe645=mc%7C1491295996%7ClSYWOrX5ljiNvjMUuOsdx3Hqd3B4PsyM6sdhaHx3VHB%7C0f85cc8928c44377d339344bd93f970151fb018ad0623b09964e252bc6104127
    [REQUEST_TIME_FLOAT] => 1491131923.8711
    [REQUEST_TIME] => 1491131923
)

Change History (4)

#1 @dd32
9 years ago

  • Keywords needs-patch needs-testing added

Hey @Magenta-Cuda and welcome back to Trac.

In most cases, PHP_INFO should only be the trailing component, however that's not always the case unfortunately.
IIS (particularly older versions I think) actually pass the whole REQUEST_URI in there - see ​https://msdn.microsoft.com/en-us/library/ms524602(v=vs.90).aspx

I'm wondering if we can change this regex to effectively be:

$home_path_regex = '!^wp(?:/|$)!i'
$pathinfo = 'wp-json/wc/v1/products'

#2 @keiyamiura01234
2 months ago

  • Priority normal → high
  • Severity normal → critical

I'd like to add another concrete real-world case that hits the exact same root cause described in this ticket — not wp vs wp-json, but site vs sitemap.xml / sitemap_index.xml (e.g. as generated by Yoast SEO, and potentially any plugin/core feature whose rewrite target happens to start with the same string as the install's subdirectory).

Root cause (same as reported above)

In WP::parse_request() (wp-includes/class-wp.php, still present on master as of this writing):

`php
$home_path = parse_url( home_url(), PHP_URL_PATH );
$home_path_regex = ;
if ( is_string( $home_path ) &&
!== $home_path ) {

$home_path = trim( $home_path, '/' );
$home_path_regex = sprintf( '|%s|i', preg_quote( $home_path, '|' ) );

}
...
$req_uri = preg_replace( $home_path_regex, , $req_uri );
$pathinfo = preg_replace( $home_path_regex,
, $pathinfo );
`

$home_path_regex has no boundary anchor (no trailing / or $), so it strips the home path as a plain string *prefix match* rather than a path-segment match. Any requested path segment that happens to start with the same characters as the subdirectory name gets mangled before rewrite-rule matching runs.

Concrete case

  • WordPress installed under a subdirectory: home / siteurl = https://example.com/site
  • Pretty permalinks enabled
  • A plugin registers a rewrite rule for sitemap_index.xml (Yoast SEO does this; the same applies to any custom add_rewrite_rule() target starting with the same string)
  • Request: https://example.com/site/sitemap_index.xml
  • Inside parse_request(), $home_path = 'site', so $home_path_regex = '|^site|i'
  • $pathinfo (or $req_uri after leading segments are trimmed) is sitemap_index.xml, which itself starts with site
  • preg_replace('|^site|i', '', 'sitemap_index.xml') → map_index.xml
  • The mangled path no longer matches the registered rewrite rule (^sitemap_index\.xml$), so WordPress falls through and returns a 404, even though the rewrite rule is correctly registered and rewrite rules have been flushed.

Renaming the subdirectory to anything that isn't a literal prefix of sitemap_index.xml (e.g. blog) resolves it immediately, confirming the cause.

Minimal, plugin-independent reproduction

No third-party plugin is required to reproduce this — any custom rewrite rule reproduces it as long as its target string shares a prefix with the subdirectory name:

  1. Install WordPress under a subdirectory whose name is a literal prefix of some URL you intend to rewrite, e.g. https://example.test/site/.
  2. Enable pretty permalinks ("Post name" or similar) so rewrite rules are in play.
  3. Add this to a must-use plugin:

`php
<?php
add_action('init', function () {

add_rewrite_rule('sitemap_test\.xml$', 'index.php?sitemap_test=1', 'top');

});
add_filter('query_vars', function ($vars) {

$vars[] = 'sitemap_test';
return $vars;

});
add_action('template_redirect', function () {

if (get_query_var('sitemap_test')) {

header('Content-Type: text/plain');
echo 'sitemap_test OK';
exit;

}

});
`

  1. Flush rewrite rules (wp rewrite flush, or save Settings → Permalinks).
  2. Request https://example.test/site/sitemap_test.xml → 404, instead of the expected sitemap_test OK.
  3. Re-run the same install with the subdirectory renamed to e.g. blog (no shared prefix with sitemap_test.xml) → the same request now succeeds.

This shows the bug isn't specific to wp/wp-json, or to any one plugin's sitemap feature — it's a general defect class: any subdirectory install breaks for any rewrite target whose relative path shares a literal string prefix with the subdirectory name, because $home_path_regex strips by prefix instead of by path segment.

Suggested direction for a fix

Anchor the strip to a path-segment boundary instead of a bare prefix, e.g.:

`php
$home_path_regex = sprintf( '|%s(?:/|$)|i', preg_quote( $home_path, '|' ) );
`

so that site matches site/... or an exact site, but not sitemap.... (Same fix shape would also resolve the original wp vs wp-json case in this ticket.)

Happy to attach a patch if that's useful — let me know if this should go on a separate ticket instead, since the underlying cause is identical to what's already reported here.

This ticket was mentioned in ​PR #13373 on ​WordPress/wordpress-develop by ​@irozum.


2 months ago
#3

  • Keywords has-patch has-unit-tests added; needs-patch removed

WP::parse_request() strips the site's home path off the front of PATH_INFO/REQUEST_URI/PHP_SELF using a regex built with sprintf( '|^%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 for wp-json/wc/v1/products (a REST API route) gets the literal substring wp stripped 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. a sitemap.xml request 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 like wp-json is left alone, while a real subdirectory match like wp/wp-json/... still has wp/ 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.

## 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.

#4 @jamesbregenzer
5 weeks ago

The boundary change correctly distinguishes a path segment from a string prefix. The new regression test covers the non-match case, where wp-json/... must remain unchanged.

Could we also add the paired positive case, if it is not already covered elsewhere in the parse-request tests?

With the home URL path set to /wp, use PATH_INFO of /wp/wp-json/wc/v1/products and assert that the normalized request is wp-json/wc/v1/products.

That would guard the intended stripping path alongside the false-positive regression. I have only reviewed the supplied diff, so an existing test outside this patch may already cover it. No test run was performed for this review.

Note: See TracTickets for help on using tickets.