Plugin Directory

Changeset 3825366


Ignore:
Timestamp:
11/26/2026 05:11:16 AM (less than one hour ago)
Author:
mra13
Message:
  • Fixed a path traversal issue where a crafted "Downloadable File" URL containing "../" could be used to download files from outside the intended folder. The file path is now validated to stay inside the expected folder, and such URLs can no longer be saved. PHP-type file extensions are now always blocked from PHP dispatch.
  • The export logs feature now checks the user's permission, only accepts valid sort columns, and no longer shows database error messages in the browser.
Location:
simple-download-monitor/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • simple-download-monitor/trunk/includes/admin-side/sdm-admin-edit-download.php

    r3690777 r3825366  
    88                // Grabs the inserted post data so we can sanitize/modify it.
    99                add_filter( 'wp_insert_post_data' , array( $this, 'insert_post_sdm_post_title' ), '99', 1 );
     10                add_action( 'admin_notices', array( $this, 'show_rejected_upload_url_notice' ) );
    1011
    1112                if ( wp_doing_ajax() ) {
    … …  
    347348        }
    348349
     350        public function show_rejected_upload_url_notice() {
     351                $transient_key = 'sdm_upload_url_rejected_' . get_current_user_id();
     352                if ( ! get_transient( $transient_key ) ) {
     353                        return;
     354                }
     355                delete_transient( $transient_key );
     356                echo '<div class="notice notice-error is-dismissible"><p>';
     357                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' );
     358                echo '</p></div>';
     359        }
     360
    349361        public function save_post_handler( $post_id, $post, $update ) {
    350362                if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! $update || empty( $post_id ) ) {
    … …  
    374386                // *** File Upload ***
    375387                if ( isset( $_POST['sdm_upload'] ) ) {
    376                         update_post_meta( $post_id, 'sdm_upload', esc_url_raw( $_POST['sdm_upload'], array( 'http', 'https', 'dropbox' ) ) );
     388                        $sdm_upload_url = esc_url_raw( wp_unslash( $_POST['sdm_upload'] ), array( 'http', 'https', 'dropbox' ) );
     389                        if ( SDM_Utils_File_System_Related::path_has_traversal( wp_parse_url( $sdm_upload_url, PHP_URL_PATH ) ) ) {
     390                                // Don't save a file URL that contains "../" (path traversal). Keep the previous value and show an error notice.
     391                                set_transient( 'sdm_upload_url_rejected_' . get_current_user_id(), 1, 60 );
     392                        } else {
     393                                update_post_meta( $post_id, 'sdm_upload', $sdm_upload_url );
     394                        }
    377395                }
    378396
  • simple-download-monitor/trunk/includes/sdm-utility-file-system-related.php

    r3168162 r3825366  
    5252         * @param array $sub_dirs  Subdirectory. Use multiple items for nested directories.
    5353         *
    54          * @return string|false File Path. FALSE if path couldn't be resolved.
     54         * @return string|false File Path. FALSE if path couldn't be resolved, or if it resolves to a location outside the expected directory.
    5555         */
    5656        public static function get_uploaded_file_path_from_url($file_url, $sub_dirs = array()){
    5757                if(!empty($sub_dirs)){
    58                         $sub_dirs = implode(DIRECTORY_SEPARATOR, $sub_dirs);
    59                         $sub_dirs = trim(trim($sub_dirs), DIRECTORY_SEPARATOR);
     58                        $sub_dirs = implode('/', $sub_dirs);
     59                        $sub_dirs = trim(trim($sub_dirs), '/\\');
    6060
    6161                        $wp_content_dir = path_join(WP_CONTENT_DIR, $sub_dirs);
    … …  
    6666                }
    6767
    68                 $file_path = path_join( $wp_content_dir, ltrim( substr( $file_url, strlen( $wp_content_url ) ), '/' ) );
    69                 $file_path = realpath( $file_path );
    70 
    71                 return $file_path;
     68                if ( ! is_string( $file_url ) || $file_url === '' ) {
     69                        return false;
     70                }
     71
     72                // 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.
     73                $file_url_no_scheme = self::remove_url_scheme( trim( $file_url ) );
     74                $base_url_no_scheme = rtrim( self::remove_url_scheme( $wp_content_url ), '/' ) . '/';
     75                if ( stripos( $file_url_no_scheme, $base_url_no_scheme ) !== 0 ) {
     76                        return false;
     77                }
     78
     79                $relative_path = substr( $file_url_no_scheme, strlen( $base_url_no_scheme ) );
     80
     81                // Reject path traversal ("." or ".." path segments).
     82                if ( self::path_has_traversal( $relative_path ) ) {
     83                        return false;
     84                }
     85
     86                $file_path = realpath( path_join( $wp_content_dir, ltrim( $relative_path, '/\\' ) ) );
     87                if ( false === $file_path ) {
     88                        return false;
     89                }
     90
     91                // Make sure the resolved path is still inside the expected directory.
     92                $allowed_base_dirs = array( $wp_content_dir );
     93                if ( empty( $sub_dirs ) && function_exists( 'wp_upload_dir' ) ) {
     94                        // The uploads folder is sometimes a symlink to a location outside wp-content. Files in it are allowed too.
     95                        $upload_dir = wp_upload_dir( null, false );
     96                        if ( ! empty( $upload_dir['basedir'] ) ) {
     97                                $allowed_base_dirs[] = $upload_dir['basedir'];
     98                        }
     99                }
     100
     101                foreach ( $allowed_base_dirs as $base_dir ) {
     102                        $base_dir = realpath( $base_dir );
     103                        if ( false !== $base_dir && strpos( $file_path, rtrim( $base_dir, '/\\' ) . DIRECTORY_SEPARATOR ) === 0 ) {
     104                                return $file_path;
     105                        }
     106                }
     107
     108                return false;
     109        }
     110
     111        /**
     112         * Removes the scheme (for example "https:") from a URL. The result starts with "//" for absolute URLs.
     113         *
     114         * @param string $url
     115         *
     116         * @return string
     117         */
     118        public static function remove_url_scheme( $url ) {
     119                return preg_replace( '#^[a-z][a-z0-9+.\-]*:#i', '', $url );
     120        }
     121
     122        /**
     123         * Checks if a path or URL contains path traversal segments ("." or ".."), including URL-encoded ones.
     124         *
     125         * @param string $path
     126         *
     127         * @return bool TRUE if the path contains traversal segments, FALSE otherwise.
     128         */
     129        public static function path_has_traversal( $path ) {
     130                // Ignore the query string and fragment (if any).
     131                $path = preg_replace( '/[?#].*$/s', '', (string) $path );
     132                // Decode twice to catch double-encoded sequences, and treat backslashes as path separators.
     133                $path = str_replace( '\\', '/', rawurldecode( rawurldecode( $path ) ) );
     134
     135                return (bool) preg_match( '#(^|/)\.{1,2}(/|$)#', $path );
    72136        }
    73137
    … …  
    106170                $disallowed_ext_arr_raw = explode( ',', strtolower( $disallowed_ext_opt ) );
    107171
    108                 $disallowed_ext_arr = array();
     172                // These PHP and script extensions are always blocked, regardless of the settings.
     173                $disallowed_ext_arr = array( 'php', 'php3', 'php4', 'php5', 'php7', 'php8', 'phps', 'phtml', 'phar', 'pht' );
    109174
    110175                foreach ( $disallowed_ext_arr_raw as $item ) {
  • simple-download-monitor/trunk/main.php

    r3816108 r3825366  
    44 * Plugin URI: https://simple-download-monitor.com/
    55 * Description: Easily manage downloadable files and monitor downloads of your digital files from your WordPress site.
    6  * Version: 4.1.5
     6 * Version: 4.1.6
    77 * Author: Tips and Tricks HQ, Ruhul Amin, Josh Lobe
    88 * Author URI: https://www.tipsandtricks-hq.com/development-center
    … …  
    1515}
    1616
    17 define( 'WP_SIMPLE_DL_MONITOR_VERSION', '4.1.5' );
     17define( 'WP_SIMPLE_DL_MONITOR_VERSION', '4.1.6' );
    1818define( 'WP_SIMPLE_DL_MONITOR_DIR_NAME', dirname( plugin_basename( __FILE__ ) ) );
    1919define( 'WP_SIMPLE_DL_MONITOR_URL', plugins_url( '', __FILE__ ) );
    … …  
    261261        }
    262262
     263        if ( ! current_user_can( get_sdm_admin_access_permission() ) ) {
     264                wp_send_json_error(array(
     265                        'message' => __('You do not have permission to export the download logs.', 'simple-download-monitor')
     266                ));
     267        }
     268
    263269        // Set the maximum execution time to 2 minutes.
    264270        set_time_limit( 60 * 2 );
    … …  
    268274        $table_name = $wpdb->prefix . 'sdm_downloads';
    269275
    270         $search_text = isset( $_POST['search'] ) ? sanitize_text_field( $_POST['search'] ) : '';
    271         $orderby_column = isset( $_POST['orderBy'] ) ? sanitize_text_field( $_POST['orderBy'] ) : 'id';
    272         $sort_order     = isset( $_POST['order'] ) ? sanitize_text_field( $_POST['order'] ) : 'desc';
    273 
    274         $order_by_clause = sanitize_sql_orderby("$orderby_column $sort_order");
     276        $search_text    = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
     277        $orderby_column = isset( $_POST['orderBy'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['orderBy'] ) ) ) : '';
     278        $sort_order     = isset( $_POST['order'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['order'] ) ) ) : '';
     279
     280        // Only allow sorting by the columns that the download logs table can be sorted by.
     281        $allowed_orderby_columns = array( 'id', 'post_title', 'file_url', 'visitor_ip', 'date_time', 'visitor_country', 'visitor_name', 'user_agent', 'referrer_url' );
     282        if ( ! in_array( $orderby_column, $allowed_orderby_columns, true ) ) {
     283                $orderby_column = 'id';
     284        }
     285        if ( ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
     286                $sort_order = 'DESC';
     287        }
     288
     289        $order_by_clause = "$orderby_column $sort_order";
    275290
    276291        $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'";
    277292
     293        $where_args = array();
    278294        if(empty($search_text)){
    279295                $total_items = $wpdb->get_var(  "SELECT COUNT(*) FROM $table_name" );
    280296                $query       = "SELECT $select_cols FROM $table_name ORDER BY $order_by_clause LIMIT %d, %d";
    281297        } else {
    282                 $columns = ['post_title', 'post_id', 'visitor_name', 'visitor_ip', 'visitor_country'];
    283                 $like_query = [];
     298                $columns     = array( 'post_title', 'post_id', 'visitor_name', 'visitor_ip', 'visitor_country' );
     299                $search_like = '%' . $wpdb->esc_like( $search_text ) . '%';
     300                $like_query  = array();
    284301                foreach ($columns as $column) {
    285                         $like_query[] = "$column LIKE '%" . esc_sql($search_text) . "%'";
     302                        $like_query[] = "$column LIKE %s";
     303                        $where_args[] = $search_like;
    286304                }
    287305
    288306                $where_clause = implode(' OR ', $like_query);
    289307
    290                 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name WHERE $where_clause" );
     308                $total_items = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE $where_clause", $where_args ) );
    291309                $query       = "SELECT $select_cols FROM $table_name WHERE $where_clause ORDER BY $order_by_clause LIMIT %d, %d";
    292310        }
    … …  
    301319                $offset = ( $current_page - 1 ) * $records_per_page;
    302320
    303                 $query_prepared = $wpdb->prepare( $query, $offset, $records_per_page );
     321                $query_prepared = $wpdb->prepare( $query, array_merge( $where_args, array( $offset, $records_per_page ) ) );
    304322
    305323                $results = $wpdb->get_results( $query_prepared, ARRAY_A );
    306324
    307                 // Check if there were any error during query.
     325                // 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).
    308326                if ( ! empty( $wpdb->last_error ) ) {
     327                        SDM_Debug::log( 'Export logs query failed. Database error: ' . $wpdb->last_error, false );
    309328                        wp_send_json_error(array(
    310                                 'message' => $wpdb->last_error,
     329                                'message' => __('Error! The download logs could not be retrieved.', 'simple-download-monitor'),
    311330                        ));
    312331                }
  • simple-download-monitor/trunk/readme.txt

    r3816108 r3825366  
    55Requires at least: 6.0
    66Tested up to: 7.1
    7 Stable tag: 4.1.5
     7Stable tag: 4.1.6
    88License: GPLv2 or later
    99
    … …  
    202202== Changelog ==
    203203
     204= 4.1.6 =
     205- Fixed a path traversal issue where a crafted "Downloadable File" URL containing "../" could be used to download files from outside the intended folder. The file path is now validated to stay inside the expected folder, and such URLs can no longer be saved. PHP-type file extensions are now always blocked from PHP dispatch.
     206- The export logs feature now checks the user's permission, only accepts valid sort columns, and no longer shows database error messages in the browser.
     207
    204208= 4.1.5 =
    205209- The debug log file is now stored in the plugin's "logs" folder, which is protected from direct web access, and its file name has a random prefix that is unique to each site.
Note: See TracChangeset for help on using the changeset viewer.