github.com/argoproj/argo-cd/v2@v2.10.9/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  
    33  test('URLs format', () => {
    34      expect(new ExternalLink('https://localhost:8080/applications')).toEqual({
    35          ref: 'https://localhost:8080/applications',
    36          title: 'https://localhost:8080/applications',
    37      })
    38      expect(new ExternalLink('title|https://localhost:8080/applications')).toEqual({
    39          ref: 'https://localhost:8080/applications',
    40          title: 'title',
    41      })
    42  });
    43  
    44  
    45  test('malicious URLs from list to be removed', () => {
    46      const urls: string[] = [
    47          'javascript:alert("hi")',
    48          'https://localhost:8080/applications',
    49      ]
    50      const links = ExternalLinks(urls);
    51  
    52      expect(links).toHaveLength(1);
    53      expect(links).toContainEqual({
    54          ref: 'https://localhost:8080/applications',
    55          title: 'https://localhost:8080/applications',
    56      });
    57  });
    58  
    59  
    60  test('list to be sorted', () => {
    61      const urls: string[] = [
    62          'https://a',
    63          'https://b',
    64          'a|https://c',
    65          'z|https://c',
    66          'x|https://d',
    67          'x|https://c',
    68      ]
    69      const links = ExternalLinks(urls);
    70  
    71      // 'a|https://c',
    72      // 'x|https://c',
    73      // 'x|https://d',
    74      // 'z|https://c',
    75      // 'https://a',
    76      // 'https://b',
    77      expect(links).toHaveLength(6);
    78      expect(links[0].title).toEqual('a')
    79      expect(links[1].title).toEqual('x')
    80      expect(links[1].ref).toEqual('https://c')
    81      expect(links[2].title).toEqual('x')
    82      expect(links[2].ref).toEqual('https://d')
    83      expect(links[3].title).toEqual('z')
    84      expect(links[4].title).toEqual('https://a')
    85      expect(links[5].title).toEqual('https://b')
    86  });