CVE-2022-0377 (PHP, published 2022-02-28T09:15Z)
Description: value: 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 v3 base score: 4.3)
Weakness: CWE-327 \u2014 Use of a Broken or Risky Cryptographic Algorithm
The product uses a broken or risky cryptographic algorithm or protocol.
Repository: https://github.com/LearnPress/learnpress
Files changed:
assets/src/js/frontend/profile.js
inc/admin/class-lp-admin-ajax.php
inc/class-lp-ajax.php
inc/user/lp-user-functions.php
Commit message: = 4.1.4.2 =
~ Modified: function upload, crop avatar
~ Added: function remove avatar


Vulnerable code:
				url: '?lp-ajax=save-uploaded-user-avatar',
			this.$().removeAttr( 'data-custom' );
			this.$( '.profile-picture' ).toggleClass( 'profile-avatar-current' );
			this.$( '#submit' ).prop( 'disabled', false );
			$( '.lp-user-profile-avatar' ).html( this.$( '.profile-avatar-current' ).find( 'img' ).clone() );
				'upload-user-avatar',
				'save-uploaded-user-avatar',
			add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
		public static function upload_user_avatar() {
		}

		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();
		}
	$data = learn_press_get_request( 'lp-user-avatar-crop' );
	if ( ! $data || ! ( $path = $upload_dir['basedir'] . $data['name'] ) && file_exists( $path ) ) {

		$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 );
		}

		update_user_meta( $user_id, '_lp_profile_picture', $new_avatar );

Fixed code:
				//url: '?lp-ajax=save-uploaded-user-avatar',
				url: ( typeof lpGlobalSettings !== 'undefined' ? lpGlobalSettings.ajax : '' ).addQueryVar( 'action', 'learnpress_save-uploaded-user-avatar' ),
			const el = this;
			$.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 avatar of user
		 *
		 * @editor tungnx
		 * @modify 4.1.4.2
		 */
			$user_id = get_current_user_id();

			if ( ! $user_id ) {
				return;
			}

			$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 );
				}
			}

				update_user_meta( $user_id, '_lp_profile_picture', $result['name'] );
		/**
		 * 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 );
		}

		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' ) );
				// 'upload-user-avatar',
				// 'save-uploaded-user-avatar',
			//add_action( 'wp_ajax_learnpress_upload-user-avatar', array( __CLASS__, 'upload_user_avatar' ) );
		/*public static function upload_user_avatar() {
		}*/
	$data    = learn_press_get_request( 'lp-user-avatar-crop' );
	$path_img = get_user_meta( $user_id, '_lp_profile_picture', true );
	$path = $upload_dir['basedir'] . $path_img;

	if ( ! file_exists( $path ) ) {
		update_user_meta( $user_id, '_lp_profile_picture', '/' . $new_avatar );