1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.reporting;
19
20 import java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.Set;
23 import javax.annotation.concurrent.ThreadSafe;
24 import static java.nio.charset.StandardCharsets.UTF_8;
25 import org.apache.commons.text.StringEscapeUtils;
26 import org.owasp.dependencycheck.dependency.naming.Identifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37 @ThreadSafe
38 public class EscapeTool {
39
40
41
42
43 private static final Logger LOGGER = LoggerFactory.getLogger(EscapeTool.class);
44
45
46
47
48
49
50
51 public String url(String text) {
52 if (text == null || text.isEmpty()) {
53 return text;
54 }
55 try {
56 return URLEncoder.encode(text, UTF_8.name());
57 } catch (UnsupportedEncodingException ex) {
58 LOGGER.warn("UTF-8 is not supported?");
59 LOGGER.info("", ex);
60 }
61 return "";
62 }
63
64
65
66
67
68
69
70 public String html(String text) {
71 if (text == null || text.isEmpty()) {
72 return text;
73 }
74 return StringEscapeUtils.escapeHtml4(text);
75 }
76
77
78
79
80
81
82
83 public String xml(String text) {
84 if (text == null || text.isEmpty()) {
85 return text;
86 }
87 return StringEscapeUtils.escapeXml11(text);
88 }
89
90
91
92
93
94
95
96 public String json(String text) {
97 if (text == null || text.isEmpty()) {
98 return text;
99 }
100 return StringEscapeUtils.escapeJson(text);
101 }
102
103
104
105
106
107
108
109 public String javascript(String text) {
110 if (text == null || text.isEmpty()) {
111 return text;
112 }
113 return StringEscapeUtils.escapeEcmaScript(text);
114 }
115
116
117
118
119
120
121
122
123
124 public String csv(String text) {
125 if (text == null || text.isEmpty()) {
126 return "\"\"";
127 }
128 final String str = text.trim().replace("\n", " ");
129 if (str.trim().length() == 0) {
130 return "\"\"";
131 }
132 return StringEscapeUtils.escapeCsv(str);
133 }
134
135
136
137
138
139
140
141
142 public String csvIdentifiers(Set<Identifier> ids) {
143 if (ids == null || ids.isEmpty()) {
144 return "\"\"";
145 }
146 boolean addComma = false;
147 final StringBuilder sb = new StringBuilder();
148 for (Identifier id : ids) {
149 if (addComma) {
150 sb.append(", ");
151 } else {
152 addComma = true;
153 }
154 sb.append(id.getValue());
155 }
156 if (sb.length() == 0) {
157 return "\"\"";
158 }
159 return StringEscapeUtils.escapeCsv(sb.toString());
160 }
161
162
163
164
165
166
167
168
169 public String csvCpeConfidence(Set<Identifier> ids) {
170 if (ids == null || ids.isEmpty()) {
171 return "\"\"";
172 }
173 boolean addComma = false;
174 final StringBuilder sb = new StringBuilder();
175 for (Identifier id : ids) {
176 if (addComma) {
177 sb.append(", ");
178 } else {
179 addComma = true;
180 }
181 sb.append(id.getConfidence());
182 }
183 if (sb.length() == 0) {
184 return "\"\"";
185 }
186 return StringEscapeUtils.escapeCsv(sb.toString());
187 }
188 }