Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/simple-download-monitor/includes/admin-side/sdm-admin-edit-download.php
+++ b/simple-download-monitor/includes/admin-side/sdm-admin-edit-download.php
@@ -7,6 +7,7 @@
add_action( 'save_post_sdm_downloads', array( $this, 'save_post_handler' ), 10, 3 );
// Grabs the inserted post data so we can sanitize/modify it.
add_filter( 'wp_insert_post_data' , array( $this, 'insert_post_sdm_post_title' ), '99', 1 );
+ add_action( 'admin_notices', array( $this, 'show_rejected_upload_url_notice' ) );
if ( wp_doing_ajax() ) {
add_action( 'wp_ajax_sdm_remove_thumbnail_image', array( $this, 'remove_thumbnail_image_ajax_handler' ) );
@@ -346,6 +347,17 @@
return $data; // Returns the modified data.
}
+ public function show_rejected_upload_url_notice() {
+ $transient_key = 'sdm_upload_url_rejected_' . get_current_user_id();
+ if ( ! get_transient( $transient_key ) ) {
+ return;
+ }
+ delete_transient( $transient_key );
+ echo '<div class="notice notice-error is-dismissible"><p>';
+ esc_html_e( 'The Downloadable File URL was not saved because it contains "../" (path traversal), which is not allowed. Please enter the direct URL of the file.', 'simple-download-monitor' );
+ echo '</p></div>';
+ }
+
public function save_post_handler( $post_id, $post, $update ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! $update || empty( $post_id ) ) {
return;
@@ -373,7 +385,13 @@
// *** File Upload ***
if ( isset( $_POST['sdm_upload'] ) ) {
- update_post_meta( $post_id, 'sdm_upload', esc_url_raw( $_POST['sdm_upload'], array( 'http', 'https', 'dropbox' ) ) );
+ $sdm_upload_url = esc_url_raw( wp_unslash( $_POST['sdm_upload'] ), array( 'http', 'https', 'dropbox' ) );
+ if ( SDM_Utils_File_System_Related::path_has_traversal( wp_parse_url( $sdm_upload_url, PHP_URL_PATH ) ) ) {
+ // Don't save a file URL that contains "../" (path traversal). Keep the previous value and show an error notice.
+ set_transient( 'sdm_upload_url_rejected_' . get_current_user_id(), 1, 60 );
+ } else {
+ update_post_meta( $post_id, 'sdm_upload', $sdm_upload_url );
+ }
}
// *** PHP Dispatch or Redirect ***
--- a/simple-download-monitor/includes/sdm-utility-file-system-related.php
+++ b/simple-download-monitor/includes/sdm-utility-file-system-related.php
@@ -51,12 +51,12 @@
* @param string $file_url File URL
* @param array $sub_dirs Subdirectory. Use multiple items for nested directories.
*
- * @return string|false File Path. FALSE if path couldn't be resolved.
+ * @return string|false File Path. FALSE if path couldn't be resolved, or if it resolves to a location outside the expected directory.
*/
public static function get_uploaded_file_path_from_url($file_url, $sub_dirs = array()){
if(!empty($sub_dirs)){
- $sub_dirs = implode(DIRECTORY_SEPARATOR, $sub_dirs);
- $sub_dirs = trim(trim($sub_dirs), DIRECTORY_SEPARATOR);
+ $sub_dirs = implode('/', $sub_dirs);
+ $sub_dirs = trim(trim($sub_dirs), '/\');
$wp_content_dir = path_join(WP_CONTENT_DIR, $sub_dirs);
$wp_content_url = rtrim(WP_CONTENT_URL, '/') . '/' . $sub_dirs;
@@ -65,10 +65,74 @@
$wp_content_url = WP_CONTENT_URL;
}
- $file_path = path_join( $wp_content_dir, ltrim( substr( $file_url, strlen( $wp_content_url ) ), '/' ) );
- $file_path = realpath( $file_path );
+ if ( ! is_string( $file_url ) || $file_url === '' ) {
+ return false;
+ }
+
+ // The file URL must start with the expected base URL. The scheme is ignored so that URLs saved before a site moved from http to https keep working.
+ $file_url_no_scheme = self::remove_url_scheme( trim( $file_url ) );
+ $base_url_no_scheme = rtrim( self::remove_url_scheme( $wp_content_url ), '/' ) . '/';
+ if ( stripos( $file_url_no_scheme, $base_url_no_scheme ) !== 0 ) {
+ return false;
+ }
+
+ $relative_path = substr( $file_url_no_scheme, strlen( $base_url_no_scheme ) );
+
+ // Reject path traversal ("." or ".." path segments).
+ if ( self::path_has_traversal( $relative_path ) ) {
+ return false;
+ }
+
+ $file_path = realpath( path_join( $wp_content_dir, ltrim( $relative_path, '/\' ) ) );
+ if ( false === $file_path ) {
+ return false;
+ }
+
+ // Make sure the resolved path is still inside the expected directory.
+ $allowed_base_dirs = array( $wp_content_dir );
+ if ( empty( $sub_dirs ) && function_exists( 'wp_upload_dir' ) ) {
+ // The uploads folder is sometimes a symlink to a location outside wp-content. Files in it are allowed too.
+ $upload_dir = wp_upload_dir( null, false );
+ if ( ! empty( $upload_dir['basedir'] ) ) {
+ $allowed_base_dirs[] = $upload_dir['basedir'];
+ }
+ }
+
+ foreach ( $allowed_base_dirs as $base_dir ) {
+ $base_dir = realpath( $base_dir );
+ if ( false !== $base_dir && strpos( $file_path, rtrim( $base_dir, '/\' ) . DIRECTORY_SEPARATOR ) === 0 ) {
+ return $file_path;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Removes the scheme (for example "https:") from a URL. The result starts with "//" for absolute URLs.
+ *
+ * @param string $url
+ *
+ * @return string
+ */
+ public static function remove_url_scheme( $url ) {
+ return preg_replace( '#^[a-z][a-z0-9+.-]*:#i', '', $url );
+ }
+
+ /**
+ * Checks if a path or URL contains path traversal segments ("." or ".."), including URL-encoded ones.
+ *
+ * @param string $path
+ *
+ * @return bool TRUE if the path contains traversal segments, FALSE otherwise.
+ */
+ public static function path_has_traversal( $path ) {
+ // Ignore the query string and fragment (if any).
+ $path = preg_replace( '/[?#].*$/s', '', (string) $path );
+ // Decode twice to catch double-encoded sequences, and treat backslashes as path separators.
+ $path = str_replace( '\', '/', rawurldecode( rawurldecode( $path ) ) );
- return $file_path;
+ return (bool) preg_match( '#(^|/).{1,2}(/|$)#', $path );
}
/**
@@ -105,7 +169,8 @@
$disallowed_ext_arr_raw = explode( ',', strtolower( $disallowed_ext_opt ) );
- $disallowed_ext_arr = array();
+ // These PHP and script extensions are always blocked, regardless of the settings.
+ $disallowed_ext_arr = array( 'php', 'php3', 'php4', 'php5', 'php7', 'php8', 'phps', 'phtml', 'phar', 'pht' );
foreach ( $disallowed_ext_arr_raw as $item ) {
array_push( $disallowed_ext_arr, sanitize_text_field( $item ) );
--- a/simple-download-monitor/main.php
+++ b/simple-download-monitor/main.php
@@ -3,7 +3,7 @@
* Plugin Name: Simple Download Monitor
* Plugin URI: https://simple-download-monitor.com/
* Description: Easily manage downloadable files and monitor downloads of your digital files from your WordPress site.
- * Version: 4.1.5
+ * Version: 4.1.6
* Author: Tips and Tricks HQ, Ruhul Amin, Josh Lobe
* Author URI: https://www.tipsandtricks-hq.com/development-center
* License: GPL2
@@ -14,7 +14,7 @@
exit;
}
-define( 'WP_SIMPLE_DL_MONITOR_VERSION', '4.1.5' );
+define( 'WP_SIMPLE_DL_MONITOR_VERSION', '4.1.6' );
define( 'WP_SIMPLE_DL_MONITOR_DIR_NAME', dirname( plugin_basename( __FILE__ ) ) );
define( 'WP_SIMPLE_DL_MONITOR_URL', plugins_url( '', __FILE__ ) );
define( 'WP_SIMPLE_DL_MONITOR_PATH', plugin_dir_path( __FILE__ ) );
@@ -260,6 +260,12 @@
));
}
+ if ( ! current_user_can( get_sdm_admin_access_permission() ) ) {
+ wp_send_json_error(array(
+ 'message' => __('You do not have permission to export the download logs.', 'simple-download-monitor')
+ ));
+ }
+
// Set the maximum execution time to 2 minutes.
set_time_limit( 60 * 2 );
@@ -267,27 +273,39 @@
$table_name = $wpdb->prefix . 'sdm_downloads';
- $search_text = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
- $orderby_column = isset( $_POST['orderBy'] ) ? sanitize_text_field( $_POST['orderBy'] ) : 'id';
- $sort_order = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'desc';
+ $search_text = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
+ $orderby_column = isset( $_POST['orderBy'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['orderBy'] ) ) ) : '';
+ $sort_order = isset( $_POST['order'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['order'] ) ) ) : '';
+
+ // Only allow sorting by the columns that the download logs table can be sorted by.
+ $allowed_orderby_columns = array( 'id', 'post_title', 'file_url', 'visitor_ip', 'date_time', 'visitor_country', 'visitor_name', 'user_agent', 'referrer_url' );
+ if ( ! in_array( $orderby_column, $allowed_orderby_columns, true ) ) {
+ $orderby_column = 'id';
+ }
+ if ( ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
+ $sort_order = 'DESC';
+ }
- $order_by_clause = sanitize_sql_orderby("$orderby_column $sort_order");
+ $order_by_clause = "$orderby_column $sort_order";
$select_cols = "id as 'Log ID', post_id as 'Download ID', post_title as 'Download Title', file_url as 'File URL',date_time as 'Date',visitor_ip as 'IP Address', visitor_country as 'Country', visitor_name as 'Name'";
+ $where_args = array();
if(empty($search_text)){
$total_items = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
$query = "SELECT $select_cols FROM $table_name ORDER BY $order_by_clause LIMIT %d, %d";
} else {
- $columns = ['post_title', 'post_id', 'visitor_name', 'visitor_ip', 'visitor_country'];
- $like_query = [];
+ $columns = array( 'post_title', 'post_id', 'visitor_name', 'visitor_ip', 'visitor_country' );
+ $search_like = '%' . $wpdb->esc_like( $search_text ) . '%';
+ $like_query = array();
foreach ($columns as $column) {
- $like_query[] = "$column LIKE '%" . esc_sql($search_text) . "%'";
+ $like_query[] = "$column LIKE %s";
+ $where_args[] = $search_like;
}
$where_clause = implode(' OR ', $like_query);
- $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name WHERE $where_clause" );
+ $total_items = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE $where_clause", $where_args ) );
$query = "SELECT $select_cols FROM $table_name WHERE $where_clause ORDER BY $order_by_clause LIMIT %d, %d";
}
@@ -300,14 +318,15 @@
for ( $current_page = 1; $current_page <= $total_pages; $current_page++ ) {
$offset = ( $current_page - 1 ) * $records_per_page;
- $query_prepared = $wpdb->prepare( $query, $offset, $records_per_page );
+ $query_prepared = $wpdb->prepare( $query, array_merge( $where_args, array( $offset, $records_per_page ) ) );
$results = $wpdb->get_results( $query_prepared, ARRAY_A );
- // Check if there were any error during query.
+ // Check if there were any error during query. Log the database error and show a generic message (don't send the database error to the browser).
if ( ! empty( $wpdb->last_error ) ) {
+ SDM_Debug::log( 'Export logs query failed. Database error: ' . $wpdb->last_error, false );
wp_send_json_error(array(
- 'message' => $wpdb->last_error,
+ 'message' => __('Error! The download logs could not be retrieved.', 'simple-download-monitor'),
));
}