|
| 1 | +import { sanitizeUserDataForEmail } from './sanitizeUserDataForEmail' |
| 2 | + |
| 3 | +describe('sanitizeUserDataForEmail', () => { |
| 4 | + it('should remove anchor tags', () => { |
| 5 | + const input = '<a href="https://example.com">Click me</a>' |
| 6 | + const result = sanitizeUserDataForEmail(input) |
| 7 | + expect(result).toBe('Click me') |
| 8 | + }) |
| 9 | + |
| 10 | + it('should remove script tags', () => { |
| 11 | + const unsanitizedData = '<script>alert</script>' |
| 12 | + const sanitizedData = sanitizeUserDataForEmail(unsanitizedData) |
| 13 | + expect(sanitizedData).toBe('alert') |
| 14 | + }) |
| 15 | + |
| 16 | + it('should remove mixed-case script tags', () => { |
| 17 | + const input = '<ScRipT>alert(1)</sCrIpT>' |
| 18 | + const result = sanitizeUserDataForEmail(input) |
| 19 | + expect(result).toBe('alert1') |
| 20 | + }) |
| 21 | + |
| 22 | + it('should remove embedded base64-encoded scripts', () => { |
| 23 | + const input = '<img src="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">' |
| 24 | + const result = sanitizeUserDataForEmail(input) |
| 25 | + expect(result).toBe('') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should remove iframe elements', () => { |
| 29 | + const input = '<iframe src="malicious.com"></iframe>Frame' |
| 30 | + const result = sanitizeUserDataForEmail(input) |
| 31 | + expect(result).toBe('Frame') |
| 32 | + }) |
| 33 | + |
| 34 | + it('should remove javascript: links in attributes', () => { |
| 35 | + const input = '<a href="javascript:alert(1)">click</a>' |
| 36 | + const result = sanitizeUserDataForEmail(input) |
| 37 | + expect(result).toBe('click') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should remove mismatched script input', () => { |
| 41 | + const input = '<script>console.log("test")' |
| 42 | + const result = sanitizeUserDataForEmail(input) |
| 43 | + expect(result).toBe('console.log\"test\"') |
| 44 | + }) |
| 45 | + |
| 46 | + it('should remove encoded scripts via HTML entities', () => { |
| 47 | + const input = '<script>alert(1)</script>' |
| 48 | + const result = sanitizeUserDataForEmail(input) |
| 49 | + expect(result).toBe('alert1') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should remove template injection syntax', () => { |
| 53 | + const input = '{{7*7}}' |
| 54 | + const result = sanitizeUserDataForEmail(input) |
| 55 | + expect(result).toBe('77') |
| 56 | + }) |
| 57 | + |
| 58 | + it('should remove invisible zero-width characters', () => { |
| 59 | + const input = 'a\u200Bler\u200Bt("XSS")' |
| 60 | + const result = sanitizeUserDataForEmail(input) |
| 61 | + expect(result).toBe('alert\"XSS\"') |
| 62 | + }) |
| 63 | + |
| 64 | + it('should remove CSS expressions within style attributes', () => { |
| 65 | + const input = '<div style="width: expression(alert(\'XSS\'));">Hello</div>' |
| 66 | + const result = sanitizeUserDataForEmail(input) |
| 67 | + expect(result).toBe('Hello') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not render SVG with onload event', () => { |
| 71 | + const input = '<svg onload="alert(\'XSS\')">Graphic</svg>' |
| 72 | + const result = sanitizeUserDataForEmail(input) |
| 73 | + expect(result).toBe('Graphic') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should not allow backtick-based patterns', () => { |
| 77 | + const input = '`alert("XSS")`' |
| 78 | + const result = sanitizeUserDataForEmail(input) |
| 79 | + expect(result).toBe('alert\"XSS\"') |
| 80 | + }) |
| 81 | + |
| 82 | + it('should preserve allowed punctuation', () => { |
| 83 | + const input = `Hello "world" - it's safe!` |
| 84 | + const result = sanitizeUserDataForEmail(input) |
| 85 | + expect(result).toBe(`Hello "world" - it's safe!`) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should return empty string for non-string input', () => { |
| 89 | + expect(sanitizeUserDataForEmail(null)).toBe('') |
| 90 | + expect(sanitizeUserDataForEmail(undefined)).toBe('') |
| 91 | + expect(sanitizeUserDataForEmail(123)).toBe('') |
| 92 | + expect(sanitizeUserDataForEmail({})).toBe('') |
| 93 | + }) |
| 94 | + |
| 95 | + it('should return empty string for an empty string', () => { |
| 96 | + expect(sanitizeUserDataForEmail('')).toBe('') |
| 97 | + }) |
| 98 | + |
| 99 | + it('should collapse excessive whitespace', () => { |
| 100 | + const input = 'This is \n\n a test' |
| 101 | + expect(sanitizeUserDataForEmail(input)).toBe('This is a test') |
| 102 | + }) |
| 103 | + |
| 104 | + it('should truncate to maxLength characters', () => { |
| 105 | + const input = 'a'.repeat(200) |
| 106 | + const result = sanitizeUserDataForEmail(input, 50) |
| 107 | + expect(result.length).toBe(50) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should remove characters outside allowed punctuation', () => { |
| 111 | + const input = 'Hello @#$%^*()_+=[]{}|\\~`' |
| 112 | + const result = sanitizeUserDataForEmail(input) |
| 113 | + expect(result).toBe('Hello') |
| 114 | + }) |
| 115 | + it('should sanitize syntax in regex-like input', () => { |
| 116 | + const input = '(?=XSS)(?:abc)' |
| 117 | + const result = sanitizeUserDataForEmail(input) |
| 118 | + expect(result).toBe('XSSabc') |
| 119 | + }) |
| 120 | + |
| 121 | + it('should handle string of only control characters', () => { |
| 122 | + const input = '\x01\x02\x03\x04' |
| 123 | + const result = sanitizeUserDataForEmail(input) |
| 124 | + expect(result).toBe('') |
| 125 | + }) |
| 126 | + |
| 127 | + it('should sanitize complex script attempt with mixed encoding', () => { |
| 128 | + const input = '<script>alert(String.fromCharCode(88,83,83))</script>' |
| 129 | + const result = sanitizeUserDataForEmail(input) |
| 130 | + expect(result).toBe('alertString.fromCharCode88,83,83') |
| 131 | + }) |
| 132 | + |
| 133 | + it('should handle deeply nested HTML tags correctly', () => { |
| 134 | + const input = `<div><section><article><p>Hello <strong>world <em>from <span>deep <a href="#">tags</a></span></em></strong></p></article></section></div>` |
| 135 | + const result = sanitizeUserDataForEmail(input) |
| 136 | + expect(result).toBe('Hello world from deep tags') |
| 137 | + }) |
| 138 | + |
| 139 | + it('should preserve accented Spanish characters', () => { |
| 140 | + const input = '¡Hola! ¿Cómo estás? ÁÉÍÓÚ ÜÑ ñáéíóú ü' |
| 141 | + const result = sanitizeUserDataForEmail(input) |
| 142 | + expect(result).toBe('¡Hola! ¿Cómo estás? ÁÉÍÓÚ ÜÑ ñáéíóú ü') |
| 143 | + }) |
| 144 | + |
| 145 | + it('should preserve Arabic characters with diacritics', () => { |
| 146 | + const input = 'مَرْحَبًا بِكَ فِي الْمَوْقِعِ' |
| 147 | + const result = sanitizeUserDataForEmail(input) |
| 148 | + expect(result).toBe('مَرْحَبًا بِكَ فِي الْمَوْقِعِ') |
| 149 | + }) |
| 150 | + |
| 151 | + it('should preserve Japanese characters', () => { |
| 152 | + const input = 'こんにちゎ、世界!!〆' |
| 153 | + const result = sanitizeUserDataForEmail(input) |
| 154 | + expect(result).toBe('こんにちゎ、世界!!〆') |
| 155 | + }) |
| 156 | +}) |
0 commit comments