-1

I have trying to decode Recursively encoded url . But for even valid URL it is giving error .

I am calling

decodeURIRecursively({},"https%3A%2F%2Fgmail.com%3Frate%3D95%25")

It should return "https://gmail.com?rate=95%" But return string.

If I modify code and change catch block to return url it works well for this scenario but for malformed URL like decodeURIRecursively({},'%E0%A4%A'). It gives same url .

    const decodeURIRecursively = (req, url) => {
        // Early return on missing required parameter
        if (!req || !url) {
          return '';
        }
        
        try {
          while (decodeURIComponent(url) !== url) {
            
            url = decodeURIComponent(url);         
           
          }
        } catch (e) {
         
          return ''; // Return empty string as it is not a safe string to decode and use in
        }
      
        return url;
      };

console.log(decodeURIRecursively({},"https%3A%2F%2Fgmail.com%3Frate%3D95%25"));
Tim
  • 5,435
  • 7
  • 42
  • 62
Ankita
  • 31
  • 7

1 Answers1

0

The url "https://gmail.com?rate=95%" has one invalid character which is "%". So that why you get exception when run final step decodeURLComponent. %3A, %2F, %3F... can be decode but only "%" is impossible.

catch (e) {
   return url; 
}

Return url when get the error is good choice as you mentioned. My question is why you need decode recursively? As I see you only need run decodeURLComponent one time.

Sang Dang
  • 422
  • 2
  • 5
  • 13