To protect the xss attack I try to encode every messages which I want to display to user using below function.
function encodeRFC5987ValueChars(str)
{
return encodeURIComponent(str).
// Note that although RFC3986 reserves "!", RFC5987 does not,
// so we do not need to escape it
replace(/['()]/g, escape). // i.e., %27 %28 %29
replace(/\*/g, '%2A').
// The following are not required for percent-encoding per RFC5987,
// so we can allow for a little better readability over the wire: |`^
replace(/%(?:7C|60|5E)/g, unescape);
}
Example input message is like below
Start 20 TV's test; star ;; '' -- testagain., comma. </>
The output message I received from this function is like below.
Start%2020%20TV%27s%20test%3B%20star%20%3B%3B%20%27%27%20--%20testagain.%2C%20comma.%20%3C%2F%3E
I try to display like below.
document.getElementById("labelResult").innerHTML = "Start%2020%20TV%27s%20test%3B%20star%20%3B%3B%20%27%27%20--%20testagain.%2C%20comma.%20%3C%2F%3E";
Everything work fine, but user readability is very very poor.
Do i need to decode? If I decoded, then I afraid it will lead to xss attack?
Any suggestion please.