Skip to content

Commit c02645b

Browse files
Rewrite: In url_to_postid(), bail early if the URL does not belong to the site.
Props ivankristianto, swissspidy, jkhongusc, SergeyBiryukov. Fixes #39373. git-svn-id: https://develop.svn.wordpress.org/trunk@41786 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 944d28b commit c02645b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/wp-includes/rewrite.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,14 @@ function url_to_postid( $url ) {
471471
*/
472472
$url = apply_filters( 'url_to_postid', $url );
473473

474+
$url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
475+
$home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
476+
477+
// Bail early if the URL does not belong to this site.
478+
if ( $url_host && $url_host !== $home_url_host ) {
479+
return 0;
480+
}
481+
474482
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
475483
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
476484
$id = absint($values[2]);

tests/phpunit/tests/rewrite.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,20 @@ function test_url_to_postid_static_front_page() {
359359
update_option( 'show_on_front', 'posts' );
360360
}
361361

362+
/**
363+
* @ticket 39373
364+
*/
365+
public function test_url_to_postid_should_bail_when_host_does_not_match() {
366+
$this->set_permalink_structure( '/%postname%/' );
367+
368+
$post_id = self::factory()->post->create( array( 'post_name' => 'foo-bar-baz' ) );
369+
$permalink = get_permalink( $post_id );
370+
$url = str_replace( home_url(), 'http://some-other-domain.com', get_permalink( $post_id ) );
371+
372+
$this->assertSame( $post_id, url_to_postid( $permalink ) );
373+
$this->assertSame( 0, url_to_postid( $url ) );
374+
}
375+
362376
/**
363377
* @ticket 21970
364378
*/

0 commit comments

Comments
 (0)