summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/util/SecurityXssValidator.java
blob: 9754550821f50fa5bb0d30a8c062284e7531cbd9 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.portalapp.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.codecs.Codec;
import org.owasp.esapi.codecs.MySQLCodec;
import org.owasp.esapi.codecs.OracleCodec;
import org.owasp.esapi.codecs.MySQLCodec.Mode;

public class SecurityXssValidator {
	private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SecurityXssValidator.class);

	private static final String MYSQL_DB = "mysql";
	private static final String ORACLE_DB = "oracle";
	private static final String MARIA_DB = "mariadb";
	private static final int FLAGS = Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL;

	static SecurityXssValidator validator = null;
	private static Codec instance;
	private static final Lock lock = new ReentrantLock();

	public static SecurityXssValidator getInstance() {

		if (validator == null) {
			lock.lock();
			try {
				if (validator == null)
					validator = new SecurityXssValidator();
			} finally {
				lock.unlock();
			}
		}

		return validator;
	}

	private SecurityXssValidator() {
		// Avoid anything between script tags
		XSS_INPUT_PATTERNS.add(Pattern.compile("<script>(.*?)</script>", FLAGS));

		// avoid iframes
		XSS_INPUT_PATTERNS.add(Pattern.compile("<iframe(.*?)>(.*?)</iframe>", FLAGS));

		// Avoid anything in a src='...' type of expression
		XSS_INPUT_PATTERNS.add(Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", FLAGS));

		XSS_INPUT_PATTERNS.add(Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", FLAGS));

		XSS_INPUT_PATTERNS.add(Pattern.compile("src[\r\n]*=[\r\n]*([^>]+)", FLAGS));

		// Remove any lonesome </script> tag
		XSS_INPUT_PATTERNS.add(Pattern.compile("</script>", FLAGS));

		XSS_INPUT_PATTERNS.add(Pattern.compile(".*(<script>|</script>).*", FLAGS));

		XSS_INPUT_PATTERNS.add(Pattern.compile(".*(<iframe>|</iframe>).*", FLAGS));

		// Remove any lonesome <script ...> tag
		XSS_INPUT_PATTERNS.add(Pattern.compile("<script(.*?)>", FLAGS));

		// Avoid eval(...) expressions
		XSS_INPUT_PATTERNS.add(Pattern.compile("eval\\((.*?)\\)", FLAGS));

		// Avoid expression(...) expressions
		XSS_INPUT_PATTERNS.add(Pattern.compile("expression\\((.*?)\\)", FLAGS));

		// Avoid javascript:... expressions
		XSS_INPUT_PATTERNS.add(Pattern.compile(".*(javascript:|vbscript:).*", FLAGS));

		// Avoid onload= expressions
		XSS_INPUT_PATTERNS.add(Pattern.compile(".*(onload(.*?)=).*", FLAGS));
	}

	private List<Pattern> XSS_INPUT_PATTERNS = new ArrayList<Pattern>();

	/**
	 * * This method takes a string and strips out any potential script injections.
	 * 
	 * @param value
	 * @return String - the new "sanitized" string.
	 */
	public String stripXSS(String value) {

		try {

			if (StringUtils.isNotBlank(value)) {

				value = StringEscapeUtils.escapeHtml4(value);

				value = ESAPI.encoder().canonicalize(value);

				// Avoid null characters
				value = value.replaceAll("\0", "");

				for (Pattern xssInputPattern : XSS_INPUT_PATTERNS) {
					value = xssInputPattern.matcher(value).replaceAll("");
				}
			}

		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "stripXSS() failed", e);
		}

		return value;
	}

	public Boolean denyXSS(String value) {
		Boolean flag = Boolean.FALSE;
		try {
			if (StringUtils.isNotBlank(value)) {
				value = ESAPI.encoder().canonicalize(value);
				for (Pattern xssInputPattern : XSS_INPUT_PATTERNS) {
					if (xssInputPattern.matcher(value).matches()) {
						flag = Boolean.TRUE;
						break;
					}

				}
			}

		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "denyXSS() failed", e);
		}

		return flag;
	}

	public Codec getCodec() {
		try {
			if (null == instance) {
				if (StringUtils.containsIgnoreCase(SystemProperties.getProperty(SystemProperties.DB_DRIVER), MYSQL_DB)
						|| StringUtils.containsIgnoreCase(SystemProperties.getProperty(SystemProperties.DB_DRIVER),
								MARIA_DB)) {
					instance = new MySQLCodec(Mode.STANDARD);

				} else if (StringUtils.containsIgnoreCase(SystemProperties.getProperty(SystemProperties.DB_DRIVER),
						ORACLE_DB)) {
					instance = new OracleCodec();
				} else {
					throw new NotImplementedException("Handling for data base \""
							+ SystemProperties.getProperty(SystemProperties.DB_DRIVER) + "\" not yet implemented.");
				}
			}

		} catch (Exception ex) {
			logger.error(EELFLoggerDelegate.errorLogger, "getCodec() failed", ex);
		}
		return instance;

	}

	public List<Pattern> getXSS_INPUT_PATTERNS() {
		return XSS_INPUT_PATTERNS;
	}

	public void setXSS_INPUT_PATTERNS(List<Pattern> xSS_INPUT_PATTERNS) {
		XSS_INPUT_PATTERNS = xSS_INPUT_PATTERNS;
	}

}