-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathXSS.java
More file actions
136 lines (130 loc) · 6.03 KB
/
XSS.java
File metadata and controls
136 lines (130 loc) · 6.03 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
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import org.jspecify.annotations.Nullable;
import io.jooby.internal.unbescape.html.HtmlEscapeLevel;
import io.jooby.internal.unbescape.html.HtmlEscapeType;
import io.jooby.internal.unbescape.html.HtmlEscapeUtil;
import io.jooby.internal.unbescape.json.JsonEscapeLevel;
import io.jooby.internal.unbescape.json.JsonEscapeType;
import io.jooby.internal.unbescape.json.JsonEscapeUtil;
import io.jooby.internal.unbescape.uri.UriEscapeUtil;
/** Set of escaping routines for fixing cross-site scripting (XSS). */
public final class XSS {
private XSS() {}
/**
* Perform am URI path <strong>escape</strong> operation on a <code>String</code> input using
* <code>UTF-8</code> as encoding.
*
* <p>The following are the only allowed chars in an URI path (will not be escaped):
*
* <ul>
* <li><code>A-Z a-z 0-9</code>
* <li><code>- . _ ~</code>
* <li><code>! $ & ' ( ) * + , ; =</code>
* <li><code>: @</code>
* <li><code>/</code>
* </ul>
*
* <p>All other chars will be escaped by converting them to the sequence of bytes that represents
* them in the <code>UTF-8</code> and then representing each byte in <code>%HH</code> syntax,
* being <code>HH</code> the hexadecimal representation of the byte.
*
* <p>This method is <strong>thread-safe</strong>.
*
* @param value the <code>String</code> to be escaped.
* @return The escaped result <code>String</code>. As a memory-performance improvement, will
* return the exact same object as the <code>text</code> input argument if no escaping
* modifications were required (and no additional <code>String</code> objects will be created
* during processing). Will return <code>null</code> if input is <code>null</code>.
*/
public static String uri(@Nullable String value) {
if (value == null || value.isEmpty()) {
return "";
}
return UriEscapeUtil.escape(value, UriEscapeUtil.UriEscapeType.PATH, "UTF-8");
}
/**
* Perform an HTML5 level 2 (result is ASCII) <strong>escape</strong> operation on a <code>String
* </code> input.
*
* <p><em>Level 2</em> means this method will escape:
*
* <ul>
* <li>The five markup-significant characters: <code><</code>, <code>></code>, <code>&
* </code>, <code>"</code> and <code>'</code>
* <li>All non ASCII characters.
* </ul>
*
* <p>This escape will be performed by replacing those chars by the corresponding HTML5 Named
* Character References (e.g. <code>'&acute;'</code>) when such NCR exists for the replaced
* character, and replacing by a decimal character reference (e.g. <code>'&#8345;'</code>)
* when there is no NCR for the replaced character.
*
* <p>This method is <strong>thread-safe</strong>.
*
* @param value the <code>String</code> to be escaped.
* @return The escaped result <code>String</code>. As a memory-performance improvement, will
* return the exact same object as the <code>text</code> input argument if no escaping
* modifications were required (and no additional <code>String</code> objects will be created
* during processing). Will return <code>null</code> if input is <code>null</code>.
*/
public static String html(@Nullable String value) {
if (value == null || value.isEmpty()) {
return "";
}
return HtmlEscapeUtil.escape(
value,
HtmlEscapeType.HTML5_NAMED_REFERENCES_DEFAULT_TO_DECIMAL,
HtmlEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_MARKUP_SIGNIFICANT);
}
/**
* Perform a JSON level 2 (basic set and all non-ASCII chars) <strong>escape</strong> operation on
* a <code>String</code> input.
*
* <p><em>Level 2</em> means this method will escape:
*
* <ul>
* <li>The JSON basic escape set:
* <ul>
* <li>The <em>Single Escape Characters</em>: <code>\b</code> (<code>U+0008</code>),
* <code>\t</code> (<code>U+0009</code>), <code>\n</code> (<code>U+000A</code>
* ), <code>\f</code> (<code>U+000C</code>), <code>\r</code> (<code>U+000D
* </code>), <code>\"</code> (<code>U+0022</code>), <code>\\</code> (
* <code>U+005C</code>) and <code>\/</code> (<code>U+002F</code>). Note that
* <code>\/</code> is optional, and will only be used when the <code>/
* </code> symbol appears after <code><</code>, as in <code></</code>. This
* is to avoid accidentally closing <code><script></code> tags in HTML.
* <li>Two ranges of non-displayable, control characters (some of which are already part
* of the <em>single escape characters</em> list): <code>U+0000</code> to <code>U+001F
* </code> (required by the JSON spec) and <code>U+007F</code> to <code>U+009F</code>
* (additional).
* </ul>
* <li>All non ASCII characters.
* </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 <code>\uFFFF</code>
* Hexadecimal Escapes.
*
* <p>This method is <strong>thread-safe</strong>.
*
* @param value the <code>String</code> to be escaped.
* @return The escaped result <code>String</code>. As a memory-performance improvement, will
* return the exact same object as the <code>text</code> input argument if no escaping
* modifications were required (and no additional <code>String</code> objects will be created
* during processing). Will return <code>null</code> if input is <code>null</code>.
*/
public static String json(@Nullable String value) {
if (value == null || value.isEmpty()) {
return "\"\"";
}
return JsonEscapeUtil.escape(
value,
JsonEscapeType.SINGLE_ESCAPE_CHARS_DEFAULT_TO_UHEXA,
JsonEscapeLevel.LEVEL_2_ALL_NON_ASCII_PLUS_BASIC_ESCAPE_SET);
}
}