Rewrite Rules: Only treat a leading www. as optional in url_to_postid() - #13019
Rewrite Rules: Only treat a leading www. as optional in url_to_postid()#13019youknowriad wants to merge 1 commit into
www. as optional in url_to_postid()#13019Conversation
|
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. |
…tid()`. `url_to_postid()` compares the URL's host to the site's host after removing `www.` from both. The removal is unanchored, so it also strips a `www.` that appears in the middle of a host name: `exwww.ample.com` and `example.cwww.om` both reduce to `example.com`. A URL on either domain therefore resolves to a post ID on a site hosted at `example.com`, and both are ordinary registrations. Anchor the removal to the start of the host, matching the idiom already used in `ms-functions.php` and `ms-blogs.php`. See #65016. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
c43aacf to
55288be
Compare
irozum
left a comment
There was a problem hiding this comment.
Good catch and a clean, minimal fix — anchoring the www. strip to the start of the host closes a real spoofing gap (exwww.ample.com / example.cwww.om currently alias to example.com), and it matches the same anchored idiom already used in ms-blogs.php/ms-functions.php, so this brings url_to_postid() in line with the rest of core rather than introducing a new pattern.
Checked out the branch and ran the affected suite plus the standard gates: Tests_Rewrite (92 tests, 279 assertions), composer lint:errors and phpstan on the two changed files — all clean. Traced the surrounding code by hand: the strict !== comparison a few lines down means this fix only ever makes matching stricter (fewer false positives), so there's no realistic backward-compat concern for existing callers.
One minor observation, non-blocking: the linked ticket #65016 is about auto-approving self-pingbacks, not this bug — the PR body explains it surfaced while reviewing #12870, which is reasonable, but there's no dedicated ticket for the url_to_postid() issue itself. Worth flagging to committers in case they'd rather this land under its own ticket for changelog/props purposes, though it's not something I'd block on.
url_to_postid()compares the URL's host to the site's host after removingwww.from both. The removal is unanchored, so it stripswww.from anywhere in the host, not just the prefix. On a site atexample.com:Both are ordinary registrations (
ample.com;cwww.omunder Oman's TLD). Since a post ID from this function is widely treated as proof that a URL is internal, a lookalike domain currently passes that test.Trac ticket: https://core.trac.wordpress.org/ticket/65016
This surfaced while reviewing #12870 on that ticket, which uses
url_to_postid()for a trust decision. Worth fixing on its own regardless of what happens to that PR.Changes
Anchor the removal so only a leading
www.is optional, matching the idiom already used inms-functions.phpandms-blogs.php:Testing
A data-provided case in
tests/phpunit/tests/rewrite.phpcovers the site host, thewww.prefix, both collisions, an unrelated host, and the site host as an attacker subdomain. Reverting only the source change fails exactly the two collision cases.npm run test:php -- --filter Tests_Rewrite # 92 tests, 279 assertionsUse of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Research, implementation, and tests; reviewed and edited by me.