Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/geodirectory/geodirectory.php
+++ b/geodirectory/geodirectory.php
@@ -11,7 +11,7 @@
* Plugin Name: GeoDirectory
* Plugin URI: https://wpgeodirectory.com/
* Description: GeoDirectory - Business Directory Plugin for WordPress.
- * Version: 2.8.208
+ * Version: 2.8.209
* Author: AyeCode - WP Business Directory Plugins
* Author URI: https://wpgeodirectory.com
* Text Domain: geodirectory
@@ -34,7 +34,7 @@
*
* @var string
*/
- public $version = '2.8.208';
+ public $version = '2.8.209';
/**
* GeoDirectory instance.
--- a/geodirectory/includes/class-geodir-comments.php
+++ b/geodirectory/includes/class-geodir-comments.php
@@ -22,6 +22,10 @@
// add ratings to comment text
add_filter( 'comment_text', array( __CLASS__, 'wrap_comment_text' ), 40, 2 );
+ // Prevent review text from being parsed as shortcodes when the listing content is re-parsed.
+ add_filter( 'comment_text', array( __CLASS__, 'neutralize_comment_shortcodes' ), PHP_INT_MAX, 2 );
+ add_filter( 'get_comment_excerpt', array( __CLASS__, 'neutralize_comment_excerpt_shortcodes' ), PHP_INT_MAX, 3 );
+
// replace comments template
add_filter( 'comments_template', array( __CLASS__, 'comments_template' ) ); // @todo, maybe we want to use the themes own template?
@@ -635,6 +639,45 @@
}
/**
+ * Encode shortcode brackets in GD review text.
+ *
+ * The single listing page content is filtered through the_content, which
+ * runs do_shortcode() on the already rendered output (including the reviews).
+ * Review text is untrusted user input and must never be parsed as shortcodes.
+ *
+ * @since 2.8.209
+ *
+ * @param string $content The comment content.
+ * @param object|string $comment The comment object.
+ * @return string The comment content.
+ */
+ public static function neutralize_comment_shortcodes( $content, $comment = '' ) {
+ if ( ! empty( $comment->comment_post_ID ) && geodir_is_gd_post_type( get_post_type( (int) $comment->comment_post_ID ) ) ) {
+ $content = geodir_neutralize_untrusted_content( $content );
+ }
+
+ return $content;
+ }
+
+ /**
+ * Encode shortcode brackets in GD review excerpt.
+ *
+ * @since 2.8.209
+ *
+ * @param string $content The comment excerpt.
+ * @param string $comment_id The comment ID as a numeric string.
+ * @param object|string $comment The comment object.
+ * @return string The comment excerpt.
+ */
+ public static function neutralize_comment_excerpt_shortcodes( $content, $comment_id = 0, $comment = '' ) {
+ if ( ! empty( $comment->comment_post_ID ) && geodir_is_gd_post_type( get_post_type( (int) $comment->comment_post_ID ) ) ) {
+ $content = geodir_neutralize_untrusted_content( $content );
+ }
+
+ return $content;
+ }
+
+ /**
* Comment HTML markup.
*
* @since 2.0.0
--- a/geodirectory/includes/core-functions.php
+++ b/geodirectory/includes/core-functions.php
@@ -540,8 +540,20 @@
*/
function geodir_column_exist( $db, $column ) {
global $wpdb;
- $exists = false;
- $columns = $wpdb->get_col( "show columns from $db" );
+
+ $exists = false;
+
+ // Allow only a valid table identifier.
+ if ( empty( $db ) || ! is_string( $db ) || ! preg_match( '/^[A-Za-z0-9_-$]+$/', $db ) ) {
+ return $exists;
+ }
+
+ $columns = $wpdb->get_col( "SHOW COLUMNS FROM `{$db}`" );
+
+ if ( empty( $columns ) || ! is_array( $columns ) ) {
+ return $exists;
+ }
+
foreach ( $columns as $c ) {
if ( $c == $column ) {
$exists = true;
--- a/geodirectory/includes/custom-fields/output-functions.php
+++ b/geodirectory/includes/custom-fields/output-functions.php
@@ -2624,6 +2624,9 @@
// Render private address.
$address_fields = geodir_post_address( $address_fields, 'address', $gd_post );
+ // Strip slashes before sanitizing so that no markup can be re-formed after sanitization.
+ $address_fields = stripslashes( $address_fields );
+
$plain_value = wp_strip_all_tags( $address_fields, true );
if ( $plain_value == '' ) {
return $html;
@@ -2632,9 +2635,12 @@
// Database / Stripped value.
if ( ! empty( $output ) && ( isset( $output['raw'] ) || isset( $output['strip'] ) ) ) {
$address_fields = str_replace( array( ",<br>", "<br>" ), array( ",", "," ), $address_fields );
- return stripslashes( wp_strip_all_tags( $address_fields, true ) );
+ return wp_strip_all_tags( $address_fields, true );
}
+ // Sanitize the address HTML before output.
+ $address_fields = geodir_kses_address( $address_fields );
+
$html = '<div class="geodir_post_meta ' . $cf['css_class'] . ' geodir-field-' . $cf['htmlvar_name'] . '" itemscope itemtype="http://schema.org/PostalAddress">';
$maybe_secondary_class = isset( $output['icon'] ) ? 'gv-secondary' : '';
@@ -2652,11 +2658,11 @@
}
if ( $output == '' || isset( $output['value'] ) ) {
- $html .= stripslashes( $address_fields );
+ $html .= $address_fields;
}
if ( ! empty( $output ) && isset( $output['link'] ) ) {
- $value = stripslashes( $address_fields );
+ $value = $address_fields;
$address = normalize_whitespace( wp_strip_all_tags( $value ) );
$map_link = 'https://www.google.com/maps?q=' . urlencode( $address );
--- a/geodirectory/includes/formatting-functions.php
+++ b/geodirectory/includes/formatting-functions.php
@@ -30,6 +30,105 @@
}
/**
+ * Neutralize shortcodes and block delimiters in visitor supplied content.
+ *
+ * @since 2.8.209
+ *
+ * @param string $content The content.
+ * @return string The text with shortcode brackets encoded.
+ */
+function geodir_neutralize_untrusted_content( $content ) {
+ if ( ! is_string( $content ) || $content === '' ) {
+ return $content;
+ }
+
+ // Strip block delimiters.
+ if ( strpos( $content, '<!--' ) !== false ) {
+ $content = preg_replace( '/<!--s*/?wp:.*?-->/s', '', $content );
+ }
+
+ // Encode square brackets so shortcodes can't be executed.
+ return geodir_encode_shortcode_brackets( $content );
+}
+
+/**
+ * Encode square brackets so the text can never be parsed as shortcodes.
+ *
+ * Brackets are encoded as hex entities ([ / ]) instead of [ / ].
+ * WordPress uses [ / ] as internal placeholders in do_shortcode() and
+ * unescape_invalid_shortcodes() turns them back into [ ] at the end of every
+ * do_shortcode() call. When review text is rendered inside a shortcode (e.g.
+ * [gd_single_tabs] / [gd_single_reviews] within the single page template) the
+ * decimal entities are converted back to brackets, and the following
+ * do_shortcode() on the_content then executes them. Hex entities are left
+ * untouched by the shortcode API and by entity normalization.
+ *
+ * @since 2.8.209
+ *
+ * @param string $content The content.
+ * @return string The content with square brackets encoded.
+ */
+function geodir_encode_shortcode_brackets( $content ) {
+ if ( ! is_string( $content ) || $content === '' ) {
+ return $content;
+ }
+
+ // Decimal bracket entities already present in the text would also be decoded by do_shortcode().
+ if ( strpos( $content, '&#' ) !== false ) {
+ $content = preg_replace( array( '/*91;/', '/*93;/' ), array( '[', ']' ), $content );
+ }
+
+ if ( strpos( $content, '[' ) !== false || strpos( $content, ']' ) !== false ) {
+ $content = str_replace( array( '[', ']' ), array( '[', ']' ), $content );
+ }
+
+ return $content;
+}
+
+/**
+ * Sanitize the address HTML built from the address template.
+ *
+ * @since 2.8.209
+ *
+ * @param string $html The address HTML.
+ * @return string Sanitized address HTML.
+ */
+function geodir_kses_address( $html ) {
+ if ( ! is_string( $html ) || $html === '' ) {
+ return $html;
+ }
+
+ static $allowed_html = null;
+
+ if ( null === $allowed_html ) {
+ $allowed_html = wp_kses_allowed_html( 'post' );
+
+ foreach ( $allowed_html as $tag => $attributes ) {
+ if ( ! is_array( $attributes ) ) {
+ $attributes = array();
+ }
+
+ $attributes['itemprop'] = true;
+ $attributes['itemscope'] = true;
+ $attributes['itemtype'] = true;
+
+ $allowed_html[ $tag ] = $attributes;
+ }
+
+ /**
+ * Filter the HTML tags & attributes allowed in the address output.
+ *
+ * @since 2.8.209
+ *
+ * @param array $allowed_html Allowed HTML.
+ */
+ $allowed_html = apply_filters( 'geodir_kses_address_allowed_html', $allowed_html );
+ }
+
+ return wp_kses( $html, $allowed_html );
+}
+
+/**
* Emulate the WP native sanitize_text_field function in a %%variable%% safe way.
*
* @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php for the original
--- a/geodirectory/includes/helper-functions.php
+++ b/geodirectory/includes/helper-functions.php
@@ -13,13 +13,16 @@
*
* @return bool|string
*/
-function geodir_db_cpt_table($post_type){
- if(!empty($post_type)){
- global $plugin_prefix;
- return $plugin_prefix . $post_type . '_detail';
+function geodir_db_cpt_table( $post_type ) {
+ global $wpdb;
+
+ $post_type = sanitize_key( $post_type );
+
+ if ( $post_type === '' ) {
+ return false;
}
- return false;
+ return $wpdb->prefix . 'geodir_' . $post_type . '_detail';
}
/**
--- a/geodirectory/includes/post-functions.php
+++ b/geodirectory/includes/post-functions.php
@@ -1987,40 +1987,47 @@
global $gd_post;
// Only run if we have a GD post and the start of a var
- if ( ! empty( $gd_post->ID ) && strpos( $text, '%%' ) !== false ) {
- $non_replace = geodir_get_no_replace_fields();
+ if ( empty( $gd_post->ID ) || ! is_string( $text ) || strpos( $text, '%%' ) === false ) {
+ return $text;
+ }
+
+ // Only run if the text contains a %%variable%%.
+ if ( ! preg_match( '/%%[A-Za-z0-9_-]+%%/', $text ) ) {
+ return $text;
+ }
+
+ $non_replace = geodir_get_no_replace_fields();
- // Field key => type info, used to escape each replacement value for
- // the context it is output in.
- $field_types = geodir_dynamic_content_field_types( ! empty( $gd_post->post_type ) ? $gd_post->post_type : '' );
-
- foreach( $gd_post as $key => $val ) {
- if ( ! in_array( $key, $non_replace ) ) {
- // Replace plain variables.
- if ( strpos( $text, '%%' . $key . '%%' ) !== false ) {
- $val = apply_filters( 'geodir_replace_variables_' . $key, $val, $text );
-
- $replace_val = $val;
- if ( ! empty( $replace_val ) ) {
- $field_type = isset( $field_types[ $key ]['field_type'] ) ? $field_types[ $key ]['field_type'] : '';
- $data_type = isset( $field_types[ $key ]['data_type'] ) ? $field_types[ $key ]['data_type'] : '';
-
- if ( in_array( $key, array( 'post_content', 'post_excerpt' ), true ) ) {
- $field_type = 'html';
- }
+ // Field key => type info, used to escape each replacement value for
+ // the context it is output in.
+ $field_types = geodir_dynamic_content_field_types( ! empty( $gd_post->post_type ) ? $gd_post->post_type : '' );
+
+ foreach( $gd_post as $key => $val ) {
+ if ( ! in_array( $key, $non_replace ) ) {
+ // Replace plain variables.
+ if ( strpos( $text, '%%' . $key . '%%' ) !== false ) {
+ $val = apply_filters( 'geodir_replace_variables_' . $key, $val, $text );
+
+ $replace_val = $val;
+ if ( ! empty( $replace_val ) ) {
+ $field_type = isset( $field_types[ $key ]['field_type'] ) ? $field_types[ $key ]['field_type'] : '';
+ $data_type = isset( $field_types[ $key ]['data_type'] ) ? $field_types[ $key ]['data_type'] : '';
- $replace_val = geodir_escape_dynamic_value( $replace_val, $field_type, $data_type, array( 'key' => $key, 'text' => $text ) );
+ if ( in_array( $key, array( 'post_content', 'post_excerpt' ), true ) ) {
+ $field_type = 'html';
}
- $text = str_replace( '%%' . $key . '%%', $replace_val, $text );
+ $replace_val = geodir_escape_dynamic_value( $replace_val, $field_type, $data_type, array( 'key' => $key, 'text' => $text ) );
}
- // Replace encoded variables.
- if ( strpos( $text, '%%' . $key . '_encode%%' ) !== false ) {
- $encode_val = ! empty( $val ) ? urlencode( trim( $val ) ) : '';
- $encode_val = apply_filters( 'geodir_replace_variables_encode_' . $key, $encode_val, $text );
- $text = str_replace( '%%' . $key . '_encode%%', $encode_val, $text );
- }
+ $text = str_replace( '%%' . $key . '%%', $replace_val, $text );
+ }
+
+ // Replace encoded variables.
+ if ( strpos( $text, '%%' . $key . '_encode%%' ) !== false ) {
+ $encode_val = ! empty( $val ) ? urlencode( trim( $val ) ) : '';
+ $encode_val = apply_filters( 'geodir_replace_variables_encode_' . $key, $encode_val, $text );
+ $text = str_replace( '%%' . $key . '_encode%%', $encode_val, $text );
}
}
}
--- a/geodirectory/includes/post-types-functions.php
+++ b/geodirectory/includes/post-types-functions.php
@@ -125,33 +125,33 @@
* @return bool True if given post type is GD post type, otherwise False.
*/
function geodir_is_gd_post_type( $post_type ) {
- global $gd_is_post_type;
+ global $gd_is_post_type;
- if ( empty( $post_type ) || is_array($post_type) ) {
- return false;
- }
+ // Must be a scalar following the GD post type naming convention (gd_*).
+ if ( ! is_scalar( $post_type ) || strpos( (string) $post_type, 'gd_' ) !== 0 ) {
+ return false;
+ }
- if ( strpos( $post_type, 'gd_' ) !== 0 ) {
- return false;
- }
+ $post_type = (string) $post_type;
- if ( !empty( $gd_is_post_type ) && !empty( $gd_is_post_type[ $post_type ] ) ) {
- return true;
- }
+ // Fast path: already confirmed earlier in this request.
+ if ( ! empty( $gd_is_post_type[ $post_type ] ) ) {
+ return true;
+ }
- $gd_posttypes = geodir_get_posttypes();
+ $post_types = geodir_get_posttypes();
- if ( !empty( $gd_posttypes ) && in_array( $post_type, $gd_posttypes ) ) {
- if ( !is_array( $gd_is_post_type ) ) {
- $gd_is_post_type = array();
- }
+ if ( ! empty( $post_types ) && in_array( $post_type, $post_types, true ) ) {
+ if ( ! is_array( $gd_is_post_type ) ) {
+ $gd_is_post_type = array();
+ }
- $gd_is_post_type[ $post_type ] = true;
+ $gd_is_post_type[ $post_type ] = true;
- return true;
- }
+ return true;
+ }
- return false;
+ return false;
}
/**
@@ -626,53 +626,60 @@
* @return string The post type.
*/
function geodir_get_current_posttype() {
- global $wp_query, $post, $geodir_post_type;
+ global $post, $geodir_post_type;
- $geodir_post_type = get_query_var('post_type');
+ // Get from the queried post type.
+ $geodir_post_type = get_query_var( 'post_type' );
- if (geodir_is_page('add-listing') || geodir_is_page('preview')) {
- if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '')
- $geodir_post_type = get_post_type((int)$_REQUEST['pid']);
- elseif (isset($_REQUEST['listing_type']))
- $geodir_post_type = sanitize_text_field($_REQUEST['listing_type']);
- }
-
- if ((geodir_is_page('search') || geodir_is_page('author')) && isset($_REQUEST['stype']))
- $geodir_post_type = sanitize_text_field($_REQUEST['stype']);
+ // Add listing / preview pages: use the edited post, else the chosen listing type.
+ if ( geodir_is_page( 'add-listing' ) || geodir_is_page( 'preview' ) ) {
+ if ( ! empty( $_REQUEST['pid'] ) ) {
+ $geodir_post_type = get_post_type( absint( $_REQUEST['pid'] ) );
+ } elseif ( isset( $_REQUEST['listing_type'] ) && is_scalar( $_REQUEST['listing_type'] ) ) {
+ $geodir_post_type = sanitize_key( wp_unslash( $_REQUEST['listing_type'] ) );
+ }
+ }
- if (is_tax())
- $geodir_post_type = geodir_get_taxonomy_posttype();
+ // Search / author pages: use the submitted search type.
+ if ( ( geodir_is_page( 'search' ) || geodir_is_page( 'author' ) ) && isset( $_REQUEST['stype'] ) && is_scalar( $_REQUEST['stype'] ) ) {
+ $geodir_post_type = sanitize_key( wp_unslash( $_REQUEST['stype'] ) );
+ }
- // Retrieve post type for map marker html ajax request on preview page.
- if (empty($geodir_post_type) && defined('DOING_AJAX') && !empty($post)) {
- if (!empty($post->post_type)) {
- $geodir_post_type = $post->post_type;
- } else if (!empty($post->listing_type)) {
- $geodir_post_type = $post->listing_type;
- }
- }
+ // Taxonomy archives.
+ if ( is_tax() ) {
+ $geodir_post_type = geodir_get_taxonomy_posttype();
+ }
- $all_postypes = geodir_get_posttypes();
- $all_postypes = stripslashes_deep($all_postypes);
+ // AJAX search requests (eg: GD Fast AJAX) pass the post type via "stype".
+ if ( defined( 'DOING_AJAX' ) && isset( $_REQUEST['stype'] ) && is_scalar( $_REQUEST['stype'] ) ) {
+ $geodir_post_type = sanitize_key( wp_unslash( $_REQUEST['stype'] ) );
+ }
- if (is_array($all_postypes) && !in_array($geodir_post_type, $all_postypes))
- $geodir_post_type = '';
+ // Map marker HTML AJAX request on the preview page: read from the global post.
+ if ( empty( $geodir_post_type ) && defined( 'DOING_AJAX' ) && ! empty( $post ) ) {
+ if ( ! empty( $post->post_type ) ) {
+ $geodir_post_type = $post->post_type;
+ } elseif ( ! empty( $post->listing_type ) ) {
+ $geodir_post_type = $post->listing_type;
+ }
+ }
- if( defined( 'DOING_AJAX' ) && isset($_REQUEST['stype'])){
- $geodir_post_type = sanitize_text_field($_REQUEST['stype']);
- }
+ // Check GD post type.
+ if ( ! geodir_is_gd_post_type( $geodir_post_type ) ) {
+ $geodir_post_type = '';
+ }
- // Set default past type on search page when stype is not set.
- if ( empty( $geodir_post_type ) && geodir_is_page( 'search' ) ) {
- $geodir_post_type = geodir_get_default_posttype();
- }
+ // Fall back to the default post type on the search page.
+ if ( empty( $geodir_post_type ) && geodir_is_page( 'search' ) ) {
+ $geodir_post_type = geodir_get_default_posttype();
+ }
- /**
- * Filter the default CPT return.
- *
- * @since 1.6.9
- */
- return apply_filters('geodir_get_current_posttype',$geodir_post_type);
+ /**
+ * Filter the default CPT return.
+ *
+ * @since 1.6.9
+ */
+ return apply_filters( 'geodir_get_current_posttype', $geodir_post_type );
}
/**
--- a/geodirectory/includes/widgets/class-geodir-widget-recent-reviews.php
+++ b/geodirectory/includes/widgets/class-geodir-widget-recent-reviews.php
@@ -593,6 +593,7 @@
$comment_id = $comment->comment_ID;
$comment_content = strip_tags( $comment->comment_content );
$comment_content = preg_replace( '#(\[img\]).+(\[\/img\])#', '', $comment_content );
+ $comment_content = geodir_neutralize_untrusted_content( $comment_content );
$permalink = get_permalink( $comment->post_id ) . '#comment-' . $comment->comment_ID;
$comment_author_email = $comment->comment_author_email;
--- a/geodirectory/includes/widgets/class-geodir-widget-single-tabs.php
+++ b/geodirectory/includes/widgets/class-geodir-widget-single-tabs.php
@@ -409,7 +409,14 @@
// Main content
if ( ! empty( $tab->tab_content ) ) { // override content
- $content = geodir_replace_variables( stripslashes( $tab->tab_content ) );
+ $_tab_content = stripslashes( $tab->tab_content );
+ $content = geodir_replace_variables( $_tab_content );
+
+ // Sanitize after %%variables%% are replaced, so field values can't inject attributes/scripts.
+ if ( $content !== $_tab_content ) {
+ $content = geodir_sanitize_html_field( wp_slash( $content ) );
+ }
+
echo do_shortcode( $content );
} elseif ( $tab->tab_type == 'meta' ) { // meta info
echo do_shortcode( '[gd_post_meta key="' . $tab->tab_key . '" show="value"]' );
@@ -467,7 +474,14 @@
do_action( 'geodir_single_tab_content_before', $child_tab, $tab );
if ( ! empty( $child_tab->tab_content ) ) { // override content
- $_content = geodir_replace_variables( stripslashes( $child_tab->tab_content ) );
+ $_tab_content = stripslashes( $child_tab->tab_content );
+ $_content = geodir_replace_variables( $_tab_content );
+
+ // Sanitize after %%variables%% are replaced, so field values can't inject attributes/scripts.
+ if ( $_content !== $_tab_content ) {
+ $_content = geodir_sanitize_html_field( wp_slash( $_content ) );
+ }
+
echo do_shortcode( $_content );
} elseif ( $child_tab->tab_type == 'meta' ) { // meta info
echo do_shortcode( '[gd_post_meta key="' . $child_tab->tab_key . '"]' );
--- a/geodirectory/templates/bootstrap/reviews/recent-item-clean.php
+++ b/geodirectory/templates/bootstrap/reviews/recent-item-clean.php
@@ -1,12 +1,19 @@
<?php
-
-if ( ! defined( 'ABSPATH' ) ) {
- exit;
-}
-
-global $aui_bs5;
-
/**
+ * Display Recent Review Item
+ *
+ * This template can be overridden by copying it to yourtheme/geodirectory/bootstrap/reviews/recent-item-clean.php.
+ *
+ * HOWEVER, on occasion GeoDirectory will need to update template files and you
+ * (the theme developer) will need to copy the new files to your theme to
+ * maintain compatibility. We try to do this as little as possible, but it does
+ * happen. When this occurs the version of the template file will be bumped and
+ * the readme will list any important changes.
+ *
+ * @see https://wpgeodirectory.com/documentation/article/how-tos/customizing-templates/
+ * @package GeoDirectory
+ * @version 2.8.209
+ *
* Variables.
*
* @var object $comment The comment object.
@@ -21,6 +28,12 @@
* @var bool $active If the item is in a carousel and active.
*/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+global $aui_bs5;
+
$comment_content = wp_strip_all_tags( $comment->comment_content );
$comment_content = preg_replace( '#(\[img\]).+(\[\/img\])#', '', $comment_content );
$comment_excerpt = geodir_utf8_substr( $comment_content, 0, 150 ) . '…';
@@ -29,7 +42,7 @@
<div class="card h-100 mw-100 shadow-sm border-0">
<div class="card-body">
<div class="fs-xs mb-2"><?php echo geodir_get_rating_stars( $comment->rating, $comment_post_ID ); ?></div>
- <p class="geodir_reviewer_text p-0 m-0 fs-base text-muted"><?php echo esc_html( $comment_excerpt ); ?></p>
+ <p class="geodir_reviewer_text p-0 m-0 fs-base text-muted"><?php echo geodir_neutralize_untrusted_content( esc_html( $comment_excerpt ) ); ?></p>
</div>
<div class="card-footer border-0 pt-0 d-flex align-items-center">