객체들은 순차적으로 표현되고 마지막에는 파일내의 각 객체들을 OFFSET을 나열하는 상호참조 테이블로 구성하며 마지막 부분은 root 객체를 가리키고 있고 또한 상호 참조 테이블의 시작까지의 OFFSET을 포함 합니다.
혹시 블로그 스킨이 깨져 보이시나요? 최신버전의 Internet Explorer(Windows용), Opera, Firefox를 사용해보세요.
Found 2 article(s) for 'Servlet'.
- 2015/08/24 [java servlet에서 pdf 다루기,오라클자바커뮤니티 자바서블릿강좌]
- 2013/04/06 Simple Cross Site Scripting (XSS) Servlet Filter
객체들은 순차적으로 표현되고 마지막에는 파일내의 각 객체들을 OFFSET을 나열하는 상호참조 테이블로 구성하며 마지막 부분은 root 객체를 가리키고 있고 또한 상호 참조 테이블의 시작까지의 OFFSET을 포함 합니다.
Simple Cross Site Scripting (XSS) Servlet Filter
Ran into some issues on some of our Java sites today and needed a quick fix to protect the sites from malicious Cross Site Scripting (XSS) attempts. If you're not aware of what XSS is and have websites that have sensitive user data, you may want to read up, you're probably vulnerable, which means your users are vulnerable. I'm not claiming this is a perfect solution, but it was easy to implement and corrected the vulnerabilities with form and url injection. We basically have a Servlet Filter that's going to intercept every request sent to the web application and then we use an HttpServletRequestWrapper to wrap and override the getParameter methods and clean any potential script injection.
Here's the Filter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.greatwebguy.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class CrossScriptingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { this .filterConfig = filterConfig; } public void destroy() { this .filterConfig = null ; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter( new RequestWrapper((HttpServletRequest) request), response); } } |
Here's the wrapper:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.greatwebguy.filter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public final class RequestWrapper extends HttpServletRequestWrapper { public RequestWrapper(HttpServletRequest servletRequest) { super (servletRequest); } public String[] getParameterValues(String parameter) { String[] values = super .getParameterValues(parameter); if (values== null ) { return null ; } int count = values.length; String[] encodedValues = new String[count]; for ( int i = 0 ; i < count; i++) { encodedValues[i] = cleanXSS(values[i]); } return encodedValues; } public String getParameter(String parameter) { String value = super .getParameter(parameter); if (value == null ) { return null ; } return cleanXSS(value); } public String getHeader(String name) { String value = super .getHeader(name); if (value == null ) return null ; return cleanXSS(value); } private String cleanXSS(String value) { //You'll need to remove the spaces from the html entities below value = value.replaceAll( "<" , "& lt;" ).replaceAll( ">" , "& gt;" ); value = value.replaceAll( "\\(" , "& #40;" ).replaceAll( "\\)" , "& #41;" ); value = value.replaceAll( "'" , "& #39;" ); value = value.replaceAll( "eval\\((.*)\\)" , "" ); value = value.replaceAll( "[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']" , "\"\"" ); value = value.replaceAll( "script" , "" ); return value; } } |
Add this to the top of your web.xml:
1 2 3 4 5 6 7 8 9 10 | < filter > < filter-name >XSS</ filter-name > < display-name >XSS</ display-name > < description ></ description > < filter-class >com.greatwebguy.filter.CrossScriptingFilter</ filter-class > </ filter > < filter-mapping > < filter-name >XSS</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
I'm sure the cleanXSS replacements aren't the most efficient way of doing this, you could replace it StringEscapeUtils.escapeHtml from commons lang to simplify it a little, it's up to you, it all depends on what your site is doing and whether it's going to be a pain having all the html escaped, you could also adjust the url-pattern of the filter to be more specific to your application urls, so that everything under your app isn't running through the filter.
Some things to be aware of with this approach, you'll need to account for what you've encoded or in some cases you'll end up with weird characters in your database and possibly in validation of your input boxes. Some would recommend a more positive validation rather than negative validation and only allow a certain range of characters, it's up to you, but it is something to think about.
value = value.replaceAll(“(?i)script”, “”); instead of
value = value.replaceAll(“(?i)script”, “”); for a case insensitive replacement.