|
| 1 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import process from '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 | +let tempDir: string |
| 13 | + |
| 14 | +beforeEach(async () => { |
| 15 | + tempDir = await mkdtemp(join(tmpdir(), 'nuxt-untrusted-lock-')) |
| 16 | + delete process.env.NUXT_IGNORE_LOCK |
| 17 | + delete process.env.NUXT_LOCK |
| 18 | +}) |
| 19 | + |
| 20 | +afterEach(async () => { |
| 21 | + vi.restoreAllMocks() |
| 22 | + await rm(tempDir, { recursive: true, force: true }) |
| 23 | +}) |
| 24 | + |
| 25 | +function baseLock(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 | +async function writeLock(contents: unknown): Promise<string> { |
| 37 | + await writeFile(join(tempDir, 'nuxt.lock'), typeof contents === 'string' ? contents : JSON.stringify(contents)) |
| 38 | + return tempDir |
| 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 | + const info = 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(await writeLock('}{'))).toBeUndefined() |
| 95 | + }) |
| 96 | + |
| 97 | + it('should ignore a lock claiming a negative pid', async () => { |
| 98 | + expect(readLock(await writeLock(baseLock({ pid: -1, port: 3000 })))).toBeUndefined() |
| 99 | + }) |
| 100 | + |
| 101 | + it('should never signal a process group during takeover', async () => { |
| 102 | + const kill = vi.spyOn(process, 'kill') |
| 103 | + const result = await takeOverDevServer( |
| 104 | + await writeLock(baseLock({ pid: -1, port: 3000, url: 'http://localhost:3000' })), |
| 105 | + { takeover: true }, |
| 106 | + ) |
| 107 | + |
| 108 | + expect(result.action).toBe('none') |
| 109 | + for (const call of kill.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 | + const dir = await writeLock(baseLock({ pid: process.ppid, url: 'file:///etc/passwd' })) |
| 116 | + await expect(findDevServer(dir, dir)).resolves.toBeUndefined() |
| 117 | + }) |
| 118 | + |
| 119 | + it('should resolve a dev server recorded with an http url', async () => { |
| 120 | + const dir = await writeLock(baseLock({ pid: process.ppid, url: 'http://localhost:3000' })) |
| 121 | + await expect(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 | + await writeFile(join(tempDir, 'nitro.json'), JSON.stringify({ |
| 128 | + dev: { pid: process.pid, workerAddress: { host: 'evil.example.com', port: 80 } }, |
| 129 | + })) |
| 130 | + |
| 131 | + await expect(findNitroDevWorker(tempDir, tempDir)).resolves.toBeUndefined() |
| 132 | + }) |
| 133 | + |
| 134 | + it('should accept a loopback worker address', async () => { |
| 135 | + await writeFile(join(tempDir, 'nitro.json'), JSON.stringify({ |
| 136 | + dev: { pid: process.pid, workerAddress: { host: '127.0.0.1', port: 3000 } }, |
| 137 | + })) |
| 138 | + |
| 139 | + await expect(findNitroDevWorker(tempDir, tempDir)).resolves.toMatchObject({ url: 'http://127.0.0.1:3000' }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments