CVE-2015-10100: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: A vulnerability, which was classified as critical, has been found in Dynamic Widgets Plugin up to 1.5.10 on WordPress. This issue affects some unknown processing of the file classes/dynwid_class.php. The manipulation leads to sql injection. The attack may be initiated remotely. Upgrading to version 1.5.11 is able to address this issue. The identifier of the patch is d0a19c6efcdc86d7093b369bc9e29a0629e57795. It is recommended to upgrade the affected component. The identifier VDB-225353 was assigned to this vulnerability.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 9.8
Published: 2023-04-10T18:15Z
Repository: https://github.com/wp-plugins/dynamic-widgets
Language: PHP
Files changed: classes/dynwid_class.php
Commit date: 2015-08-14 10:40:56 +0000
Commit message: Change DB query methods to prevent SQL injection

git-svn-id: https://plugins.svn.wordpress.org/dynamic-widgets/trunk@1220789 b8457f37-d9ea-0310-8a92-e5e31aec5664

Diff: diff --git a/classes/dynwid_class.php b/classes/dynwid_class.php
index 87465a0..342f1ec 100644
--- a/classes/dynwid_class.php
+++ b/classes/dynwid_class.php
@@ -117,22 +117,42 @@
 		 *
 		 * @param string $widget_id ID of the widget
 		 * @param array $dates Dates
 		 */
 		public function addDate($widget_id, $dates) {
+			$fields = array(
+				'widget_id'		=> $widget_id,
+				'maintype'		=> 'date',
+				'name'			=> 'default',
+				'value'			=> '0'
+			);
+			$this->wpdb->insert($this->dbtable, $fields);
+
+			/*
 			$query = "INSERT INTO " . $this->dbtable . "
                     (widget_id, maintype, name, value)
                   VALUES
                     ('" . $widget_id . "', 'date', 'default', '0')";
 			$this->wpdb->query($query);
+			*/
 
 			foreach ( $dates as $name => $date ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> 'date',
+					'name'			=> $name,
+					'value'			=> $date
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
                     (widget_id, maintype, name, value)
                   VALUES
                     ('" . esc_sql($widget_id) . "', 'date', '" . esc_sql($name) . "', '" . esc_sql($date) . "')";
 				$this->wpdb->query($query);
+				*/
 			}
 		}
 
 		/**
          * dynWid::addIPs() Saves IP options
@@ -141,42 +161,83 @@
 		 * @param array $default Default setting
 		 * @param string $ips IPs
 		 */
 		public function addIPs($widget_id, $default, $ips) {
 			$value = serialize($ips);
+
 			if ( $default == 'no' ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> 'ip',
+					'name'			=> 'default',
+					'value'			=> '0'
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'ip', 'default', '0')";
 				$this->wpdb->query($query);
+				*/
 			}
 
+			$fields = array(
+				'widget_id'		=> $widget_id,
+				'maintype'		=> 'ip',
+				'name'			=> 'ip',
+				'value'			=> $value
+			);
+			$this->wpdb->insert($this->dbtable, $fields);
+
+			/*
 			$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'ip', 'ip', '" . $value . "')";
 			$this->wpdb->query($query);
+			*/
 		}
 
 		public function addShortcode($widget_id, $default, $value, $match, $operator) {
 			$value = array( 'value' => $value, 'match' => $match, 'operator' => $operator );
 			$value = serialize($value);
 
 			if ( $default == 'no' ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> 'shortcode',
+					'name'			=> 'default',
+					'value'			=> '0'
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'shortcode', 'default', '0')";
 				$this->wpdb->query($query);
+				*/
 			}
 
+			$fields = array(
+				'widget_id'		=> $widget_id,
+				'maintype'		=> 'shortcode',
+				'name'			=> 'shortcode',
+				'value'			=> $value
+			);
+			$this->wpdb->insert($this->dbtable, $fields);
+
+			/*
 			$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'shortcode', 'shortcode', '" . $value . "')";
 			$this->wpdb->query($query);
+			*/
 		}
 
 		/**
 		 * dynWid::addUrls() Saves url options
 		 *
@@ -185,22 +246,42 @@
 		 * @param string $urls URLs
 		 */
 		public function addUrls($widget_id, $default, $urls) {
 			$value = serialize($urls);
 			if ( $default == 'no' ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> 'url',
+					'name'			=> 'default',
+					'value'			=> '0'
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'url', 'default', '0')";
 				$this->wpdb->query($query);
+				*/
 			}
 
+			$fields = array(
+				'widget_id'		=> $widget_id,
+				'maintype'		=> 'url',
+				'name'			=> 'url',
+				'value'			=> $value
+			);
+			$this->wpdb->insert($this->dbtable, $fields);
+
+			/*
 			$query = "INSERT INTO " . $this->dbtable . "
 										(widget_id, maintype, name, value)
 									VALUES
 										('" . esc_sql($widget_id) . "', 'url', 'url', '" . $value . "')";
 			$this->wpdb->query($query);
+			*/
 		}
 
 		/**
 		 * dynWid::addMultiOption() Save multi (complex) options
 		 *
@@ -231,30 +312,51 @@
 				$types[ ] = $t . '-post';
 			}
 			$post_types = array_merge( $types, array('single-post', 'single-tag') );
 
 			if ( in_array($maintype, $post_types) ) {
-				$query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype . "' AND name = 'default'";
+				$query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . " WHERE widget_id = %s AND maintype = %s AND name = %s";
+				$query = $this->wpdb->prepare($query, $widget_id, $maintype, 'default');
 				$count = $this->wpdb->get_var($query);
 				if ( $count > 0 ) {
 					$insert = FALSE;
 				}
 			}
 
 			if ( $insert ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> $maintype,
+					'name'			=> 'default',
+					'value'			=> $opt_default
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
                       (widget_id, maintype, name, value)
                     VALUES
                       ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', 'default', '" . esc_sql($opt_default) . "')";
 				$this->wpdb->query($query);
+				*/
 			}
 			foreach ( $act as $option ) {
+				$fields = array(
+					'widget_id'		=> $widget_id,
+					'maintype'		=> $maintype,
+					'name'			=> $option,
+					'value'			=> $opt_act
+				);
+				$this->wpdb->insert($this->dbtable, $fields);
+
+				/*
 				$query = "INSERT INTO " . $this->dbtable . "
                       (widget_id, maintype, name, value)
                     VALUES
                       ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', '" . esc_sql($option) . "', '" . esc_sql($opt_act) . "')";
 				$this->wpdb->query($query);
+				*/
 			}
 		}
 
 		/**
 		 * dynWid::addSingleOption() Save single (simple) options
@@ -262,15 +364,24 @@
 		 * @param string $widget_id ID of the widget
 		 * @param string $maintype Name of the module
 		 * @param integer $value Default setting
 		 */
 		public function addSingleOption($widget_id, $maintype, $value = '0') {
+			$fields = array(
+				'widget_id'		=> $widget_id,
+				'maintype'		=> $maintype,
+				'value'			=> $value
+			);
+			$this->wpdb->insert($this->dbtable, $fields);
+
+			/*
 			$query = "INSERT INTO " . $this->dbtable . "
                     (widget_id, maintype, value)
                   VALUES
                     ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', '" . esc_sql($value) . "')";
 			$this->wpdb->query($query);
+			*/
 		}
 
 		/**
 		 * dynWid::checkWPhead() Checks for wp_head()
 		 *
@@ -318,14 +429,18 @@
 		 * @param string $widget_id ID of widget
 		 * @param string $maintype Name of module
 		 * @param string $name Name of option
 		 */
 		public function deleteOption($widget_id, $maintype, $name = '') {
-			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype ."'";
+			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = %s AND maintype = %s";
 			if (! empty($name) ) {
-				$query .= " AND name = '" . $name . "'";
+				$query .= " AND name = %s";
+				$query = $this->wpdb->prepare($query, $widget_id, $maintype, $name);
+			} else {
+				$query = $this->wpdb->prepare($query, $widget_id, $maintype);
 			}
+
 			$this->wpdb->query($query);
 		}
 
 		/**
 		 * dynWid::detectPage() Page detection
@@ -428,11 +543,11 @@
 			if ( $whereami == 'home' ) {
 				$whereami = 'page';
 			}
 
 			$query = "SELECT DISTINCT widget_id FROM " . $this->dbtable . "
-                     WHERE  maintype LIKE '" . $whereami . "%'";
+                     WHERE  maintype LIKE '" . esc_sql($whereami) . "%'";
 
 			if ( count($this->overrule_maintype) > 0 ) {
 				$query .= " OR maintype IN ";
 				$q = array();
 				foreach ( $this->overrule_maintype as $omt ) {
@@ -487,12 +602,12 @@
 			if ( $maintype == 'home' ) {
 				$maintype = 'page';
 			}
 
 			$query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
-                 WHERE widget_id LIKE '" . $widget_id . "'
-                   AND maintype LIKE '" . $maintype . "%'
+                 WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
+                   AND maintype LIKE '" . esc_sql($maintype) . "%'
                  ORDER BY maintype, name";
 			$results = new DWOpts($this->wpdb->get_results($query), $maintype);
 			return $results;
 		}
 
@@ -586,27 +701,27 @@
 		public function getOpt($widget_id, $maintype, $admin = TRUE) {
 			$opt = array();
 
 			if ( $admin ) {
 				$query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
-                  WHERE widget_id LIKE '" . $widget_id . "'
-                    AND maintype LIKE '" . $maintype . "%'
+                  WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
+                    AND maintype LIKE '" . esc_sql($maintype) . "%'
                   ORDER BY maintype, name";
 
 			} else {
 				if ( $maintype == 'home' ) {
 					$maintype = 'page';
 				}
 				$query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
-                  WHERE widget_id LIKE '" . $widget_id . "'
-                    AND (maintype LIKE '" . $maintype . "%'";
+                  WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
+                    AND (maintype LIKE '" . esc_sql($maintype) . "%'";
 
 				if ( count($this->overrule_maintype) > 0 ) {
 					$query .= " OR maintype IN (";
 					$q = array();
 					foreach ( $this->overrule_maintype as $omt ) {
-						$q[ ] = "'" . $omt . "'";
+						$q[ ] = "'" . esc_sql($omt) . "'";
 					}
 					$query .= implode(', ', $q);
 					$query .= ")";
 				}
 
@@ -712,12 +827,13 @@
 		 * @param string $widget_id ID of the widget
 		 * @return boolean
 		 */
 		public function hasOptions($widget_id) {
 			$query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . "
-                  WHERE widget_id = '" . $widget_id . "' AND
-                        maintype != 'individual'";
+                  WHERE widget_id = %s AND
+                        maintype != %s";
+			$query = $this->wpdb->prepare($query, $widget_id, 'individual');
 			$count = $this->wpdb->get_var($query);
 
 			if ( $count > 0 ) {
 				return TRUE;
 			}
@@ -893,10 +1009,11 @@
 		 * dynWid::resetOptions() Full reset (remove) of options
 		 *
 		 * @param string $widget_id ID of the widget
 		 */
 		public function resetOptions($widget_id) {
-			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "'";
+			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = %s";
+			$query = $this->wpdb->prepare($query, $widget_id);
 			$this->wpdb->query($query);
 		}
 	}
 ?>
\ No newline at end of file

Vulnerable code: 				$query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype . "' AND name = 'default'";
			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype ."'";
				$query .= " AND name = '" . $name . "'";
                     WHERE  maintype LIKE '" . $whereami . "%'";
                 WHERE widget_id LIKE '" . $widget_id . "'
                   AND maintype LIKE '" . $maintype . "%'
                  WHERE widget_id LIKE '" . $widget_id . "'
                    AND maintype LIKE '" . $maintype . "%'
                  WHERE widget_id LIKE '" . $widget_id . "'
                    AND (maintype LIKE '" . $maintype . "%'";
						$q[ ] = "'" . $omt . "'";
                  WHERE widget_id = '" . $widget_id . "' AND
                        maintype != 'individual'";
			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "'";
Fixed code: 			$fields = array(
				'widget_id'		=> $widget_id,
				'maintype'		=> 'date',
				'name'			=> 'default',
				'value'			=> '0'
			);
			$this->wpdb->insert($this->dbtable, $fields);

			/*
			*/
				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> 'date',
					'name'			=> $name,
					'value'			=> $date
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/

				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> 'ip',
					'name'			=> 'default',
					'value'			=> '0'
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/
			$fields = array(
				'widget_id'		=> $widget_id,
				'maintype'		=> 'ip',
				'name'			=> 'ip',
				'value'			=> $value
			);
			$this->wpdb->insert($this->dbtable, $fields);

			/*
			*/
				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> 'shortcode',
					'name'			=> 'default',
					'value'			=> '0'
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/
			$fields = array(
				'widget_id'		=> $widget_id,
				'maintype'		=> 'shortcode',
				'name'			=> 'shortcode',
				'value'			=> $value
			);
			$this->wpdb->insert($this->dbtable, $fields);

			/*
			*/
				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> 'url',
					'name'			=> 'default',
					'value'			=> '0'
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/
			$fields = array(
				'widget_id'		=> $widget_id,
				'maintype'		=> 'url',
				'name'			=> 'url',
				'value'			=> $value
			);
			$this->wpdb->insert($this->dbtable, $fields);

			/*
			*/
				$query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . " WHERE widget_id = %s AND maintype = %s AND name = %s";
				$query = $this->wpdb->prepare($query, $widget_id, $maintype, 'default');
				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> $maintype,
					'name'			=> 'default',
					'value'			=> $opt_default
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/
				$fields = array(
					'widget_id'		=> $widget_id,
					'maintype'		=> $maintype,
					'name'			=> $option,
					'value'			=> $opt_act
				);
				$this->wpdb->insert($this->dbtable, $fields);

				/*
				*/
			$fields = array(
				'widget_id'		=> $widget_id,
				'maintype'		=> $maintype,
				'value'			=> $value
			);
			$this->wpdb->insert($this->dbtable, $fields);

			/*
			*/
			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = %s AND maintype = %s";
				$query .= " AND name = %s";
				$query = $this->wpdb->prepare($query, $widget_id, $maintype, $name);
			} else {
				$query = $this->wpdb->prepare($query, $widget_id, $maintype);

                     WHERE  maintype LIKE '" . esc_sql($whereami) . "%'";
                 WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
                   AND maintype LIKE '" . esc_sql($maintype) . "%'
                  WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
                    AND maintype LIKE '" . esc_sql($maintype) . "%'
                  WHERE widget_id LIKE '" . esc_sql($widget_id) . "'
                    AND (maintype LIKE '" . esc_sql($maintype) . "%'";
						$q[ ] = "'" . esc_sql($omt) . "'";
                  WHERE widget_id = %s AND
                        maintype != %s";
			$query = $this->wpdb->prepare($query, $widget_id, 'individual');
			$query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = %s";
			$query = $this->wpdb->prepare($query, $widget_id);