MCPcopy Create free account
hub / github.com/0opslab/opslabJutil / replaceXSS

Method replaceXSS

src/main/java/com/opslab/helper/WebHelper.java:99–151  ·  view source on GitHub ↗

去除待带script、src的语句,转义替换后的value值

(String value)

Source from the content-addressed store, hash-verified

97 * 去除待带script、src的语句,转义替换后的value值
98 */
99 public static String replaceXSS(String value) {
100 if (value != null) {
101 try {
102 value = value.replace("+", "%2B"); //'+' replace to '%2B'
103 value = URLDecoder.decode(value, "utf-8");
104 } catch (UnsupportedEncodingException e) {
105 } catch (IllegalArgumentException e) {
106 }
107
108 // Avoid null characters
109 value = value.replaceAll("\0", "");
110
111 // Avoid anything between script tags
112 Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
113 value = scriptPattern.matcher(value).replaceAll("");
114
115 // Avoid anything in a src='...' type of e­xpression
116 scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
117 value = scriptPattern.matcher(value).replaceAll("");
118
119 scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
120 value = scriptPattern.matcher(value).replaceAll("");
121
122 // Remove any lonesome </script> tag
123 scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
124 value = scriptPattern.matcher(value).replaceAll("");
125
126 // Remove any lonesome <script ...> tag
127 scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
128 value = scriptPattern.matcher(value).replaceAll("");
129
130 // Avoid eval(...) e­xpressions
131 scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
132 value = scriptPattern.matcher(value).replaceAll("");
133
134 // Avoid e­xpression(...) e­xpressions
135 scriptPattern = Pattern.compile("e­xpression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
136 value = scriptPattern.matcher(value).replaceAll("");
137
138 // Avoid javascript:... e­xpressions
139 scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
140 value = scriptPattern.matcher(value).replaceAll("");
141 // Avoid alert:... e­xpressions
142 scriptPattern = Pattern.compile("alert", Pattern.CASE_INSENSITIVE);
143 value = scriptPattern.matcher(value).replaceAll("");
144 // Avoid onload= e­xpressions
145 scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
146 value = scriptPattern.matcher(value).replaceAll("");
147 scriptPattern = Pattern.compile("vbscript[\r\n| | ]*:[\r\n| | ]*", Pattern.CASE_INSENSITIVE);
148 value = scriptPattern.matcher(value).replaceAll("");
149 }
150 return filter(value);
151 }
152
153 /**
154 * 过滤特殊字符

Callers 4

getParameterMethod · 0.95
getParameterValuesMethod · 0.95
getHeaderMethod · 0.95
serializeMethod · 0.95

Calls 3

filterMethod · 0.95
replaceMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected