Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript decodeURI(Component) malformed uri exception

I entered the following in Chrome's console:

decodeURIComponent('a%AFc'); 

Instead of resulting to a0xAFc, it caused a URIError exception (malformed uri).

I've heard several excuses why this may be possible, but what I don't understand is why?

The decodeURIComponent() function in particular is supposed to decode data, not verify the URI.

  • Wikipedia: Percent Encoding
  • RFC3986: URI Generic Syntax (2005)
like image 394
Christian Avatar asked Jan 30 '12 13:01

Christian


People also ask

How do you fix a URI malformed error?

In order to fix "Uncaught URIError: URI malformed" errors in your code, you need to ensure that you are passing valid characters to both encodeURI and decodeURI too. If you would like to convert Unicode code points to UTF8, you can use this online tool.

What is URI malformed?

The JavaScript exception "malformed URI sequence" occurs when URI encoding or decoding wasn't successful.

What is a URI error?

The URIError object represents an error when a global URI handling function was used in a wrong way. URIError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

What is decodeURIComponent?

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.


1 Answers

%AF is not a character on his own but part of Unicode sequence (MACRON - %C2%AF).

%AF wasn't produced by encodeURIComponent but something like escape, so it can be decoded by unescape.

What you probably need is decodeURIComponent('%C2%AF')

like image 74
Juicy Scripter Avatar answered Sep 18 '22 09:09

Juicy Scripter