Playframework 如何在play framework 2中防止XSS?

Playframework 如何在play framework 2中防止XSS?,playframework,xss,Playframework,Xss,据 在PlayFramework2文档中,我只能找到@Html方法来转义Html。 如何处理其他情况?在一般情况下,当使用内置模板时,Play Framework在默认情况下提供了相当好的XSS保护(请参阅文档中的部分)。@Html方法的作用正好相反,禁用转义以呈现原始的、受信任的Html。在一般的Play框架中,默认情况下,当使用内置模板时(请参阅文档中的一节),可以很好地防止XSS。@Html方法的作用正好相反,禁用转义以呈现原始、可信的Html。我在我的web应用程序中创建了一个简单的特性

在PlayFramework2文档中,我只能找到@Html方法来转义Html。
如何处理其他情况?

在一般情况下,当使用内置模板时,Play Framework在默认情况下提供了相当好的XSS保护(请参阅文档中的部分)。
@Html
方法的作用正好相反,禁用转义以呈现原始的、受信任的Html。

在一般的Play框架中,默认情况下,当使用内置模板时(请参阅文档中的一节),可以很好地防止XSS。
@Html
方法的作用正好相反,禁用转义以呈现原始、可信的Html。

我在我的web应用程序中创建了一个简单的特性,我在后端使用它,如下所示:

import java.util.regex.Pattern;
trait XssFilter {
  def filter(input: String): String = {
    var value: String = input;
    if (value != null) {
      // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
      // avoid encoded attacks.
      // value = ESAPI.encoder().canonicalize(value);

      // Avoid null characters
      value = value.replaceAll("", "");

      // Avoid anything between script tags
      var scriptPattern: Pattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE)
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid anything in a src='...' type of expression
      scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Remove any lonesome </script> tag
      scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Remove any lonesome <script ...> tag
      scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid eval(...) expressions
      scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid expression(...) expressions
      scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid javascript:... expressions
      scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid vbscript:... expressions
      scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid onload= expressions
      scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");
    }
    return value;
  }
}
case class FormHelper(requestBody: AnyContent) extends XssFilter {
  def getAsFormUrlEncoded(key: String): String = {
    var ret = ""
    requestBody.asFormUrlEncoded.get.get(key) match {
      case None => {
        requestBody.asFormUrlEncoded.get.get(key + "[]") match {
          case None => ret = ""
          case s: Some[Seq[String]] => {
            ret = s.get.mkString(",")
          }
        }
      }
      case s: Some[Seq[String]] => ret = s.get.head
    }
    filter(ret)
    //requestBody.asFormUrlEncoded.get(key)(0)
  }
}
最后,在控制器代码中的某个地方:

val fh = FormHelper(request.body)
val transId: String = fh.getAsFormUrlEncoded("transId")

我在我的web应用程序中创建了一个简单的特性,并在后端使用它,如下所示:

import java.util.regex.Pattern;
trait XssFilter {
  def filter(input: String): String = {
    var value: String = input;
    if (value != null) {
      // NOTE: It's highly recommended to use the ESAPI library and uncomment the following line to
      // avoid encoded attacks.
      // value = ESAPI.encoder().canonicalize(value);

      // Avoid null characters
      value = value.replaceAll("", "");

      // Avoid anything between script tags
      var scriptPattern: Pattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE)
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid anything in a src='...' type of expression
      scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Remove any lonesome </script> tag
      scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Remove any lonesome <script ...> tag
      scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid eval(...) expressions
      scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid expression(...) expressions
      scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid javascript:... expressions
      scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid vbscript:... expressions
      scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
      value = scriptPattern.matcher(value).replaceAll("");

      // Avoid onload= expressions
      scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
      value = scriptPattern.matcher(value).replaceAll("");
    }
    return value;
  }
}
case class FormHelper(requestBody: AnyContent) extends XssFilter {
  def getAsFormUrlEncoded(key: String): String = {
    var ret = ""
    requestBody.asFormUrlEncoded.get.get(key) match {
      case None => {
        requestBody.asFormUrlEncoded.get.get(key + "[]") match {
          case None => ret = ""
          case s: Some[Seq[String]] => {
            ret = s.get.mkString(",")
          }
        }
      }
      case s: Some[Seq[String]] => ret = s.get.head
    }
    filter(ret)
    //requestBody.asFormUrlEncoded.get(key)(0)
  }
}
最后,在控制器代码中的某个地方:

val fh = FormHelper(request.body)
val transId: String = fh.getAsFormUrlEncoded("transId")

根据Play Framework 1.2.4文档:您可以使用flag

future.escapeInTemplates=true,位于application.conf设置中,这将帮助您阻止跨站点脚本攻击

从文档:

强烈建议启用此选项。当您确实需要在模板中插入未缩放的HTML时,可以使用字符串上的raw()方法。但是,如果字符串来自用户输入,则需要确保首先对其进行消毒。 当清理用户输入时,总是喜欢白名单(只允许安全标签列表)而不是黑名单(禁止不安全标签列表,允许所有其他标签)。
根据Play Framework 1.2.4文档:您可以使用flag

future.escapeInTemplates=true,位于application.conf设置中,这将帮助您阻止跨站点脚本攻击

从文档:

强烈建议启用此选项。当您确实需要在模板中插入未缩放的HTML时,可以使用字符串上的raw()方法。但是,如果字符串来自用户输入,则需要确保首先对其进行消毒。 当清理用户输入时,总是喜欢白名单(只允许安全标签列表)而不是黑名单(禁止不安全标签列表,允许所有其他标签)。
这是一个非常广泛的问题。你应该在这里陈述你的具体问题和你的尝试。这是一个非常广泛的问题。您应该在此处说明您的具体问题以及您尝试过的方法。其他规则如何,是否有转义HTML属性的方法其他规则如何,是否有转义HTML属性的方法