Fix security risk 'Improper Input Validation'
[sdc.git] / openecomp-be / lib / openecomp-common-lib / src / main / java / org / openecomp / sdc / common / filters / DataValidatorFilter.java
1 /*
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.filters;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletResponse;
32 import org.apache.commons.lang3.StringUtils;
33 import org.openecomp.sdc.common.CommonConfigurationManager;
34 import org.openecomp.sdc.common.errors.DefaultExceptionMapper;
35 import org.openecomp.sdc.exception.NotAllowedSpecialCharsException;
36
37 /**
38  * Implements DataValidatorFilter for onboarding.
39  * Extends {@link DataValidatorFilterAbstract}
40  */
41 public class DataValidatorFilter extends DataValidatorFilterAbstract {
42
43     private final DefaultExceptionMapper defaultExceptionMapper;
44
45     public DataValidatorFilter() {
46         defaultExceptionMapper = new DefaultExceptionMapper();
47     }
48
49     @Override
50     public void doFilter(final ServletRequest request, ServletResponse response, final FilterChain chain)
51         throws IOException, ServletException, NotAllowedSpecialCharsException {
52         try {
53             super.doFilter(request, response, chain);
54         } catch (final NotAllowedSpecialCharsException e) {
55             defaultExceptionMapper.writeToResponse(e, (HttpServletResponse) response);
56         }
57     }
58
59     @Override
60     protected List<String> getDataValidatorFilterExcludedUrls() {
61         final CommonConfigurationManager commonConfigurationManager = CommonConfigurationManager.getInstance();
62         if (commonConfigurationManager != null) {
63             final String dataValidatorFilterExcludedUrls = commonConfigurationManager.getConfigValue(DATA_VALIDATOR_FILTER_EXCLUDED_URLS, "");
64             if (StringUtils.isNotBlank(dataValidatorFilterExcludedUrls)) {
65                 return Arrays.asList(dataValidatorFilterExcludedUrls.split(","));
66             }
67         }
68         return new ArrayList<>();
69     }
70
71 }