|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { sanitizeHtml } from '../sanitizeHtml' |
| 3 | + |
| 4 | +describe('sanitizeHtml', () => { |
| 5 | + it('preserves allowed tags and content', () => { |
| 6 | + const input = '<p>Hello <strong>World</strong></p>' |
| 7 | + const output = sanitizeHtml(input) |
| 8 | + expect(output).toBe('<p>Hello <strong>World</strong></p>') |
| 9 | + }) |
| 10 | + |
| 11 | + it('removes disallowed tags but preserves inner content', () => { |
| 12 | + const input = '<script>alert("XSS")</script><p>Safe</p>' |
| 13 | + const output = sanitizeHtml(input) |
| 14 | + expect(output).toBe('alert("XSS")<p>Safe</p>') |
| 15 | + }) |
| 16 | + |
| 17 | + it('removes disallowed attributes', () => { |
| 18 | + const input = '<p style="color: red;" onclick="alert(1)">Hello</p>' |
| 19 | + const output = sanitizeHtml(input) |
| 20 | + expect(output).toBe('<p>Hello</p>') |
| 21 | + }) |
| 22 | + |
| 23 | + it('preserves allowed attributes on allowed tags', () => { |
| 24 | + const input = '<a href="https://example.com" target="_blank">Link</a>' |
| 25 | + const output = sanitizeHtml(input) |
| 26 | + expect(output).toBe( |
| 27 | + '<a href="https://example.com" target="_blank">Link</a>' |
| 28 | + ) |
| 29 | + }) |
| 30 | + |
| 31 | + it('removes dangerous javascript: hrefs', () => { |
| 32 | + const input = '<a href="javascript:alert(1)">Click me</a>' |
| 33 | + const output = sanitizeHtml(input) |
| 34 | + expect(output).toBe('<a>Click me</a>') |
| 35 | + }) |
| 36 | + |
| 37 | + it('removes dangerous data: URIs', () => { |
| 38 | + const input = '<a href="data:text/html;base64,...">Click me</a>' |
| 39 | + const output = sanitizeHtml(input) |
| 40 | + expect(output).toBe('<a>Click me</a>') |
| 41 | + }) |
| 42 | + |
| 43 | + it('allows custom tags and attributes if provided', () => { |
| 44 | + const input = '<custom-el data-id="123">Test</custom-el>' |
| 45 | + const output = sanitizeHtml(input, ['custom-el'], { |
| 46 | + 'custom-el': ['data-id'] |
| 47 | + }) |
| 48 | + expect(output).toBe('<custom-el data-id="123">Test</custom-el>') |
| 49 | + }) |
| 50 | + |
| 51 | + it('unwraps unknown custom elements if not allowed', () => { |
| 52 | + const input = '<my-tag><b>Bold</b></my-tag>' |
| 53 | + const output = sanitizeHtml(input) |
| 54 | + expect(output).toBe('<b>Bold</b>') |
| 55 | + }) |
| 56 | + |
| 57 | + it('is case-insensitive for tag and attribute matching', () => { |
| 58 | + const input = '<A HREF="https://example.com" TARGET="_blank">Link</A>' |
| 59 | + const output = sanitizeHtml(input) |
| 60 | + expect(output).toBe( |
| 61 | + '<a href="https://example.com" target="_blank">Link</a>' |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('unwraps nested disallowed tags correctly', () => { |
| 66 | + const input = '<div><span><script>alert(1)</script></span></div>' |
| 67 | + const output = sanitizeHtml(input) |
| 68 | + expect(output).toBe('alert(1)') |
| 69 | + }) |
| 70 | +}) |
0 commit comments