CVE-2022-1931 (JavaScript, published 2022-05-31T02:15Z)
Description: value: Incorrect Synchronization in GitHub repository polonel/trudesk prior to 1.2.3.
Severity: MEDIUM (CVSS v3 base score: 8.1)
Weakness: CWE-662 \u2014 Improper Synchronization
The product utilizes multiple threads or processes to allow temporary access to a shared resource that can only be exclusive to one process at a time, but it does not properly synchronize these actions, which might cause simultaneous accesses of this resource by multiple threads or processes.
Repository: https://github.com/polonel/trudesk
Files changed:
src/client/containers/Tickets/TicketsContainer.jsx
src/controllers/api/v1/messages.js
src/public/js/pages/messages.js
Commit message: fix(messages): validation check

Vulnerable code:
    console.log(data)
var winston = require('winston')
        ConversationSchema.findOneAndUpdate({ _id: cId }, { updatedAt: new Date() }, { new: false }, function (
          err,
          convo
        ) {
          if (convo === null || convo === undefined) {
            return done('Invalid Conversation: ' + convo)
          }
          return done(null, convo)
        var Message = new MessageSchema({
          owner: owner,
  var messagesPage = {}
      var $messageScroller = $('#message-content.scrollable')

      var $messagesWrapper = $('#messages')

      var $scrollspy = $('#conversation-scrollspy')

      var $spinner = $scrollspy.find('i')

      var $searchBox = $('.search-box').find('input')

      var $nextPage = 2

      var $enabled = true

      var $loading = false

      var $recentMessages = {}

      var $convoId = $('#message-content[data-conversation-id]').attr('data-conversation-id')

      var $loggedInAccountId = window.trudeskSessionService.getUser()._id
        var $li = $(target)
        var convoId = $li.attr('data-conversation-id')
          var item = $('ul > li[data-conversation-id="' + $convoId + '"]')
            var self = $(this)
              var run = _.throttle(loadMoreMessages, 100)
              var $convo = $('#message-content[data-conversation-id="' + response.conversation._id + '"]')
                var $convoLI = $('#convo-list').find('li[data-conversation-id="' + response.conversation._id + '"]')
        var searchTerm = $searchBox.val().toLowerCase()
            var messages = data.messages
            var html = ''
              var h = buildMessageHTML(m)
            var stage = $('<div></div>')
            var height = $(stage).outerHeight()
        var html = ''
        var loggedInAccountId = window.trudeskSessionService.getUser()._id
        var left = true
        var image = message.owner.image === undefined ? 'defaultProfile.jpg' : message.owner.image
        var convoListItem = $('#convo-list').find('li[data-conversation-id="' + data.cid + '"]')
        var convoListItem = $('#convo-list').find('li[data-conversation-id="' + data.cid + '"]')
        var message = {
        var html = buildMessageHTML(message)
        var messageWrapper = $('#message-content[data-conversation-id="' + message.conversation + '"]')
        var convoListItem = $('li[data-conversation-id="' + data.conversation + '"]')
          var ul = convoListItem.parent('ul')
          var li = ul.children('li')
          var fromName = message.owner.fullname
          var timezone = helpers.getTimezone()
          var convoUL = $('#convo-list > ul.message-items')
            var partner = message.owner
            var newLI = buildConversationListItem({
            var $injector = angular.injector(['ng', 'trudesk'])
                var $scope = convoUL.prepend(newLI).scope()
        var html = ''
        var imageUrl = 'defaultProfile.jpg'

Fixed code:
var winston = require('../../../logger')
var UserSchema = require('../../../models/user')
        ConversationSchema.findOne({ _id: cId }, function (err, convo) {
          if (err || !convo) return done('Invalid Conversation')

          return done(null, convo)
        })
      },
      function (convo, done) {
        convo.updatedAt = new Date()
        convo.save(function (err, savedConvo) {

          return done(null, savedConvo)
        UserSchema.findOne({ _id: owner }, function (err, user) {
          if (err || !user) return done('Invalid Conversation')

          return done(null, user, convo)
        })
      },
      function (user, convo, done) {
        const Message = new MessageSchema({
          owner: user._id,
  const messagesPage = {}
      const $messageScroller = $('#message-content.scrollable')
      const $messagesWrapper = $('#messages')
      const $scrollspy = $('#conversation-scrollspy')
      const $spinner = $scrollspy.find('i')
      const $searchBox = $('.search-box').find('input')
      let $nextPage = 2
      let $enabled = true
      let $loading = false
      const $recentMessages = {}
      const $convoId = $('#message-content[data-conversation-id]').attr('data-conversation-id')
      const $loggedInAccountId = window.trudeskSessionService.getUser()._id
      setTimeout(function () {
        $('script#preloader').remove()
      }, 1000)
        let $li = $(target)
        const convoId = $li.attr('data-conversation-id')
          const item = $('ul > li[data-conversation-id="' + $convoId + '"]')
            const self = $(this)
              const run = _.throttle(loadMoreMessages, 100)
              const $convo = $('#message-content[data-conversation-id="' + response.conversation._id + '"]')
                const $convoLI = $('#convo-list').find('li[data-conversation-id="' + response.conversation._id + '"]')
        const searchTerm = $searchBox.val().toLowerCase()
            const messages = data.messages
            let html = ''
              const h = buildMessageHTML(m)
            const stage = $('<div></div>')
            const height = $(stage).outerHeight()
        let html = ''
        const loggedInAccountId = window.trudeskSessionService.getUser()._id
        let left = true
        const image = message.owner.image === undefined ? 'defaultProfile.jpg' : message.owner.image
        const convoListItem = $('#convo-list').find('li[data-conversation-id="' + data.cid + '"]')
        const convoListItem = $('#convo-list').find('li[data-conversation-id="' + data.cid + '"]')
        const message = {
        const html = buildMessageHTML(message)
        const messageWrapper = $('#message-content[data-conversation-id="' + message.conversation + '"]')
        const convoListItem = $('li[data-conversation-id="' + data.conversation + '"]')
          const ul = convoListItem.parent('ul')
          const li = ul.children('li')
          let fromName = message.owner.fullname
          const timezone = helpers.getTimezone()
          const convoUL = $('#convo-list > ul.message-items')
            let partner = message.owner
            const newLI = buildConversationListItem({
            const $injector = angular.injector(['ng', 'trudesk'])
                const $scope = convoUL.prepend(newLI).scope()
        let html = ''
        let imageUrl = 'defaultProfile.jpg'