/*
 * Copyright © WebServices pour l'Éducation, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file 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.
 */

package fr.wseduc.webutils.security;

import org.vertx.java.core.MultiMap;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public final class XSSUtils {

	private XSSUtils() {}

	private static final Pattern[] patterns = new Pattern[]{
			Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE),
//			Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
//			Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("<script>", Pattern.CASE_INSENSITIVE),
			Pattern.compile("</script>", Pattern.CASE_INSENSITIVE),
			Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE),
			Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE),
			Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("onmouse(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL),
			Pattern.compile("onclick(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL)
	};

	public static MultiMap safeMultiMap(MultiMap m) {
		for (String name : m.names()) {
			List<String> values = m.getAll(name);
			List<String> safeValues = new ArrayList<>();
			if (values == null) continue;
			for (String value: values) {
				safeValues.add(stripXSS(value));
			}
			m.set(name, safeValues);
		}
		return m;
	}

	public static String stripXSS(String value) {
		if (value != null) {
			//value = ESAPI.encoder().canonicalize(value);
			value = value.replaceAll("\0", "");
			for (Pattern scriptPattern : patterns){
				value = scriptPattern.matcher(value).replaceAll("");
			}
		}
		return value;
	}

}

@@ -1767,220 +1767,325 @@
 (%22on
-load(.*?)=%22, Pattern.CASE_INSENSITIVE %7C Pattern.MULTILINE %7C Pattern.DOTALL),%0A%09%09%09Pattern.compile(%22onmouse(.*?)=%22, Pattern.CASE_INSENSITIVE %7C Pattern.MULTILINE %7C Pattern.DOTALL),%0A%09%09%09Pattern.compile(%22onclick
+(click%7Ccontext%7Cmouse%7Cdblclick%7Ckey%7Cabort%7Cerror%7Cbefore%7Chash%7Cload%7Cpage%7C%22 +%0A%09%09%09%09%09%22resize%7Cscroll%7Cunload%7Cblur%7Cchange%7Cfocus%7Cin%7Creset%7Cse%7Csubmit%7Cdrag%7Cdrop%7Ccopy%7Ccut%7Cpaste%7C%22 +%0A%09%09%09%09%09%22after%7C before%7Ccan%7Cend%7Cduration%7Cemp%7Cp%7Cseek%7Cstall%7Csus%7Ctime%7Cvolume%7Cwaiting%7Cmessage%7Copen%7Ctouch%7C%22 +%0A%09%09%09%09%09%22on%7Coff%7Cpop%7Cshow%7Cstorage%7Ctoggle%7Cwheel)
 (.*?)=%22,
  Pat
@@ -2080,17 +2080,22 @@
 (.*?)=%22,
- 
+%0A%09%09%09%09%09
 Pattern.
@@ -2148,16 +2148,17 @@
 .DOTALL)
+,
 %0A%09%7D;%0A%0A%09p

improve patterns in xss utils