|
| 1 | +import{mkdtempSync,rmSync}from'node:fs' |
| 2 | +import{tmpdir}from'node:os' |
| 3 | +importprocessfrom'node:process' |
| 4 | + |
| 5 | +import{join}from'pathe' |
| 6 | +import{afterEach,beforeEach,describe,expect,it,vi}from'vitest' |
| 7 | + |
| 8 | +import{getCacheDir,resolveTool}from'../../../src/dev/binaries' |
| 9 | +import{resolveCloudflaredVersion}from'../../../src/dev/tunnel' |
| 10 | + |
| 11 | +letcacheHome: string |
| 12 | +constoriginalCacheHome=process.env.XDG_CACHE_HOME |
| 13 | + |
| 14 | +beforeEach(()=>{ |
| 15 | +cacheHome=mkdtempSync(join(tmpdir(),'nuxt-binaries-')) |
| 16 | +process.env.XDG_CACHE_HOME=cacheHome |
| 17 | +}) |
| 18 | + |
| 19 | +afterEach(()=>{ |
| 20 | +vi.restoreAllMocks() |
| 21 | +rmSync(cacheHome,{recursive: true,force: true}) |
| 22 | +if(originalCacheHome===undefined){ |
| 23 | +deleteprocess.env.XDG_CACHE_HOME |
| 24 | +} |
| 25 | +else{ |
| 26 | +process.env.XDG_CACHE_HOME=originalCacheHome |
| 27 | +} |
| 28 | +}) |
| 29 | + |
| 30 | +describe('resolveCloudflaredVersion',()=>{ |
| 31 | +it('should fall back to the pinned version when the override is unset',()=>{ |
| 32 | +expect(resolveCloudflaredVersion(undefined)).toMatch(/^\d/) |
| 33 | +expect(resolveCloudflaredVersion('')).toBe(resolveCloudflaredVersion(undefined)) |
| 34 | +}) |
| 35 | + |
| 36 | +it('should accept a plain version or dist tag',()=>{ |
| 37 | +expect(resolveCloudflaredVersion('2026.7.3')).toBe('2026.7.3') |
| 38 | +expect(resolveCloudflaredVersion('latest')).toBe('latest') |
| 39 | +}) |
| 40 | + |
| 41 | +it('should reject a version that could traverse the release url or the cache',()=>{ |
| 42 | +constpinned=resolveCloudflaredVersion(undefined) |
| 43 | +expect(resolveCloudflaredVersion('../../../../tmp/evil')).toBe(pinned) |
| 44 | +expect(resolveCloudflaredVersion('2026.7.3/../..')).toBe(pinned) |
| 45 | +expect(resolveCloudflaredVersion('a b')).toBe(pinned) |
| 46 | +}) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('resolveTool',()=>{ |
| 50 | +it('should refuse a cache name that escapes the cache directory',async()=>{ |
| 51 | +constfetchSpy=vi.spyOn(globalThis,'fetch') |
| 52 | + |
| 53 | +awaitexpect(resolveTool('nuxt-tool-that-does-not-exist',{ |
| 54 | +url: 'https://example.com/cloudflared', |
| 55 | +cacheName: '../../escaped', |
| 56 | +consent: {key: 'nuxt-tool-that-does-not-exist',notice: [],message: '',nonInteractiveWarning: ''}, |
| 57 | +})).resolves.toBeUndefined() |
| 58 | + |
| 59 | +expect(fetchSpy).not.toHaveBeenCalled() |
| 60 | +}) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('getCacheDir',()=>{ |
| 64 | +it('should create the directory under XDG_CACHE_HOME',()=>{ |
| 65 | +expect(getCacheDir('bin')).toBe(join(cacheHome,'nuxt','bin')) |
| 66 | +}) |
| 67 | +}) |
0 commit comments