// Vulnerable: WordPress plugin with object injection
// In a WordPress plugin file
class VulnerablePlugin {
public function __construct() {
add_action('wp_ajax_save_settings', [$this, 'save_settings']);
add_action('wp_ajax_nopriv_save_settings', [$this, 'save_settings']);
}
public function save_settings() {
// Dangerous: Direct unserialize of user input
$settings = unserialize($_POST['settings']);
update_option('plugin_settings', $settings);
wp_die('Settings saved');
}
// Another vulnerable pattern
public function import_data() {
if (isset($_POST['import_data'])) {
// Dangerous: Unserializing uploaded file content
$data = unserialize(file_get_contents($_FILES['import']['tmp_name']));
foreach ($data as $item) {
// Process imported data
$this->process_item($item);
}
}
}
}
// Dangerous class with magic methods
class FileManager {
private $filename;
public function __construct($filename) {
$this->filename = $filename;
}
// Dangerous: Destructor can be triggered during deserialization
public function __destruct() {
if (file_exists($this->filename)) {
unlink($this->filename); // Could delete arbitrary files
}
}
public function __wakeup() {
// Dangerous: Code execution during deserialization
eval($this->filename); // Extremely dangerous
}
}
// Secure: WordPress plugin with proper input handling
class SecurePlugin {
public function __construct() {
add_action('wp_ajax_save_settings', [$this, 'save_settings']);
// Note: Removed nopriv action for security
}
public function save_settings() {
// Security: Verify nonce
if (!wp_verify_nonce($_POST['nonce'], 'save_settings_nonce')) {
wp_die('Security check failed');
}
// Security: Check user capabilities
if (!current_user_can('manage_options')) {
wp_die('Insufficient permissions');
}
// Secure: Use JSON instead of serialize
if (isset($_POST['settings'])) {
$settings_json = sanitize_textarea_field($_POST['settings']);
// Validate JSON format
$settings = json_decode($settings_json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_die('Invalid settings format');
}
// Validate settings structure
$validated_settings = $this->validate_settings($settings);
update_option('plugin_settings', $validated_settings);
wp_die('Settings saved successfully');
}
}
private function validate_settings($settings) {
$allowed_keys = ['theme', 'language', 'notifications'];
$validated = [];
foreach ($allowed_keys as $key) {
if (isset($settings[$key])) {
switch ($key) {
case 'theme':
$validated[$key] = in_array($settings[$key], ['light', 'dark'])
? $settings[$key] : 'light';
break;
case 'language':
$validated[$key] = sanitize_text_field($settings[$key]);
break;
case 'notifications':
$validated[$key] = (bool) $settings[$key];
break;
}
}
}
return $validated;
}
// Secure import functionality
public function import_data() {
// Security checks
if (!wp_verify_nonce($_POST['import_nonce'], 'import_data_nonce')) {
wp_die('Security check failed');
}
if (!current_user_can('import')) {
wp_die('Insufficient permissions');
}
if (isset($_FILES['import']) && $_FILES['import']['error'] === UPLOAD_ERR_OK) {
// Validate file type
$allowed_types = ['application/json', 'text/plain'];
$file_type = $_FILES['import']['type'];
if (!in_array($file_type, $allowed_types)) {
wp_die('Invalid file type. Only JSON files allowed.');
}
// Read and validate file content
$file_content = file_get_contents($_FILES['import']['tmp_name']);
// Secure: Use JSON instead of unserialize
$data = json_decode($file_content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_die('Invalid JSON format');
}
// Validate data structure
if (!is_array($data)) {
wp_die('Invalid data format');
}
foreach ($data as $item) {
$this->process_item_safely($item);
}
}
}
private function process_item_safely($item) {
// Validate each item before processing
if (!is_array($item) || !isset($item['id'], $item['title'])) {
return false; // Skip invalid items
}
$safe_item = [
'id' => intval($item['id']),
'title' => sanitize_text_field($item['title']),
'content' => isset($item['content'])
? wp_kses_post($item['content']) : '',
'status' => isset($item['status']) &&
in_array($item['status'], ['draft', 'published'])
? $item['status'] : 'draft'
];
// Process the validated item
return wp_insert_post($safe_item);
}
}
// Secure class design without dangerous magic methods
class SecureFileManager {
private $allowed_files = [];
private $base_path;
public function __construct($base_path) {
$this->base_path = realpath($base_path);
if (!$this->base_path) {
throw new InvalidArgumentException('Invalid base path');
}
}
public function add_allowed_file($filename) {
$safe_filename = sanitize_file_name($filename);
$full_path = realpath($this->base_path . '/' . $safe_filename);
// Ensure file is within base path
if ($full_path && strpos($full_path, $this->base_path) === 0) {
$this->allowed_files[] = $full_path;
}
}
public function delete_file($filename) {
$safe_filename = sanitize_file_name($filename);
$full_path = realpath($this->base_path . '/' . $safe_filename);
// Security: Only delete allowed files within base path
if ($full_path &&
strpos($full_path, $this->base_path) === 0 &&
in_array($full_path, $this->allowed_files)) {
return unlink($full_path);
}
return false;
}
// No dangerous magic methods
}