|
| 1 | +// http://1nfosec4all.blogspot.com/2012/07/bulletin-board-code-bbcode-xss-exploit.html |
| 2 | +import React from 'react'; |
| 3 | +import { shallow } from 'enzyme'; |
| 4 | + |
| 5 | +import parser from '../'; |
| 6 | + |
| 7 | +describe('security test', () => { |
| 8 | + it('should not allow [URL] Tag injection', () => { |
| 9 | + const bbcode = '[url]javascript:alert(0)[/url]'; |
| 10 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>).children().first(); |
| 11 | + |
| 12 | + expect(wrapper.text()).toBe('javascript:alert(0)'); |
| 13 | + expect(wrapper.type()).toBeUndefined(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should not allow [COLOR] Tag Injection', () => { |
| 17 | + const bbcode = '[color=#ff0000;font-size:100px;]Got You[/color]'; |
| 18 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>).children().first(); |
| 19 | + |
| 20 | + expect(wrapper.text()).toBe('Got You'); |
| 21 | + expect(wrapper.prop('style').color).not.toBe('#ff0000;'); |
| 22 | + expect(wrapper.prop('style').fontSize).toBeUndefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not allow [COLOR] Tag Injection', () => { |
| 26 | + const bbcode = '[color=#ff0000;You:expression(alert(String.fromCharCode(88,83,83)));]Got You[/color]'; |
| 27 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>).children().first(); |
| 28 | + |
| 29 | + expect(wrapper.text()).toBe('Got You'); |
| 30 | + expect(wrapper.prop('style').color).not.toBe('#ff0000;'); |
| 31 | + expect(wrapper.prop('style').color).toBe('#ff0000;You:expression(alert(String.fromCharCode(88,83,83)));'); |
| 32 | + expect(wrapper.prop('style').expression).toBeUndefined(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should not allow [FONT] Tag Injection', () => { |
| 36 | + const bbcode = '[font=Impact, Compacta, Chicago, sans-serif;color:red;]Got You[/font]'; |
| 37 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>); |
| 38 | + |
| 39 | + expect(wrapper.html()).toBe('<div>[font=Impact, Compacta, Chicago, sans-serif;color:red;]Got You[/font]</div>'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not allow [FONT] Tag Injection', () => { |
| 43 | + const bbcode = '[font=Impact, Compacta, Chicago, sans-serif;You:expression(alert(String.fromCharCode(88,83,83)));]Got You[/font]'; |
| 44 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>); |
| 45 | + |
| 46 | + expect(wrapper.html()).toBe('<div>[font=Impact, Compacta, Chicago, sans-serif;You:expression(alert(String.fromCharCode(88,83,83)));]Got You[/font]</div>'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not allow [IMG] Tag Injection', () => { |
| 50 | + const bbcode = '[img]NotExist.png" onerror="alert(String.fromCharCode(88,83,83))[/img]'; |
| 51 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>).children().first(); |
| 52 | + |
| 53 | + expect(wrapper.type()).toBe('img'); |
| 54 | + expect(wrapper.prop('onerror')).toBeUndefined(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not allow [TABLE] Tag Injection', () => { |
| 58 | + const bbcode = '[table cellSpacing="0" cellPadding="0" width="100%"][tbody][tr][td width="*" onmouseover="alert(String.fromCharCode(88,83,83))"]Got You[/td][/tr][/tbody][/table]'; |
| 59 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>); |
| 60 | + |
| 61 | + expect(wrapper.find('td').first().prop('width')).toBe('*'); |
| 62 | + expect(wrapper.find('td').first().prop('onmouseover')).toBeUndefined(); |
| 63 | + expect(wrapper.find('td').first().text()).toBe('Got You'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should not allow Nested Tags Injection', () => { |
| 67 | + const bbcode = '[url]http://www.good.com?[url] onmousemove=javascript:alert(String.fromCharCode(88,83,83));//[/url][/url]'; |
| 68 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>); |
| 69 | + |
| 70 | + expect(wrapper.find('a').length).toBe(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not allow Nested Tags Injection', () => { |
| 74 | + const bbcode = '[img]http://foo.com/NotExist.png [img] onerror=javascript:alert(String.fromCharCode(88,83,83)) [/img] [/img]'; |
| 75 | + const wrapper = shallow(<div>{parser.toReact(bbcode)}</div>); |
| 76 | + |
| 77 | + expect(wrapper.find('img').length).toBe(1); |
| 78 | + expect(wrapper.find('img').first().type()).toBe('img'); |
| 79 | + expect(wrapper.find('img').first().prop('onerror')).toBeUndefined(); |
| 80 | + }); |
| 81 | +}); |
0 commit comments