From 63d04d2ccf1f668783bfea781d8138dfb6b613da Mon Sep 17 00:00:00 2001 From: Daksh Bhardwaj Date: Fri, 26 Jul 2024 17:44:10 +0530 Subject: [PATCH 1/2] added check when file path is absolute --- sirji/vscode-extension/package.json | 2 +- .../src/utils/executor/create_file.ts | 10 ++++++++-- .../src/utils/executor/execute_task.ts | 16 ++++++++++++++-- .../src/utils/executor/fetch_recipe.ts | 11 +++++++++-- .../executor/find_and_replace_in_project_file.ts | 11 ++++++++++- .../src/utils/executor/helper.ts | 13 +++++++++++++ .../src/utils/executor/insert_text.ts | 11 ++++++++--- .../src/utils/executor/read_content.ts | 10 ++++++++-- 8 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 sirji/vscode-extension/src/utils/executor/helper.ts diff --git a/sirji/vscode-extension/package.json b/sirji/vscode-extension/package.json index 73650cca..8d6ccd8e 100644 --- a/sirji/vscode-extension/package.json +++ b/sirji/vscode-extension/package.json @@ -2,7 +2,7 @@ "name": "sirji", "displayName": "Sirji", "description": "Sirji is an AI Software Development Agent.", - "version": "0.0.30", + "version": "0.0.30-beta.1", "publisher": "TrueSparrow", "icon": "out/assets/sirji.png", "repository": { diff --git a/sirji/vscode-extension/src/utils/executor/create_file.ts b/sirji/vscode-extension/src/utils/executor/create_file.ts index e80a257f..52ab54cd 100644 --- a/sirji/vscode-extension/src/utils/executor/create_file.ts +++ b/sirji/vscode-extension/src/utils/executor/create_file.ts @@ -1,18 +1,24 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; +import { getFilePath } from './helper'; export function sanitizePath(filePath: string): string { return filePath.replace(/^['"]|['"]$/g, ''); } -export async function createFile(rootPath: string, isProjectRoot: boolean, body: string): Promise { +export async function createFile(rootPath: string, isProjectRoot: boolean, body: string): Promise { try { const [filePathPart, fileContent] = body.split('---'); const filePath = filePathPart.replace('File path:', '').trim(); const sanitizedFilePath = sanitizePath(filePath); - const fullPath = path.isAbsolute(sanitizedFilePath) ? sanitizedFilePath : path.join(rootPath, sanitizedFilePath); + let fullPath = ''; + try { + fullPath = getFilePath(sanitizedFilePath, rootPath); + } catch (error) { + return error; + } const uri = vscode.Uri.file(fullPath); if (isProjectRoot) { diff --git a/sirji/vscode-extension/src/utils/executor/execute_task.ts b/sirji/vscode-extension/src/utils/executor/execute_task.ts index 3b6cfc9e..3b24af06 100644 --- a/sirji/vscode-extension/src/utils/executor/execute_task.ts +++ b/sirji/vscode-extension/src/utils/executor/execute_task.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; +import { getFilePath } from './helper'; let currentTaskExecution: any = null; @@ -27,8 +28,19 @@ export async function executeTask(command: string, projectRootPath: string): Pro return new Promise(async (resolve, reject) => { const tempFileName = `output_${Date.now()}.txt`; - const tempFileRelativePath = path.join(projectRootPath, tempFileName); - const tempFilePath = path.join(projectRootPath, tempFileRelativePath); + let tempFileRelativePath = ''; + try { + tempFileRelativePath = getFilePath(tempFileName, projectRootPath); + } catch (error) { + return error; + } + + let tempFilePath = ''; + try { + tempFilePath = getFilePath(tempFileRelativePath, projectRootPath); + } catch (error) { + return error; + } let tempFileContent = ''; diff --git a/sirji/vscode-extension/src/utils/executor/fetch_recipe.ts b/sirji/vscode-extension/src/utils/executor/fetch_recipe.ts index 2f03a0ef..8c789b78 100644 --- a/sirji/vscode-extension/src/utils/executor/fetch_recipe.ts +++ b/sirji/vscode-extension/src/utils/executor/fetch_recipe.ts @@ -1,12 +1,19 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; +import { getFilePath } from './helper'; -export async function fetchRecipe(FolderPath: string, body: string): Promise { +export async function fetchRecipe(FolderPath: string, body: string): Promise { try { const filePath = body.split('File path:')[1].trim(); + let fullFilePath = ''; + try { + fullFilePath = getFilePath(filePath, FolderPath); + } catch (error) { + return error; + } - const uri = vscode.Uri.file(path.join(FolderPath, filePath)); + const uri = vscode.Uri.file(fullFilePath); if (!fs.existsSync(uri.fsPath)) { return 'Index not found'; diff --git a/sirji/vscode-extension/src/utils/executor/find_and_replace_in_project_file.ts b/sirji/vscode-extension/src/utils/executor/find_and_replace_in_project_file.ts index 47be2478..20e9f9da 100644 --- a/sirji/vscode-extension/src/utils/executor/find_and_replace_in_project_file.ts +++ b/sirji/vscode-extension/src/utils/executor/find_and_replace_in_project_file.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import path from 'path'; +import { getFilePath } from './helper'; const escapeRegExp = (string: string) => { return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); @@ -30,7 +31,15 @@ export const findAndReplaceInProjectFile = async (body: string, projectRootPath: return `The error in parsing the BODY: ${error}. Either the FILE_PATH key is missing or not in the correct format. The correct format is FILE_PATH: {{File path}} ---. Your response must conform strictly to FIND_AND_REPLACE Response Template with all the keys present in the BODY`; } - filePath = path.join(projectRootPath, filePath); + console.log('File Path - projectRootPath', projectRootPath); + + console.log('File Path - projectRootPath', filePath); + + try { + filePath = getFilePath(filePath, projectRootPath); + } catch (error) { + return error; + } console.log(`Searching for files with pattern: ${searchText} in folder: ${filePath} and replacing with: ${replacement}`); diff --git a/sirji/vscode-extension/src/utils/executor/helper.ts b/sirji/vscode-extension/src/utils/executor/helper.ts new file mode 100644 index 00000000..63323647 --- /dev/null +++ b/sirji/vscode-extension/src/utils/executor/helper.ts @@ -0,0 +1,13 @@ +import path from 'path'; + +export const getFilePath = (filePath: string, projectRootPath: string): string => { + if (path.isAbsolute(filePath)) { + if (filePath.includes(projectRootPath)) { + return filePath; + } else { + throw new Error('Error: File does not exist in the project root path'); + } + } else { + return path.join(projectRootPath, filePath); + } +}; diff --git a/sirji/vscode-extension/src/utils/executor/insert_text.ts b/sirji/vscode-extension/src/utils/executor/insert_text.ts index 80ec6651..2bcaee30 100644 --- a/sirji/vscode-extension/src/utils/executor/insert_text.ts +++ b/sirji/vscode-extension/src/utils/executor/insert_text.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import path from 'path'; +import { getFilePath } from './helper'; function delay(ms: number) { return new Promise((res) => setTimeout(res, ms)); @@ -40,7 +41,7 @@ async function insertCode(document: vscode.TextDocument, editor: vscode.TextEdit }); } -export const insertText = async (body: string, projectRootPath: string, insertPosition: string, globPattern?: string, exclude: string = '**/node_modules/**'): Promise => { +export const insertText = async (body: string, projectRootPath: string, insertPosition: string, globPattern?: string, exclude: string = '**/node_modules/**'): Promise => { let searchText, textToInsert, filePath; if (insertPosition.toLowerCase() === 'below') { @@ -75,7 +76,11 @@ export const insertText = async (body: string, projectRootPath: string, insertPo return `The error in parsing the BODY: ${error}. Either the FILE_PATH key is missing or not in the correct format. The correct format is FILE_PATH: {{File path without any special characaters}} ---. Your response must conform strictly to Response Template with all the keys present in the BODY`; } - filePath = path.join(projectRootPath, filePath); + try { + filePath = getFilePath(filePath, projectRootPath); + } catch (error) { + return error; + } console.log(`Inserting text into file: ${filePath} based on search text: '${searchText}' with replacement: '${textToInsert}' and insert position: '${insertPosition}'`); @@ -99,7 +104,7 @@ export const insertText = async (body: string, projectRootPath: string, insertPo for (let i = 0; i < lines.length; i++) { const block = lines.slice(i, i + normalizedSearchText.split('\n').length).join('\n'); - + if (normalizeIndentation(block).includes(normalizedSearchText)) { console.log('Block found:', block); startIndex = text.indexOf(block); diff --git a/sirji/vscode-extension/src/utils/executor/read_content.ts b/sirji/vscode-extension/src/utils/executor/read_content.ts index bf1fc2f6..9ffd5a81 100644 --- a/sirji/vscode-extension/src/utils/executor/read_content.ts +++ b/sirji/vscode-extension/src/utils/executor/read_content.ts @@ -2,6 +2,7 @@ import * as path from 'path'; import * as fs from 'fs/promises'; import * as mimetype from 'mime-types'; import { sanitizePath } from './create_file'; +import { getFilePath } from './helper'; const SKIP_LIST = ['__pycache__', '.git', '.github', '.gitlab', '.vscode', '.idea', 'node_modules', '.DS_Store', 'venv', '.venv', '.sass-cache', 'dist', 'out', 'build', 'logs', '.npm', 'temp', 'tmp']; @@ -32,7 +33,7 @@ function isPathInsideRoot(rootPath: string, targetPath: string): boolean { return resolvedTarget.startsWith(resolvedRoot); } -export async function readContent(projectRootPath: string, body: string, isDirectory: boolean): Promise { +export async function readContent(projectRootPath: string, body: string, isDirectory: boolean): Promise { async function shouldSkip(name: string): Promise { return SKIP_LIST.includes(name); } @@ -107,7 +108,12 @@ export async function readContent(projectRootPath: string, body: string, isDirec for (const inputPath of inputPaths) { try { - const fullPath = path.isAbsolute(inputPath) ? inputPath : path.join(projectRootPath, inputPath); + let fullPath = ''; + try { + fullPath = getFilePath(inputPath, projectRootPath); + } catch (error) { + return error; + } if (!isPathInsideRoot(projectRootPath, fullPath)) { throw new Error(`Path '${inputPath}' is not within the project root.`); From 988c27f0cbc24357ecb65704b9504d1925bc18ac Mon Sep 17 00:00:00 2001 From: Daksh Bhardwaj Date: Fri, 26 Jul 2024 17:47:00 +0530 Subject: [PATCH 2/2] updated package version --- sirji/vscode-extension/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sirji/vscode-extension/package.json b/sirji/vscode-extension/package.json index 8d6ccd8e..42fdd1f5 100644 --- a/sirji/vscode-extension/package.json +++ b/sirji/vscode-extension/package.json @@ -2,7 +2,7 @@ "name": "sirji", "displayName": "Sirji", "description": "Sirji is an AI Software Development Agent.", - "version": "0.0.30-beta.1", + "version": "0.0.31-beta.1", "publisher": "TrueSparrow", "icon": "out/assets/sirji.png", "repository": {