| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.sanitize' ); |
| 8 |
|
| 9 |
QUnit.test( 've.sanitizeHtml', ( assert ) => { |
| 10 |
|
| 11 |
const cases = [ |
| 12 |
{ |
| 13 |
msg: 'Unsafe link href removed', |
| 14 |
html: '<a href="javascript:alert(1);">Foo</a>', |
| 15 |
expected: '<a>Foo</a>' |
| 16 |
}, |
| 17 |
{ |
| 18 |
msg: 'Unsafe onclick removed', |
| 19 |
html: '<span onclick="alert(1);">Foo</span>', |
| 20 |
expected: '<span>Foo</span>' |
| 21 |
}, |
| 22 |
{ |
| 23 |
msg: 'script and style elements removed', |
| 24 |
html: '<script></script><style></style>', |
| 25 |
expected: '' |
| 26 |
}, |
| 27 |
{ |
| 28 |
msg: 'style attribute allowed', |
| 29 |
html: '<span style="font-weight: 700;">Foo</span>', |
| 30 |
expected: '<span style="font-weight: 700;">Foo</span>' |
| 31 |
}, |
| 32 |
{ |
| 33 |
msg: 'RDFa attributes allowed', |
| 34 |
html: '<span about="#1" rel="#2" resource="#3" property="#4" content="#5" dataType="#6" typeof="#7">Foo</span>', |
| 35 |
expected: '<span about="#1" rel="#2" resource="#3" property="#4" content="#5" dataType="#6" typeof="#7">Foo</span>' |
| 36 |
}, |
| 37 |
{ |
| 38 |
msg: 'figure-inline element allowed', |
| 39 |
html: '<figure-inline></figure-inline>', |
| 40 |
expected: '<figure-inline></figure-inline>' |
| 41 |
}, |
| 42 |
{ |
| 43 |
msg: 'HTML tag in attribute allowed', |
| 44 |
html: '<span content="<b>Foo</b>">Foo</span>', |
| 45 |
expected: '<span content="<b>Foo</b>">Foo</span>' |
| 46 |
}, |
| 47 |
{ |
| 48 |
msg: 'Self-closing HTML tag in attribute allowed', |
| 49 |
html: '<span content="<img/>">Foo</span>', |
| 50 |
expected: '<span content="<img/>">Foo</span>' |
| 51 |
} |
| 52 |
]; |
| 53 |
|
| 54 |
cases.forEach( ( caseItem ) => { |
| 55 |
const actual = document.createElement( 'div' ); |
| 56 |
const nodes = ve.sanitizeHtml( caseItem.html ); |
| 57 |
Array.prototype.forEach.call( nodes, ( node ) => { |
| 58 |
actual.appendChild( node ); |
| 59 |
} ); |
| 60 |
|
| 61 |
const expected = document.createElement( 'div' ); |
| 62 |
expected.innerHTML = caseItem.expected; |
| 63 |
assert.equalDomElement( actual, expected, caseItem.msg ); |
| 64 |
} ); |
| 65 |
|
| 66 |
} ); |
| 67 |
|
| 68 |
QUnit.test( 've.sanitizeHtmlToDocument', ( assert ) => { |
| 69 |
|
| 70 |
const nodes = ve.sanitizeHtml( '<a href="allowed">Foo</a>' ); |
| 71 |
const body = document.createElement( 'body' ); |
| 72 |
Array.prototype.forEach.call( nodes, ( node ) => { |
| 73 |
body.appendChild( node ); |
| 74 |
} ); |
| 75 |
const sanitizedDocument = ve.sanitizeHtmlToDocument( '<a href="allowed">Foo</a>' ); |
| 76 |
|
| 77 |
assert.equalDomElement( sanitizedDocument.body, body, 'Body node is the same as if we used ve.sanitizeHtml' ); |
| 78 |
|
| 79 |
} ); |
| 80 |
|
| 81 |
QUnit.test( 've.setAttributeSafe', ( assert ) => { |
| 82 |
|
| 83 |
const cases = [ |
| 84 |
{ |
| 85 |
msg: 'Unsafe link href sets fallback instead', |
| 86 |
element: '<a rel="bar">Foo</a>', |
| 87 |
attr: 'href', |
| 88 |
|
| 89 |
val: 'javascript:alert(1)', |
| 90 |
fallbackVal: '#', |
| 91 |
expected: '<a href="#" rel="bar">Foo</a>' |
| 92 |
}, |
| 93 |
{ |
| 94 |
msg: 'Unsafe link href without fallback is no-op', |
| 95 |
element: '<a rel="bar">Foo</a>', |
| 96 |
attr: 'href', |
| 97 |
|
| 98 |
val: 'javascript:alert(1)', |
| 99 |
expected: '<a rel="bar">Foo</a>' |
| 100 |
}, |
| 101 |
{ |
| 102 |
msg: 'Safe link is set', |
| 103 |
element: '<a rel="bar">Foo</a>', |
| 104 |
attr: 'href', |
| 105 |
val: '#javascript', |
| 106 |
expected: '<a href="#javascript" rel="bar">Foo</a>' |
| 107 |
}, |
| 108 |
{ |
| 109 |
msg: 'onclick is not set', |
| 110 |
element: '<div>Foo</div>', |
| 111 |
attr: 'onclick', |
| 112 |
val: 'alert()', |
| 113 |
expected: '<div>Foo</div>' |
| 114 |
}, |
| 115 |
{ |
| 116 |
msg: 'RDFa attribute is set', |
| 117 |
element: '<div>Foo</div>', |
| 118 |
attr: 'about', |
| 119 |
val: '#1', |
| 120 |
expected: '<div about="#1">Foo</div>' |
| 121 |
} |
| 122 |
]; |
| 123 |
|
| 124 |
cases.forEach( ( caseItem ) => { |
| 125 |
const actual = document.createElement( 'div' ); |
| 126 |
actual.innerHTML = caseItem.element; |
| 127 |
ve.setAttributeSafe( |
| 128 |
actual.childNodes[ 0 ], |
| 129 |
caseItem.attr, |
| 130 |
caseItem.val, |
| 131 |
caseItem.fallbackVal |
| 132 |
); |
| 133 |
|
| 134 |
const expected = document.createElement( 'div' ); |
| 135 |
expected.innerHTML = caseItem.expected; |
| 136 |
|
| 137 |
assert.equalDomElement( actual, expected, caseItem.msg ); |
| 138 |
} ); |
| 139 |
|
| 140 |
} ); |