Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wpvivid-backuprestore/admin/class-wpvivid-admin.php
+++ b/wpvivid-backuprestore/admin/class-wpvivid-admin.php
@@ -577,6 +577,16 @@
<?php
}
+ /**
+ * Displays the legacy Free migration and restore completion notices.
+ *
+ * The method is no longer registered by the current Free version. It is
+ * retained temporarily for compatibility with older Free/Pro combinations.
+ *
+ * @deprecated 0.9.135
+ *
+ * @return void
+ */
public function migrate_notice()
{
$migrate_notice=false;
--- a/wpvivid-backuprestore/includes/backup-registration/class-wpvivid-backup-registration.php
+++ b/wpvivid-backuprestore/includes/backup-registration/class-wpvivid-backup-registration.php
@@ -0,0 +1,331 @@
+<?php
+
+if (!defined('WPVIVID_PLUGIN_DIR'))
+{
+ die;
+}
+
+class WPvivid_Backup_Registration
+{
+ public function register_backup($backup_id, $manifest, $args=array())
+ {
+ if (!$this->is_valid_backup_id($backup_id))
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Invalid backup ID.');
+ }
+
+ if (!is_array($manifest) || empty($manifest))
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Invalid backup file manifest.');
+ }
+
+ $backup_dir=realpath(WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir());
+ if ($backup_dir===false || !is_dir($backup_dir))
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Failed to get the local backup directory.');
+ }
+
+ $normalized_backup_dir=trailingslashit(wp_normalize_path($backup_dir));
+ $backup_files=array();
+ $seen=array();
+
+ $verify_md5 = isset($args['verify_md5']) ? (bool)$args['verify_md5'] : true;
+
+ foreach ($manifest as $item)
+ {
+ $ret=$this->validate_file($backup_id, $backup_dir, $normalized_backup_dir, $item, $verify_md5);
+ if ($ret['result']!==WPVIVID_SUCCESS)
+ {
+ return $ret;
+ }
+
+ $file_name=$ret['file']['file_name'];
+ if (isset($seen[$file_name]))
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Duplicate backup file in manifest.');
+ }
+
+ $seen[$file_name]=true;
+ $backup_files[]=$ret['file'];
+ }
+
+ usort($backup_files, function($a, $b)
+ {
+ return strcmp($a['file_name'], $b['file_name']);
+ });
+
+ $backup_result=array(
+ 'result'=>WPVIVID_SUCCESS,
+ 'files'=>$backup_files,
+ );
+
+ $type=isset($args['type']) && is_string($args['type']) ? $args['type'] : 'Upload';
+ $log=isset($args['log']) && is_string($args['log']) ? $args['log'] : '';
+
+ $backup_data=array();
+ $backup_data['type']=$type;
+ $backup_data['create_time']=$this->get_create_time($backup_files);
+ $backup_data['manual_delete']=0;
+ $backup_data['local']['path']=WPvivid_Setting::get_backupdir();
+ $backup_data['compress']['compress_type']='zip';
+ $backup_data['save_local']=1;
+ $backup_data['log']=$log;
+ $backup_data['backup']=$backup_result;
+ $backup_data['remote']=array();
+ $backup_data['lock']=0;
+
+ $list_name='wpvivid_backup_list';
+ $list_name=apply_filters('get_wpvivid_backup_list_name', $list_name, $backup_id, $backup_data);
+ $list=WPvivid_Setting::get_option($list_name);
+ if (!is_array($list))
+ {
+ $list=array();
+ }
+
+ if (isset($list[$backup_id]))
+ {
+ if ($this->has_same_files($list[$backup_id], $backup_files))
+ {
+ return array(
+ 'result'=>WPVIVID_SUCCESS,
+ 'backup_id'=>$backup_id,
+ 'existing'=>true,
+ );
+ }
+
+ return array(
+ 'result'=>WPVIVID_FAILED,
+ 'error'=>'A different backup with the same ID already exists.',
+ );
+ }
+
+ $list[$backup_id]=$backup_data;
+ WPvivid_Setting::update_option($list_name, $list);
+
+ return array(
+ 'result'=>WPVIVID_SUCCESS,
+ 'backup_id'=>$backup_id,
+ 'existing'=>false,
+ );
+ }
+
+ private function validate_file($backup_id, $backup_dir, $normalized_backup_dir, $item, $verify_md5)
+ {
+ if (!is_array($item) || !isset($item['file_name']) || !is_string($item['file_name']))
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Invalid backup file information.');
+ }
+
+ $file_name=wp_normalize_path($item['file_name']);
+ if ($file_name==='' ||
+ $file_name==='.' ||
+ $file_name==='..' ||
+ basename($file_name)!==$file_name ||
+ validate_file($file_name)!==0 ||
+ preg_match('/A[a-zA-Z0-9._-]+z/', $file_name)!==1 ||
+ strtolower(pathinfo($file_name, PATHINFO_EXTENSION))!=='zip')
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Invalid backup file name.');
+ }
+
+ $file_backup_id=$this->get_backup_id_from_file_name($file_name);
+
+ $request_token = $this->get_backup_id_token($backup_id);
+ $file_token = $this->get_backup_id_token($file_backup_id);
+ if ($request_token === false || $file_token === false || !hash_equals($request_token, $file_token))
+ {
+ return array(
+ 'result' => WPVIVID_FAILED,
+ 'error' => 'The backup file ID does not match.',
+ );
+ }
+
+ $file_path=realpath($backup_dir.DIRECTORY_SEPARATOR.$file_name);
+ if ($file_path===false || !is_file($file_path) || strpos(wp_normalize_path($file_path), $normalized_backup_dir)!==0)
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Backup file is outside the backup directory or does not exist.');
+ }
+
+ $actual_size=filesize($file_path);
+ if ($actual_size === false)
+ {
+ return array(
+ 'result' => WPVIVID_FAILED,
+ 'error' => 'Failed to read the backup file.',
+ );
+ }
+
+ if (isset($item['size']))
+ {
+ $expected_size=filter_var($item['size'], FILTER_VALIDATE_INT, array('options'=>array('min_range'=>0)));
+ if ($expected_size===false || $actual_size!==$expected_size)
+ {
+ return array('result'=>WPVIVID_FAILED, 'error'=>'Backup file size does not match.');
+ }
+ }
+
+ $actual_md5 = '';
+ if ($verify_md5)
+ {
+ if (!isset($item['md5']) || !is_string($item['md5']) || preg_match('/A[a-fA-F0-9]{32}z/', $item['md5']) !== 1)
+ {
+ return array(
+ 'result' => WPVIVID_FAILED,
+ 'error' => 'Invalid backup file checksum.',
+ );
+ }
+
+ $actual_md5 = md5_file($file_path);
+ if ($actual_md5 === false || !hash_equals(strtolower($item['md5']), strtolower($actual_md5)))
+ {
+ return array(
+ 'result' => WPVIVID_FAILED,
+ 'error' => 'Backup file checksum does not match.',
+ );
+ }
+ }
+
+ $info=WPvivid_Backup_Item::get_backup_file_info($file_path);
+ if (!is_array($info) || !isset($info['result']) || $info['result']!==WPVIVID_SUCCESS)
+ {
+ $error=is_array($info) && isset($info['error']) ? $info['error'] : 'Invalid WPvivid backup file.';
+ return array('result'=>WPVIVID_FAILED, 'error'=>$error);
+ }
+
+ $file = array(
+ 'file_name' => $file_name,
+ 'size' => $actual_size,
+ );
+
+ if ($verify_md5)
+ {
+ $file['md5'] = $actual_md5;
+ }
+
+ return array(
+ 'result' => WPVIVID_SUCCESS,
+ 'file' => $file,
+ );
+ }
+
+ private function is_valid_backup_id($backup_id)
+ {
+ return is_string($backup_id) &&
+ $backup_id!=='' &&
+ $backup_id!=='0' &&
+ preg_match('/A[a-zA-Z0-9_-]+z/', $backup_id)===1;
+ }
+
+ private function get_backup_id_from_file_name($file_name)
+ {
+ if (!is_string($file_name))
+ {
+ return false;
+ }
+
+ $prefixes = array('wpvivid');
+
+ $white_label_prefix = apply_filters('wpvivid_white_label_file_prefix', 'wpvivid');
+
+ if (is_string($white_label_prefix) && $white_label_prefix !== '')
+ {
+ $prefixes[] = $white_label_prefix;
+ }
+
+ foreach (array_unique($prefixes) as $prefix)
+ {
+ if (
+ preg_match(
+ '/' .
+ preg_quote($prefix, '/') .
+ '-([a-zA-Z0-9]+)_/',
+ $file_name,
+ $matches
+ ) === 1
+ )
+ {
+ return 'wpvivid-' . $matches[1];
+ }
+ }
+
+ if (
+ preg_match(
+ '/(?:^|_)[^_]+-([a-zA-Z0-9]+)_' .
+ '[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}(?:_|$)/',
+ $file_name,
+ $matches
+ ) === 1
+ )
+ {
+ return 'wpvivid-' . $matches[1];
+ }
+
+ return false;
+ }
+
+ private function get_create_time($files)
+ {
+ foreach ($files as $file)
+ {
+ if (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}/', $file['file_name'], $matches)===1)
+ {
+ $parts=explode('-', $matches[0]);
+ if (count($parts)===5)
+ {
+ $time=strtotime($parts[0].'-'.$parts[1].'-'.$parts[2].' '.$parts[3].':'.$parts[4]);
+ if ($time!==false)
+ {
+ return $time;
+ }
+ }
+ }
+ }
+
+ return time();
+ }
+
+ private function has_same_files($backup_data, $files)
+ {
+ if (!isset($backup_data['backup']['files']) || !is_array($backup_data['backup']['files']))
+ {
+ return false;
+ }
+
+ $existing=array();
+ foreach ($backup_data['backup']['files'] as $file)
+ {
+ if (!is_array($file) || !isset($file['file_name'], $file['size']))
+ {
+ return false;
+ }
+ $existing[$file['file_name']]=(int)$file['size'];
+ }
+
+ $incoming=array();
+ foreach ($files as $file)
+ {
+ $incoming[$file['file_name']]=(int)$file['size'];
+ }
+
+ ksort($existing);
+ ksort($incoming);
+ return $existing===$incoming;
+ }
+
+ private function get_backup_id_token($backup_id)
+ {
+ if (
+ !is_string($backup_id) ||
+ preg_match(
+ '/-([a-zA-Z0-9]+)$/',
+ $backup_id,
+ $matches
+ ) !== 1
+ )
+ {
+ return false;
+ }
+
+ return $matches[1];
+ }
+}
--- a/wpvivid-backuprestore/includes/class-wpvivid-backup-uploader.php
+++ b/wpvivid-backuprestore/includes/class-wpvivid-backup-uploader.php
@@ -639,6 +639,8 @@
}
function _rescan_local_folder_set_backup(){
+ $ret = array();
+
$path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR;
$this->wpvivid_check_remove_update_backup($path);
@@ -689,37 +691,49 @@
{
foreach ($backups as $backup_id =>$backup)
{
- $backup_data['result']='success';
- $backup_data['files']=array();
+ $manifest = array();
+
if(empty($backup['files']))
continue;
- $time=false;
+
foreach ($backup['files'] as $file)
{
- if($time===false)
+ $file_size = filesize($path . $file);
+ if ($file_size === false)
{
- if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}/',$file,$matches))
- {
- $backup_time=$matches[0];
- $time_array=explode('-',$backup_time);
- if(sizeof($time_array)>4)
- $time=$time_array[0].'-'.$time_array[1].'-'.$time_array[2].' '.$time_array[3].':'.$time_array[4];
- else
- $time=$backup_time;
- $time=strtotime($time);
- }
- else
- {
- $time=time();
- }
+ $ret['incomplete_backup'][] = $file;
+ continue;
}
- $add_file['file_name']=$file;
- $add_file['size']=filesize($path.$file);
- $backup_data['files'][]=$add_file;
+ $manifest[] = array(
+ 'file_name' => $file,
+ 'size' => $file_size,
+ );
}
- WPvivid_Backuplist::add_new_upload_backup($backup_id,$backup_data,$time,'');
+ if (!empty($manifest))
+ {
+ if (!class_exists('WPvivid_Backup_Registration'))
+ {
+ include_once WPVIVID_PLUGIN_DIR . '/includes/backup-registration/class-wpvivid-backup-registration.php';
+ }
+
+ $registration = new WPvivid_Backup_Registration();
+ $register_ret = $registration->register_backup(
+ $backup_id,
+ $manifest,
+ array(
+ 'type' => 'Upload',
+ 'log' => '',
+ 'verify_md5' => false,
+ )
+ );
+
+ if ($register_ret['result'] !== WPVIVID_SUCCESS)
+ {
+ $ret['incomplete_backup'] = array_merge(isset($ret['incomplete_backup']) ? $ret['incomplete_backup'] : array(), $backup['files']);
+ }
+ }
}
}
$ret['result']=WPVIVID_SUCCESS;
--- a/wpvivid-backuprestore/includes/class-wpvivid-backup.php
+++ b/wpvivid-backuprestore/includes/class-wpvivid-backup.php
@@ -2269,29 +2269,39 @@
public function cleanup_local_backup()
{
- $files=array();
- $download_dir=$this->config['local']['path'];
- $file=$this->get_files(false);
+ $files = $this->get_files(false);
- foreach ($file as $filename)
+ if (empty($files) || !is_array($files))
{
- $files[] = $filename;
+ return;
}
- foreach ($files as $file)
+ $configured_path = '';
+
+ if (isset($this->config['local']['path']) && is_string($this->config['local']['path']))
+ {
+ $configured_path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $this->config['local']['path'];
+ }
+
+ $default_path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . WPvivid_Setting::get_backupdir();
+
+ foreach ($files as $file_name)
{
- $download_path = WP_CONTENT_DIR .DIRECTORY_SEPARATOR . $download_dir . DIRECTORY_SEPARATOR . $file;
- if (file_exists($download_path))
+ $download_path = false;
+
+ if ($configured_path !== '')
{
- @wp_delete_file($download_path);
+ $download_path = $this->get_safe_local_delete_path($configured_path, $file_name);
}
- else{
- $backup_dir=WPvivid_Setting::get_backupdir();
- $download_path = WP_CONTENT_DIR .DIRECTORY_SEPARATOR . $backup_dir . DIRECTORY_SEPARATOR . $file;
- if (file_exists($download_path))
- {
- @wp_delete_file($download_path);
- }
+
+ if ($download_path === false)
+ {
+ $download_path = $this->get_safe_local_delete_path($default_path, $file_name);
+ }
+
+ if ($download_path !== false)
+ {
+ @wp_delete_file($download_path);
}
}
}
@@ -2455,6 +2465,49 @@
return true;
}
}
+
+ private function get_safe_local_delete_path($base_dir, $file_name)
+ {
+ if (!is_string($file_name) ||
+ $file_name === '' ||
+ basename($file_name) !== $file_name ||
+ preg_match('/A[a-zA-Z0-9._-]+z/', $file_name) !== 1)
+ {
+ return false;
+ }
+
+ $real_base_dir = realpath($base_dir);
+
+ if ($real_base_dir === false || !is_dir($real_base_dir))
+ {
+ return false;
+ }
+
+ $candidate = $real_base_dir . DIRECTORY_SEPARATOR . $file_name;
+
+ if (!file_exists($candidate))
+ {
+ return false;
+ }
+
+ $real_candidate = realpath($candidate);
+
+ if ($real_candidate === false || !is_file($real_candidate))
+ {
+ return false;
+ }
+
+ $normalized_base = trailingslashit(wp_normalize_path($real_base_dir));
+
+ $normalized_candidate = wp_normalize_path($real_candidate);
+
+ if (strpos($normalized_candidate, $normalized_base) !== 0)
+ {
+ return false;
+ }
+
+ return $real_candidate;
+ }
}
class WPvivid_Backup
--- a/wpvivid-backuprestore/includes/class-wpvivid-backuplist.php
+++ b/wpvivid-backuprestore/includes/class-wpvivid-backuplist.php
@@ -16,7 +16,7 @@
{
foreach ($list as $k=>$backup)
{
- if ($id == $k)
+ if ($id === $k)
{
return $backup;
}
@@ -37,7 +37,7 @@
{
foreach ($list as $k=>$backup)
{
- if ($backup_id == $k)
+ if ($backup_id === $k)
{
$list[$backup_id]=$backup_new;
WPvivid_Setting::update_option($list_name,$list);
@@ -76,7 +76,7 @@
$list = WPvivid_Setting::get_option('wpvivid_backup_list');
foreach ($list as $k=>$backup)
{
- if ($id == $k)
+ if ($id === $k)
{
$ret['list_name'] = 'wpvivid_backup_list';
$ret['list_data'] = $list;
@@ -99,7 +99,7 @@
$list = WPvivid_Setting::get_option('wpvivid_backup_list');
foreach ($list as $k=>$backup)
{
- if ($key == $k)
+ if ($key === $k)
{
return $backup;
}
@@ -155,7 +155,7 @@
$list = WPvivid_Setting::get_option($list_name);
foreach ($list as $k=>$backup)
{
- if ($key == $k)
+ if ($key === $k)
{
unset($list[$key]);
WPvivid_Setting::update_option($list_name, $list);
@@ -190,13 +190,13 @@
public static function get_oldest_backup_id($list)
{
- $oldest_id='';
- $oldest=0;
+ $oldest_id = false;
+ $oldest = null;
foreach ($list as $k=>$backup)
{
if(!array_key_exists('lock',$backup) || (isset($backup['lock']) && $backup['lock'] == '0'))
{
- if ($oldest == 0)
+ if ($oldest === null)
{
$oldest = $backup['create_time'];
$oldest_id = $k;
@@ -232,7 +232,7 @@
if($size>=$max_count)
{
$oldest_id=self::get_oldest_backup_id($list);
- if(empty($oldest_id))
+ if ($oldest_id === false || $oldest_id === '')
{
return false;
}
--- a/wpvivid-backuprestore/includes/class-wpvivid-export-import.php
+++ b/wpvivid-backuprestore/includes/class-wpvivid-export-import.php
@@ -619,7 +619,17 @@
if(isset($_POST['post_type']))
{
global $wpdb;
- $post_type = sanitize_text_field($_POST['post_type']);
+
+ $post_type = sanitize_key(wp_unslash($_POST['post_type']));
+ if (!in_array($post_type, array('post', 'page'), true))
+ {
+ echo wp_json_encode(array(
+ 'result' => 'failed',
+ 'error' => __('Invalid post type.', 'wpvivid-backuprestore'),
+ ));
+ die();
+ }
+
$descript_type = $post_type === 'post' ? 'posts' : 'pages';
$btn_text = $post_type === 'post' ? __('Show Posts', 'wpvivid-backuprestore') : __('Show Pages', 'wpvivid-backuprestore');
@@ -676,7 +686,7 @@
<td class="plugin-title column-primary">
<div class="wpvivid-storage-form regular-text">
<?php
- $authors = $wpdb->get_col( "SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = '$post_type'" );
+ $authors = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_author FROM {$wpdb->posts} WHERE post_type = %s", $post_type));
wp_dropdown_users(
array(
'class' => 'regular-text',
@@ -1483,7 +1493,19 @@
{
foreach ($item['export'] as $file)
{
- $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR.DIRECTORY_SEPARATOR.$file['file_name'];
+ if (!isset($file['file_name']) || !is_string($file['file_name']))
+ {
+ continue;
+ }
+
+ $file_name = wp_unslash($file['file_name']);
+
+ if ($file_name === '' || basename($file_name) !== $file_name || preg_match('/A[a-zA-Z0-9._-]+z/', $file_name) !== 1)
+ {
+ continue;
+ }
+
+ $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR.DIRECTORY_SEPARATOR.$file_name;
@wp_delete_file($path);
}
}
@@ -2350,71 +2372,98 @@
}
$ret['html']=false;
- if(isset($_POST['files']))
+
+ if (!isset($_POST['files']) || !is_string($_POST['files']))
{
- $files =sanitize_text_field($_POST['files']);
- $files =stripslashes($files);
- $files =json_decode($files,true);
- if(is_null($files))
+ $ret['result'] = WPVIVID_FAILED;
+ $ret['error'] = 'Invalid files data.';
+ echo wp_json_encode($ret);
+ die();
+ }
+
+ $files_json = wp_unslash($_POST['files']);
+ $files = json_decode($files_json, true);
+ if (json_last_error() !== JSON_ERROR_NONE || !is_array($files) || empty($files))
+ {
+ $ret['result'] = WPVIVID_FAILED;
+ $ret['error'] = 'Failed to decode files.';
+ echo wp_json_encode($ret);
+ die();
+ }
+
+ $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR.DIRECTORY_SEPARATOR;
+
+ foreach ($files as $key => $file)
+ {
+ if (!is_array($file) || !isset($file['name']) || !is_string($file['name']))
{
- $ret['result']=WPVIVID_FAILED;
- $ret['error']= 'Failed to decode files.';
+ $ret['result'] = WPVIVID_FAILED;
+ $ret['error'] = 'Invalid import file name.';
echo wp_json_encode($ret);
die();
}
- $path=WP_CONTENT_DIR.DIRECTORY_SEPARATOR.WPvivid_Setting::get_backupdir().DIRECTORY_SEPARATOR.WPVIVID_IMPORT_EXPORT_DIR.DIRECTORY_SEPARATOR;
+ $file_name = $file['name'];
- //if(preg_match('/wpvivid-.*_.*_to_.*.zip$/',$files[0]['name']))
- //{
- $data=array();
- $check_result=true;
- foreach ($files as $file)
- {
- $res=$this->check_is_import_file($path.$file['name']);
- if($res['result'] =='success')
- {
- $add_file['file_name']=$file['name'];
- $add_file['size']=filesize($path.$file['name']);
- $add_file['export_type']=$res['export_type'];
- $add_file['export_comment']=$res['export_comment'];
- $add_file['posts_count']=$res['posts_count'];
- $add_file['media_size']=size_format($res['media_size'],2);
- $add_file['time']=$res['time'];
- $data[]=$add_file;
- }
- else
- {
- $check_result=false;
- }
- }
+ if ($file_name === '' ||
+ $file_name === '.' ||
+ $file_name === '..' ||
+ strpos($file_name, "