2014-12-19 4 views
0

https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#XSS_Prevention_Rules_Summary재생 프레임 워크 2에서 XSS를 방지하는 방법은 무엇입니까?

플레이 프레임 워크 2 문서에 따르면, 나는 오직 html을 이스케이프하기 위해 @Html 메서드를 찾을 수 있습니다. 다른 조건을 어떻게 다룰 예정입니까?

+0

이것은 매우 광범위한 질문입니다. 여기에 귀하의 특정 문제와 귀하가 시도한 것을 진술해야합니다. –

답변

2

일반적으로 Play Framework는 기본적으로 내장 템플릿을 사용할 때 기본적으로 XSS를 효과적으로 차단합니다 (문서의 Escaping 섹션 참조). @Html 메서드는 반대로 을 원시, 신뢰할 수있는 HTML 렌더링을 위해 이스케이프를 사용하지 않도록 설정합니다.

+0

다른 규칙은 HTML 속성을 이스케이프 처리하는 방법입니다. –

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") 
관련 문제