Skip to content

Commit d27add4

Browse files
committed
Validate target file paths before applying responses and checking applicability
- when applying response; the response should be considered invalid if it tries to edit/rename/delete non-existing files - create a shared util for this [Changes] in `apps/editor/src/features/apply-response/utils`
1 parent b83175c commit d27add4

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎apps/editor/src/features/apply-response/apply-response.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { get_all_workspace_files } from '@/context/helpers/get-all-workspace-files'
3030
import { parse_response, PatchRepairItem } from './utils/response-parser'
3131
import { Checkpoint } from '@/features/checkpoints/types'
32+
import { get_invalid_file_path } from './utils/file-checks'
3233
import { WebSocketManager } from '@/services/websocket-manager'
3334
import { handle_patch_repair } from './response-processor/handlers/patch-repair-handler'
3435
import {
@@ -105,7 +106,10 @@ export const setup_apply_response = (params: {
105106
options: { skip_confirmation: true, hide_notification: true }
106107
})
107108
} catch (err) {
108-
console.error('Failed to restore interrupted code review checkpoint', err)
109+
console.error(
110+
'Failed to restore interrupted code review checkpoint',
111+
err
112+
)
109113
} finally {
110114
await delete_checkpoint({
111115
extension_context: params.extension_context,
@@ -231,6 +235,18 @@ export const setup_apply_response = (params: {
231235
return true
232236
})
233237

238+
const invalid_file_path = get_invalid_file_path(response_items)
239+
240+
if (invalid_file_path) {
241+
vscode.window.showErrorMessage(
242+
t('command.apply-response-command.error.file-not-found', {
243+
path: invalid_file_path
244+
})
245+
)
246+
params.prompt_view_provider.send_message({ command: 'HIDE_PROGRESS' })
247+
return false
248+
}
249+
234250
const commit_message_item = response_items.find(
235251
(i) => i.type === 'commit-message'
236252
) as any

‎apps/editor/src/features/apply-response/utils/file-checks.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import * as fs from 'fs'
12
import { FileItem } from './response-parser'
3+
import {
4+
get_workspace_map_and_default,
5+
resolve_workspace_root
6+
} from './workspace'
7+
import { create_safe_path } from '@/utils/path-sanitizer'
28

39
export const check_for_conflict_markers = (files: FileItem[]): boolean => {
410
for (const file of files) {
@@ -13,3 +19,63 @@ export const check_for_conflict_markers = (files: FileItem[]): boolean => {
1319
}
1420
return false
1521
}
22+
23+
export const get_invalid_file_path = (response_items: any[]): string | null => {
24+
const { workspace_map, default_workspace } = get_workspace_map_and_default()
25+
26+
for (const item of response_items) {
27+
if (item.type === 'file') {
28+
const source_path = item.renamed_from ?? item.file_path
29+
const source_workspace = item.renamed_from
30+
? item.renamed_from_workspace
31+
: item.workspace_name
32+
const root = resolve_workspace_root({
33+
workspace_name: source_workspace,
34+
workspace_map,
35+
default_workspace
36+
})
37+
const safe_path = create_safe_path(root, source_path)
38+
39+
if (item.is_deleted || item.renamed_from) {
40+
if (!safe_path || !fs.existsSync(safe_path)) {
41+
return source_path
42+
}
43+
} else {
44+
const is_search_replace =
45+
item.content.includes('<<<<<<<') &&
46+
item.content.includes('=======') &&
47+
item.content.includes('>>>>>>>')
48+
if (is_search_replace) {
49+
if (!safe_path || !fs.existsSync(safe_path)) {
50+
return source_path
51+
}
52+
}
53+
}
54+
} else if (item.type === 'diff') {
55+
const is_new_file = item.content.includes('--- /dev/null')
56+
if (!is_new_file) {
57+
const root = resolve_workspace_root({
58+
workspace_name: item.workspace_name,
59+
workspace_map,
60+
default_workspace
61+
})
62+
const safe_path = create_safe_path(root, item.file_path)
63+
if (!safe_path || !fs.existsSync(safe_path)) {
64+
return item.file_path
65+
}
66+
}
67+
} else if (item.type === 'patch-repair' || item.type === 'code-at-cursor') {
68+
const root = resolve_workspace_root({
69+
workspace_name: item.workspace_name,
70+
workspace_map,
71+
default_workspace
72+
})
73+
const safe_path = create_safe_path(root, item.file_path)
74+
if (!safe_path || !fs.existsSync(safe_path)) {
75+
return item.file_path
76+
}
77+
}
78+
}
79+
80+
return null
81+
}

‎apps/editor/src/utils/is-response-applicable.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parse_response } from '@/features/apply-response/utils/response-parser'
2+
import { get_invalid_file_path } from '@/features/apply-response/utils/file-checks'
23

34
export const is_response_applicable = (params: {
45
response: string
@@ -16,6 +17,15 @@ export const is_response_applicable = (params: {
1617
workspace_files: params.workspace_files
1718
})
1819

20+
try {
21+
const invalid_file_path = get_invalid_file_path(response_items)
22+
if (invalid_file_path) {
23+
return false
24+
}
25+
} catch {
26+
// Ignore
27+
}
28+
1929
return response_items.some(
2030
(item) =>
2131
item.type === 'commit-message' ||

0 commit comments

Comments
 (0)