forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXSS.java
More file actions
230 lines (222 loc) · 9.86 KB
/
XSS.java
File metadata and controls
230 lines (222 loc) · 9.86 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import org.unbescape.html.HtmlEscape;
import org.unbescape.javascript.JavaScriptEscape;
import org.unbescape.json.JsonEscape;
import org.unbescape.uri.UriEscape;
import org.unbescape.xml.XmlEscape;
import javax.annotation.Nullable;
/**
* Set of escaping routines for fixing cross-site scripting (XSS).
*/
public final class XSS {
private XSS() {
}
/**
* <p>
* Perform am URI path <strong>escape</strong> operation
* on a <tt>String</tt> input using <tt>UTF-8</tt> as encoding.
* </p>
* <p>
* The following are the only allowed chars in an URI path (will not be escaped):
* </p>
* <ul>
* <li><tt>A-Z a-z 0-9</tt></li>
* <li><tt>- . _ ~</tt></li>
* <li><tt>! $ & ' ( ) * + , ; =</tt></li>
* <li><tt>: @</tt></li>
* <li><tt>/</tt></li>
* </ul>
* <p>
* All other chars will be escaped by converting them to the sequence of bytes that
* represents them in the <tt>UTF-8</tt> and then representing each byte
* in <tt>%HH</tt> syntax, being <tt>HH</tt> the hexadecimal representation of the byte.
* </p>
* <p>
* This method is <strong>thread-safe</strong>.
* </p>
*
* @param value the <tt>String</tt> to be escaped.
* @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return the exact
* same object as the <tt>text</tt> input argument if no escaping modifications were required (and
* no additional <tt>String</tt> objects will be created during processing). Will
* return <tt>null</tt> if input is <tt>null</tt>.
*/
public static @Nullable String uri(@Nullable String value) {
return UriEscape.escapeUriPath(value);
}
/**
* <p>
* Perform an HTML5 level 2 (result is ASCII) <strong>escape</strong> operation on a <tt>String</tt> input.
* </p>
* <p>
* <em>Level 2</em> means this method will escape:
* </p>
* <ul>
* <li>The five markup-significant characters: <tt><</tt>, <tt>></tt>, <tt>&</tt>,
* <tt>"</tt> and <tt>'</tt></li>
* <li>All non ASCII characters.</li>
* </ul>
* <p>
* This escape will be performed by replacing those chars by the corresponding HTML5 Named Character References
* (e.g. <tt>'&acute;'</tt>) when such NCR exists for the replaced character, and replacing by a decimal
* character reference (e.g. <tt>'&#8345;'</tt>) when there there is no NCR for the replaced character.
* </p>
* <p>
* This method is <strong>thread-safe</strong>.
* </p>
*
* @param value the <tt>String</tt> to be escaped.
* @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return the exact
* same object as the <tt>text</tt> input argument if no escaping modifications were required (and
* no additional <tt>String</tt> objects will be created during processing). Will
* return <tt>null</tt> if input is <tt>null</tt>.
*/
public static @Nullable String html(@Nullable String value) {
return HtmlEscape.escapeHtml5(value);
}
/**
* <p>
* Perform a JavaScript level 2 (basic set and all non-ASCII chars) <strong>escape</strong> operation
* on a <tt>String</tt> input.
* </p>
* <p>
* <em>Level 2</em> means this method will escape:
* </p>
* <ul>
* <li>The JavaScript basic escape set:
* <ul>
* <li>The <em>Single Escape Characters</em>:
* <tt>\0</tt> (<tt>U+0000</tt>),
* <tt>\b</tt> (<tt>U+0008</tt>),
* <tt>\t</tt> (<tt>U+0009</tt>),
* <tt>\n</tt> (<tt>U+000A</tt>),
* <tt>\v</tt> (<tt>U+000B</tt>),
* <tt>\f</tt> (<tt>U+000C</tt>),
* <tt>\r</tt> (<tt>U+000D</tt>),
* <tt>\"</tt> (<tt>U+0022</tt>),
* <tt>\'</tt> (<tt>U+0027</tt>),
* <tt>\\</tt> (<tt>U+005C</tt>) and
* <tt>\/</tt> (<tt>U+002F</tt>).
* Note that <tt>\/</tt> is optional, and will only be used when the <tt>/</tt>
* symbol appears after <tt><</tt>, as in <tt></</tt>. This is to avoid accidentally
* closing <tt><script></tt> tags in HTML. Also, note that <tt>\v</tt>
* (<tt>U+000B</tt>) is actually included as a Single Escape
* Character in the JavaScript (ECMAScript) specification, but will not be used as it
* is not supported by Microsoft Internet Explorer versions < 9.
* </li>
* <li>
* Two ranges of non-displayable, control characters (some of which are already part of the
* <em>single escape characters</em> list): <tt>U+0001</tt> to <tt>U+001F</tt> and
* <tt>U+007F</tt> to <tt>U+009F</tt>.
* </li>
* </ul>
* </li>
* <li>All non ASCII characters.</li>
* </ul>
* <p>
* This escape will be performed by using the Single Escape Chars whenever possible. For escaped
* characters that do not have an associated SEC, default to using <tt>\xFF</tt> Hexadecimal Escapes
* if possible (characters <= <tt>U+00FF</tt>), then default to <tt>\uFFFF</tt>
* Hexadecimal Escapes. This type of escape <u>produces the smallest escaped string possible</u>.
* </p>
* <p>
* This method is <strong>thread-safe</strong>.
* </p>
*
* @param value the <tt>String</tt> to be escaped.
* @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return the exact
* same object as the <tt>text</tt> input argument if no escaping modifications were required (and
* no additional <tt>String</tt> objects will be created during processing). Will
* return <tt>null</tt> if input is <tt>null</tt>.
*/
public static @Nullable String javaScript(@Nullable String value) {
return JavaScriptEscape.escapeJavaScript(value);
}
/**
* <p>
* Perform a JSON level 2 (basic set and all non-ASCII chars) <strong>escape</strong> operation
* on a <tt>String</tt> input.
* </p>
* <p>
* <em>Level 2</em> means this method will escape:
* </p>
* <ul>
* <li>The JSON basic escape set:
* <ul>
* <li>The <em>Single Escape Characters</em>:
* <tt>\b</tt> (<tt>U+0008</tt>),
* <tt>\t</tt> (<tt>U+0009</tt>),
* <tt>\n</tt> (<tt>U+000A</tt>),
* <tt>\f</tt> (<tt>U+000C</tt>),
* <tt>\r</tt> (<tt>U+000D</tt>),
* <tt>\"</tt> (<tt>U+0022</tt>),
* <tt>\\</tt> (<tt>U+005C</tt>) and
* <tt>\/</tt> (<tt>U+002F</tt>).
* Note that <tt>\/</tt> is optional, and will only be used when the <tt>/</tt>
* symbol appears after <tt><</tt>, as in <tt></</tt>. This is to avoid accidentally
* closing <tt><script></tt> tags in HTML.
* </li>
* <li>
* Two ranges of non-displayable, control characters (some of which are already part of the
* <em>single escape characters</em> list): <tt>U+0000</tt> to <tt>U+001F</tt> (required
* by the JSON spec) and <tt>U+007F</tt> to <tt>U+009F</tt> (additional).
* </li>
* </ul>
* </li>
* <li>All non ASCII characters.</li>
* </ul>
* <p>
* This escape will be performed by using the Single Escape Chars whenever possible. For escaped
* characters that do not have an associated SEC, default to <tt>\uFFFF</tt>
* Hexadecimal Escapes.
* </p>
* <p>
* This method is <strong>thread-safe</strong>.
* </p>
*
* @param value the <tt>String</tt> to be escaped.
* @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return the exact
* same object as the <tt>text</tt> input argument if no escaping modifications were required (and
* no additional <tt>String</tt> objects will be created during processing). Will
* return <tt>null</tt> if input is <tt>null</tt>.
*/
public static @Nullable String json(@Nullable String value) {
return JsonEscape.escapeJson(value);
}
/**
* <p>
* Perform an XML 1.1 level 2 (markup-significant and all non-ASCII chars) <strong>escape</strong> operation
* on a <tt>String</tt> input.
* </p>
* <p>
* <em>Level 2</em> means this method will escape:
* </p>
* <ul>
* <li>The five markup-significant characters: <tt><</tt>, <tt>></tt>, <tt>&</tt>,
* <tt>"</tt> and <tt>'</tt></li>
* <li>All non ASCII characters.</li>
* </ul>
* <p>
* This escape will be performed by replacing those chars by the corresponding XML Character Entity References
* (e.g. <tt>'&lt;'</tt>) when such CER exists for the replaced character, and replacing by a hexadecimal
* character reference (e.g. <tt>'&#x2430;'</tt>) when there there is no CER for the replaced character.
* </p>
* <p>
* This method is <strong>thread-safe</strong>.
* </p>
*
* @param value the <tt>String</tt> to be escaped.
* @return The escaped result <tt>String</tt>. As a memory-performance improvement, will return the exact
* same object as the <tt>text</tt> input argument if no escaping modifications were required (and
* no additional <tt>String</tt> objects will be created during processing). Will
* return <tt>null</tt> if input is <tt>null</tt>.
*/
public static @Nullable String xml(@Nullable String value) {
return XmlEscape.escapeXml11(value);
}
}