CVE-2022-0377 (published 2022-02-28T09:15Z)

Users of the LearnPress WordPress plugin before 4.1.5 can upload an image as a profile avatar after the registration.  After this process the user crops and saves the image. Then a "POST" request that contains user supplied name of the image is sent to the server for renaming and cropping of the image. As a result of this request, the name of the user-supplied image is changed with a MD5 value. This process can be conducted only when type of the image is JPG or PNG. An attacker can use this vulnerability in order to rename an arbitrary image file. By doing this, they could destroy the design of the web site.

Severity: LOW
CVSS v2 base score: 3.5 (AV:N/AC:M/Au:S/C:N/I:P/A:N)
CVSS v3 base score: 4.3, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:L/A:N)

Weakness information:
cwe_id: CWE-327
cwe_name: Use of a Broken or Risky Cryptographic Algorithm
description: The product uses a broken or risky cryptographic algorithm or protocol.
extended_description: 
	   Cryptographic algorithms are the methods by which data is scrambled to prevent observation or influence by unauthorized actors. Insecure cryptography can be exploited to expose sensitive information, modify data in unexpected ways, spoof identities of other users or devices, or other impacts.
           It is very difficult to produce a secure algorithm, and even high-profile algorithms by accomplished cryptographic experts have been broken. Well-known techniques exist to break or weaken various kinds of cryptography. Accordingly, there are a small number of well-understood and heavily studied algorithms that should be used by most products. Using a non-standard or known-insecure algorithm is dangerous because a determined adversary may be able to break the algorithm and compromise whatever data has been protected.
           Since the state of cryptography advances so rapidly, it is common for an algorithm to be considered "unsafe" even if it was once thought to be strong. This can happen when new attacks are discovered, or if computing power increases so much that the cryptographic algorithm no longer provides the amount of protection that was originally thought.
	   For a number of reasons, this weakness is even more challenging to manage with hardware deployment of cryptographic algorithms as opposed to software implementation. First, if a flaw is discovered with hardware-implemented cryptography, the flaw cannot be fixed in most cases without a recall of the product, because hardware is not easily replaceable like software. Second, because the hardware product is expected to work for years, the adversary's computing power will only increase over time.
	 
         

Fix information:
repo_url: https://github.com/LearnPress/learnpress
commit_details: repo_url: https://github.com/LearnPress/learnpress
author: tungnx
author_date: 2022-01-14 09:56:52+07:00
msg: = 4.1.4.2 =
~ Modified: function upload, crop avatar
~ Added: function remove avatar
repository: repo_url: https://github.com/LearnPress/learnpress
repo_name: LearnPress/learnpress
description: LearnPress WordPress LMS Plugin by ThimPress
repo_language: PHP
owner: LearnPress
file_changes: filename: profile.js
change_type: ModificationType.MODIFY
diff: @@ -24,7 +24,8 @@
 			e.preventDefault();
 			const self = this;
 			$.ajax( {
-				url: '?lp-ajax=save-uploaded-user-avatar',
+				//url: '?lp-ajax=save-uploaded-user-avatar',
+				url: ( typeof lpGlobalSettings !== 'undefined' ? lpGlobalSettings.ajax : '' ).addQueryVar( 'action', 'learnpress_save-uploaded-user-avatar' ),
 				data: this.$( '.lp-avatar-crop-image' ).serializeJSON(),
 				type: 'post',
 				success( response ) {
@@ -53,11 +54,20 @@
 				return;
 			}
 
-			this.$().removeAttr( 'data-custom' );
-			this.$( '.profile-picture' ).toggleClass( 'profile-avatar-current' );
-			this.$( '#submit' ).prop( 'disabled', false );
+			const el = this;
 
-			$( '.lp-user-profile-avatar' ).html( this.$( '.profile-avatar-current' ).find( 'img' ).clone() );
+			$.ajax( {
+				url: ( typeof lpGlobalSettings !== 'undefined' ? lpGlobalSettings.ajax : '' ).addQueryVar( 'action', 'learnpress_remove-avatar' ),
+				data: {},
+				type: 'post',
+				success( response ) {
+					el.$().removeAttr( 'data-custom' );
+					el.$( '.profile-picture' ).toggleClass( 'profile-avatar-current' );
+					el.$( '#submit' ).prop( 'disabled', false );
+
+					$( '.lp-user-profile-avatar' ).html( el.$( '.profile-avatar-current' ).find( 'img' ).clone() );
+				},
+			} );
 		},
 		_upload( e ) {
 			e.preventDefault();

programming_language: JavaScript
filename: class-lp-admin-ajax.php
change_type: ModificationType.MODIFY
diff: @@ -1196,12 +1196,39 @@ if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
 			die();
 		}*/
 
+		/**
+		 * Upload avatar of user
+		 *
+		 * @editor tungnx
+		 * @modify 4.1.4.2
+		 */
 		public static function upload_user_avatar() {
+			$user_id = get_current_user_id();
+
+			if ( ! $user_id ) {
+				return;
+			}
+
 			$file       = $_FILES['lp-upload-avatar'];
 			$upload_dir = learn_press_user_profile_picture_upload_dir();
 
 			add_filter( 'upload_dir', array( __CLASS__, '_user_avatar_upload_dir' ), 10000 );
 
+			$file_info_arr        = explode( '.', $file['name'] );
+			$file_info_arr_length = count( $file_info_arr );
+			$file_ext_index       = $file_info_arr_length - 1;
+			$file_ext             = $file_info_arr[ $file_ext_index ];
+			$file['name']         = $user_id . '.' . $file_ext;
+
+			// Delete old image if exists
+			$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
+			if ( $path_img ) {
+				$path = $upload_dir['basedir'] . '/' . $path_img;
+				if ( file_exists( $path ) ) {
+					@unlink( $path );
+				}
+			}
+
 			$result = wp_handle_upload(
 				$file,
 				array(
@@ -1212,6 +1239,7 @@ if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
 			remove_filter( 'upload_dir', array( __CLASS__, '_user_avatar_upload_dir' ), 10000 );
 			if ( is_array( $result ) ) {
 				$result['name'] = $upload_dir['subdir'] . '/' . basename( $result['file'] );
+				update_user_meta( $user_id, '_lp_profile_picture', $result['name'] );
 				unset( $result['file'] );
 			} else {
 				$result = array(
@@ -1221,6 +1249,77 @@ if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
 			learn_press_send_json( $result );
 		}
 
+		/**
+		 * Crop avatar of user
+		 *
+		 * @editor tungnx
+		 * @return void
+		 */
+		public static function save_uploaded_user_avatar() {
+			$avatar_data = wp_parse_args(
+				LP_Request::get( 'lp-user-avatar-crop' ),
+				array(
+					'name'   => '',
+					'width'  => '',
+					'height' => '',
+					'points' => '',
+					'nonce'  => '',
+				)
+			);
+
+			$current_user_id = get_current_user_id();
+
+			if ( ! wp_verify_nonce( $avatar_data['nonce'], 'save-uploaded-profile-' . $current_user_id ) ) {
+				die( 'ERROR VERIFY NONCE!' );
+			}
+
+			$url = learn_press_update_user_profile_avatar();
+			if ( $url ) {
+				learn_press_send_json(
+					array(
+						'success' => true,
+						'avatar'  => sprintf( '<img src="%s" />', $url ),
+					)
+				);
+			};
+
+			wp_die();
+		}
+
+		/**
+		 * Remove avatar of user
+		 *
+		 * @author tungnx
+		 * @since 4.1.4.2
+		 * @version 1.0.0
+		 * @return void
+		 */
+		public static function remove_avatar() {
+			$response = new LP_REST_Response();
+
+			try {
+				$user_id = get_current_user_id();
+				if ( ! $user_id ) {
+					throw new Exception( __( 'User is invalid', 'learnpress' ) );
+				}
+
+				// Delete old image if exists
+				$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
+				if ( $path_img ) {
+					$upload_dir = learn_press_user_profile_picture_upload_dir();
+					$path       = $upload_dir['basedir'] . '/' . $path_img;
+					if ( file_exists( $path ) ) {
+						unlink( $path );
+						$response->status = 'success';
+					}
+				}
+			} catch ( Throwable $e ) {
+				$response->message = $e->getMessage();
+			}
+
+			wp_send_json( $response );
+		}
+
 		public static function _user_avatar_upload_dir( $dir ) {
 			$dir = learn_press_user_profile_picture_upload_dir();
 
@@ -1260,6 +1359,8 @@ if ( ! class_exists( 'LP_Admin_Ajax' ) ) {
 
 	if ( defined( 'DOING_AJAX' ) ) {
 		add_action( 'wp_ajax_learnpress_upload-user-avatar', array( 'LP_Admin_Ajax', 'upload_user_avatar' ) );
+		add_action( 'wp_ajax_learnpress_save-uploaded-user-avatar', array( 'LP_Admin_Ajax', 'save_uploaded_user_avatar' ) );
+		add_action( 'wp_ajax_learnpress_remove-avatar', array( 'LP_Admin_Ajax', 'remove_avatar' ) );
 	}
 
 	add_action( 'init', array( 'LP_Admin_Ajax', 'init' ) );

programming_language: PHP
filename: class-lp-ajax.php
change_type: ModificationType.MODIFY
diff: @@ -11,13 +11,13 @@ if ( ! class_exists( 'LP_AJAX' ) ) {
 				'checkout-user-email-exists:nopriv',
 				'recover-order',
 				'request-become-a-teacher:nonce',
-				'upload-user-avatar',
+				// 'upload-user-avatar',
 				'checkout:nopriv',
 				'complete-lesson',
 				'finish-course', // finish_course.
 				// 'retake-course', // retake_course.
 				'external-link:nopriv',
-				'save-uploaded-user-avatar',
+				// 'save-uploaded-user-avatar',
 				'load-more-courses',
 			);
 
@@ -40,7 +40,7 @@ if ( ! class_exists( 'LP_AJAX' ) ) {
 				LP_Request::register_ajax( $action, $callback );
 			}
 
-			add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
+			//add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
 		}
 
 		public static function load_more_courses() {
@@ -171,7 +171,7 @@ if ( ! class_exists( 'LP_AJAX' ) ) {
 			learn_press_maybe_send_json( $response );
 		}
 
-		public static function upload_user_avatar() {
+		/*public static function upload_user_avatar() {
 			$file       = $_FILES['lp-upload-avatar'];
 			$upload_dir = learn_press_user_profile_picture_upload_dir();
 
@@ -196,40 +196,7 @@ if ( ! class_exists( 'LP_AJAX' ) ) {
 			}
 
 			learn_press_send_json( $result );
-		}
-
-		public static function save_uploaded_user_avatar() {
-			$avatar_data = wp_parse_args(
-				LP_Request::get( 'lp-user-avatar-crop' ),
-				array(
-					'name'   => '',
-					'width'  => '',
-					'height' => '',
-					'points' => '',
-					'nonce'  => '',
-				)
-			);
-
-			$current_user_id = get_current_user_id();
-
-			if ( ! wp_verify_nonce( $avatar_data['nonce'], 'save-uploaded-profile-' . $current_user_id ) ) {
-				die( 'ERROR VERIFY NONCE!' );
-			}
-
-			$url = learn_press_update_user_profile_avatar();
-			if ( $url ) {
-				$user = learn_press_get_current_user();
-
-				learn_press_send_json(
-					array(
-						'success' => true,
-						'avatar'  => sprintf( '<img src="%s" />', $url ),
-					)
-				);
-			};
-
-			wp_die();
-		}
+		}*/
 
 		public static function _user_avatar_upload_dir( $dir ) {
 			$dir = learn_press_user_profile_picture_upload_dir();

programming_language: PHP
filename: lp-user-functions.php
change_type: ModificationType.MODIFY
diff: @@ -1079,6 +1079,7 @@ function learn_press_update_user_profile() {
  */
 function learn_press_update_user_profile_avatar() {
 	$user_id = get_current_user_id();
+	$data    = learn_press_get_request( 'lp-user-avatar-crop' );
 
 	if ( ! $user_id ) {
 		return new WP_Error( 2, 'User is invalid!' );
@@ -1092,9 +1093,11 @@ function learn_press_update_user_profile_avatar() {
 		return false;
 	}
 
-	$data = learn_press_get_request( 'lp-user-avatar-crop' );
+	$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
 
-	if ( ! $data || ! ( $path = $upload_dir['basedir'] . $data['name'] ) && file_exists( $path ) ) {
+	$path = $upload_dir['basedir'] . $path_img;
+
+	if ( ! file_exists( $path ) ) {
 		return false;
 	}
 
@@ -1144,15 +1147,8 @@ function learn_press_update_user_profile_avatar() {
 	$new_avatar = false;
 
 	if ( file_exists( $output ) ) {
-
-		$old_avatar = get_user_meta( $user_id, '_lp_profile_picture', true );
-
-		if ( file_exists( $upload_dir['basedir'] . '/' . $old_avatar ) ) {
-			@unlink( $upload_dir['basedir'] . '/' . $old_avatar );
-		}
-
 		$new_avatar = preg_replace( '!^/!', '', $upload_dir['subdir'] ) . '/' . $newname;
-		update_user_meta( $user_id, '_lp_profile_picture', $new_avatar );
+		update_user_meta( $user_id, '_lp_profile_picture', '/' . $new_avatar );
 		update_user_meta( $user_id, '_lp_profile_picture_changed', 'yes' );
 
 		$new_avatar = $upload_dir['baseurl'] . '/' . $new_avatar;

programming_language: PHP