-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathvalidateUrl.test.ts
More file actions
111 lines (92 loc) · 4.2 KB
/
validateUrl.test.ts
File metadata and controls
111 lines (92 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { describe, it, expect } from 'vitest'
import { isDeceptiveUrl, getSafeUrl, getUrlDisplayText } from './validateUrl'
describe('isDeceptiveUrl', () => {
it('should return false for valid URLs', () => {
expect(isDeceptiveUrl('https://google.com')).toBe(false)
expect(isDeceptiveUrl('https://app.ens.domains')).toBe(false)
expect(isDeceptiveUrl('http://example.com')).toBe(false)
})
it('should return true for URLs with @ symbol (userinfo)', () => {
expect(isDeceptiveUrl('https://google.com@evil.com')).toBe(true)
expect(isDeceptiveUrl('https://user@evil.com')).toBe(true)
expect(isDeceptiveUrl('https://user:pass@evil.com')).toBe(true)
})
it('should return true for URLs with Unicode spaces', () => {
// EM SPACE
expect(isDeceptiveUrl('https://google.com\u2003@evil.com')).toBe(true)
// ZERO WIDTH SPACE
expect(isDeceptiveUrl('https://google.com\u200b@evil.com')).toBe(true)
// NO-BREAK SPACE
expect(isDeceptiveUrl('https://google.com\u00a0@evil.com')).toBe(true)
})
it('should return true for javascript: and data: URIs', () => {
expect(isDeceptiveUrl('javascript:alert(1)')).toBe(true)
expect(isDeceptiveUrl('Javascript:alert(1)')).toBe(true)
expect(isDeceptiveUrl('data:text/html,<script>alert(1)</script>')).toBe(true)
expect(isDeceptiveUrl('DATA:text/html,<h1>test</h1>')).toBe(true)
})
it('should return true for file:// protocol', () => {
expect(isDeceptiveUrl('file:///etc/passwd')).toBe(true)
expect(isDeceptiveUrl('FILE:///c:/windows/system.ini')).toBe(true)
})
it('should return true for URLs with null bytes', () => {
expect(isDeceptiveUrl('https://example.com\0/evil')).toBe(true)
})
it('should return true for URLs with multiple slashes after protocol', () => {
expect(isDeceptiveUrl('https:///evil.com')).toBe(true)
expect(isDeceptiveUrl('http:////evil.com')).toBe(true)
})
it('should return true for punycode domains', () => {
expect(isDeceptiveUrl('https://xn--e1afmkfd.xn--p1ai')).toBe(true)
expect(isDeceptiveUrl('https://xn--80ak6aa92e.com')).toBe(true)
})
it('should return true for mixed Cyrillic and Latin characters', () => {
// аррӏе.com (Cyrillic 'a' and Latin)
expect(isDeceptiveUrl('https://аррӏе.com')).toBe(true)
})
it('should return true for invalid URLs', () => {
expect(isDeceptiveUrl('not-a-url')).toBe(true)
expect(isDeceptiveUrl('ftp://example.com')).toBe(true)
})
it('should handle undefined and empty strings', () => {
expect(isDeceptiveUrl(undefined)).toBe(false)
expect(isDeceptiveUrl('')).toBe(false)
})
})
describe('getSafeUrl', () => {
it('should return the URL if it is safe', () => {
expect(getSafeUrl('https://google.com')).toBe('https://google.com')
expect(getSafeUrl('http://example.com')).toBe('http://example.com')
})
it('should return undefined for deceptive URLs', () => {
expect(getSafeUrl('https://google.com@evil.com')).toBeUndefined()
expect(getSafeUrl('javascript:alert(1)')).toBeUndefined()
expect(getSafeUrl('file:///etc/passwd')).toBeUndefined()
})
it('should return undefined for URLs without protocol', () => {
expect(getSafeUrl('google.com')).toBeUndefined()
expect(getSafeUrl('www.example.com')).toBeUndefined()
})
it('should handle undefined', () => {
expect(getSafeUrl(undefined)).toBeUndefined()
})
})
describe('getUrlDisplayText', () => {
it('should return hostname and path without protocol', () => {
expect(getUrlDisplayText('https://google.com')).toBe('google.com')
expect(getUrlDisplayText('https://example.com/path')).toBe('example.com/path')
expect(getUrlDisplayText('http://test.com/foo/bar')).toBe('test.com/foo/bar')
})
it('should remove trailing slashes', () => {
expect(getUrlDisplayText('https://google.com/')).toBe('google.com')
expect(getUrlDisplayText('https://example.com/path/')).toBe('example.com/path')
})
it('should handle invalid URLs gracefully', () => {
expect(getUrlDisplayText('not-a-url')).toBe('not-a-url')
expect(getUrlDisplayText('google.com')).toBe('google.com')
})
it('should handle undefined and empty strings', () => {
expect(getUrlDisplayText(undefined)).toBe('')
expect(getUrlDisplayText('')).toBe('')
})
})