Skip to content

Commit

Permalink
Fix pathing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftyos committed Sep 28, 2023
1 parent ece0f91 commit 4f15b1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion autogpts/forge/forge/sdk/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ def __init__(self, base_path: str):
self.base_path = Path(base_path).resolve()

def _resolve_path(self, task_id: str, path: str) -> Path:
path = path if not path.startswith("/") else path[1:]
abs_path = (self.base_path / task_id / path).resolve()
if not str(abs_path).startswith(str(self.base_path)):
print("Error")
raise ValueError(f"Directory traversal is not allowed! - {abs_path}")
abs_path.parent.mkdir(parents=True, exist_ok=True)
try:
abs_path.parent.mkdir(parents=True, exist_ok=True)
except FileExistsError:
pass
return abs_path

def read(self, task_id: str, path: str) -> bytes:
Expand Down
21 changes: 17 additions & 4 deletions benchmark/agbenchmark/agent_api_interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import logging
import os
import pathlib
import time
from typing import Any, Dict, Optional

Expand All @@ -14,6 +16,8 @@
from agbenchmark.agent_protocol_client.models.step import Step
from agbenchmark.utils.data_types import ChallengeData

LOG = logging.getLogger(__name__)


async def run_api_agent(
task: ChallengeData, config: Dict[str, Any], artifacts_location: str, timeout: int
Expand Down Expand Up @@ -63,13 +67,22 @@ async def copy_agent_artifacts_into_temp_folder(api_instance, task_id):
artifacts = await api_instance.list_agent_task_artifacts(task_id=task_id)
for artifact in artifacts.artifacts:
# current absolute path of the directory of the file
directory_location = TEMP_FOLDER_ABS_PATH
directory_location = pathlib.Path(TEMP_FOLDER_ABS_PATH)
if artifact.relative_path:
directory_location = os.path.dirname(directory_location / artifact.relative_path)

os.mkdir(directory_location, recursive=True, exist_ok=True)
path = (
artifact.relative_path
if not artifact.relative_path.startswith("/")
else artifact.relative_path[1:]
)
directory_location = pathlib.Path(
os.path.dirname(directory_location / path)
)
LOG.info(f"Creating directory {directory_location}")

directory_location.mkdir(parents=True, exist_ok=True)

file_path = directory_location / artifact.file_name
LOG.info(f"Writing file {file_path}")
with open(file_path, "wb") as f:
content = await api_instance.download_agent_task_artifact(
task_id=task_id, artifact_id=artifact.artifact_id
Expand Down

0 comments on commit 4f15b1c

Please sign in to comment.