Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { createExecBackedGit } from '../src/git-exec'
3import type {
4 ExecResult,
5 ProcessOptions,
6 SandboxProcess,
7} from '../src/contracts'
8
9interface ExecCall {
10 command: string
11 options?: ProcessOptions
12}
13
14function recordingProcess(stdout = ''): {
15 process: SandboxProcess
16 calls: Array<ExecCall>
17} {
18 const calls: Array<ExecCall> = []
19 const process: SandboxProcess = {
20 exec: (command, options): Promise<ExecResult> => {
21 calls.push({ command, options })
22 return Promise.resolve({ stdout, stderr: '', exitCode: 0 })
23 },
24 spawn: () => Promise.reject(new Error('unused')),
25 }
26 return { process, calls }
27}
28
29describe('createExecBackedGit security', () => {
30 it('rejects a non-positive-integer depth before it reaches the shell', async () => {
31 const { process, calls } = recordingProcess()
32 const git = createExecBackedGit(process, '/workspace')
33 await expect(
34 // An untyped caller could smuggle shell metacharacters via `depth`.
35 git.clone({
36 url: 'https://github.com/me/app',
37 depth: '1; rm -rf /' as never,
38 }),
39 ).rejects.toThrow(/depth must be a positive integer/)
40 await expect(
41 git.clone({ url: 'https://github.com/me/app', depth: 0 }),
42 ).rejects.toThrow(/depth must be a positive integer/)
43 expect(calls).toHaveLength(0)
44 })
45
46 it('keeps the auth token out of argv (env-only credential helper)', async () => {
47 const { process, calls } = recordingProcess()
48 const git = createExecBackedGit(process, '/workspace')
49 await git.clone({
50 url: 'https://github.com/org/repo.git',
51 auth: { username: 'x-access-token', token: 'super-secret-token' },
52 })
53 const { command, options } = calls[0]!
54 // Token must NOT appear in the command line (would leak via ps/logs).
55 expect(command).not.toContain('super-secret-token')
56 // It must be supplied via the child env instead.
57 expect(options?.env?.GIT_ASKPASS_TOKEN).toBe('super-secret-token')
58 // The helper references the env var, not the literal token.
59 expect(command).toContain('credential.helper')
60 expect(command).toContain('${GIT_ASKPASS_TOKEN}')
61 })
62
63 it('inserts a -- end-of-options separator before positionals', async () => {
64 const { process, calls } = recordingProcess()
65 const git = createExecBackedGit(process, '/workspace')
66 await git.clone({ url: 'https://example.com/r.git', dir: '/workspace' })
67 expect(calls[0]!.command).toContain(' -- ')
68 await git.add(['a.ts', 'b.ts'])
69 expect(calls[1]!.command).toContain('add -- ')
70 })
71
72 it('rejects flag-smuggling values (leading dash)', async () => {
73 const { process } = recordingProcess()
74 const git = createExecBackedGit(process, '/workspace')
75 await expect(
76 git.clone({ url: '--upload-pack=touch /tmp/pwned' }),
77 ).rejects.toThrow(/argument-injection guard/)
78 await expect(git.add(['--output=/etc/x'])).rejects.toThrow(
79 /argument-injection guard/,
80 )
81 await expect(git.status('-C/evil')).rejects.toThrow(
82 /argument-injection guard/,
83 )
84 })
85
86 it('escapes embedded single quotes in values', async () => {
87 const { process, calls } = recordingProcess()
88 const git = createExecBackedGit(process, '/workspace')
89 await git.commit("it's done")
90 // The command runs without throwing and the message is single-quote escaped.
91 expect(calls[0]!.command).toContain(`commit -m 'it'\\''s done'`)
92 })
93})
94
95it('defaults to a shallow single-branch clone', async () => {
96 const { process, calls } = recordingProcess()
97 const git = createExecBackedGit(process, '/workspace')
98 await git.clone({ url: 'https://github.com/me/app' })
99 const first = calls[0]
100 expect(first).toBeDefined()
101 if (first !== undefined) {
102 expect(first.command).toContain('--depth 1')
103 expect(first.command).toContain('--single-branch')
104 }
105})
106
107it('omits depth for depth: "full" and uses N for a number', async () => {
108 const { process, calls } = recordingProcess()
109 const git = createExecBackedGit(process, '/workspace')
110 await git.clone({ url: 'https://github.com/me/app', depth: 'full' })
111 await git.clone({ url: 'https://github.com/me/app', depth: 50 })
112 const full = calls[0]
113 const numeric = calls[1]
114 expect(full).toBeDefined()
115 expect(numeric).toBeDefined()
116 if (full !== undefined) {
117 expect(full.command).not.toContain('--depth')
118 }
119 if (numeric !== undefined) {
120 expect(numeric.command).toContain('--depth 50')
121 }
122})
123
124// git clone fails when the parent of `dir` does not exist. gitSkill clones
125// into `<root>/.tanstack-skills/<name>`, so the parent must be created first.
126// Issue #1081 item 2.
127it('creates the clone parent directory before git clone', async () => {
128 const { process, calls } = recordingProcess()
129 const git = createExecBackedGit(process, '/workspace')
130 await git.clone({
131 url: 'https://github.com/owner/skills-pack',
132 dir: '/workspace/.tanstack-skills/skills-pack',
133 })
134 expect(calls[0]?.command).toBe("mkdir -p '/workspace/.tanstack-skills'")
135 expect(calls[1]?.command).toContain('git clone')
136 expect(calls[1]?.command).toContain(
137 "'/workspace/.tanstack-skills/skills-pack'",
138 )
139})