|
| 1 | +import{mkdtemp,rm,writeFile}from'node:fs/promises' |
| 2 | +import{tmpdir}from'node:os' |
| 3 | +import{join}from'node:path' |
| 4 | +importprocessfrom'node:process' |
| 5 | + |
| 6 | +import{afterEach,beforeEach,describe,expect,it,vi}from'vitest' |
| 7 | + |
| 8 | +import{takeOverDevServer}from'../../../src/dev/takeover' |
| 9 | +import{findDevServer,findNitroDevWorker}from'../../../src/utils/dev-server' |
| 10 | +import{parseLockInfo,readLock}from'../../../src/utils/lockfile' |
| 11 | + |
| 12 | +lettempDir: string |
| 13 | + |
| 14 | +beforeEach(async()=>{ |
| 15 | +tempDir=awaitmkdtemp(join(tmpdir(),'nuxt-untrusted-lock-')) |
| 16 | +deleteprocess.env.NUXT_IGNORE_LOCK |
| 17 | +deleteprocess.env.NUXT_LOCK |
| 18 | +}) |
| 19 | + |
| 20 | +afterEach(async()=>{ |
| 21 | +vi.restoreAllMocks() |
| 22 | +awaitrm(tempDir,{recursive: true,force: true}) |
| 23 | +}) |
| 24 | + |
| 25 | +functionbaseLock(overrides: Record<string,unknown>={}){ |
| 26 | +return{ |
| 27 | +pid: process.pid, |
| 28 | +startedAt: Date.now(), |
| 29 | +command: 'dev', |
| 30 | +cwd: '/tmp/project', |
| 31 | +interactive: false, |
| 32 | + ...overrides, |
| 33 | +} |
| 34 | +} |
| 35 | + |
| 36 | +asyncfunctionwriteLock(contents: unknown): Promise<string>{ |
| 37 | +awaitwriteFile(join(tempDir,'nuxt.lock'),typeofcontents==='string' ? contents : JSON.stringify(contents)) |
| 38 | +returntempDir |
| 39 | +} |
| 40 | + |
| 41 | +describe('parseLockInfo',()=>{ |
| 42 | +it('should reject a lock with a non-positive pid',()=>{ |
| 43 | +expect(parseLockInfo(baseLock({pid: -1}))).toBeUndefined() |
| 44 | +expect(parseLockInfo(baseLock({pid: 0}))).toBeUndefined() |
| 45 | +expect(parseLockInfo(baseLock({pid: 1.5}))).toBeUndefined() |
| 46 | +expect(parseLockInfo(baseLock({pid: '123'}))).toBeUndefined() |
| 47 | +}) |
| 48 | + |
| 49 | +it('should reject a lock with an unknown command',()=>{ |
| 50 | +expect(parseLockInfo(baseLock({command: 'rm -rf /'}))).toBeUndefined() |
| 51 | +}) |
| 52 | + |
| 53 | +it('should reject anything that is not an object',()=>{ |
| 54 | +expect(parseLockInfo(null)).toBeUndefined() |
| 55 | +expect(parseLockInfo([baseLock()])).toBeUndefined() |
| 56 | +expect(parseLockInfo('dev')).toBeUndefined() |
| 57 | +}) |
| 58 | + |
| 59 | +it('should drop a negative parent pid rather than the whole lock',()=>{ |
| 60 | +expect(parseLockInfo(baseLock({parentPid: -1}))?.parentPid).toBeUndefined() |
| 61 | +expect(parseLockInfo(baseLock({takenOverBy: -1}))?.takenOverBy).toBeUndefined() |
| 62 | +}) |
| 63 | + |
| 64 | +it('should drop an out-of-range port',()=>{ |
| 65 | +expect(parseLockInfo(baseLock({port: 0}))?.port).toBeUndefined() |
| 66 | +expect(parseLockInfo(baseLock({port: 70_000}))?.port).toBeUndefined() |
| 67 | +expect(parseLockInfo(baseLock({port: 3000}))?.port).toBe(3000) |
| 68 | +}) |
| 69 | + |
| 70 | +it('should drop a url that is not http or https',()=>{ |
| 71 | +expect(parseLockInfo(baseLock({url: 'file:///etc/passwd'}))?.url).toBeUndefined() |
| 72 | +expect(parseLockInfo(baseLock({url: 'not a url'}))?.url).toBeUndefined() |
| 73 | +expect(parseLockInfo(baseLock({url: 'http://localhost:3000'}))?.url).toBe('http://localhost:3000') |
| 74 | +}) |
| 75 | + |
| 76 | +it('should strip control characters from displayed strings',()=>{ |
| 77 | +constinfo=parseLockInfo(baseLock({cwd: '/tmp/\u001B[2Jproject\u0007'})) |
| 78 | +expect(info?.cwd).toBe('/tmp/[2Jproject') |
| 79 | +}) |
| 80 | + |
| 81 | +it('should reject a lock timestamped far into the future',()=>{ |
| 82 | +expect(parseLockInfo(baseLock({startedAt: Number.MAX_VALUE}))).toBeUndefined() |
| 83 | +expect(parseLockInfo(baseLock({startedAt: Date.now()+60*60*1000}))).toBeUndefined() |
| 84 | +expect(parseLockInfo(baseLock({startedAt: Date.now()+1000}))).toBeDefined() |
| 85 | +}) |
| 86 | + |
| 87 | +it('should cap the length of strings it keeps',()=>{ |
| 88 | +expect(parseLockInfo(baseLock({cwd: 'a'.repeat(5000)}))?.cwd).toHaveLength(1024) |
| 89 | +}) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('reading an untrusted lock',()=>{ |
| 93 | +it('should ignore a lock file that is not valid json',async()=>{ |
| 94 | +expect(readLock(awaitwriteLock('}{'))).toBeUndefined() |
| 95 | +}) |
| 96 | + |
| 97 | +it('should ignore a lock claiming a negative pid',async()=>{ |
| 98 | +expect(readLock(awaitwriteLock(baseLock({pid: -1,port: 3000})))).toBeUndefined() |
| 99 | +}) |
| 100 | + |
| 101 | +it('should never signal a process group during takeover',async()=>{ |
| 102 | +constkill=vi.spyOn(process,'kill') |
| 103 | +constresult=awaittakeOverDevServer( |
| 104 | +awaitwriteLock(baseLock({pid: -1,port: 3000,url: 'http://localhost:3000'})), |
| 105 | +{takeover: true}, |
| 106 | +) |
| 107 | + |
| 108 | +expect(result.action).toBe('none') |
| 109 | +for(constcallofkill.mock.calls){ |
| 110 | +expect(call[0]).toBeGreaterThan(0) |
| 111 | +} |
| 112 | +}) |
| 113 | + |
| 114 | +it('should not resolve a dev server url pointing at another scheme',async()=>{ |
| 115 | +constdir=awaitwriteLock(baseLock({pid: process.ppid,url: 'file:///etc/passwd'})) |
| 116 | +awaitexpect(findDevServer(dir,dir)).resolves.toBeUndefined() |
| 117 | +}) |
| 118 | + |
| 119 | +it('should resolve a dev server recorded with an http url',async()=>{ |
| 120 | +constdir=awaitwriteLock(baseLock({pid: process.ppid,url: 'http://localhost:3000'})) |
| 121 | +awaitexpect(findDevServer(dir,dir)).resolves.toMatchObject({url: 'http://localhost:3000'}) |
| 122 | +}) |
| 123 | +}) |
| 124 | + |
| 125 | +describe('findNitroDevWorker',()=>{ |
| 126 | +it('should ignore a worker address on a remote host',async()=>{ |
| 127 | +awaitwriteFile(join(tempDir,'nitro.json'),JSON.stringify({ |
| 128 | +dev: {pid: process.pid,workerAddress: {host: 'evil.example.com',port: 80}}, |
| 129 | +})) |
| 130 | + |
| 131 | +awaitexpect(findNitroDevWorker(tempDir,tempDir)).resolves.toBeUndefined() |
| 132 | +}) |
| 133 | + |
| 134 | +it('should accept a loopback worker address',async()=>{ |
| 135 | +awaitwriteFile(join(tempDir,'nitro.json'),JSON.stringify({ |
| 136 | +dev: {pid: process.pid,workerAddress: {host: '127.0.0.1',port: 3000}}, |
| 137 | +})) |
| 138 | + |
| 139 | +awaitexpect(findNitroDevWorker(tempDir,tempDir)).resolves.toMatchObject({url: 'http://127.0.0.1:3000'}) |
| 140 | +}) |
| 141 | +}) |
0 commit comments