Skip to content

Commit cbe965f

Browse files
Validate git branch names and use execFileSync with argument arrays f… (#488)
Validate git branch names and use execFileSync with argument arrays for branch-based git commands to prevent shell injection
1 parent 4d4c5e5 commit cbe965f

5 files changed

Lines changed: 110 additions & 26 deletions

File tree

‎apps/editor/src/commands/copy-merge-commit-details-command.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
2-
import { execSync } from 'child_process'
2+
import { execSync, execFileSync } from 'child_process'
33
import { get_git_repository } from '@/utils/git-repository-utils'
4+
import { is_valid_git_branch_name } from '@/utils/is-valid-git-branch-name'
45
import { Logger } from '@shared/utils/logger'
56
import { t } from '@/i18n'
67
import { AsciiTree } from '@/utils/ascii-tree'
@@ -45,7 +46,7 @@ export const copy_merge_commit_details_command = (): vscode.Disposable => {
4546
name
4647
}
4748
})
48-
.filter((b) => b.name.length > 0)
49+
.filter((b) => is_valid_git_branch_name(b.name))
4950

5051
if (branches.length === 0) {
5152
vscode.window.showInformationMessage(
@@ -103,8 +104,14 @@ export const copy_merge_commit_details_command = (): vscode.Disposable => {
103104

104105
let log_output = ''
105106
try {
106-
log_output = execSync(
107-
`git log ${selected_branch.name}..HEAD --pretty=format:"%s%n%b%n%x00"`,
107+
log_output = execFileSync(
108+
'git',
109+
[
110+
'log',
111+
`${selected_branch.name}..HEAD`,
112+
'--pretty=format:%s%n%b%n%x00',
113+
'--'
114+
],
108115
{
109116
cwd: repository.rootUri.fsPath,
110117
encoding: 'utf-8'

‎apps/editor/src/commands/select-files-of-commit-command.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execSync } from 'child_process'
1+
import { execSync, execFileSync } from 'child_process'
22
import * as fs from 'fs'
33
import * as path from 'path'
44
import * as vscode from 'vscode'
@@ -13,6 +13,7 @@ import { AsciiTree } from '@/utils/ascii-tree'
1313
import { WebSocketManager } from '@/services/websocket-manager'
1414
import { show_parent_folder_quick_pick } from '@/utils/show-parent-folder-quick-pick'
1515
import { get_error_message } from '@/utils/get-error-message'
16+
import { is_valid_git_branch_name } from '@/utils/is-valid-git-branch-name'
1617

1718
export const select_files_of_commit_command = (
1819
workspace_provider: WorkspaceProvider,
@@ -169,7 +170,7 @@ export const select_files_of_commit_command = (
169170
name
170171
}
171172
})
172-
.filter((b) => b.name.length > 0)
173+
.filter((b) => is_valid_git_branch_name(b.name))
173174

174175
if (branches.length === 0) {
175176
vscode.window.showInformationMessage(
@@ -232,8 +233,15 @@ export const select_files_of_commit_command = (
232233
}
233234

234235
try {
235-
files_output = execSync(
236-
`git log ${selected_branch.name}..HEAD --name-only --pretty="format:%B"`,
236+
files_output = execFileSync(
237+
'git',
238+
[
239+
'log',
240+
`${selected_branch.name}..HEAD`,
241+
'--name-only',
242+
'--pretty=format:%B',
243+
'--'
244+
],
237245
{
238246
cwd: repository.rootUri.fsPath,
239247
encoding: 'utf-8'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const is_valid_git_branch_name = (name: string): boolean => {
2+
if (!name || name == '@' || name.startsWith('-')) {
3+
return false
4+
}
5+
6+
const has_control_char = [...name].some((char) => {
7+
const code = char.charCodeAt(0)
8+
return code <= 0x20 || code == 0x7f
9+
})
10+
if (has_control_char || /[~^:?*[\\]/.test(name)) {
11+
return false
12+
}
13+
14+
if (name.includes('..') || name.includes('@{') || name.includes('//')) {
15+
return false
16+
}
17+
18+
if (name.startsWith('/') || name.endsWith('/') || name.endsWith('.')) {
19+
return false
20+
}
21+
22+
return name
23+
.split('/')
24+
.every(
25+
(component) => !component.startsWith('.') && !component.endsWith('.lock')
26+
)
27+
}

‎apps/editor/src/views/prompt/backend/utils/symbols/git/replace-git-symbols.ts‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
2-
import { execSync } from 'child_process'
2+
import { execSync, execFileSync } from 'child_process'
33
import { get_git_repository } from '@/utils/git-repository-utils'
4+
import { is_valid_git_branch_name } from '@/utils/is-valid-git-branch-name'
45
import { AsciiTree } from '@/utils/ascii-tree/ascii-tree'
56
import { Logger } from '@shared/utils/logger'
67
import { t } from '@/i18n'
@@ -156,6 +157,23 @@ export const replace_changes_symbol = async (params: {
156157
if (multi_root_match) {
157158
const [, folder_name, branch_name] = multi_root_match
158159

160+
if (!is_valid_git_branch_name(branch_name)) {
161+
vscode.window.showErrorMessage(
162+
t('common.error.failed-to-get-changes-from-branch-in-folder', {
163+
branch_name,
164+
folder_name
165+
})
166+
)
167+
if (params.symbols_cache) {
168+
params.symbols_cache.set(full_match, '', '')
169+
}
170+
result_instructions[i] = result_instructions[i].replace(
171+
replacement_regex,
172+
''
173+
)
174+
continue
175+
}
176+
159177
const workspace_folders = vscode.workspace.workspaceFolders
160178
if (!workspace_folders) {
161179
vscode.window.showErrorMessage(
@@ -221,16 +239,19 @@ export const replace_changes_symbol = async (params: {
221239

222240
let diff_base: string
223241
if (current_branch == branch_name) {
224-
diff_base = execSync(`git merge-base HEAD ${branch_name}`, {
225-
cwd: target_folder.uri.fsPath
226-
})
242+
diff_base = execFileSync(
243+
'git',
244+
['merge-base', 'HEAD', branch_name],
245+
{
246+
cwd: target_folder.uri.fsPath
247+
}
248+
)
227249
.toString()
228250
.trim()
229251
} else {
230252
diff_base = branch_name
231253
}
232-
const diff_command = `git diff ${diff_base}`
233-
const diff = execSync(diff_command, {
254+
const diff = execFileSync('git', ['diff', diff_base, '--'], {
234255
cwd: target_folder.uri.fsPath
235256
}).toString()
236257

@@ -296,6 +317,21 @@ export const replace_changes_symbol = async (params: {
296317
}
297318
} else {
298319
const branch_name = branch_spec
320+
321+
if (!is_valid_git_branch_name(branch_name)) {
322+
vscode.window.showErrorMessage(
323+
t('common.error.failed-to-get-changes-from-branch', { branch_name })
324+
)
325+
if (params.symbols_cache) {
326+
params.symbols_cache.set(full_match, '', '')
327+
}
328+
result_instructions[i] = result_instructions[i].replace(
329+
replacement_regex,
330+
''
331+
)
332+
continue
333+
}
334+
299335
const repository = await get_git_repository()
300336
if (!repository) {
301337
vscode.window.showErrorMessage(
@@ -344,16 +380,19 @@ export const replace_changes_symbol = async (params: {
344380

345381
let diff_base: string
346382
if (current_branch == branch_name) {
347-
diff_base = execSync(`git merge-base HEAD ${branch_name}`, {
348-
cwd: repository.rootUri.fsPath
349-
})
383+
diff_base = execFileSync(
384+
'git',
385+
['merge-base', 'HEAD', branch_name],
386+
{
387+
cwd: repository.rootUri.fsPath
388+
}
389+
)
350390
.toString()
351391
.trim()
352392
} else {
353393
diff_base = branch_name
354394
}
355-
const diff_command = `git diff ${diff_base}`
356-
const diff = execSync(diff_command, {
395+
const diff = execFileSync('git', ['diff', diff_base, '--'], {
357396
cwd: repository.rootUri.fsPath
358397
}).toString()
359398

‎apps/editor/src/views/shared/utils/hash-sign-quick-pick/symbols/changes-symbol.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as vscode from 'vscode'
2-
import { execSync, exec } from 'child_process'
2+
import { execSync, execFile } from 'child_process'
33
import { promisify } from 'util'
44
import { t } from '@/i18n'
55
import { LAST_SELECTED_REPOSITORY_IN_SYMBOLS_QUCK_PICK_STATE_KEY } from '@/constants/state-keys'
66
import { display_token_count } from '@shared/utils/display-token-count'
77
import { build_changes_markdown } from '@/views/prompt/backend/utils/symbols/git/replace-git-symbols'
8+
import { is_valid_git_branch_name } from '@/utils/is-valid-git-branch-name'
89

9-
const execAsync = promisify(exec)
10+
const execFileAsync = promisify(execFile)
1011

1112
export const handle_changes_item = async (
1213
extension_context: vscode.ExtensionContext
@@ -43,7 +44,7 @@ export const handle_changes_item = async (
4344
const branches = branches_output
4445
.split('\n')
4546
.map((b) => b.replace(/^\*/, '').trim())
46-
.filter((b) => b.length > 0)
47+
.filter((b) => is_valid_git_branch_name(b))
4748

4849
if (branches.length > 0) {
4950
workspace_with_branches.push({ folder, branches })
@@ -176,17 +177,19 @@ export const handle_changes_item = async (
176177
try {
177178
let diff_base: string
178179
if (current_branch === branch_name) {
179-
const { stdout } = await execAsync(
180-
`git merge-base HEAD ${branch_name}`,
180+
const { stdout } = await execFileAsync(
181+
'git',
182+
['merge-base', 'HEAD', branch_name],
181183
{ cwd }
182184
)
183185
diff_base = stdout.trim()
184186
} else {
185187
diff_base = branch_name
186188
}
187189

188-
const { stdout: diff } = await execAsync(
189-
`git diff ${diff_base}`,
190+
const { stdout: diff } = await execFileAsync(
191+
'git',
192+
['diff', diff_base, '--'],
190193
{
191194
cwd,
192195
maxBuffer: 1024 * 1024 * 100

0 commit comments

Comments
 (0)