Introduce JS unit tests into VID
[vid.git] / epsdk-app-onap / src / main / java / org / onap / portalapp / filter / SecurityXssFilter.java
1
2 /*-
3  * ============LICENSE_START==========================================
4  * ONAP Portal
5  * ===================================================================
6  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7  * ===================================================================
8  *
9  * Unless otherwise specified, all software contained herein is licensed
10  * under the Apache License, Version 2.0 (the "License");
11  * you may not use this software except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *             http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * Unless otherwise specified, all documentation contained herein is licensed
23  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
24  * you may not use this documentation except in compliance with the License.
25  * You may obtain a copy of the License at
26  *
27  *             https://creativecommons.org/licenses/by/4.0/
28  *
29  * Unless required by applicable law or agreed to in writing, documentation
30  * distributed under the License is distributed on an "AS IS" BASIS,
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32  * See the License for the specific language governing permissions and
33  * limitations under the License.
34  *
35  * ============LICENSE_END============================================
36  *
37  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38  */
39 package org.onap.portalapp.filter;
40
41 import java.io.IOException;
42 import java.io.UnsupportedEncodingException;
43
44 import javax.servlet.FilterChain;
45 import javax.servlet.ServletException;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.lang.StringUtils;
50 import org.onap.portalapp.util.SecurityXssValidator;
51 import org.springframework.web.filter.OncePerRequestFilter;
52 import org.springframework.web.util.ContentCachingRequestWrapper;
53 import org.springframework.web.util.ContentCachingResponseWrapper;
54 import org.springframework.web.util.WebUtils;
55
56 public class SecurityXssFilter extends OncePerRequestFilter {
57
58         private static final String BAD_REQUEST = "BAD_REQUEST";
59
60         private SecurityXssValidator validator = SecurityXssValidator.getInstance();
61
62         private static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
63                 String payload = null;
64                 ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
65                 if (wrapper != null) {
66                         byte[] buf = wrapper.getContentAsByteArray();
67                         if (buf.length > 0) {
68                                 payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
69                         }
70                 }
71                 return payload;
72         }
73
74         private static String getResponseData(final HttpServletResponse response) throws IOException {
75                 String payload = null;
76                 ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response,
77                                 ContentCachingResponseWrapper.class);
78                 if (wrapper != null) {
79                         byte[] buf = wrapper.getContentAsByteArray();
80                         if (buf.length > 0) {
81                                 payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
82                                 wrapper.copyBodyToResponse();
83                         }
84                 }
85                 return payload;
86         }
87
88         @Override
89         protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
90                         throws ServletException, IOException {
91
92                 if (request.getMethod().equalsIgnoreCase("POST") || request.getMethod().equalsIgnoreCase("PUT")) {
93
94                         HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request);
95                         HttpServletResponse responseToCache = new ContentCachingResponseWrapper(response);
96                         filterChain.doFilter(requestToCache, responseToCache);
97                         String requestData = getRequestData(requestToCache);
98                         String responseData = getResponseData(responseToCache);
99                         if (StringUtils.isNotBlank(requestData) && validator.denyXSS(requestData)) {
100                                 throw new SecurityException(BAD_REQUEST);
101                         }
102
103                 } else {
104                         filterChain.doFilter(request, response);
105                 }
106
107         }
108 }