答案 0 :(得分:3)
通常,Play Framework在使用内置模板时默认提供针对XSS的良好保护(请参阅文档中的Escaping部分)。 @Html
方法执行相反的操作,禁用转义,以便呈现原始可信赖的HTML。
答案 1 :(得分:3)
我在我的网络应用程序中创建了一个简单的特征XssFilter,我在后端使用它,如下所示:
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")
答案 2 :(得分:0)
根据Play Framework 1.2.4文档:您可以使用标志
future.escapeInTemplates = true,处于application.conf设置中,可帮助您阻止跨站点脚本攻击。
来自文档:
强烈建议启用此选项。当您确实需要在模板中插入未转义的HTML时,可以在字符串上使用raw()方法。但是,如果字符串来自用户输入,则需要确保首先对其进行了清理。 在清理用户输入时,始终喜欢将白名单(仅允许使用安全标签列表)而不是黑名单(禁止使用不安全标签列表,并允许所有其他标签)。