github.com/argoproj/argo-cd/v3@v3.2.1/ui/src/app/applications/components/application-urls.test.ts (about) 1 import {ExternalLink, ExternalLinks, InvalidExternalLinkError} from './application-urls'; 2 3 test('rejects malicious URLs', () => { 4 expect(() => { 5 const _ = new ExternalLink('javascript:alert("hi")'); 6 }).toThrowError(InvalidExternalLinkError); 7 expect(() => { 8 const _ = new ExternalLink('data:text/html;<h1>hi</h1>'); 9 }).toThrowError(InvalidExternalLinkError); 10 expect(() => { 11 const _ = new ExternalLink('title|data:text/html;<h1>hi</h1>'); 12 }).toThrowError(InvalidExternalLinkError); 13 expect(() => { 14 const _ = new ExternalLink('data:title|data:text/html;<h1>hi</h1>'); 15 }).toThrowError(InvalidExternalLinkError); 16 17 expect(() => { 18 const _ = new ExternalLink('data:title|https://localhost:8080/applications'); 19 }).not.toThrowError(InvalidExternalLinkError); 20 }); 21 22 test('allows absolute URLs', () => { 23 expect(new ExternalLink('https://localhost:8080/applications').ref).toEqual('https://localhost:8080/applications'); 24 }); 25 26 test('allows relative URLs', () => { 27 // @ts-ignore 28 window.location = new URL('https://localhost:8080/applications'); 29 expect(new ExternalLink('/applications').ref).toEqual('/applications'); 30 }); 31 32 test('URLs format', () => { 33 expect(new ExternalLink('https://localhost:8080/applications')).toEqual({ 34 ref: 'https://localhost:8080/applications', 35 title: 'https://localhost:8080/applications', 36 }); 37 expect(new ExternalLink('title|https://localhost:8080/applications')).toEqual({ 38 ref: 'https://localhost:8080/applications', 39 title: 'title', 40 }); 41 }); 42 43 test('malicious URLs from list to be removed', () => { 44 const urls: string[] = ['javascript:alert("hi")', 'https://localhost:8080/applications']; 45 const links = ExternalLinks(urls); 46 47 expect(links).toHaveLength(1); 48 expect(links).toContainEqual({ 49 ref: 'https://localhost:8080/applications', 50 title: 'https://localhost:8080/applications', 51 }); 52 }); 53 54 test('list to be sorted', () => { 55 const urls: string[] = ['https://a', 'https://b', 'a|https://c', 'z|https://c', 'x|https://d', 'x|https://c']; 56 const links = ExternalLinks(urls); 57 58 // 'a|https://c', 59 // 'x|https://c', 60 // 'x|https://d', 61 // 'z|https://c', 62 // 'https://a', 63 // 'https://b', 64 expect(links).toHaveLength(6); 65 expect(links[0].title).toEqual('a'); 66 expect(links[1].title).toEqual('x'); 67 expect(links[1].ref).toEqual('https://c'); 68 expect(links[2].title).toEqual('x'); 69 expect(links[2].ref).toEqual('https://d'); 70 expect(links[3].title).toEqual('z'); 71 expect(links[4].title).toEqual('https://a'); 72 expect(links[5].title).toEqual('https://b'); 73 });