<commit_before>const utils = require('../lib/utils')
const insecurity = require('../lib/insecurity')
const models = require('../models/index')
const cache = require('../data/datacache')
const challenges = cache.challenges

module.exports = function saveLoginIp () {
  return (req, res, next) => {
    var loggedInUser = insecurity.authenticatedUsers.from(req)
    if (loggedInUser !== undefined) {
      var lastLoginIp = req.headers['true-client-ip']
      if (utils.notSolved(challenges.httpHeaderXssChallenge) && lastLoginIp === '<iframe src="javascript:alert(`xss`)>') {
        utils.solve(challenges.httpHeaderXssChallenge)
      }
      if (lastLoginIp === undefined) {
        lastLoginIp = req.connection.remoteAddress
      }
      models.User.findByPk(loggedInUser.data.id).then(user => {
        user.update({ lastLoginIp: lastLoginIp }).then(user => {
          res.json(user)
        }).catch(error => {
          next(error)
        })
      }).catch(error => {
        next(error)
      })
    } else {
      res.sendStatus(401)
    }
  }
}<commit_msg>Remove prefix "::ffff:" for ipv4 addresses<commit_after>const utils = require('../lib/utils')
const insecurity = require('../lib/insecurity')
const models = require('../models/index')
const cache = require('../data/datacache')
const challenges = cache.challenges

module.exports = function saveLoginIp () {
  return (req, res, next) => {
    var loggedInUser = insecurity.authenticatedUsers.from(req)
    if (loggedInUser !== undefined) {
      var lastLoginIp = req.headers['true-client-ip']
      if (utils.notSolved(challenges.httpHeaderXssChallenge) && lastLoginIp === '<iframe src="javascript:alert(`xss`)>') {
        utils.solve(challenges.httpHeaderXssChallenge)
      }
      if (lastLoginIp === undefined) {
        lastLoginIp = req.connection.remoteAddress
      }
      if (lastLoginIp.substr(0, 7) === '::ffff:') { // Simplify ipv4 addresses
        lastLoginIp = lastLoginIp.substr(7)
      }
      models.User.findByPk(loggedInUser.data.id).then(user => {
        user.update({ lastLoginIp: lastLoginIp }).then(user => {
          res.json(user)
        }).catch(error => {
          next(error)
        })
      }).catch(error => {
        next(error)
      })
    } else {
      res.sendStatus(401)
    }
  }
}
{'fraction_non_alphanumeric': 0.15548780487804878, 'fraction_numerical': 0.004790940766550522, 'mean_word_length': 2.893220338983051, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6921054', 'n_tokens_mistral': 736, 'n_tokens_neox': 706, 'n_words': 122}