|
| 1 | +import { existsSync, mkdtempSync, readFileSync, rmSync } from 'fs' |
| 2 | +import os from 'os' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
| 6 | + |
| 7 | +import { openFileAtPath } from '../open-file' |
| 8 | + |
| 9 | +import type { CliEnv } from '../../types/env' |
| 10 | + |
| 11 | +// The editor command runs through `shell: true`; these use POSIX `sh`. |
| 12 | +describe.skipIf(process.platform === 'win32')('openFileAtPath placeholders', () => { |
| 13 | + let dir: string |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + dir = mkdtempSync(path.join(os.tmpdir(), 'open-file-')) |
| 17 | + }) |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + rmSync(dir, { recursive: true, force: true }) |
| 21 | + }) |
| 22 | + |
| 23 | + // A repo-controlled agent file name: shell metacharacters, a quote, and |
| 24 | + // `$&` / `$'` (special in String.replace replacement strings). |
| 25 | + const hostile = () => |
| 26 | + path.join(dir, `a b';touch ${path.join(dir, 'pwned')};echo '$&$'c`) |
| 27 | + |
| 28 | + const run = async (editor: string, filePath: string) => { |
| 29 | + const out = path.join(dir, 'out') |
| 30 | + const env = { |
| 31 | + CODEBUFF_EDITOR: editor.replace('OUT', out), |
| 32 | + } as unknown as CliEnv |
| 33 | + const ok = await openFileAtPath(filePath, env) |
| 34 | + return { ok, out } |
| 35 | + } |
| 36 | + |
| 37 | + // Quoted forms too: a user's `code -g "%f"` must not hand the editor the |
| 38 | + // already-quoted path wrapped in a second pair of quotes. |
| 39 | + test.each(['%f', '{file}', '"%f"', "'%f'", '"{file}"'])( |
| 40 | + '%s passes the path as one literal argument and runs nothing else', |
| 41 | + async (placeholder) => { |
| 42 | + const filePath = hostile() |
| 43 | + const { ok, out } = await run( |
| 44 | + `sh -c 'printf %s "$1" > OUT' _ ${placeholder}`, |
| 45 | + filePath, |
| 46 | + ) |
| 47 | + |
| 48 | + expect(ok).toBe(true) |
| 49 | + expect(readFileSync(out, 'utf8')).toBe(filePath) |
| 50 | + expect(existsSync(path.join(dir, 'pwned'))).toBe(false) |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + test('a command separator in the file name does not run a second command', async () => { |
| 55 | + const filePath = path.join(dir, `a;touch ${path.join(dir, 'pwned')}`) |
| 56 | + const { ok } = await run(`true %f`, filePath) |
| 57 | + |
| 58 | + expect(ok).toBe(true) |
| 59 | + expect(existsSync(path.join(dir, 'pwned'))).toBe(false) |
| 60 | + }) |
| 61 | + |
| 62 | + test('an editor without a placeholder still gets the quoted path appended', async () => { |
| 63 | + const filePath = hostile() |
| 64 | + const { ok, out } = await run( |
| 65 | + `sh -c 'printf %s "$1" > OUT' _`, |
| 66 | + filePath, |
| 67 | + ) |
| 68 | + |
| 69 | + expect(ok).toBe(true) |
| 70 | + expect(readFileSync(out, 'utf8')).toBe(filePath) |
| 71 | + expect(existsSync(path.join(dir, 'pwned'))).toBe(false) |
| 72 | + }) |
| 73 | +}) |
0 commit comments