Skip to content

Commit 033e30c

Browse files
committed
fix path problem for images
1 parent 762c5d6 commit 033e30c

6 files changed

Lines changed: 18 additions & 24 deletions

File tree

agents/crewai/websearch_agent/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import time
66
import uuid
77
from contextlib import asynccontextmanager
8-
import os
98
from os import getenv
109
from pathlib import Path
1110

@@ -386,9 +385,9 @@ async def serve_image(filename: str):
386385
"""Serve images from the project-level images directory."""
387386
base = _IMAGES_DIR.resolve()
388387
file_path = (base / filename).resolve()
389-
base_str = str(base)
390-
file_path_str = str(file_path)
391-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
388+
try:
389+
file_path.relative_to(base)
390+
except ValueError:
392391
raise HTTPException(status_code=404, detail="Image not found")
393392
if not file_path.is_file():
394393
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/agentic_rag/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6-
import os
76
from os import getenv
87
from pathlib import Path
98

@@ -410,9 +409,9 @@ async def serve_image(filename: str):
410409
"""Serve images from the project-level images directory."""
411410
base = _IMAGES_DIR.resolve()
412411
file_path = (base / filename).resolve()
413-
base_str = str(base)
414-
file_path_str = str(file_path)
415-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
412+
try:
413+
file_path.relative_to(base)
414+
except ValueError:
416415
raise HTTPException(status_code=404, detail="Image not found")
417416
if not file_path.is_file():
418417
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/react_agent/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6-
import os
76
from os import getenv
87
from pathlib import Path
98

@@ -406,9 +405,9 @@ async def serve_image(filename: str):
406405
"""Serve images from the project-level images directory."""
407406
base = _IMAGES_DIR.resolve()
408407
file_path = (base / filename).resolve()
409-
base_str = str(base)
410-
file_path_str = str(file_path)
411-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
408+
try:
409+
file_path.relative_to(base)
410+
except ValueError:
412411
raise HTTPException(status_code=404, detail="Image not found")
413412
if not file_path.is_file():
414413
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/react_with_database_memory/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from langgraph.checkpoint.postgres import PostgresSaver
1313
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
1414
from pydantic import BaseModel, Field
15-
import os
1615
from os import getenv
1716

1817
from react_with_database_memory.agent import get_graph_closure
@@ -487,9 +486,9 @@ async def serve_image(filename: str):
487486
"""Serve images from the project-level images directory."""
488487
base = _IMAGES_DIR.resolve()
489488
file_path = (base / filename).resolve()
490-
base_str = str(base)
491-
file_path_str = str(file_path)
492-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
489+
try:
490+
file_path.relative_to(base)
491+
except ValueError:
493492
raise HTTPException(status_code=404, detail="Image not found")
494493
if not file_path.is_file():
495494
raise HTTPException(status_code=404, detail="Image not found")

agents/llamaindex/websearch_agent/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6-
import os
76
from os import getenv
87
from pathlib import Path
98

@@ -472,9 +471,9 @@ async def serve_image(filename: str):
472471
"""Serve images from the project-level images directory."""
473472
base = _IMAGES_DIR.resolve()
474473
file_path = (base / filename).resolve()
475-
base_str = str(base)
476-
file_path_str = str(file_path)
477-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
474+
try:
475+
file_path.relative_to(base)
476+
except ValueError:
478477
raise HTTPException(status_code=404, detail="Image not found")
479478
if not file_path.is_file():
480479
raise HTTPException(status_code=404, detail="Image not found")

agents/vanilla_python/openai_responses_agent/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import time
55
import uuid
66
from contextlib import asynccontextmanager
7-
import os
87
from os import getenv
98
from pathlib import Path
109

@@ -407,9 +406,9 @@ async def serve_image(filename: str):
407406
"""Serve images from the project-level images directory."""
408407
base = _IMAGES_DIR.resolve()
409408
file_path = (base / filename).resolve()
410-
base_str = str(base)
411-
file_path_str = str(file_path)
412-
if not (file_path_str == base_str or file_path_str.startswith(base_str + os.path.sep)):
409+
try:
410+
file_path.relative_to(base)
411+
except ValueError:
413412
raise HTTPException(status_code=404, detail="Image not found")
414413
if not file_path.is_file():
415414
raise HTTPException(status_code=404, detail="Image not found")

0 commit comments

Comments
 (0)