|
| 1 | +/** |
| 2 | + * telegram-md2html-security.test.ts — Security tests for Issue #3173. |
| 3 | + * |
| 4 | + * Tests URI scheme allowlist (sanitizeHref), topic name sanitization |
| 5 | + * (sanitizeTopicName), and callback_data length guard (safeCallbackData). |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | + |
| 10 | +// ── Inline copies of the helpers (they're private in telegram.ts) ── |
| 11 | + |
| 12 | +const ALLOWED_HREF_SCHEMES = ['http:', 'https:', '#:', 'mailto:']; |
| 13 | + |
| 14 | +function esc(text: string): string { |
| 15 | + return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); |
| 16 | +} |
| 17 | + |
| 18 | +function sanitizeHref(href: string): string { |
| 19 | + const trimmed = href.trim(); |
| 20 | + if (trimmed.startsWith('#') || trimmed.startsWith('/')) return esc(trimmed); |
| 21 | + const scheme = trimmed.split(':')[0]?.toLowerCase() + ':'; |
| 22 | + if (ALLOWED_HREF_SCHEMES.includes(scheme)) return esc(trimmed); |
| 23 | + return '#'; |
| 24 | +} |
| 25 | + |
| 26 | +function sanitizeTopicName(name: string): string { |
| 27 | + return name |
| 28 | + .replace(/[\u0000-\u001F\u007F-\u009F\u200E-\u200F\u202A-\u202E]/g, '') |
| 29 | + .replace(/\s+/g, ' ') |
| 30 | + .trim() |
| 31 | + .slice(0, 64); |
| 32 | +} |
| 33 | + |
| 34 | +const MAX_CALLBACK_DATA_LENGTH = 64; |
| 35 | + |
| 36 | +function safeCallbackData(data: string): string { |
| 37 | + if (Buffer.byteLength(data, 'utf-8') <= MAX_CALLBACK_DATA_LENGTH) return data; |
| 38 | + const prefix = data.substring(0, data.lastIndexOf(':') + 1); |
| 39 | + const value = data.substring(prefix.length); |
| 40 | + let lo = 0, hi = value.length; |
| 41 | + while (lo < hi) { |
| 42 | + const mid = Math.ceil((lo + hi) / 2); |
| 43 | + if (Buffer.byteLength(prefix + value.slice(0, mid), 'utf-8') <= MAX_CALLBACK_DATA_LENGTH) { |
| 44 | + lo = mid; |
| 45 | + } else { |
| 46 | + hi = mid - 1; |
| 47 | + } |
| 48 | + } |
| 49 | + return prefix + value.slice(0, lo); |
| 50 | +} |
| 51 | + |
| 52 | +// ── Tests ── |
| 53 | + |
| 54 | +describe('Security: md2html URI scheme allowlist (#3173)', () => { |
| 55 | + describe('sanitizeHref', () => { |
| 56 | + it('should allow http:// links', () => { |
| 57 | + expect(sanitizeHref('http://example.com')).toBe('http://example.com'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should allow https:// links', () => { |
| 61 | + expect(sanitizeHref('https://example.com/path?q=1')).toBe('https://example.com/path?q=1'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should allow mailto: links', () => { |
| 65 | + expect(sanitizeHref('mailto:user@example.com')).toBe('mailto:user@example.com'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should allow anchor links (#)', () => { |
| 69 | + expect(sanitizeHref('#section')).toBe('#section'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should allow relative links (/)', () => { |
| 73 | + expect(sanitizeHref('/path/to/page')).toBe('/path/to/page'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should block tg:// deep links', () => { |
| 77 | + expect(sanitizeHref('tg://resolve?domain=attacker_bot')).toBe('#'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should block javascript: URIs', () => { |
| 81 | + expect(sanitizeHref('javascript:alert(1)')).toBe('#'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should block data: URIs', () => { |
| 85 | + expect(sanitizeHref('data:text/html,<script>alert(1)</script>')).toBe('#'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should block file: URIs', () => { |
| 89 | + expect(sanitizeHref('file:///etc/passwd')).toBe('#'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should block ftp:// URIs', () => { |
| 93 | + expect(sanitizeHref('ftp://evil.com/malware')).toBe('#'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should block viber:// URIs', () => { |
| 97 | + expect(sanitizeHref('viber://chat?number=123')).toBe('#'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should be case-insensitive for scheme check', () => { |
| 101 | + expect(sanitizeHref('TG://resolve?domain=bot')).toBe('#'); |
| 102 | + expect(sanitizeHref('JavaScript:alert(1)')).toBe('#'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle empty href', () => { |
| 106 | + expect(sanitizeHref('')).toBe('#'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle whitespace-padded hrefs', () => { |
| 110 | + expect(sanitizeHref(' https://example.com ')).toBe('https://example.com'); |
| 111 | + expect(sanitizeHref(' tg://evil ')).toBe('#'); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('Security: topic name sanitization (#3173)', () => { |
| 117 | + describe('sanitizeTopicName', () => { |
| 118 | + it('should preserve normal text', () => { |
| 119 | + expect(sanitizeTopicName('🤖 My Session')).toBe('🤖 My Session'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should strip null bytes', () => { |
| 123 | + expect(sanitizeTopicName('ses\u0000sion')).toBe('session'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should strip control characters', () => { |
| 127 | + expect(sanitizeTopicName('ses\u001Fsion')).toBe('session'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should strip RTL override (U+202E)', () => { |
| 131 | + expect(sanitizeTopicName('hello\u202Eworld')).toBe('helloworld'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should strip LRE/RLE/LRO/RLO/PDF', () => { |
| 135 | + const name = 'test\u202A\u202B\u202C\u202D\u202Ename'; |
| 136 | + expect(sanitizeTopicName(name)).toBe('testname'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should collapse multiple whitespace', () => { |
| 140 | + expect(sanitizeTopicName('hello world')).toBe('hello world'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should trim leading/trailing whitespace', () => { |
| 144 | + expect(sanitizeTopicName(' session ')).toBe('session'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should truncate to 64 characters', () => { |
| 148 | + const long = '🤖 ' + 'x'.repeat(100); |
| 149 | + expect(sanitizeTopicName(long).length).toBeLessThanOrEqual(64); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle empty string', () => { |
| 153 | + expect(sanitizeTopicName('')).toBe(''); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should handle string that is only control chars', () => { |
| 157 | + expect(sanitizeTopicName('\u0000\u0001\u001F')).toBe(''); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +describe('Security: callback_data length guard (#3173)', () => { |
| 163 | + describe('safeCallbackData', () => { |
| 164 | + it('should pass through short data unchanged', () => { |
| 165 | + const sid = '12345678-1234-1234-1234-123456789012'; |
| 166 | + expect(safeCallbackData(`perm_approve:${sid}`)).toBe(`perm_approve:${sid}`); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should truncate long option values', () => { |
| 170 | + const sid = '12345678-1234-1234-1234-123456789012'; |
| 171 | + const longValue = 'a'.repeat(100); |
| 172 | + const result = safeCallbackData(`cb_option:${sid}:${longValue}`); |
| 173 | + expect(Buffer.byteLength(result, 'utf-8')).toBeLessThanOrEqual(64); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should preserve prefix when truncating', () => { |
| 177 | + const sid = '12345678-1234-1234-1234-123456789012'; |
| 178 | + const longValue = 'a'.repeat(100); |
| 179 | + const result = safeCallbackData(`cb_option:${sid}:${longValue}`); |
| 180 | + expect(result).toMatch(/^cb_option:/); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should handle exactly 64-byte data', () => { |
| 184 | + const data = 'x'.repeat(64); |
| 185 | + expect(safeCallbackData(data)).toBe(data); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should handle 65-byte data', () => { |
| 189 | + const data = 'x'.repeat(65); |
| 190 | + const result = safeCallbackData(data); |
| 191 | + expect(Buffer.byteLength(result, 'utf-8')).toBeLessThanOrEqual(64); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should handle multi-byte UTF-8 characters', () => { |
| 195 | + const sid = '12345678-1234-1234-1234-123456789012'; |
| 196 | + const emojiValue = '🎉'.repeat(30); // each emoji is 4 bytes |
| 197 | + const result = safeCallbackData(`cb_option:${sid}:${emojiValue}`); |
| 198 | + expect(Buffer.byteLength(result, 'utf-8')).toBeLessThanOrEqual(64); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments