| 
 | 1 | +import { describe, expect, test } from 'vitest'  | 
 | 2 | +import {  | 
 | 3 | +  getAdditionalAllowedHosts,  | 
 | 4 | +  isHostAllowedWithoutCache,  | 
 | 5 | +} from '../hostCheck'  | 
 | 6 | + | 
 | 7 | +test('getAdditionalAllowedHosts', async () => {  | 
 | 8 | +  const actual = getAdditionalAllowedHosts(  | 
 | 9 | +    {  | 
 | 10 | +      host: 'vite.host.example.com',  | 
 | 11 | +      hmr: {  | 
 | 12 | +        host: 'vite.hmr-host.example.com',  | 
 | 13 | +      },  | 
 | 14 | +      origin: 'http://vite.origin.example.com:5173',  | 
 | 15 | +    },  | 
 | 16 | +    {  | 
 | 17 | +      host: 'vite.preview-host.example.com',  | 
 | 18 | +    },  | 
 | 19 | +  ).sort()  | 
 | 20 | +  expect(actual).toStrictEqual(  | 
 | 21 | +    [  | 
 | 22 | +      'vite.host.example.com',  | 
 | 23 | +      'vite.hmr-host.example.com',  | 
 | 24 | +      'vite.origin.example.com',  | 
 | 25 | +      'vite.preview-host.example.com',  | 
 | 26 | +    ].sort(),  | 
 | 27 | +  )  | 
 | 28 | +})  | 
 | 29 | + | 
 | 30 | +describe('isHostAllowedWithoutCache', () => {  | 
 | 31 | +  const allowCases = {  | 
 | 32 | +    'IP address': [  | 
 | 33 | +      '192.168.0.0',  | 
 | 34 | +      '[::1]',  | 
 | 35 | +      '127.0.0.1:5173',  | 
 | 36 | +      '[2001:db8:0:0:1:0:0:1]:5173',  | 
 | 37 | +    ],  | 
 | 38 | +    localhost: [  | 
 | 39 | +      'localhost',  | 
 | 40 | +      'localhost:5173',  | 
 | 41 | +      'foo.localhost',  | 
 | 42 | +      'foo.bar.localhost',  | 
 | 43 | +    ],  | 
 | 44 | +    specialProtocols: [  | 
 | 45 | +      // for electron browser window (https://github.com/webpack/webpack-dev-server/issues/3821)  | 
 | 46 | +      'file:///path/to/file.html',  | 
 | 47 | +      // for browser extensions (https://github.com/webpack/webpack-dev-server/issues/3807)  | 
 | 48 | +      'chrome-extension://foo',  | 
 | 49 | +    ],  | 
 | 50 | +  }  | 
 | 51 | + | 
 | 52 | +  const disallowCases = {  | 
 | 53 | +    'IP address': ['255.255.255.256', '[:', '[::z]'],  | 
 | 54 | +    localhost: ['localhos', 'localhost.foo'],  | 
 | 55 | +    specialProtocols:  ['mailto:[email protected]'],  | 
 | 56 | +    others: [''],  | 
 | 57 | +  }  | 
 | 58 | + | 
 | 59 | +  for (const [name, inputList] of Object.entries(allowCases)) {  | 
 | 60 | +    test.each(inputList)(`allows ${name} (%s)`, (input) => {  | 
 | 61 | +      const actual = isHostAllowedWithoutCache([], [], input)  | 
 | 62 | +      expect(actual).toBe(true)  | 
 | 63 | +    })  | 
 | 64 | +  }  | 
 | 65 | + | 
 | 66 | +  for (const [name, inputList] of Object.entries(disallowCases)) {  | 
 | 67 | +    test.each(inputList)(`disallows ${name} (%s)`, (input) => {  | 
 | 68 | +      const actual = isHostAllowedWithoutCache([], [], input)  | 
 | 69 | +      expect(actual).toBe(false)  | 
 | 70 | +    })  | 
 | 71 | +  }  | 
 | 72 | + | 
 | 73 | +  test('allows additionalAlloweHosts option', () => {  | 
 | 74 | +    const additionalAllowedHosts = ['vite.example.com']  | 
 | 75 | +    const actual = isHostAllowedWithoutCache(  | 
 | 76 | +      [],  | 
 | 77 | +      additionalAllowedHosts,  | 
 | 78 | +      'vite.example.com',  | 
 | 79 | +    )  | 
 | 80 | +    expect(actual).toBe(true)  | 
 | 81 | +  })  | 
 | 82 | + | 
 | 83 | +  test('allows single allowedHosts', () => {  | 
 | 84 | +    const cases = {  | 
 | 85 | +      allowed: ['example.com'],  | 
 | 86 | +      disallowed: ['vite.dev'],  | 
 | 87 | +    }  | 
 | 88 | +    for (const c of cases.allowed) {  | 
 | 89 | +      const actual = isHostAllowedWithoutCache(['example.com'], [], c)  | 
 | 90 | +      expect(actual, c).toBe(true)  | 
 | 91 | +    }  | 
 | 92 | +    for (const c of cases.disallowed) {  | 
 | 93 | +      const actual = isHostAllowedWithoutCache(['example.com'], [], c)  | 
 | 94 | +      expect(actual, c).toBe(false)  | 
 | 95 | +    }  | 
 | 96 | +  })  | 
 | 97 | + | 
 | 98 | +  test('allows all subdomain allowedHosts', () => {  | 
 | 99 | +    const cases = {  | 
 | 100 | +      allowed: ['example.com', 'foo.example.com', 'foo.bar.example.com'],  | 
 | 101 | +      disallowed: ['vite.dev'],  | 
 | 102 | +    }  | 
 | 103 | +    for (const c of cases.allowed) {  | 
 | 104 | +      const actual = isHostAllowedWithoutCache(['.example.com'], [], c)  | 
 | 105 | +      expect(actual, c).toBe(true)  | 
 | 106 | +    }  | 
 | 107 | +    for (const c of cases.disallowed) {  | 
 | 108 | +      const actual = isHostAllowedWithoutCache(['.example.com'], [], c)  | 
 | 109 | +      expect(actual, c).toBe(false)  | 
 | 110 | +    }  | 
 | 111 | +  })  | 
 | 112 | +})  | 
0 commit comments