/** * Tests for wildcardToSafeRegex utility * Critical for ReDoS vulnerability prevention */ import { wildcardToSafeRegex } from '../wildcardToSafeRegex'; describe('wildcardToSafeRegex', () => { it('should handle basic wildcard patterns', () => { const regex = wildcardToSafeRegex('*.example.com'); expect(regex.test('api.example.com')).toBe(true); expect(regex.test('www.example.com')).toBe(true); expect(regex.test('example.com')).toBe(false); expect(regex.test('example.org')).toBe(false); }); it('should escape regex metacharacters', () => { const regex = wildcardToSafeRegex('test.+pattern'); expect(regex.test('test.+pattern')).toBe(true); expect(regex.test('testXpattern')).toBe(false); }); it('should handle multiple wildcards', () => { const regex = wildcardToSafeRegex('*.*'); expect(regex.test('api.example.com')).toBe(true); expect(regex.test('localhost')).toBe(false); }); it('should prevent ReDoS attacks with consecutive wildcards', () => { const start = Date.now(); const regex = wildcardToSafeRegex('a*****b'); // This should complete quickly (< 100ms) const testResult = regex.test('a' + 'x'.repeat(100) + 'c'); const elapsed = Date.now() - start; expect(elapsed).toBeLessThan(100); expect(testResult).toBe(false); }); it('should handle edge cases', () => { expect(wildcardToSafeRegex('').test('')).toBe(true); expect(wildcardToSafeRegex('*').test('anything')).toBe(true); expect(wildcardToSafeRegex('exact').test('exact')).toBe(true); }); it('should handle complex patterns', () => { const regex = wildcardToSafeRegex('https://*.api.*.com'); expect(regex.test('https://v1.api.example.com')).toBe(true); expect(regex.test('https://v2.api.test.com')).toBe(true); expect(regex.test('http://api.example.com')).toBe(false); }); });