For SafeHtml, Do we need to sanitize the “link” in <img src=link> tag, GWT?

别等时光非礼了梦想. 提交于 2019-12-05 15:53:57

You are deliberately allowing the user to input anything he want that will go into the src and the alt attributes of the img tag. This is indeed open to any kind of XSS attack. Have a look here for some examples that still work in recent browsers.

Also, you are storing the string in your DB for later use (guessing), so the attack may occur at later time, when you will use such string to create a node in the DOM, with even more unpredictable results.

One solution could be to store only the URL and the alternative string in the database (with a proper input validation, if any), and generate the safe img snippet right when you need it, with a simple template like the following (or programmatically using SafeHtmlBuilder).

public interface Template extends SafeHtmlTemplates {
  @Template("<img src=\"{0}\" alt=\"{1}\"/>")
  SafeHtml img(SafeUri uri, SafeHtml alternativeText);
}

To be used like:

template.img(
    UriUtils.fromString(yourValidatedDbUrl),
    SafeHtmlUtils.fromString(yourValidatedAlternativeText));

This way you:

  • validate the user input;
  • store only the validated values (as-are);
  • generate the img snippet in a safe way only when really needed.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!