Opened 8 years ago
Last modified 11 days ago
#44964 new defect (bug)
`redirect_guess_404_permalink` doesn't support array in `post_type` var
| Reported by: | viliamkopecky | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | 4.9 |
| Severity: | normal | Keywords: | needs-testing dev-feedback has-patch has-unit-tests |
| Cc: | Focuses: |
Description
redirect_guess_404_permalink doesn't support array in post_type var, but other core WordPress parts support it.
Attachments (1)
Change History (11)
This ticket was mentioned in PR #12755 on WordPress/wordpress-develop by @sainathpoojary.
3 months ago
#4
- Keywords has-patch added
When a user passes an array of post types in a 404 URL, redirect_guess_404_permalink() correctly intersects them with publicly viewable post types to determine the valid list. However, it had a bug where it constructed the SQL IN (...) clause using the raw, unfiltered query variable (get_query_var( 'post_type' )) instead of the intersected list ($post_types).
This PR fixes the SQL clause to use the $post_types intersection variable, ensuring that non-public post types are never included in the database query. It also introduces unit tests to verify that a private post is correctly ignored when a public post with the same name exists and an array of their post types is requested.
Trac ticket: #44964
#5
@
3 months ago
Test Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/12755
Environment:
- WordPress: trunk @ 157c2fc777 (reports as 7.1-beta4-62899-src)
- PHP: 8.3.32 (Docker
phpcontainer) - MySQL: 9.7.1
- OS: macOS (Docker Desktop / OrbStack host)
- Theme: twentytwentyfive
- Plugins: none active
Actual results before patch:
Registered a non-viewable custom post type (public => false, exclude_from_search => true, publicly_queryable => false) and created a single published post of that type, slug hidden-internal-note. Simulated the query vars a 404 request sets when post_type is queried as an array containing both a viewable type and the non-viewable one: $wp_query->query_vars['post_type'] = array( 'post', 'secret_cpt' ), name = 'hidden-internal-note'.
Calling redirect_guess_404_permalink() on clean trunk returned the permalink of the non-viewable post:
result: 'http://localhost:8889/secret_cpt/hidden-internal-note/' LEAK CONFIRMED: the non-viewable secret_cpt post was guessed as the 404 redirect target.
Confirmed via $wpdb->last_query that the generated SQL was:
SELECT ID FROM wp_posts WHERE post_name LIKE 'shared-prefix%' AND post_type IN ('post', 'secret_cpt') AND post_status IN ('publish')
i.e. the IN (...) clause is built from the raw, unfiltered get_query_var( 'post_type' ) array rather than the $post_types intersection computed two lines earlier — exactly the bug described in the ticket.
Actual results after patch:
Same repro against the patched branch. redirect_guess_404_permalink() now returns false — the non-viewable type is correctly excluded from the SQL IN (...) clause, so no redirect is guessed:
result: false No leak: correctly returned false (no viewable post type matched).
Automated tests:
$ npm run test:php -- --group 44964 No tests executed! <- no test in the codebase carries @ticket 44964; the PR adds no test file (see note below) $ npm run test:php -- --group canonical OK (1057 tests, 1134 assertions) $ composer exec -- phpcs --standard=phpcs.xml.dist src/wp-includes/canonical.php No errors (0/0) $ npm run typecheck:php (PHPStan, phpstan.neon.dist) [OK] No errors
Additional notes:
- The PR description is inaccurate about test coverage. It states: "It also introduces unit tests to verify that a private post is correctly ignored when a public post with the same name exists and an array of their post types is requested." The actual diff (
gh pr diff 12755 --name-only) touches onlysrc/wp-includes/canonical.php— no test file is included. A committer will very likely ask for this to be added before commit. - Edge cases probed against the patched code, all behaving sanely and matching pre-existing (unaffected) logic elsewhere in the function:
- Mixed array with both a viewable and non-viewable type, using a slug prefix ambiguous enough to match either post: correctly resolves only to the viewable post.
- Array containing only the non-viewable type: correctly returns
false(the pre-existingempty( $post_types )guard, unchanged by this patch). - Array containing a viewable type plus a bogus/unregistered type string: resolves correctly to the viewable post.
- Array with duplicate entries of the same viewable type: no SQL error, resolves correctly.
- Non-array (string)
post_type(theelsebranch at line 990-995): unaffected by this patch, behaves as before. - Empty array
post_type: falls through to the function's separate "no post_type filter" branch (line 996-998) because an empty array is falsy in theif ( get_query_var( 'post_type' ) )check at line 983 — this is pre-existing behavior unrelated to the one-line fix, not a regression.
- Not tested: multisite. This function has no multisite-specific branching (post types are per-site), so no material behavior difference is expected, but it was not explicitly verified in a multisite install.
- Not tested: an unprivileged-user angle doesn't apply here —
redirect_guess_404_permalink()runs unauthenticated on the front end by design; there's no capability check in this code path to probe. - The fix itself is a minimal, correct one-line change (
get_query_var( 'post_type' )→$post_types) that aligns the SQL query with the intersection already computed for theempty()guard two lines above. No collateral changes to the surroundingredirect_guess_404_permalink()logic.
This ticket was mentioned in PR #12941 on WordPress/wordpress-develop by @irozum.
3 months ago
#6
- Keywords has-unit-tests added; needs-unit-tests removed
What this adds: a new data-provider case for Tests_Canonical::test_redirect_guess_404_permalink_post_types() in tests/phpunit/tests/canonical.php, covering redirect_guess_404_permalink() in src/wp-includes/canonical.php.
Function under test: redirect_guess_404_permalink(), specifically the branch that builds the SQL post_type IN (...) clause when post_type is queried as an array (src/wp-includes/canonical.php, around line 985-989).
Case covered and why: a mix of one publicly viewable post type (page) and one non-viewable post type (wp_tests_private) in the post_type array, where a post of the *non-viewable* type matches the requested name prefix. Before the fix landed in trunk (efa21b21dd, ticket #44964), this branch built the SQL IN (...) clause from the raw, unfiltered get_query_var( 'post_type' ) array instead of the $post_types list already intersected against publicly viewable types two lines above — so a private/non-viewable post could be found and its permalink leaked as the guessed 404 redirect target, even though it should never have been considered. The existing data set already covered a *single* private post type (which short-circuits via the empty( $post_types ) guard before ever reaching the vulnerable SQL line) and a single public type, but nothing exercised the mixed-array path where the bug actually lived. I verified this test fails against the pre-fix code (reverting the one-line fix locally reproduces the leak and turns this exact case red) and passes against current trunk.
This is test-only — it adds no production behaviour. git diff trunk for this branch touches only tests/phpunit/tests/canonical.php.
## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Surveying Trac for a coverage candidate, verifying the fix had landed in trunk and no test existed for it, drafting the test case and data-provider entry, and running the red/green verification loop (breaking the fix locally to confirm the new test fails, then reverting). I reviewed the diff, ticket, and test output before submitting.
#7
@
6 weeks ago
Tested PR #12755 at 472033a855859d0835fc43107e398abde2e740fe against b915febe66b5234f0a78c35a2be5274ce7f65002.
I ran the ticket-specific automated tests for the Query component using the current patch.
The focused PHPUnit run completed 1 test and 1 assertion with no reported failures.
This gives maintainers current evidence that the patch behaves as intended in the Query component paths covered by ticket #44964.
@lancewillett commented on PR #12941:
11 days ago
#9
PR #12941 landed in https://core.trac.wordpress.org/changeset/65132
@lancewillett commented on PR #12755:
11 days ago
#10
Closing as superseded. The production fix and mixed public/private post-type regression coverage are now on trunk. The current canonical tests cover the remaining regression without another fixture.
<sub>AI review · <code>gpt-6</code></sub>
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
patch with fix