| 1 |
import { sanitizeString } from '@shared/utils/sanitize'; |
| 2 |
|
| 3 |
describe('sanitizeString', () => { |
| 4 |
it('should return an empty string if input is null or undefined', () => { |
| 5 |
expect(sanitizeString(null)).toBe(''); |
| 6 |
expect(sanitizeString(undefined)).toBe(''); |
| 7 |
expect(sanitizeString('')).toBe(''); |
| 8 |
}); |
| 9 |
|
| 10 |
it('should not modify safe HTML content', () => { |
| 11 |
const safeHtml = '<p>Hello, <b>World</b>!</p>'; |
| 12 |
expect(sanitizeString(safeHtml)).toBe(safeHtml); |
| 13 |
}); |
| 14 |
|
| 15 |
it('should remove disallowed HTML tags', () => { |
| 16 |
const dirtyHtml = '<script>alert("XSS")</script><p>Secure text</p>'; |
| 17 |
expect(sanitizeString(dirtyHtml)).toBe('alert("XSS")<p>Secure text</p>'); |
| 18 |
}); |
| 19 |
|
| 20 |
it('should remove multiple disallowed tags', () => { |
| 21 |
const dirtyHtml = |
| 22 |
'<iframe src="http://malicious.com"></iframe><meta><style>body{background:red;}</style><p>Ok</p>'; |
| 23 |
expect(sanitizeString(dirtyHtml)).toBe('body{background:red;}<p>Ok</p>'); |
| 24 |
}); |
| 25 |
|
| 26 |
it('should remove JavaScript URLs', () => { |
| 27 |
const dirtyHtml = '<a href="javascript:alert(\'XSS\')">Click here</a>'; |
| 28 |
expect(sanitizeString(dirtyHtml)).toBe( |
| 29 |
'<a href="alert(\'XSS\')">Click here</a>', |
| 30 |
); |
| 31 |
}); |
| 32 |
}); |
| 33 |
|