-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathBenchmark.java
More file actions
214 lines (182 loc) · 6.31 KB
/
Benchmark.java
File metadata and controls
214 lines (182 loc) · 6.31 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
// Copyright (c) 2011, Mike Samuel
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the OWASP nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package org.owasp.html;
import java.io.File;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.ListIterator;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import nu.validator.htmlparser.dom.HtmlDocumentBuilder;
/**
* An executable that benchmarks the sanitizer against an alternative.
* <p>
* Can be run thus
* <pre>
* mvn exec:java -Dexec.mainClass=org.owasp.html.Benchmark \
* -Dexec.classpathScope=test \
* -Dexec.args=src/test/resources/benchmark-data/Yahoo\!.html
* </pre>
*/
public class Benchmark {
/**
* By default times all alternatives.
* If there is an input of the form {@code /[hsp]+/} then each letter
* specifies a benchmark to run and unspecified ones are not run.
*/
public static void main(String[] args) throws Exception {
String html = new String(Files.readAllBytes(new File(args[0]).toPath()), StandardCharsets.UTF_8);
boolean timeLibhtmlparser = true;
boolean timeSanitize = true;
boolean timePolicyBuilder = true;
if (args.length > 1) {
String s = args[1];
timeLibhtmlparser = s.contains("h");
timeSanitize = s.contains("s");
timePolicyBuilder = s.contains("p");
}
int n = 0; // Defeat optimizations.
if (timeLibhtmlparser) {
for (int i = 100; --i >= 0;) {
n += parseUsingLibhtmlparser(html);
}
}
if (timeSanitize) {
for (int i = 100; --i >= 0;) {
n += sanitize(html).length();
}
}
if (timePolicyBuilder) {
for (int i = 100; --i >= 0;) {
n += sanitizeUsingPolicyBuilder(html).length();
}
}
long t0 = 0, t1 = -1;
if (timeLibhtmlparser) {
t0 = System.nanoTime();
for (int i = 100; --i >= 0;) {
n += parseUsingLibhtmlparser(html);
}
t1 = System.nanoTime();
}
long t2 = 0, t3 = -1;
if (timeSanitize) {
t2 = System.nanoTime();
for (int i = 100; --i >= 0;) {
n += sanitize(html).length();
}
t3 = System.nanoTime();
}
long t4 = 0, t5 = -1;
if (timePolicyBuilder) {
t4 = System.nanoTime();
for (int i = 100; --i >= 0;) {
n += sanitizeUsingPolicyBuilder(html).length();
}
t5 = System.nanoTime();
}
// Defeat optimization by using n.
if (n < 0) {
throw new AssertionError("Oh noes underflow");
}
if (timeLibhtmlparser) {
System.err.println(String.format(
"Tree parse : %12d", (t1 - t0)));
}
if (timeSanitize) {
System.err.println(String.format(
"Full sanitize custom : %12d", (t3 - t2)));
}
if (timePolicyBuilder) {
System.err.println(String.format(
"Full sanitize w/ PB : %12d", (t5 - t4)));
}
}
private static int parseUsingLibhtmlparser(String html) throws Exception {
HtmlDocumentBuilder parser = new HtmlDocumentBuilder();
Node node = parser.parse(new InputSource(new StringReader(html)));
return System.identityHashCode(node) >> 24;
}
private static String sanitize(String html) {
StringBuilder sb = new StringBuilder(html.length());
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
sb, x -> {
throw new AssertionError(x);
});
HtmlSanitizer.sanitize(html, new HtmlSanitizer.Policy() {
public void openDocument() {
renderer.openDocument();
}
public void closeDocument() {
renderer.closeDocument();
}
public void text(String textChunk) {
renderer.text(textChunk);
}
public void openTag(String elementName, List<String> attrs) {
if ("a".equals(elementName)) {
for (ListIterator<String> it = attrs.listIterator(); it.hasNext();) {
String name = it.next();
if ("href".equals(name)) {
it.next();
} else {
it.remove();
it.next();
it.remove();
}
}
renderer.openTag(elementName, attrs);
}
}
public void closeTag(String elementName) {
if ("a".equals(elementName)) {
renderer.closeTag(elementName);
}
}
});
return sb.toString();
}
private static HtmlPolicyBuilder policyBuilder;
private static String sanitizeUsingPolicyBuilder(String html) {
if (policyBuilder == null) {
policyBuilder = new HtmlPolicyBuilder()
.allowStandardUrlProtocols()
.allowElements("a")
.allowAttributes("href").onElements("a");
}
StringBuilder sb = new StringBuilder(html.length());
HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
sb, x -> {
throw new AssertionError(x);
});
HtmlSanitizer.sanitize(html, policyBuilder.build(renderer));
return sb.toString();
}
}