|
| 1 | +import { describe, expect, it, afterEach } from '@jest/globals' |
| 2 | +import { getSecureAppUrl, getSecureTokenLink } from '../../../src/enterprise/utils/url.util' |
| 3 | + |
| 4 | +describe('URL Security Utilities', () => { |
| 5 | + const originalEnv = process.env.APP_URL |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + if (originalEnv) { |
| 9 | + process.env.APP_URL = originalEnv |
| 10 | + } else { |
| 11 | + delete process.env.APP_URL |
| 12 | + } |
| 13 | + }) |
| 14 | + |
| 15 | + describe('getSecureAppUrl', () => { |
| 16 | + it('should throw error if APP_URL is not configured', () => { |
| 17 | + delete process.env.APP_URL |
| 18 | + expect(() => getSecureAppUrl()).toThrow('APP_URL environment variable is not configured') |
| 19 | + }) |
| 20 | + |
| 21 | + it('should throw error if APP_URL is not a valid URL', () => { |
| 22 | + process.env.APP_URL = 'example.com' |
| 23 | + expect(() => getSecureAppUrl()).toThrow('APP_URL environment variable is not a valid URL: "example.com"') |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return HTTPS URL unchanged', () => { |
| 27 | + process.env.APP_URL = 'https://example.com' |
| 28 | + expect(getSecureAppUrl()).toBe('https://example.com') |
| 29 | + }) |
| 30 | + |
| 31 | + it('should convert HTTP to HTTPS for production URLs', () => { |
| 32 | + process.env.APP_URL = 'http://example.com' |
| 33 | + const result = getSecureAppUrl() |
| 34 | + expect(result).toBe('https://example.com') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should allow HTTP for localhost', () => { |
| 38 | + process.env.APP_URL = 'http://localhost:3000' |
| 39 | + expect(getSecureAppUrl()).toBe('http://localhost:3000') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should allow HTTP for 127.0.0.1', () => { |
| 43 | + process.env.APP_URL = 'http://127.0.0.1:3000' |
| 44 | + expect(getSecureAppUrl()).toBe('http://127.0.0.1:3000') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should allow HTTP for ::1 (IPv6 localhost)', () => { |
| 48 | + process.env.APP_URL = 'http://[::1]:3000' |
| 49 | + expect(getSecureAppUrl()).toBe('http://[::1]:3000') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should allow HTTP for 0.0.0.0', () => { |
| 53 | + process.env.APP_URL = 'http://0.0.0.0:3000' |
| 54 | + expect(getSecureAppUrl()).toBe('http://0.0.0.0:3000') |
| 55 | + }) |
| 56 | + |
| 57 | + it('should append path correctly', () => { |
| 58 | + process.env.APP_URL = 'https://example.com' |
| 59 | + expect(getSecureAppUrl('/reset-password')).toBe('https://example.com/reset-password') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should handle trailing slash in base URL', () => { |
| 63 | + process.env.APP_URL = 'https://example.com/' |
| 64 | + expect(getSecureAppUrl('/reset-password')).toBe('https://example.com/reset-password') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should handle path without leading slash', () => { |
| 68 | + process.env.APP_URL = 'https://example.com' |
| 69 | + expect(getSecureAppUrl('reset-password')).toBe('https://example.com/reset-password') |
| 70 | + }) |
| 71 | + |
| 72 | + it('should convert HTTP to HTTPS and append path', () => { |
| 73 | + process.env.APP_URL = 'http://example.com' |
| 74 | + expect(getSecureAppUrl('/verify')).toBe('https://example.com/verify') |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('getSecureTokenLink', () => { |
| 79 | + it('should create secure link with token', () => { |
| 80 | + process.env.APP_URL = 'https://example.com' |
| 81 | + const result = getSecureTokenLink('/reset-password', 'abc123') |
| 82 | + expect(result).toBe('https://example.com/reset-password?token=abc123') |
| 83 | + }) |
| 84 | + |
| 85 | + it('should convert HTTP to HTTPS in token link', () => { |
| 86 | + process.env.APP_URL = 'http://example.com' |
| 87 | + const result = getSecureTokenLink('/reset-password', 'abc123') |
| 88 | + expect(result).toBe('https://example.com/reset-password?token=abc123') |
| 89 | + }) |
| 90 | + |
| 91 | + it('should allow HTTP localhost in token link', () => { |
| 92 | + process.env.APP_URL = 'http://localhost:3000' |
| 93 | + const result = getSecureTokenLink('/verify', 'xyz789') |
| 94 | + expect(result).toBe('http://localhost:3000/verify?token=xyz789') |
| 95 | + }) |
| 96 | + |
| 97 | + it('should handle complex tokens', () => { |
| 98 | + process.env.APP_URL = 'https://example.com' |
| 99 | + const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0' |
| 100 | + const result = getSecureTokenLink('/register', token) |
| 101 | + expect(result).toBe(`https://example.com/register?token=${token}`) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe('Security scenarios', () => { |
| 106 | + it('should prevent HTTP password reset links in production', () => { |
| 107 | + process.env.APP_URL = 'http://myapp.com' |
| 108 | + const resetLink = getSecureTokenLink('/reset-password', 'secret-token') |
| 109 | + expect(resetLink).toMatch(/^https:\/\//) |
| 110 | + expect(resetLink).not.toMatch(/^http:\/\//) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should prevent HTTP verification links in production', () => { |
| 114 | + process.env.APP_URL = 'http://myapp.com' |
| 115 | + const verifyLink = getSecureTokenLink('/verify', 'verify-token') |
| 116 | + expect(verifyLink).toMatch(/^https:\/\//) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should prevent HTTP registration links in production', () => { |
| 120 | + process.env.APP_URL = 'http://myapp.com' |
| 121 | + const registerLink = getSecureTokenLink('/register', 'invite-token') |
| 122 | + expect(registerLink).toMatch(/^https:\/\//) |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments