XSS prevention in JSP/Servlet web application

前端 未结 9 1463
自闭症患者
自闭症患者 2020-11-21 13:55

How can I prevent XSS attacks in a JSP/Servlet web application?

9条回答
  •  攒了一身酷
    2020-11-21 14:09

    If you want to make sure that your $ operator does not suffer from XSS hack you can implement ServletContextListener and do some checks there.

    The complete solution at: http://pukkaone.github.io/2011/01/03/jsp-cross-site-scripting-elresolver.html

    @WebListener
    public class EscapeXmlELResolverListener implements ServletContextListener {
        private static final Logger LOG = LoggerFactory.getLogger(EscapeXmlELResolverListener.class);
    
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            LOG.info("EscapeXmlELResolverListener initialized ...");        
            JspFactory.getDefaultFactory()
                    .getJspApplicationContext(event.getServletContext())
                    .addELResolver(new EscapeXmlELResolver());
    
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            LOG.info("EscapeXmlELResolverListener destroyed");
        }
    
    
        /**
         * {@link ELResolver} which escapes XML in String values.
         */
        public class EscapeXmlELResolver extends ELResolver {
    
            private ThreadLocal excludeMe = new ThreadLocal() {
                @Override
                protected Boolean initialValue() {
                    return Boolean.FALSE;
                }
            };
    
            @Override
            public Object getValue(ELContext context, Object base, Object property) {
    
                try {
                        if (excludeMe.get()) {
                            return null;
                        }
    
                        // This resolver is in the original resolver chain. To prevent
                        // infinite recursion, set a flag to prevent this resolver from
                        // invoking the original resolver chain again when its turn in the
                        // chain comes around.
                        excludeMe.set(Boolean.TRUE);
                        Object value = context.getELResolver().getValue(
                                context, base, property);
    
                        if (value instanceof String) {
                            value = StringEscapeUtils.escapeHtml4((String) value);
                        }
                        return value;
                } finally {
                    excludeMe.remove();
                }
            }
    
            @Override
            public Class getCommonPropertyType(ELContext context, Object base) {
                return null;
            }
    
            @Override
            public Iterator getFeatureDescriptors(ELContext context, Object base){
                return null;
            }
    
            @Override
            public Class getType(ELContext context, Object base, Object property) {
                return null;
            }
    
            @Override
            public boolean isReadOnly(ELContext context, Object base, Object property) {
                return true;
            }
    
            @Override
            public void setValue(ELContext context, Object base, Object property, Object value){
                throw new UnsupportedOperationException();
            }
    
        }
    
    }
    

    Again: This only guards the $. Please also see other answers.

提交回复
热议问题