<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23330] branches/3.5/wp-includes: Validate pingback source URIs.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23330">23330</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-01-22 22:32:06 +0000 (Tue, 22 Jan 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Validate pingback source URIs. Less verbose errors.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches35wpincludesclasswpxmlrpcserverphp">branches/3.5/wp-includes/class-wp-xmlrpc-server.php</a></li>
<li><a href="#branches35wpincludescommentphp">branches/3.5/wp-includes/comment.php</a></li>
<li><a href="#branches35wpincludesdefaultfiltersphp">branches/3.5/wp-includes/default-filters.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches35wpincludesclasswpxmlrpcserverphp"></a>
<div class="modfile"><h4>Modified: branches/3.5/wp-includes/class-wp-xmlrpc-server.php (23329 => 23330)</h4>
<pre class="diff"><span>
<span class="info">--- branches/3.5/wp-includes/class-wp-xmlrpc-server.php        2013-01-22 22:30:08 UTC (rev 23329)
+++ branches/3.5/wp-includes/class-wp-xmlrpc-server.php        2013-01-22 22:32:06 UTC (rev 23330)
</span><span class="lines">@@ -5309,10 +5309,14 @@
</span><span class="cx">                 $pagelinkedto = str_replace('&amp;', '&', $pagelinkedto);
</span><span class="cx">                 $pagelinkedto = str_replace('&', '&amp;', $pagelinkedto);
</span><span class="cx">
</span><ins>+                $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
+                if ( ! $pagelinkedfrom )
+                        return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
+
</ins><span class="cx">                 // Check if the page linked to is in our site
</span><span class="cx">                 $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', get_option('home')));
</span><span class="cx">                 if ( !$pos1 )
</span><del>-                        return new IXR_Error(0, __('Is there no link to us?'));
</del><ins>+                        return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
</ins><span class="cx">
</span><span class="cx">                 // let's find which post is linked to
</span><span class="cx">                 // FIXME: does url_to_postid() cover all these cases already?
</span><span class="lines">@@ -5346,39 +5350,39 @@
</span><span class="cx">                                 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", like_escape( $title ) );
</span><span class="cx">                                 if (! ($post_ID = $wpdb->get_var($sql)) ) {
</span><span class="cx">                                         // returning unknown error '0' is better than die()ing
</span><del>-                                         return new IXR_Error(0, '');
</del><ins>+                                         return $this->pingback_error( 0, '' );
</ins><span class="cx">                                 }
</span><span class="cx">                                 $way = 'from the fragment (title)';
</span><span class="cx">                         }
</span><span class="cx">                 } else {
</span><span class="cx">                         // TODO: Attempt to extract a post ID from the given URL
</span><del>-                         return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
</del><ins>+                         return $this->pingback_error( 33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
</ins><span class="cx">                 }
</span><span class="cx">                 $post_ID = (int) $post_ID;
</span><span class="cx">
</span><span class="cx">                 $post = get_post($post_ID);
</span><span class="cx">
</span><span class="cx">                 if ( !$post ) // Post_ID not found
</span><del>-                         return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
</del><ins>+                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
</ins><span class="cx">
</span><span class="cx">                 if ( $post_ID == url_to_postid($pagelinkedfrom) )
</span><del>-                        return new IXR_Error(0, __('The source URL and the target URL cannot both point to the same resource.'));
</del><ins>+                        return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
</ins><span class="cx">
</span><span class="cx">                 // Check if pings are on
</span><span class="cx">                 if ( !pings_open($post) )
</span><del>-                         return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
</del><ins>+                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
</ins><span class="cx">
</span><span class="cx">                 // Let's check that the remote site didn't already pingback this entry
</span><span class="cx">                 if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
</span><del>-                        return new IXR_Error( 48, __( 'The pingback has already been registered.' ) );
</del><ins>+                        return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
</ins><span class="cx">
</span><span class="cx">                 // very stupid, but gives time to the 'from' server to publish !
</span><span class="cx">                 sleep(1);
</span><span class="cx">
</span><span class="cx">                 // Let's check the remote site
</span><del>-                $linea = wp_remote_fopen( $pagelinkedfrom );
</del><ins>+                $linea = wp_remote_retrieve_body( wp_remote_get( $pagelinkedfrom, array( 'timeout' => 10, 'redirection' => 0 ) ) );
</ins><span class="cx">                 if ( !$linea )
</span><del>-                         return new IXR_Error(16, __('The source URL does not exist.'));
</del><ins>+                         return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
</ins><span class="cx">
</span><span class="cx">                 $linea = apply_filters('pre_remote_source', $linea, $pagelinkedto);
</span><span class="cx">
</span><span class="lines">@@ -5390,7 +5394,7 @@
</span><span class="cx">                 preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
</span><span class="cx">                 $title = $matchtitle[1];
</span><span class="cx">                 if ( empty( $title ) )
</span><del>-                        return new IXR_Error(32, __('We cannot find a title on that page.'));
</del><ins>+                        return $this->pingback_error( 32, __('We cannot find a title on that page.' ) );
</ins><span class="cx">
</span><span class="cx">                 $linea = strip_tags( $linea, '<a>' ); // just keep the tag we need
</span><span class="cx">
</span><span class="lines">@@ -5426,7 +5430,7 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( empty($context) ) // Link to target not found
</span><del>-                        return new IXR_Error(17, __('The source URL does not contain a link to the target URL, and so cannot be used as a source.'));
</del><ins>+                        return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
</ins><span class="cx">
</span><span class="cx">                 $pagelinkedfrom = str_replace('&', '&amp;', $pagelinkedfrom);
</span><span class="cx">
</span><span class="lines">@@ -5473,14 +5477,14 @@
</span><span class="cx">                 $post_ID = url_to_postid($url);
</span><span class="cx">                 if ( !$post_ID ) {
</span><span class="cx">                         // We aren't sure that the resource is available and/or pingback enabled
</span><del>-                         return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.'));
</del><ins>+                         return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 $actual_post = get_post($post_ID, ARRAY_A);
</span><span class="cx">
</span><span class="cx">                 if ( !$actual_post ) {
</span><span class="cx">                         // No such post = resource not found
</span><del>-                         return new IXR_Error(32, __('The specified target URL does not exist.'));
</del><ins>+                         return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
</span><span class="lines">@@ -5496,4 +5500,8 @@
</span><span class="cx">
</span><span class="cx">                 return $pingbacks;
</span><span class="cx">         }
</span><ins>+
+        protected function pingback_error( $code, $message ) {
+                return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );
+        }
</ins><span class="cx"> }
</span></span></pre></div>
<a id="branches35wpincludescommentphp"></a>
<div class="modfile"><h4>Modified: branches/3.5/wp-includes/comment.php (23329 => 23330)</h4>
<pre class="diff"><span>
<span class="info">--- branches/3.5/wp-includes/comment.php        2013-01-22 22:30:08 UTC (rev 23329)
+++ branches/3.5/wp-includes/comment.php        2013-01-22 22:32:06 UTC (rev 23330)
</span><span class="lines">@@ -1951,6 +1951,86 @@
</span><span class="cx">                 $client->query('weblogUpdates.ping', get_option('blogname'), $home);
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI
+ *
+ * @since 3.5.1
+ *
+ * @param string $source_uri
+ * @return string
+ */
+function pingback_ping_source_uri( $source_uri ) {
+        $uri = esc_url_raw( $source_uri, array( 'http', 'https' ) );
+        if ( ! $uri )
+                return '';
+
+        $parsed_url = @parse_url( $uri );
+        if ( ! $parsed_url )
+                return '';
+
+        if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) )
+                return '';
+
+        if ( false !== strpos( $parsed_url['host'], ':' ) )
+                return '';
+
+        $parsed_home = @parse_url( get_option( 'home' ) );
+
+        $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
+
+        if ( ! $same_host ) {
+                $host = trim( $parsed_url['host'], '.' );
+                if ( preg_match( '#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $host ) ) {
+                        $ip = $host;
+                } else {
+                        $ip = gethostbyname( $host );
+                        if ( $ip === $host ) // Error condition for gethostbyname()
+                                $ip = false;
+                }
+                if ( $ip ) {
+                        if ( '127.0.0.1' === $ip )
+                                return '';
+                        $parts = array_map( 'intval', explode( '.', $ip ) );
+                        if ( 10 === $parts[0] )
+                                return '';
+                        if ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
+                                return '';
+                        if ( 192 === $parts[0] && 168 === $parts[1] )
+                                return '';
+                }
+        }
+
+        if ( empty( $parsed_url['port'] ) )
+                return $uri;
+
+        $port = $parsed_url['port'];
+        if ( 80 === $port || 443 === $port || 8080 === $port )
+                return $uri;
+
+        if ( $parsed_home && $same_host && $parsed_home['port'] === $port )
+                return $uri;
+
+        return '';
+}
+
+/**
+ * Default filter attached to xmlrpc_pingback_error.
+ *
+ * Returns a generic pingback error code unless the error code is 48,
+ * which reports that the pingback is already registered.
+ *
+ * @since 3.5.1
+ * @link http://www.hixie.ch/specs/pingback/pingback#TOC3
+ *
+ * @param IXR_Error $ixr_error
+ * @return IXR_Error
+ */
+function xmlrpc_pingback_error( $ixr_error ) {
+        if ( $ixr_error->code === 48 )
+                return $ixr_error;
+        return new IXR_Error( 0, '' );
+}
+
</ins><span class="cx"> //
</span><span class="cx"> // Cache
</span><span class="cx"> //
</span></span></pre></div>
<a id="branches35wpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: branches/3.5/wp-includes/default-filters.php (23329 => 23330)</h4>
<pre class="diff"><span>
<span class="info">--- branches/3.5/wp-includes/default-filters.php        2013-01-22 22:30:08 UTC (rev 23329)
+++ branches/3.5/wp-includes/default-filters.php        2013-01-22 22:32:06 UTC (rev 23330)
</span><span class="lines">@@ -192,6 +192,8 @@
</span><span class="cx"> add_filter( 'editable_slug', 'urldecode' );
</span><span class="cx"> add_filter( 'editable_slug', 'esc_textarea' );
</span><span class="cx"> add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object' );
</span><ins>+add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri' );
+add_filter( 'xmlrpc_pingback_error', 'xmlrpc_pingback_error' );
</ins><span class="cx">
</span><span class="cx"> // Actions
</span><span class="cx"> add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
</span></span></pre>
</div>
</div>
</body>
</html>