Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/subscribe-to-comments/subscribe-to-comments.php
+++ b/subscribe-to-comments/subscribe-to-comments.php
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Subscribe To Comments
-Version: 2.3.1
+Version: 2.3.2
Plugin URI: http://txfx.net/wordpress-plugins/subscribe-to-comments/
Description: Allows readers to choose to receive notifications of new comments that are posted to an entry.
Author: Mark Jaquith
@@ -42,6 +42,7 @@
function show_manual_subscription_form() {
global $id, $sg_subscribe, $user_email;
_stc()->show_errors( 'solo_subscribe', '<div class="solo-subscribe-errors">', '</div>', __( '<strong>Error: </strong>', 'subscribe-to-comments' ), '<br />' );
+ _stc()->show_messages();
if ( !_stc()->current_viewer_subscription_status() ) :
get_currentuserinfo(); ?>
@@ -174,6 +175,7 @@
add_action( 'wp_set_comment_status', array( $this, 'send_notifications' ));
add_action( 'admin_menu', array( $this, 'add_admin_menu' ));
add_action( 'admin_head', array( $this, 'admin_head' ));
+ add_action( 'admin_notices', array( $this, 'management_key_notice' ));
add_action( 'edit_comment', array( $this, 'on_edit' ));
add_action( 'delete_comment', array( $this, 'on_delete' ));
add_filter( 'the_content', array( $this, 'manager' ));
@@ -263,9 +265,12 @@
$this->after_manager = $this->settings['after_manager'];
}
- foreach ( array( 'email', 'key', 'ref', 'new_email' ) as $var )
- if ( isset( $_REQUEST[$var]) && !empty($_REQUEST[$var] ) )
+ foreach ( array( 'email', 'key', 'new_email' ) as $var )
+ if ( isset( $_REQUEST[$var]) && is_string( $_REQUEST[$var] ) && !empty($_REQUEST[$var] ) )
$this->{$var} = esc_attr( trim( stripslashes( $_REQUEST[$var] ) ) );
+ $this->ref = '';
+ if ( isset( $_REQUEST['ref'] ) && is_string( $_REQUEST['ref'] ) )
+ $this->ref = esc_url_raw( stripslashes( $_REQUEST['ref'] ), array( 'http', 'https' ) );
if ( !$this->key )
$this->key = 'unset';
}
@@ -455,12 +460,11 @@
if ( strtolower( $post_author->user_email ) == ( $email ) )
$this->add_error( __( 'You appear to be already subscribed to this entry.', 'subscribe-to-comments' ),'solo_subscribe' );
- if ( !is_array( $this->errors['solo_subscribe'] ) ) {
+ if ( empty( $this->errors['solo_subscribe'] ) ) {
add_post_meta( $postid, '_sg_subscribe-to-comments', strtolower( $email ) );
setcookie( 'comment_author_email_' . COOKIEHASH, $email, time() + 30000000, COOKIEPATH );
- $location = $this->manage_link( $email, false, false ) . '&subscribeid=' . $postid;
- header( "Location: $location" );
- exit();
+ // A submitted e-mail address is not proof of ownership. Never expose its management key here.
+ $this->add_message( __( 'Your subscription request has been received. You can manage your subscription using the link in notification e-mails.', 'subscribe-to-comments' ) );
}
}
@@ -674,7 +678,7 @@
function hidden_form_fields() { ?>
- <input type="hidden" name="ref" value="<?php echo $this->ref; ?>" />
+ <input type="hidden" name="ref" value="<?php echo esc_attr( $this->ref ); ?>" />
<input type="hidden" name="key" value="<?php echo $this->key; ?>" />
<input type="hidden" name="email" value="<?php echo $this->email; ?>" />
<?php
@@ -690,14 +694,31 @@
}
+ /**
+ * Recognizes retired keys only to explain their rejection, never to grant access.
+ */
+ function is_revoked_key() {
+ if ( empty( $this->settings['revoked_salt'] ) || !is_email( $this->email ) || !is_string( $this->key ) )
+ return false;
+ foreach ( array( $this->email, $this->email . $this->new_email, $this->email . 'blockrequest', 'opt_in:' . $this->email ) as $data ) {
+ if ( $this->key === md5( md5( $this->settings['revoked_salt'] . $data ) ) )
+ return true;
+ }
+ return false;
+ }
+
function validate_key() {
- if ( $this->key == $this->generate_key( $this->email ) )
+ $this->key_type = '';
+ // Reject retired links even for administrators, before their capability bypass.
+ if ( $this->is_revoked_key() )
+ return false;
+ if ( $this->key === $this->generate_key( $this->email ) )
$this->key_type = 'normal';
- elseif ( $this->key == $this->generate_key( $this->email . $this->new_email ) )
+ elseif ( $this->key === $this->generate_key( $this->email . $this->new_email ) )
$this->key_type = 'change_email';
- elseif ( $this->key == $this->generate_key( $this->email . 'blockrequest' ) )
+ elseif ( $this->key === $this->generate_key( $this->email . 'blockrequest' ) )
$this->key_type = 'block';
- elseif ( $this->key == $this->generate_key( 'opt_in:' . $this->email ) )
+ elseif ( $this->key === $this->generate_key( 'opt_in:' . $this->email ) )
$this->key_type = 'opt_in';
elseif ( current_user_can( 'manage_options' ) )
$this->key_type = 'admin';
@@ -836,9 +857,10 @@
$subject = '[' . get_bloginfo( 'name' ) . '] ' . $subject;
// strip out some chars that might cause issues, and assemble vars
- $site_name = str_replace( '"', "'", $this->site_name );
- $site_email = str_replace( array( '<', '>' ), array( '', '' ), $this->site_email );
- $charset = get_option( 'blog_charset' );
+ // Sanitize at send time as well, so previously saved settings cannot inject headers.
+ $site_name = str_replace( array( "r", "n", '"', '\' ), array( '', '', "'", '' ), $this->site_name );
+ $site_email = sanitize_email( $this->site_email );
+ $charset = str_replace( array( "r", "n", '"' ), '', get_option( 'blog_charset' ) );
$headers = "From: "{$site_name}" <{$site_email}>n";
$headers .= "MIME-Version: 1.0n";
@@ -869,7 +891,8 @@
switch_to_blog( $bid );
}
if ( empty( $uri ) )
- $uri = esc_url( get_permalink( $postid ) );
+ $uri = get_permalink( $postid );
+ $uri = esc_url( $uri, array( 'http', 'https' ) );
$postid = (int) $postid;
$title = get_the_title( $postid );
if ( empty( $title ) )
@@ -893,18 +916,38 @@
}
+ function management_key_notice() {
+ if ( !current_user_can( 'manage_options' ) || empty( $this->settings['management_keys_rotated'] ) )
+ return;
+ echo '<div class="updated"><p>' . esc_html( __( 'Subscribe to Comments: Subscription-management keys have been rotated following a security update. Links in older e-mails have been revoked, but subscriptions have not been removed. New notification e-mails will contain working management links.', 'subscribe-to-comments' ) ) . '</p></div>';
+ // Show the notice once, to an administrator, rather than on every admin page.
+ $this->settings['management_keys_rotated'] = false;
+ $this->update_settings( $this->settings );
+ }
+
function db_upgrade_check () {
global $wpdb, $blog_id;
$update = false;
// add the options
- add_option( 'sg_subscribe_settings', array( 'use_custom_style' => '', 'email' => get_bloginfo( 'admin_email' ), 'name' => get_bloginfo( 'name' ), 'header' => '[theme_path]/header.php', 'sidebar' => '', 'footer' => '[theme_path]/footer.php', 'before_manager' => '<div id="content" class="widecolumn subscription-manager">', 'after_manager' => '</div>', 'not_subscribed_text' => __( 'Notify me of followup comments via e-mail', 'subscribe-to-comments' ), 'subscribed_text' => __( 'You are subscribed to this entry. <a href="[manager_link]">Manage your subscriptions</a>.', 'subscribe-to-comments' ), 'author_text' => __( 'You are the author of this entry. <a href="[manager_link]">Manage subscriptions</a>.', 'subscribe-to-comments' ), 'version' => 0, 'double_opt_in' => '', 'subscribed_format' => '%NAME%' ) );
+ add_option( 'sg_subscribe_settings', array( 'use_custom_style' => '', 'email' => get_bloginfo( 'admin_email' ), 'name' => get_bloginfo( 'name' ), 'header' => '[theme_path]/header.php', 'sidebar' => '', 'footer' => '[theme_path]/footer.php', 'before_manager' => '<div id="content" class="widecolumn subscription-manager">', 'after_manager' => '</div>', 'not_subscribed_text' => __( 'Notify me of followup comments via e-mail', 'subscribe-to-comments' ), 'subscribed_text' => __( 'You are subscribed to this entry. <a href="[manager_link]">Manage your subscriptions</a>.', 'subscribe-to-comments' ), 'author_text' => __( 'You are the author of this entry. <a href="[manager_link]">Manage subscriptions</a>.', 'subscribe-to-comments' ), 'version' => 0, 'management_key_version' => 1, 'double_opt_in' => '', 'subscribed_format' => '%NAME%' ) );
$settings = $this->get_options();
+ // Existing installations must revoke keys that may have been disclosed before the security fix.
+ // Fresh installations start at this key version and have no retired links to recognize.
+ if ( empty( $settings['management_key_version'] ) ) {
+ if ( !empty( $settings['salt'] ) )
+ $settings['revoked_salt'] = $settings['salt'];
+ $settings['salt'] = wp_generate_password( 64, true, true );
+ $settings['management_key_version'] = 1;
+ $settings['management_keys_rotated'] = true;
+ $update = true;
+ }
+
if ( !isset( $settings['salt'] ) ) {
- $settings['salt'] = md5( md5( uniqid( rand() . rand() . rand() . rand() . rand(), true ) ) ); // random MD5 hash
+ $settings['salt'] = wp_generate_password( 64, true, true );
$update = true;
}
@@ -976,6 +1019,10 @@
}
function sanitize_settings( $settings ) {
+ if ( isset( $settings['name'] ) )
+ $settings['name'] = sanitize_text_field( $settings['name'] );
+ if ( isset( $settings['email'] ) )
+ $settings['email'] = sanitize_email( $settings['email'] );
if ( strpos( $settings['subscribed_format'], '%NAME%' ) === false ) {
$settings['subscribed_format'] = '%NAME%';
}
@@ -993,22 +1040,16 @@
function current_viewer_subscription_status(){
- global $wpdb, $blog_id, $post, $user_email;
-
- $comment_author_email = ( isset( $_COOKIE['comment_author_email_'. COOKIEHASH] ) ) ? trim( $_COOKIE['comment_author_email_'. COOKIEHASH] ) : '';
- get_currentuserinfo();
+ global $post;
- if ( is_email( $user_email ) ) {
- $email = strtolower( $user_email );
- $loggedin = true;
- } elseif ( is_email( $comment_author_email ) ) {
- $email = strtolower( $comment_author_email );
- } else {
+ // Comment cookies are unsigned and cannot authenticate a subscriber.
+ $user = wp_get_current_user();
+ if ( !$user->ID || !is_email( $user->user_email ) )
return false;
- }
+ $email = strtolower( $user->user_email );
$post_author = get_userdata( $post->post_author );
- if ( strtolower( $post_author->user_email ) == $email && $loggedin )
+ if ( $post_author && strtolower( $post_author->user_email ) == $email )
return 'admin';
if ( in_array( $email, (array) $this->subscriptions_from_post( $post->ID ) ) )
@@ -1017,6 +1058,11 @@
}
function manage_link( $email='', $html=true, $echo=true ) {
+ // Only authenticated owners or administrators may receive a newly generated management link.
+ // Guests manage subscriptions using the keys delivered to them by e-mail.
+ $user = wp_get_current_user();
+ if ( !$user->ID || ( !current_user_can( 'manage_options' ) && ( !is_email( $email ) || strtolower( $user->user_email ) != strtolower( $email ) ) ) )
+ return '';
$link = get_option( 'home' ) . '/?wp-subscription-manager=1';
if ( $email != 'admin' ) {
$link = add_query_arg( 'email', urlencode( $email ), $link );
@@ -1067,7 +1113,7 @@
function add_admin_menu() {
$manager_callback = add_submenu_page( 'edit-comments.php', __( 'Comment Subscription Manager', 'subscribe-to-comments' ), __( 'Subscriptions', 'subscribe-to-comments' ), 'manage_options', 'stc-management', 'sg_subscribe_admin' );
- $opt_callback = add_options_page( __( 'Subscribe to Comments', 'subscribe-to-comments' ), __( 'Subscribe to Comments', 'subscribe-to-comments' ), 'publish_posts', 'stc-options', array( _stc(), 'options_page' ) );
+ $opt_callback = add_options_page( __( 'Subscribe to Comments', 'subscribe-to-comments' ), __( 'Subscribe to Comments', 'subscribe-to-comments' ), 'manage_options', 'stc-options', array( _stc(), 'options_page' ) );
add_action( "load-{$opt_callback}", array( _stc(), 'save_options' ) );
}
@@ -1094,8 +1140,19 @@
function save_options() {
if ( isset( $_POST['sg_subscribe_settings_submit'] ) ) {
+ if ( !current_user_can( 'manage_options' ) )
+ wp_die( __( 'You do not have permission to change these settings.', 'subscribe-to-comments' ) );
check_admin_referer( 'subscribe-to-comments-update_options' );
+ if ( !isset( $_POST['sg_subscribe_settings'] ) || !is_array( $_POST['sg_subscribe_settings'] ) )
+ wp_die( __( 'Invalid settings.', 'subscribe-to-comments' ) );
$update_settings = stripslashes_deep( $_POST['sg_subscribe_settings'] );
+ // These private fields are not editable form settings. Preserve them across saves.
+ $current_settings = $this->get_options();
+ foreach ( array( 'salt', 'revoked_salt', 'management_key_version', 'management_keys_rotated', 'manage_id' ) as $field ) {
+ unset( $update_settings[$field] );
+ if ( isset( $current_settings[$field] ) )
+ $update_settings[$field] = $current_settings[$field];
+ }
_stc()->update_settings( $update_settings );
wp_redirect( add_query_arg( 'updated', '1', menu_page_url( 'stc-options', false ) ) );
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' );
@@ -1184,6 +1241,8 @@
}
function options_page() {
+ if ( !current_user_can( 'manage_options' ) )
+ wp_die( __( 'You do not have permission to change these settings.', 'subscribe-to-comments' ) );
echo '<form method="post">';
screen_icon();
echo '<div class="wrap">';
@@ -1287,8 +1346,11 @@
get_currentuserinfo();
- if ( !_stc()->validate_key() )
- die ( __( 'You may not access this page without a valid key.', 'subscribe-to-comments' ) );
+ if ( !_stc()->validate_key() ) {
+ if ( _stc()->is_revoked_key() )
+ wp_die( __( 'This subscription-management link has been revoked following a security update. Your subscriptions have not been removed. Please use the link in a new notification e-mail, or contact the site administrator.', 'subscribe-to-comments' ) );
+ wp_die( __( 'You may not access this page without a valid key.', 'subscribe-to-comments' ) );
+ }
_stc()->determine_action();
@@ -1433,7 +1495,7 @@
</p>
<form name="getemail" method="post" action="<?php echo esc_url( _stc()->form_action ); ?>">
- <input type="hidden" name="ref" value="<?php echo _stc()->ref; ?>" />
+ <input type="hidden" name="ref" value="<?php echo esc_attr( _stc()->ref ); ?>" />
<p>
<input name="email" type="text" id="email" size="40" />