How to write a util function that takes map as an input iterate over all the keys and calls this function and returns a sanitized key value map?

Viewed 28

This is the function addorUpdateAddress instead of using this sanitizeXSS and decrypt_key again and again write a util function that takes map as an input iterate over all the keys and calls this function and returns a sanitized key value map.

addOrUpdateAddress : function (request, resolve) 
    {
        const customerKey = sanitizeXSS(decrypt_key(request.customer_key));
        const lat = sanitizeXSS(decrypt_key(request.lat));
        const lng = sanitizeXSS(decrypt_key(request.lng));
        const line1 = sanitizeXSS(decrypt_key(request.line1));
        const line2 = sanitizeXSS(decrypt_key(request.line2));
        const city = sanitizeXSS(decrypt_key(request.city));
        const pincode = sanitizeXSS(decrypt_key(request.pincode));
        const state = sanitizeXSS(decrypt_key(request.state));
       
        const contact = sanitizeXSS(decrypt_key(request.contact));
        const landmark = request.landmark?sanitizeXSS(decrypt_key(request.landmark)):null;     
     }
 sanitizeXSS: string => {
      const map = {
          '&': '&',
          '<': '&lt;',
          '>': '&gt;',
          '"': '&quot;',
          "'": '&#x27;',
          "/": '&#x2F;',
      };
      const reg = /[&<>"'/]/ig;
      return string.replace(reg, (match)=>(map[match]));
    }
function decrypt_key(message, action)
{
    var text = message;
    var encrypted = "";

    for(var i = 0; i < text.length; i++) {
    var ASCII = text[i].charCodeAt(0);
    var n = null;

    if(i % 2 == 0) {
      n = action == 'encrypt' ? ASCII + 4 : ASCII - 4;
    }

    else if(i % 2 == 1) {
      n = action == 'encrypt' ? ASCII + 3 : ASCII - 3;
    }

    var s = String.fromCharCode(n);

        encrypted += s;
    }
    return encrypted;
}

1 Answers

Yes, you can write such function.

function sanitizeXSS(string){
      const map = {
          '&': '&amp;',
          '<': '&lt;',
          '>': '&gt;',
          '"': '&quot;',
          "'": '&#x27;',
          "/": '&#x2F;',
      };
      const reg = /[&<>"'/]/ig;
      return string.replace(reg, (match)=>(map[match]));
}

function decrypt_key(message, action)
{
    var text = message;
    var encrypted = "";

    for(var i = 0; i < text.length; i++) {
    var ASCII = text[i].charCodeAt(0);
    var n = null;

    if(i % 2 == 0) {
      n = action == 'encrypt' ? ASCII + 4 : ASCII - 4;
    }

    else if(i % 2 == 1) {
      n = action == 'encrypt' ? ASCII + 3 : ASCII - 3;
    }

    var s = String.fromCharCode(n);

        encrypted += s;
    }
    return encrypted;
}

function decryptAndSanitizeXss(obj){
  let newObj = {}
  for(const prop in obj)    {
    if(obj[prop]){
        newObj[prop] = sanitizeXSS(decrypt_key(obj[prop]));    
    }
    }
    return newObj
}

function addOrUpdateAddress(request, resolve) {
   request = decryptAndSanitizeXss(request)
}
Related