Skip to content

Commit 762c5d6

Browse files
committed
fix path problem for images
1 parent 4d841b7 commit 762c5d6

6 files changed

Lines changed: 24 additions & 6 deletions

File tree

agents/crewai/websearch_agent/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import uuid
77
from contextlib import asynccontextmanager
8+
import os
89
from os import getenv
910
from pathlib import Path
1011

@@ -385,7 +386,9 @@ async def serve_image(filename: str):
385386
"""Serve images from the project-level images directory."""
386387
base = _IMAGES_DIR.resolve()
387388
file_path = (base / filename).resolve()
388-
if not file_path.is_relative_to(base):
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)):
389392
raise HTTPException(status_code=404, detail="Image not found")
390393
if not file_path.is_file():
391394
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/agentic_rag/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6+
import os
67
from os import getenv
78
from pathlib import Path
89

@@ -409,7 +410,9 @@ async def serve_image(filename: str):
409410
"""Serve images from the project-level images directory."""
410411
base = _IMAGES_DIR.resolve()
411412
file_path = (base / filename).resolve()
412-
if not file_path.is_relative_to(base):
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)):
413416
raise HTTPException(status_code=404, detail="Image not found")
414417
if not file_path.is_file():
415418
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/react_agent/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6+
import os
67
from os import getenv
78
from pathlib import Path
89

@@ -405,7 +406,9 @@ async def serve_image(filename: str):
405406
"""Serve images from the project-level images directory."""
406407
base = _IMAGES_DIR.resolve()
407408
file_path = (base / filename).resolve()
408-
if not file_path.is_relative_to(base):
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)):
409412
raise HTTPException(status_code=404, detail="Image not found")
410413
if not file_path.is_file():
411414
raise HTTPException(status_code=404, detail="Image not found")

agents/langgraph/react_with_database_memory/main.py

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

1718
from react_with_database_memory.agent import get_graph_closure
@@ -486,7 +487,9 @@ async def serve_image(filename: str):
486487
"""Serve images from the project-level images directory."""
487488
base = _IMAGES_DIR.resolve()
488489
file_path = (base / filename).resolve()
489-
if not file_path.is_relative_to(base):
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)):
490493
raise HTTPException(status_code=404, detail="Image not found")
491494
if not file_path.is_file():
492495
raise HTTPException(status_code=404, detail="Image not found")

agents/llamaindex/websearch_agent/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import uuid
55
from contextlib import asynccontextmanager
6+
import os
67
from os import getenv
78
from pathlib import Path
89

@@ -471,7 +472,9 @@ async def serve_image(filename: str):
471472
"""Serve images from the project-level images directory."""
472473
base = _IMAGES_DIR.resolve()
473474
file_path = (base / filename).resolve()
474-
if not file_path.is_relative_to(base):
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)):
475478
raise HTTPException(status_code=404, detail="Image not found")
476479
if not file_path.is_file():
477480
raise HTTPException(status_code=404, detail="Image not found")

agents/vanilla_python/openai_responses_agent/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import uuid
66
from contextlib import asynccontextmanager
7+
import os
78
from os import getenv
89
from pathlib import Path
910

@@ -406,7 +407,9 @@ async def serve_image(filename: str):
406407
"""Serve images from the project-level images directory."""
407408
base = _IMAGES_DIR.resolve()
408409
file_path = (base / filename).resolve()
409-
if not file_path.is_relative_to(base):
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)):
410413
raise HTTPException(status_code=404, detail="Image not found")
411414
if not file_path.is_file():
412415
raise HTTPException(status_code=404, detail="Image not found")

0 commit comments

Comments
 (0)