-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.html
135 lines (126 loc) · 4.38 KB
/
index.html
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
<!DOCTYPE html>
<!--
/**
* @license
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the W3C SOFTWARE AND DOCUMENT NOTICE AND LICENSE.
*
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
*/
-->
<head>
<meta http-equiv="Content-Security-Policy" content="trusted-types unsafe escape; require-trusted-types-for 'script'">
<script src="../dist/es5/trustedtypes.build.js"></script>
<style>
table {display: block; border-collapse: collapse;}
td {border: 1px solid #888; padding: 0.5em;}
.result {width: 100px;}
.pass {background-color: green;}
.fail {background-color: red;}
#row {display: none;}
iframe {width: 20px; height: 20px;}
</style>
</head>
<body>
<h1>Trusted Types demo</h1>
<table>
<thead>
<tr>
<th>Test case</th>
<th>Name</th>
<th>Payload</th>
<th>Result</th>
</tr>
</thead>
<tbody id=table>
<tr id="row">
<td class=id></td>
<td class=name></td>
<td class=payload></td>
<td class=result> </td>
</tr>
</tbody>
</table>
<script>
var trustedValue;
(function() {
// Create an unsafe policy - it can only be used on a trusted input,
// inside of this function.
var policy = trustedTypes.createPolicy('unsafe', {
'createHTML': function(unsafe) {
return unsafe;
},
});
trustedValue = policy.createHTML('<b>trusted input</b>');
})();
// Create escaping policy
var escapePolicy = trustedTypes.createPolicy('escape', {
'createHTML': function(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
},
});
var testNumber = 0;
function runTest(names, fn, shouldThrow) {
try {
var row = document.getElementById('row').cloneNode(true);
row.id = '';
document.getElementById('table').appendChild(row);
row.querySelector('.id').innerText = ++testNumber;
row.querySelector('.name').innerText = names.join(' ');
fn(row.querySelector('.payload'));
row.querySelector('.result').className += (shouldThrow ? ' fail' : ' pass');
} catch (e) {
row.querySelector('.result').className += (!shouldThrow ? ' fail' : ' pass');
console.error(names, e);
};
};
// Use the escaping policy.
var escapedValue = escapePolicy.createHTML('<b>escape me</b>');
function setHtml(value, el) {
el.innerHTML = value;
}
runTest(['innerHTML', 'TrustedHTML (unsafe policy)'], setHtml.bind(this, trustedValue), false);
runTest(['innerHTML', 'string'], setHtml.bind(this, 'string'), true);
runTest(['innerHTML', 'TrustedHTML (escape policy)'], setHtml.bind(this, escapedValue), false);
if ('srcdoc' in HTMLIFrameElement.prototype) {
runTest(['iframe.srcdoc', 'string'], function(el) {
el.appendChild(document.createElement('iframe')).srcdoc = 'throw';
}, true);
runTest(['iframe.srcdoc', 'TrustedHTML'], function(el) {
el.appendChild(document.createElement('iframe')).srcdoc = trustedValue;
}, false);
runTest(['iframe.srcdoc', 'setAttribute', 'string'], function(el) {
el.appendChild(document.createElement('iframe')).setAttribute('srcdoc', 'throw');
}, true);
runTest(['iframe.srcdoc', 'setAttribute', 'TrustedHTML'], function(el) {
el.appendChild(document.createElement('iframe')).setAttribute('srcdoc', trustedValue);
}, false);
}
runTest(['Range.createContextualFragment', 'string'], function(el) {
el.appendChild(
document.createRange().createContextualFragment('<b>a</b>'));
}, true);
runTest(['Range.createContextualFragment', 'TrustedHTML'], function(el) {
el.appendChild(
document.createRange().createContextualFragment(trustedValue));
}, false);
runTest(['Element.insertAdjacentHTML', 'string'], function(el) {
el.insertAdjacentHTML('afterbegin', '<b>a</b>');
}, true);
runTest(['Element.insertAdjacentHTML', 'TrustedHTML'], function(el) {
el.insertAdjacentHTML('afterbegin', trustedValue);
}, false);
runTest(['HTMLScriptElement.src', 'TrustedScriptURL'], function(el) {
el.appendChild(document.createElement('script')).src = 'data:,';
}, true);
runTest(['creating policy from outside allowlist'], function(el) {
trustedTypes.createPolicy('foo', {});
}, true);
</script>
</body>