There seems to be many accepted answers regarding if one needs to encode user-generated HTML attributes (the alt
attribute of an image for instance), 'escape' them, or 'sanitize' them.
I am wondering: If I am rendering user-generated alt attributes for img elements, how should I encode them (if at all), should I both encode and sanitize? Should I both sanitize and escape?
If I know any character has the possibility of showing up how should I process this user-generated string before setting it as an alt attribute?
Sanitizing is required so there are no issues with displaying the string. If you are storing the alt attribute in a database then yyou should definitely escape the string.