forked from OWASP/java-html-sanitizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-containment.js
413 lines (386 loc) · 12 KB
/
html-containment.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var notify = [];
var experimentIdCounter = 0;
/**
* The questions above are answered by running a bunch of experiments
* exhaustively for all combinations of HTML element names.
*
* @param makeHtmlString takes one or more element names.
* Its {@code length} property specifies its arity, and runExperiment
* calls it iteratively with every permutation of length element names.
* @param checkDom receives the element names passed to makeHtmlString,
* an HTML document body created by parsing the HTML from makeHtmlString
* and initialResult/return value from last call to checkDom.
* @param initialResult the first result value to pass to checkDom.
* @param opt_elementNames an array of element names which defaults to
* window.elementNames.
*/
function runExperiment(makeHtmlString, checkDom, initialResult, onResult,
opt_elementNames) {
var experimentIndex = ++experimentIdCounter;
var iframes = document.getElementById('experiment-iframes');
var iframe = document.createElement('iframe');
iframes.appendChild(iframe);
var elementNames = opt_elementNames || window.elementNames;
var nElements = elementNames.length;
var arity = makeHtmlString.length;
var nRuns = Math.pow(nElements, arity);
var runIndex = 0;
var paramIndices = new Array(arity);
var paramValues = new Array(arity);
for (var i = 0; i < arity; ++i) {
paramIndices[i] = 0;
paramValues[i] = elementNames[0];
}
var exhausted = nRuns === 0;
var progressCounterContainer =
document.getElementById('experiment-progress-counter');
var startTime = Date.now();
var lastProgressUpdateTime = startTime;
var result = initialResult;
var progressCounter;
if (progressCounterContainer) {
progressCounter = document.createElement('li');
progressCounter.style.width = '0';
progressCounterContainer.appendChild(progressCounter);
}
function advance() {
// Advance to next permutation.
var i;
for (i = arity; --i >= 0;) {
if (++paramIndices[i] < nElements) {
paramValues[i] = elementNames[paramIndices[i]];
break;
}
paramIndices[i] = 0;
paramValues [i] = elementNames[0];
}
++runIndex;
if (progressCounter) {
var now = Date.now();
if (now - lastProgressUpdateTime > 250 ) {
var ratio = runIndex / nRuns;
progressCounter.style.width = (100 * ratio).toFixed(2) + '%';
lastProgressUpdateTime = now;
var timeSoFar = now - startTime;
if (timeSoFar > 5000) {
// Assuming time per run is constant:
// total_time / nRuns = time_so_far / runIndex
// total_time = time_so_far * nRuns / runIndex
// = time_so_far / ratio
// eta = total_time - time_so_far
// = time_so_far / ratio - time_so_far
// = time_so_far * (1/ratio - 1)
var eta = timeSoFar * (1 / ratio - 1);
progressCounter.innerHTML = eta > 250
? 'ETA:' + (eta / 1000).toFixed(1) + 's' : '';
}
}
}
exhausted = i < 0;
}
function step() {
var htmlString = null;
// Try to generate an HTML string.
// The maker can return a nullish value to abort or punt on an experiment,
// so we loop until we find work to do.
while (!exhausted) {
paramValues.length = arity;
htmlString = makeHtmlString.apply(null, paramValues);
if (htmlString != null) {
break;
}
advance();
}
if (htmlString == null) {
var endTime = Date.now();
console.log('experiment took %d millis for %d runs',
(endTime - startTime), nRuns);
if (progressCounter) {
setTimeout(function () {
iframes.removeChild(iframe);
progressCounterContainer.removeChild(progressCounter);
}, 250);
}
onResult(result);
} else {
var notifyIndex = notify.indexOf(void 0);
if (notifyIndex < 0) { notifyIndex = notify.length; }
notify[notifyIndex] = function () {
notify[notifyIndex] = void 0;
// Process result
paramValues[arity] = iframe.contentDocument.body;
paramValues[arity + 1] = result;
result = checkDom.apply(null, paramValues);
paramValues.length = arity;
// Requeue the next step on the parent frames event queue.
setTimeout(function () { advance(); step(); }, 0);
};
// Start the iframe parsing its body.
iframe.srcdoc = (
'<!doctype html><html><head></head>'
+ '<body onload="parent.notify[' + notifyIndex + ']()">'
+ htmlString
);
}
}
step();
}
function formatDataToJsonHTML(data) {
var out = [];
var htmlForNullValue = '<span class="json-kw">null</span>';
var htmlForErrorValue = '<span class="json-kw json-err">null</span>';
var depth = 0;
var spaces = ' ';
format(data);
return out.join('');
function format(v) {
if (v == null) {
out.push(htmlForNullValue);
return;
}
var t = typeof v;
if (t === 'boolean') {
out.push('<span class="json-kw">', v, '</span>');
} else if (t === 'number') {
if (isFinite(v)) {
out.push('<span class="json-val">', v, '</span>');
} else {
out.push(htmlForErrorValue);
}
} else if (t === 'string' || v instanceof String) {
var token = JSON.stringify(String(v));
token = token.replace(/&/g, '&').replace(/</g, '<');
out.push('<span class="json-str">', token, '</span>');
} else {
var length = v.length;
var isSeries = ('number' === typeof length
&& length === (length & 0x7fffffff));
// Don't put properties on their own line if there are only a few.
var inlinePropLimit = isSeries ? 8 : 4;
var inline = true;
var numProps = 0;
for (var k in v) {
if (!Object.hasOwnProperty.call(v, k)) { continue; }
var propValue = v[k];
if ((propValue != null && typeof propValue == 'object')
|| ++numProps > inlinePropLimit) {
inline = false;
break;
}
}
// Put the appropriate white-space inside brackets and after commas.
function maybeIndent(afterComma) {
if (inline) {
if (afterComma) { out.push(' '); }
} else {
out.push('\n');
var nSpaces = depth * 2;
while (nSpaces > 0) {
var nToPush = Math.min(nSpaces, spaces.length);
out.push(spaces.substring(0, nToPush));
nSpaces -= nToPush;
}
}
}
var onclick = depth
? ' onclick="return toggleJsonBlock(this, event)"'
: '';
// Mark blocks so that we can do expandos on collections.
out.push('<span class="json-ext json-block-', depth,
depth === 0 || inline ? ' json-nocollapse' : '',
'"', onclick, '>',
isSeries ? '[' : '{',
// Emit link-like ellipses that can serve as a button for
// expando-ness.
'<span class="json-ell">…</span>',
'<span class="json-int">');
++depth;
if (isSeries) {
for (var i = 0; i < length; ++i) {
if (i) { out.push(','); }
maybeIndent(i !== 0);
format(v[i]);
}
} else {
var needsComma = false;
for (var k in v) {
if (!Object.hasOwnProperty.call(v, k)) { continue; }
if (needsComma) {
out.push(',');
}
maybeIndent(needsComma);
out.push('<span class="json-prop">');
format(String(k));
out.push(': ');
format(v[k]);
out.push('</span>');
needsComma = true;
}
}
--depth;
maybeIndent(false);
out.push('</span>', isSeries ? ']' : '}', '</span>');
}
}
}
function displayJson(data, container) {
container.innerHTML = formatDataToJsonHTML(data);
}
function toggleJsonBlock(el, event) {
event && event.stopPropagation && event.stopPropagation();
var className = el.className;
var classNameCollapsed = className.replace(/\bjson-expanded\b/g, '');
className = className === classNameCollapsed
? className + ' json-expanded' : classNameCollapsed;
className = className.replace(/^ +| +$| +( [^ ])/g, "$1");
el.className = className;
return false;
}
function Promise() {
if (!(this instanceof Promise)) { return new Promise(); }
this.paused = [];
this.satisfy = function () {
var paused = this.paused;
console.log('satisfying ' + paused.length);
for (var i = 0, n = paused.length; i < n; ++i) {
setTimeout(paused[i], 0);
}
this.paused.length = 0;
};
}
Promise.prototype.toString = function () { return "Promise"; };
function when(f, var_args) {
var unsatisfied = [];
for (var i = 1, n = arguments.length; i < n; ++i) {
var argument = arguments[i];
if (argument instanceof Promise) {
unsatisfied.push(argument);
}
}
var nToWaitFor = unsatisfied.length;
if (nToWaitFor) {
var pauser = function pauser() {
if (!--nToWaitFor) {
setTimeout(f, 0);
}
};
for (var j = 0; j < nToWaitFor; ++j) {
unsatisfied[j].paused.push(pauser);
}
unsatisfied = null;
} else {
setTimeout(f, 0);
}
}
function newBlankObject() {
return (Object.create || Object)(null);
}
function getOwn(o, k, opt_default) {
return Object.hasOwnProperty.call(o, k) ? o[k] : opt_default;
}
function breadthFirstSearch(start, isEnd, eq, adjacent) {
var stack = [{ node: start, next: null }];
while (stack.length) {
var candidate = stack.shift();
if (isEnd(candidate.node)) {
var path = [candidate.node];
while (candidate.next) {
candidate = candidate.next;
path.push(candidate.node);
}
return path;
}
var adjacentNodes = adjacent(candidate.node);
adj:
for (var i = 0, n = adjacentNodes.length; i < n; ++i) {
var adjacentNode = adjacentNodes[i];
for (var dupe = candidate; dupe; dupe = dupe.next) {
if (eq(dupe.node, adjacentNode)) { continue adj; }
}
stack.push({ node: adjacentNode, next: candidate });
}
}
return null;
}
function reverseMultiMap(multimap) {
var reverse = newBlankObject();
for (var k in multimap) {
if (Object.hasOwnProperty.call(multimap, k)) {
var values = multimap[k];
for (var i = 0, n = values.length; i < n; ++i) {
var value = values[i];
var reverseKeys = getOwn(reverse, value) || [];
reverse[value] = reverseKeys;
reverseKeys.push(k);
}
}
}
return reverse;
}
function innerTextOf(element) {
function appendTextOf(node, out) {
switch (node.nodeType) {
case 1: // Element
for (var c = node.firstChild; c; c = c.nextSibling) {
appendTextOf(c, out);
}
break;
case 3: case 4: case 6: // Text / CDATA / Entity
out.push(node.nodeValue);
break;
}
}
var buf = [];
if (element) { appendTextOf(element, buf); }
return buf.join('');
}
function sortedMultiMap(mm) {
var props = [];
for (var k in mm) {
if (!Object.hasOwnProperty.call(mm, k)) { continue; }
var v = mm[k];
if (v instanceof Array) {
v = v.slice();
v.sort();
}
props.push([k, v]);
}
props.sort(
function (a, b) {
a = a[0];
b = b[0];
if (a < b) { return -1; }
if (b < a) { return 1; }
return 0;
});
var sorted = newBlankObject();
for (var i = 0, n = props.length; i < n; ++i) {
var prop = props[i];
sorted[prop[0]] = prop[1];
}
return sorted;
}
function makeSet(strs) {
var s = newBlankObject();
for (var i = 0, n = strs.length; i < n; ++i) {
s[strs[i]] = s;
}
return s;
}
function inSet(s, str) {
return s[str] === s;
}
function elementContainsComment(el) {
return elementContainsNodeOfType(el, 8);
}
function elementContainsText(el) {
return elementContainsNodeOfType(el, 3);
}
function elementContainsNodeOfType(el, nodeType) {
if (el) {
for (var c = el.firstChild; c; c = c.nextSibling) {
if (c.nodeType === nodeType) { return true; }
}
return false;
}
}