我们是否需要清理

我们是否需要清理

本文介绍了对于SafeHtml,我们是否需要清理“链接”在< img src = link>标签,GWT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了允许用户放置图片链接的文本框(例如: http://abc.test.gif )&另一个允许用户放置替代文本的文本框(例如:This is test.gif),&一个提交按钮。

当用户点击提交按钮时,程序将生成< img src =http://abc.test.gifalt =This is test.gif> this string&将其存储到数据库中供以后使用。



我的问题是:是否需要清理图像链接http://abc.test.gif &例如,我需要使用<$> c $ c> UriUtils.isSafeUri(http://abc.test.gif); & SafeHtmlUtils.fromString(This is test.gif


解决方案

您是故意允许用户输入任何他想要进入 src alt 属性的 img 标签,这对任何一种XSS攻击都是开放的。看一看

另外,您将字符串存储在数据库中供以后使用(猜测),所以攻击可能会在稍后发生,当你使用这种字符串在DOM中创建节点时,会产生更多不可预知的结果。



一个解决方案可以是仅将URL和替代字符串存储在数据库中(如果有适当的输入验证),并生成 safe img 在你需要它的时候使用一个简单的模板,比如f (或以编程方式使用 SafeHtmlBuilder )。

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

使用方式如下:

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

这样你:


  • 验证用户输入;
  • 仅存储验证值(原样);
  • 在安全区中生成img片段只有在真正需要的时候才能使用。


I got a textbox that allows users to put image link (ex: http://abc.test.gif) & another textbox that allows user to put Alternate text (ex: "This is test.gif"), & a submit button.

When a user clicks on submit buton, the program will generate <img src="http://abc.test.gif" alt="This is test.gif"> this string & store it into DB for later use.

My question is: do i need to sanitize the imagelink "http://abc.test.gif" & the text in alt tag "This is test.gif"

For example, do i need to use UriUtils.isSafeUri("http://abc.test.gif"); & SafeHtmlUtils.fromString("This is test.gif"

解决方案

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.

这篇关于对于SafeHtml,我们是否需要清理“链接”在&lt; img src = link&gt;标签,GWT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:57