From 263ecb914951b22da5431ed4c4844508c21aceff Mon Sep 17 00:00:00 2001 From: lxobr Date: Tue, 19 Nov 2024 09:10:04 +0100 Subject: [PATCH] fix: Add input validation and error handling for paths --- .../local_script_dependencies.py | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/cognee/tasks/repo_processor/local_script_dependencies.py b/cognee/tasks/repo_processor/local_script_dependencies.py index f0c3b246f..ed11640da 100644 --- a/cognee/tasks/repo_processor/local_script_dependencies.py +++ b/cognee/tasks/repo_processor/local_script_dependencies.py @@ -101,14 +101,29 @@ async def get_local_script_dependencies(script_path: str, repo_path: Optional[st """ Extract and return a list of unique module paths that the script depends on. """ - if repo_path: - repo_path_resolved = str(Path(repo_path).resolve()) - with add_sys_path(repo_path_resolved): - dependencies = await _extract_dependencies(script_path) - dependencies = [path for path in dependencies if path.startswith(repo_path_resolved)] - else: + try: + script_path = Path(script_path).resolve(strict=True) + except (FileNotFoundError, PermissionError) as e: + logger.error(f"Error resolving script path: {e}") + return [] + + if not repo_path: + return await _extract_dependencies(script_path) + + try: + repo_path = Path(repo_path).resolve(strict=True) + except (FileNotFoundError, PermissionError) as e: + logger.warning(f"Error resolving repo path: {e}. Proceeding without repo_path.") + return await _extract_dependencies(script_path) + + if not script_path.is_relative_to(repo_path): + logger.warning(f"Script {script_path} not in repo {repo_path}. Proceeding without repo_path.") + return await _extract_dependencies(script_path) + + with add_sys_path(str(repo_path)): dependencies = await _extract_dependencies(script_path) - return dependencies + + return [path for path in dependencies if path.startswith(str(repo_path))] if __name__ == "__main__":