Repository navigation
Expand file tree
/
Copy pathsecurity.test.js
More file actions
433 lines (376 loc) · 17.1 KB
/
Copy pathsecurity.test.js
File metadata and controls
433 lines (376 loc) · 17.1 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* Security Tests
* Tests for prototype pollution prevention and other security measures
*/
import { describe, assert } from './test-runner.js';
import { flushSync, defineComponent, html, createStore, Component } from '../../lib/framework.js';
describe('Prototype Pollution Prevention', function(it) {
it('blocks __proto__ in x-model paths', (done) => {
const originalProto = Object.prototype.polluted;
const TestComponent = defineComponent('test-proto-xmodel', {
data() {
return {
nested: { value: 'safe' }
};
},
template() {
return html`
<input type="text" id="safe-input" x-model="nested.value">
<input type="text" id="dangerous-input" x-model="__proto__.polluted">
`;
}
});
const el = document.createElement('test-proto-xmodel');
document.body.appendChild(el);
setTimeout(() => {
const dangerousInput = el.querySelector('#dangerous-input');
// Try to pollute via input
dangerousInput.value = 'POLLUTED';
dangerousInput.dispatchEvent(new Event('input', { bubbles: true }));
setTimeout(() => {
// Verify Object.prototype was NOT polluted
assert.equal(Object.prototype.polluted, originalProto,
'Object.prototype should not be polluted via x-model');
assert.equal({}.polluted, originalProto,
'New objects should not have polluted property');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
it('blocks constructor in x-model paths', (done) => {
const TestComponent = defineComponent('test-constructor-xmodel', {
data() {
return { value: 'safe' };
},
template() {
return html`
<input type="text" x-model="constructor.prototype.polluted">
`;
}
});
const el = document.createElement('test-constructor-xmodel');
document.body.appendChild(el);
setTimeout(() => {
const input = el.querySelector('input');
input.value = 'POLLUTED';
input.dispatchEvent(new Event('input', { bubbles: true }));
setTimeout(() => {
assert.equal(Object.prototype.polluted, undefined,
'Object.prototype should not be polluted via constructor path');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
it('blocks nested __proto__ in x-model paths', (done) => {
const TestComponent = defineComponent('test-nested-proto', {
data() {
return {
deep: { nested: { value: 'safe' } }
};
},
template() {
return html`
<input type="text" x-model="deep.__proto__.polluted">
`;
}
});
const el = document.createElement('test-nested-proto');
document.body.appendChild(el);
setTimeout(() => {
const input = el.querySelector('input');
input.value = 'POLLUTED';
input.dispatchEvent(new Event('input', { bubbles: true }));
setTimeout(() => {
assert.equal({}.polluted, undefined,
'Object.prototype should not be polluted via nested path');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
it('blocks prototype pollution in store.set()', () => {
const store = createStore({ count: 0 });
// Try to pollute via set
store.set({
count: 1,
__proto__: { polluted: 'via-set' }
});
assert.equal({}.polluted, undefined,
'Object.prototype should not be polluted via store.set()');
assert.equal(store.state.count, 1, 'Normal properties should still work');
});
it('blocks prototype pollution in store.update()', () => {
const store = createStore({ count: 0 });
// Try to pollute via update
store.update(() => ({
count: 2,
__proto__: { polluted: 'via-update' }
}));
assert.equal({}.polluted, undefined,
'Object.prototype should not be polluted via store.update()');
assert.equal(store.state.count, 2, 'Normal properties should still work');
});
it('blocks constructor.prototype in store operations', () => {
const store = createStore({ data: {} });
store.set({
data: { value: 'test' },
constructor: { prototype: { polluted: 'via-constructor' } }
});
assert.equal({}.polluted, undefined,
'Object.prototype should not be polluted via constructor path in store');
});
it('allows safe nested property updates', (done) => {
const TestComponent = defineComponent('test-safe-nested', {
data() {
return {
user: {
profile: {
name: 'Alice'
}
}
};
},
template() {
return html`
<input type="text" x-model="user.profile.name">
<span id="output">${this.state.user.profile.name}</span>
`;
}
});
const el = document.createElement('test-safe-nested');
document.body.appendChild(el);
setTimeout(() => {
const input = el.querySelector('input');
input.value = 'Bob';
input.dispatchEvent(new Event('input', { bubbles: true }));
setTimeout(() => {
assert.equal(el.state.user.profile.name, 'Bob',
'Safe nested updates should work');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
});
describe('XSS Prevention', function(it) {
it('escapes HTML in template interpolation', (done) => {
const TestComponent = defineComponent('test-xss-escape', {
data() {
return {
userInput: '<script>alert("XSS")</script>'
};
},
template() {
return html`<div id="output">${this.state.userInput}</div>`;
}
});
const el = document.createElement('test-xss-escape');
document.body.appendChild(el);
setTimeout(() => {
const output = el.querySelector('#output');
// Should be escaped, not executed
assert.ok(!output.innerHTML.includes('<script>'),
'Script tags should be escaped');
assert.ok(output.textContent.includes('<script>'),
'Script text should be visible as text');
document.body.removeChild(el);
done();
}, 100);
});
it('escapes HTML attributes', (done) => {
const TestComponent = defineComponent('test-xss-attr', {
data() {
return {
userAttr: '" onclick="alert(1)"'
};
},
template() {
return html`<div id="output" title="${this.state.userAttr}">Test</div>`;
}
});
const el = document.createElement('test-xss-attr');
document.body.appendChild(el);
setTimeout(() => {
const output = el.querySelector('#output');
// Should not have onclick attribute
assert.ok(!output.hasAttribute('onclick'),
'Should not create onclick attribute from escaped value');
document.body.removeChild(el);
done();
}, 100);
});
});
describe('XSS Prevention - attribute sink hardening (adversarial review)', function(it) {
const mount = (tag) => { const el = document.createElement(tag); document.body.appendChild(el); return el; };
const settle = () => new Promise(r => setTimeout(r, 60));
it('refuses innerHTML/srcdoc set from a template', async () => {
defineComponent('sec-inner', class extends Component {
constructor(p){ super(p); this.state = { h: '<img src=x onerror="window.__secInner=1">' }; }
template(){ return html`<div innerHTML="${this.state.h}"></div><iframe srcdoc="${this.state.h}"></iframe>`; }
});
window.__secInner = 0;
const el = mount('sec-inner'); await settle(); await settle();
assert.ok(!el.querySelector('div').querySelector('img'), 'innerHTML refused');
assert.equal(el.querySelector('iframe').getAttribute('srcdoc'), null, 'srcdoc refused');
assert.equal(window.__secInner, 0, 'no script executed');
});
it('sanitizes javascript: on formaction/object-data/xlink:href (string AND non-string)', async () => {
defineComponent('sec-url', class extends Component {
constructor(p){ super(p); this.state = {
s: 'javascript:alert(1)',
arr: ['javascript:alert(2)'] // non-string from JSON
}; }
template(){ return html`
<form><button formaction="${this.state.s}">a</button><button formaction="${this.state.arr}">b</button></form>
<object data="${this.state.s}"></object>`; }
});
const el = mount('sec-url'); await settle();
const btns = el.querySelectorAll('button');
assert.ok(!(btns[0].getAttribute('formaction') || '').includes('javascript:'), 'string formaction sanitized');
assert.ok(!(btns[1].getAttribute('formaction') || '').includes('javascript:'), 'non-string formaction sanitized');
assert.ok(!(el.querySelector('object').getAttribute('data') || '').includes('javascript:'), 'object data sanitized');
});
it('does NOT mangle a data property on a non-object element/component', async () => {
if (!customElements.get('sec-data-recv')) {
customElements.define('sec-data-recv', class extends HTMLElement {
set data(v){ this._d = v; } get data(){ return this._d; }
});
}
defineComponent('sec-data-host', class extends Component {
constructor(p){ super(p); this.state = { rows: [1, 2, 3] }; }
template(){ return html`<sec-data-recv data="${this.state.rows}"></sec-data-recv>`; }
});
const el = mount('sec-data-host'); await settle();
assert.deepEqual(el.querySelector('sec-data-recv').data, [1, 2, 3], 'array data prop passes through untouched');
});
});
describe('XSS Prevention - style and script sinks (pre-v1 hardening)', function(it) {
const tick = (ms = 60) => new Promise(r => setTimeout(r, ms));
it('refuses style values with dangerous CSS constructs', async () => {
defineComponent('sec-style-inject', {
data() {
return { css: "color: red; background: url('javascript:alert(1)')" };
},
template() {
return html`<div class="target" style="${this.state.css}">x</div>`;
}
});
const warns = [];
const origWarn = console.warn;
console.warn = (...a) => warns.push(a.join(' '));
const el = document.createElement('sec-style-inject');
document.body.appendChild(el);
await tick();
console.warn = origWarn;
const target = el.querySelector('.target');
assert.equal(target.getAttribute('style') || '', '',
'the whole dangerous style value is refused');
assert.ok(warns.some(w => w.includes('[VDX Security]')), 'refusal is warned about');
// Benign strings still work
el.state.css = 'color: rgb(1, 2, 3);';
await tick();
assert.equal(target.style.color, 'rgb(1, 2, 3)', 'benign string styles still apply');
el.remove();
});
it('refuses @import and expression() in style strings', async () => {
defineComponent('sec-style-inject2', {
data() { return {}; },
template() {
return html`
<div id="a" style="${"@import url('https://evil.example/x.css'); color: red"}">a</div>
<div id="b" style="${"width: expression(alert(1))"}">b</div>
`;
}
});
const origWarn = console.warn;
console.warn = () => {};
const el = document.createElement('sec-style-inject2');
document.body.appendChild(el);
await tick();
console.warn = origWarn;
assert.equal(el.querySelector('#a').getAttribute('style') || '', '', '@import refused');
assert.equal(el.querySelector('#b').getAttribute('style') || '', '', 'expression() refused');
el.remove();
});
it('a refused style update does not leave the previous one applied', async () => {
// Refusing the new value is right; keeping the old one on screen is
// not. The DOM then shows a style the template no longer says, and the
// existing refusal test only ever goes dangerous-then-benign, so it
// could not see this.
defineComponent('sec-style-stale', {
data() { return { s: 'color: rgb(255, 0, 0)' }; },
template() { return html`<div id="s" style="${this.state.s}">x</div>`; }
});
const el = document.createElement('sec-style-stale');
document.body.appendChild(el);
await tick();
const div = el.querySelector('#s');
assert.equal(div.style.color, 'rgb(255, 0, 0)', 'the benign style applied first');
const origWarn = console.warn;
console.warn = () => {};
flushSync(() => { el.state.s = "width: expression(alert(1))"; });
console.warn = origWarn;
assert.equal(div.style.color, '', 'the refused update cleared the stale style');
assert.equal(div.style.width, '', 'and did not apply the dangerous one');
el.remove();
});
it('refuses interpolation into an inline <script>', async () => {
window.__vdxScriptPwned = false;
defineComponent('sec-script-slot', {
data() { return { code: 'window.__vdxScriptPwned = true;' }; },
template() {
return html`<div><script>${this.state.code}</script></div>`;
}
});
const origWarn = console.warn;
const warns = [];
console.warn = (...a) => warns.push(a.join(' '));
const el = document.createElement('sec-script-slot');
document.body.appendChild(el);
await tick();
console.warn = origWarn;
assert.equal(window.__vdxScriptPwned, false, 'interpolated script text did NOT execute');
const script = el.querySelector('script');
assert.ok(!script || !script.textContent.includes('Pwned'),
'no interpolated content landed inside the script element');
el.remove();
delete window.__vdxScriptPwned;
});
});
describe('Security - literal script sinks', function(it) {
const mount = (tag) => { const el = document.createElement(tag); document.body.appendChild(el); return el; };
const settle = () => new Promise(r => requestAnimationFrame(r));
it('refuses literal on* and srcdoc in a static subtree as well as a dynamic one', async () => {
// The compiler's static-DOM path and the renderer's literal path are
// two sinks for the same source text. An inline handler written as
// literal text is still an inline handler: it runs outside the
// framework and outside CSP, and only one of the two sinks refused it.
window.__secLit = 0;
defineComponent('sec-literal', class extends Component {
constructor(p) { super(p); this.state = { x: 'dyn' }; }
template() {
return html`
<div id="static"><button onclick="window.__secLit = 1">s</button>
<iframe srcdoc="<b>x</b>"></iframe></div>
<div id="dynamic"><button onclick="window.__secLit = 2">${this.state.x}</button>
<iframe srcdoc="<b>x</b>"></iframe></div>
<svg><rect onclick="window.__secLit = 3"></rect></svg>`;
}
});
const el = mount('sec-literal'); await settle();
for (const scope of ['#static', '#dynamic']) {
const btn = el.querySelector(`${scope} button`);
assert.equal(btn.getAttribute('onclick'), null, `${scope}: literal onclick refused`);
assert.equal(btn.onclick, null, `${scope}: no handler compiled`);
btn.click();
assert.equal(el.querySelector(`${scope} iframe`).getAttribute('srcdoc'), null,
`${scope}: literal srcdoc refused`);
}
assert.equal(el.querySelector('rect').getAttribute('onclick'), null, 'svg: literal onclick refused');
el.querySelector('rect').dispatchEvent(new MouseEvent('click', { bubbles: true }));
assert.equal(window.__secLit, 0, 'no literal handler ran');
el.remove();
});
});