|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { |
| 5 | + buildPathDeclaration, |
| 6 | + isSafeDoubleQuotedShellValue, |
| 7 | + toSafeDoubleQuotedShellValue, |
| 8 | + UnsafeShellValueError |
| 9 | +} from './shell-escape.js'; |
| 10 | + |
| 11 | +const INJECTIONS = [ |
| 12 | + '$(touch /tmp/pwned)', |
| 13 | + '`touch /tmp/pwned`', |
| 14 | + 'a"; touch /tmp/pwned; echo "', |
| 15 | + 'a\ntouch /tmp/pwned', |
| 16 | + 'a\rtouch /tmp/pwned', |
| 17 | + 'a\\"; touch /tmp/pwned; #', |
| 18 | + '$((1+1))', |
| 19 | + '${(e)EVIL}', |
| 20 | + '${EVIL@P}', |
| 21 | + '${EVIL:-$(touch /tmp/pwned)}', |
| 22 | + '$', |
| 23 | + '$1', |
| 24 | + '${}', |
| 25 | +]; |
| 26 | + |
| 27 | +describe('shell-escape', () => { |
| 28 | + it('allows plain values and simple variable references', () => { |
| 29 | + for (const value of [ |
| 30 | + '/usr/local/bin', |
| 31 | + '/Applications/My App/bin', |
| 32 | + '$HOME/.local/bin', |
| 33 | + '${PNPM_HOME}/bin', |
| 34 | + '$HOME/a:$OTHER_VAR/b', |
| 35 | + "it's;fine|really&(yes)", |
| 36 | + '', |
| 37 | + ]) { |
| 38 | + expect(isSafeDoubleQuotedShellValue(value), value).toBe(true); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects command substitution, quotes, backslashes, new lines and complex expansions', () => { |
| 43 | + for (const value of INJECTIONS) { |
| 44 | + expect(isSafeDoubleQuotedShellValue(value), value).toBe(false); |
| 45 | + expect(() => toSafeDoubleQuotedShellValue(value), value).toThrow(UnsafeShellValueError); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it('converts a leading tilde to $HOME', () => { |
| 50 | + expect(toSafeDoubleQuotedShellValue('~')).toBe('$HOME'); |
| 51 | + expect(toSafeDoubleQuotedShellValue('~/bin')).toBe('$HOME/bin'); |
| 52 | + expect(toSafeDoubleQuotedShellValue('/a/~b')).toBe('/a/~b'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('builds quoted PATH declarations (prepend behaviour unchanged)', () => { |
| 56 | + expect(buildPathDeclaration('/opt/bin', false)).toBe('export PATH="/opt/bin:$PATH";'); |
| 57 | + expect(buildPathDeclaration('/opt/bin', true)).toBe('export PATH="$PATH:/opt/bin";'); |
| 58 | + expect(buildPathDeclaration('~/.local/bin', false)).toBe('export PATH="$HOME/.local/bin:$PATH";'); |
| 59 | + expect(() => buildPathDeclaration('/opt/bin; touch /tmp/pwned', false)).not.toThrow(); |
| 60 | + expect(() => buildPathDeclaration('$(touch /tmp/pwned)', false)).toThrow(UnsafeShellValueError); |
| 61 | + }); |
| 62 | + |
| 63 | + it('produces declarations that the shell evaluates without running anything', () => { |
| 64 | + // A value that is allowed but would be dangerous unquoted |
| 65 | + const declaration = buildPathDeclaration('/opt/my dir;echo INJECTED', false); |
| 66 | + const output = execFileSync('sh', ['-c', `PATH=/usr/bin:/bin; ${declaration} printf '%s' "$PATH"`], { encoding: 'utf8' }); |
| 67 | + expect(output).toBe('/opt/my dir;echo INJECTED:/usr/bin:/bin'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments