-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
/
trusted-types.spec.js
320 lines (277 loc) · 8.91 KB
/
trusted-types.spec.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
// NOTE: We emulate trusted types behaviour such that the tests
// are deterministic. These tests needs to be updated if the trusted
// types API changes.
//
// You can find trusted types repository here:
// https://github.com/WICG/trusted-types
//
// TODO: replace testing setup with polyfill, once it exports
// enforcing API.
import Vue from 'vue'
// we don't differentiate between different types of trusted values
const createTrustedValue = (value) => ({toString: () => value, isTrusted: true})
const isTrustedValue = (value) => value && value.isTrusted
const unsafeHtml = '<img src=x onerror="alert(0)">';
const unsafeScript = 'alert(0)';
describe('rendering with trusted types enforced', () => {
let descriptorEntries = [];
let setAttributeDescriptor;
let policy;
// NOTE: trusted type error is not propagated from v-html directive and application will not
// render the dangerous html, but will continue rendering other components. If the error is
// thrown by unsafe setAttribute call (e.g. srcdoc in iframe) the rendering fails completely.
// We log the errors, before throwing so we can be sure that trusted types work.
let errorLog;
let vuePolicyName;
function emulateSetAttribute() {
// enforce trusted values only on properties in this array
const unsafeAttributeList = ['srcdoc', 'onclick'];
setAttributeDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'setAttribute');
Object.defineProperty(Element.prototype, 'setAttribute', {
value: function(name, value) {
let args = [name, value];
unsafeAttributeList.forEach((attr) => {
if (attr === name) {
if (isTrustedValue(value)) {
args = [name, value.toString()];
} else {
errorLog.push(createTTErrorMessage(attr, value));
throw new Error(value);
}
}
});
setAttributeDescriptor.value.apply(this, args);
}
});
}
function emulateTrustedTypesOnProperty(object, prop) {
const desc = Object.getOwnPropertyDescriptor(object, prop);
descriptorEntries.push({object, prop, desc});
Object.defineProperty(object, prop, {
set: function(value) {
console.log('set', value, prop);
if (isTrustedValue(value)) {
desc.set.apply(this, [value.toString()]);
} else {
errorLog.push(createTTErrorMessage(prop, value));
throw new Error(value);
}
},
});
}
function removeAllTrustedTypesEmulation() {
descriptorEntries.forEach(({object, prop, desc}) => {
Object.defineProperty(object, prop, desc);
});
descriptorEntries = [];
Object.defineProperty(
Element.prototype, 'setAttribute', setAttributeDescriptor);
}
function createTTErrorMessage(name, value) {
return `TT ERROR: ${name} ${value}`;
}
beforeEach(() => {
window.trustedTypes = {
createPolicy: (name) => {
// capture the name of the vue policy so we can test it. Relies on fact
// that there are only 2 policies (for vue and for tests).
if (name !== 'test-policy') {
vuePolicyName = name;
}
return {
createHTML: createTrustedValue,
createScript: createTrustedValue,
createScriptURL: createTrustedValue,
};
},
isHTML: (v) => isTrustedValue(v),
isScript: (v) => isTrustedValue(v),
isScriptURL: (v) => isTrustedValue(v),
};
emulateTrustedTypesOnProperty(Element.prototype, 'innerHTML');
emulateTrustedTypesOnProperty(HTMLIFrameElement.prototype, 'srcdoc');
emulateSetAttribute();
// TODO: this needs to be changed once we use trusted types polyfill
policy = window.trustedTypes.createPolicy('test-policy');
errorLog = [];
vuePolicyName = '';
});
afterEach(() => {
removeAllTrustedTypesEmulation();
delete window.trustedTypes;
});
it('Trusted types emulation works', () => {
const el = document.createElement('div');
expect(el.innerHTML).toBe('');
el.innerHTML = policy.createHTML('<span>val</span>');
expect(el.innerHTML, '<span>val</span>');
expect(() => {
el.innerHTML = '<span>val</span>';
}).toThrow();
});
describe('vue policy', () => {
let innerHTMLDescriptor;
// simulate svg elements in Internet Explorer which don't have 'innerHTML' property
beforeEach(() => {
innerHTMLDescriptor = Object.getOwnPropertyDescriptor(
Element.prototype,
'innerHTML',
);
delete Element.prototype.innerHTML;
Object.defineProperty(
HTMLDivElement.prototype,
'innerHTML',
innerHTMLDescriptor,
);
});
afterEach(() => {
Vue.prototype.$clearTrustedTypesPolicy();
delete HTMLDivElement.prototype.innerHTML;
Object.defineProperty(
Element.prototype,
'innerHTML',
innerHTMLDescriptor,
);
});
it('uses default policy name "vue"', () => {
// we need to trigger creation of vue policy
const vm = new Vue({
render: (c) => {
return c('svg', {
domProps: {
innerHTML: policy.createHTML('safe html'),
},
});
}
})
vm.$mount();
expect(vuePolicyName).toBe('vue');
});
it('policy name can be configured', () => {
Vue.config.trustedTypesPolicyName = 'userProvidedPolicyName';
// we need to trigger creation of vue policy
const vm = new Vue({
render: (c) => {
return c('svg', {
domProps: {
innerHTML: policy.createHTML('safe html'),
},
});
}
})
vm.$mount();
expect(vuePolicyName).toBe('userProvidedPolicyName');
});
it('will throw an error on untrusted html', () => {
const vm = new Vue({
render: (c) => {
return c('svg', {
domProps: {
innerHTML: unsafeHtml,
},
});
}
})
expect(() => {
vm.$mount();
}).toThrowError('Expected svg innerHTML to be TrustedHTML!');
});
it('passes if payload is TrustedHTML', () => {
const vm = new Vue({
render: (c) => {
return c('svg', {
domProps: {
innerHTML: policy.createHTML('safe html'),
},
});
}
})
vm.$mount();
expect(vm.$el.textContent).toBe('safe html');
});
});
// html interpolation is safe because it's put into DOM as text node
it('interpolation is trusted', () => {
const vm = new Vue({
data: {
unsafeHtml,
},
template: '<div>{{unsafeHtml}}</div>'
})
vm.$mount();
expect(vm.$el.textContent).toBe(document.createTextNode(unsafeHtml).textContent);
});
describe('throws on untrusted values', () => {
it('v-html directive', () => {
const vm = new Vue({
data: {
unsafeHtml,
},
template: '<div v-html="unsafeHtml"></div>'
})
vm.$mount();
expect(errorLog).toEqual([createTTErrorMessage('innerHTML', unsafeHtml)]);
});
it('attribute interpolation', () => {
const vm = new Vue({
data: {
unsafeHtml,
},
template: '<iframe :srcdoc="unsafeHtml"></iframe>'
})
expect(() => {
vm.$mount();
}).toThrow();
expect(errorLog).toEqual([createTTErrorMessage('srcdoc', unsafeHtml)]);
});
it('on* events', () => {
const vm = new Vue({
data: {
unsafeScript,
},
template: '<button :onclick="unsafeScript">unsafe btn</button>'
})
expect(() => {
vm.$mount();
}).toThrow();
expect(errorLog).toEqual([createTTErrorMessage('onclick', unsafeScript)]);
});
});
describe('runs without error on trusted values', () => {
it('v-html directive', () => {
const vm = new Vue({
data: {
safeHtml: policy.createHTML('safeHtmlValue'),
},
template: '<div v-html="safeHtml"></div>'
})
vm.$mount();
expect(vm.$el.innerHTML).toBe('safeHtmlValue');
expect(errorLog).toEqual([]);
});
it('attribute interpolation', () => {
const vm = new Vue({
data: {
safeScript: policy.createScript('safeScriptValue'),
},
template: '<iframe :srcdoc="safeScript"></iframe>'
})
vm.$mount();
expect(vm.$el.srcdoc).toBe('safeScriptValue');
expect(errorLog).toEqual([]);
});
it('on* events', () => {
const vm = new Vue({
data: {
safeScript: policy.createScript('safeScriptValue'),
},
template: '<button :onclick="safeScript">unsafe btn</button>'
})
vm.$mount();
const onClickFn = vm.$el.onclick.toString();
const onClickBody = onClickFn.substring(onClickFn.indexOf("{") + 1, onClickFn.lastIndexOf("}"));
expect(onClickBody.trim()).toBe('safeScriptValue');
expect(errorLog).toEqual([]);
});
});
});