@@ -45,6 +45,7 @@ from sglang.multimodal_gen.runtime.entrypoints.openai.utils import (
|
||||
process_generation_batch,
|
||||
request_extra_value,
|
||||
resolve_sampling_params_cls,
|
||||
sanitize_upload_filename,
|
||||
save_image_to_path,
|
||||
temp_dir_if_disabled,
|
||||
)
|
||||
@@ -470,10 +471,12 @@ async def edits(
|
||||
try:
|
||||
for idx, img in enumerate(image_list):
|
||||
filename = img.filename if hasattr(img, "filename") else f"image_{idx}"
|
||||
safe_name = sanitize_upload_filename(filename, f"image_{idx}")
|
||||
input_path = await save_image_to_path(
|
||||
img,
|
||||
os.path.join(uploads_dir, f"{request_id}_{idx}_{filename}"),
|
||||
os.path.join(uploads_dir, f"{request_id}_{idx}_{safe_name}"),
|
||||
prefer_remote_source=server_args.input_save_path is None,
|
||||
uploads_root=uploads_dir,
|
||||
)
|
||||
input_paths.append(input_path)
|
||||
except Exception as e:
|
||||
|
||||
@@ -31,6 +31,7 @@ from sglang.multimodal_gen.runtime.entrypoints.openai.utils import (
|
||||
add_common_data_to_response,
|
||||
merge_image_input_list,
|
||||
process_generation_batch,
|
||||
sanitize_upload_filename,
|
||||
save_image_to_path,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.entrypoints.utils import prepare_request
|
||||
@@ -164,9 +165,12 @@ async def create_mesh(
|
||||
os.makedirs(uploads_dir, exist_ok=True)
|
||||
img = image_list[0]
|
||||
filename = img.filename if hasattr(img, "filename") else "input_image"
|
||||
safe_name = sanitize_upload_filename(filename, "input_image")
|
||||
try:
|
||||
input_path = await save_image_to_path(
|
||||
img, os.path.join(uploads_dir, f"{request_id}_{filename}")
|
||||
img,
|
||||
os.path.join(uploads_dir, f"{request_id}_{safe_name}"),
|
||||
uploads_root=uploads_dir,
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
@@ -200,6 +204,7 @@ async def create_mesh(
|
||||
input_path = await save_image_to_path(
|
||||
img_src,
|
||||
os.path.join(uploads_dir, f"{request_id}_input_image"),
|
||||
uploads_root=uploads_dir,
|
||||
)
|
||||
|
||||
req = MeshGenerationsRequest(**payload)
|
||||
|
||||
+3
-1
@@ -83,7 +83,9 @@ async def save_realtime_first_frame(
|
||||
else:
|
||||
target_path = os.path.join(uploads_dir, f"{session.id}_first_frame")
|
||||
|
||||
request.first_frame = await save_image_to_path(first_frame, target_path)
|
||||
request.first_frame = await save_image_to_path(
|
||||
first_frame, target_path, uploads_root=uploads_dir
|
||||
)
|
||||
|
||||
|
||||
def build_realtime_sampling_params(
|
||||
|
||||
@@ -4,6 +4,7 @@ import dataclasses
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
@@ -33,7 +34,10 @@ from sglang.multimodal_gen.runtime.pipelines_core.schedule_batch import OutputBa
|
||||
from sglang.multimodal_gen.runtime.scheduler_client import AsyncSchedulerClient
|
||||
from sglang.multimodal_gen.runtime.server_args import get_global_server_args
|
||||
from sglang.multimodal_gen.runtime.utils.common import parse_size
|
||||
from sglang.multimodal_gen.runtime.utils.image_io import save_base64_image_to_path
|
||||
from sglang.multimodal_gen.runtime.utils.image_io import (
|
||||
ensure_path_within_root,
|
||||
save_base64_image_to_path,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.utils.logging_utils import (
|
||||
init_logger,
|
||||
log_batch_completion,
|
||||
@@ -299,14 +303,33 @@ def resolve_sampling_params_cls(server_args: Any) -> type[SamplingParams]:
|
||||
return sampling_params_cls
|
||||
|
||||
|
||||
def sanitize_upload_filename(filename: str, fallback: str) -> str:
|
||||
name = os.path.basename((filename or "").replace("\\", "/"))
|
||||
if not name or name in {".", ".."}:
|
||||
name = fallback
|
||||
|
||||
stem, ext = os.path.splitext(name)
|
||||
safe_stem = re.sub(r"[^A-Za-z0-9._-]+", "_", stem).strip("._")
|
||||
safe_ext = re.sub(r"[^A-Za-z0-9.]+", "", ext)
|
||||
if not safe_stem:
|
||||
safe_stem = re.sub(r"[^A-Za-z0-9._-]+", "_", fallback).strip("._") or "upload"
|
||||
return f"{safe_stem}{safe_ext}"
|
||||
|
||||
|
||||
async def save_image_to_path(
|
||||
image: Union[UploadFile, bytes, str],
|
||||
target_path: str,
|
||||
*,
|
||||
prefer_remote_source: bool = False,
|
||||
uploads_root: str | None = None,
|
||||
) -> str:
|
||||
if uploads_root is not None:
|
||||
target_path = ensure_path_within_root(target_path, uploads_root)
|
||||
input_path = await _maybe_url_image(
|
||||
image, target_path, prefer_remote_source=prefer_remote_source
|
||||
image,
|
||||
target_path,
|
||||
prefer_remote_source=prefer_remote_source,
|
||||
uploads_root=uploads_root,
|
||||
)
|
||||
if input_path is None:
|
||||
input_path = await _save_upload_to_path(image, target_path)
|
||||
@@ -345,6 +368,7 @@ async def _maybe_url_image(
|
||||
target_path: str,
|
||||
*,
|
||||
prefer_remote_source: bool = False,
|
||||
uploads_root: str | None = None,
|
||||
) -> str | None:
|
||||
if not isinstance(img_url, str):
|
||||
return None
|
||||
@@ -355,19 +379,25 @@ async def _maybe_url_image(
|
||||
if prefer_remote_source:
|
||||
return img_url
|
||||
# download image from URL and persist on disk
|
||||
input_path = await _save_url_image_to_path(img_url, target_path)
|
||||
input_path = await _save_url_image_to_path(
|
||||
img_url, target_path, uploads_root=uploads_root
|
||||
)
|
||||
return input_path
|
||||
elif img_url.startswith("data:image"):
|
||||
if prefer_remote_source:
|
||||
return img_url
|
||||
# encode image base64 url and persist on disk
|
||||
input_path = save_base64_image_to_path(img_url, target_path)
|
||||
input_path = save_base64_image_to_path(
|
||||
img_url, target_path, uploads_root=uploads_root
|
||||
)
|
||||
return input_path
|
||||
else:
|
||||
raise ValueError("Unsupported image url format")
|
||||
|
||||
|
||||
async def _save_url_image_to_path(image_url: str, target_path: str) -> str:
|
||||
async def _save_url_image_to_path(
|
||||
image_url: str, target_path: str, *, uploads_root: str | None = None
|
||||
) -> str:
|
||||
"""Download image from URL and save to target path."""
|
||||
|
||||
def _is_retryable_download_error(error: Exception) -> bool:
|
||||
@@ -433,6 +463,8 @@ async def _save_url_image_to_path(image_url: str, target_path: str) -> str:
|
||||
)
|
||||
target_path = f"{target_path}{ext}"
|
||||
|
||||
if uploads_root is not None:
|
||||
target_path = ensure_path_within_root(target_path, uploads_root)
|
||||
with open(target_path, "wb") as f:
|
||||
f.write(response.content)
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ from sglang.multimodal_gen.runtime.entrypoints.openai.utils import (
|
||||
process_generation_batch,
|
||||
request_extra_value,
|
||||
resolve_sampling_params_cls,
|
||||
sanitize_upload_filename,
|
||||
save_image_to_path,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.entrypoints.utils import prepare_request
|
||||
@@ -371,9 +372,13 @@ async def _save_first_input_image(
|
||||
os.makedirs(uploads_dir, exist_ok=True)
|
||||
|
||||
filename = image.filename if hasattr(image, "filename") else "url_image"
|
||||
target_path = os.path.join(uploads_dir, f"{request_id}_{filename}")
|
||||
safe_name = sanitize_upload_filename(filename, "url_image")
|
||||
target_path = os.path.join(uploads_dir, f"{request_id}_{safe_name}")
|
||||
return await save_image_to_path(
|
||||
image, target_path, prefer_remote_source=prefer_remote_source
|
||||
image,
|
||||
target_path,
|
||||
prefer_remote_source=prefer_remote_source,
|
||||
uploads_root=uploads_dir,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,9 +2,20 @@
|
||||
import base64
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def save_base64_image_to_path(base64_data: str, target_path: str) -> str:
|
||||
def ensure_path_within_root(target_path: str, root_dir: str) -> str:
|
||||
root_path = Path(root_dir).resolve()
|
||||
candidate = Path(target_path).resolve()
|
||||
if root_path != candidate and root_path not in candidate.parents:
|
||||
raise ValueError("Upload path escapes the uploads root")
|
||||
return str(candidate)
|
||||
|
||||
|
||||
def save_base64_image_to_path(
|
||||
base64_data: str, target_path: str, *, uploads_root: str | None = None
|
||||
) -> str:
|
||||
b64_format_hint = (
|
||||
"Failed to decode base64 image. "
|
||||
"Expected format: `data:[<media-type>];base64,<data>`"
|
||||
@@ -28,6 +39,8 @@ def save_base64_image_to_path(base64_data: str, target_path: str) -> str:
|
||||
else:
|
||||
ext = "jpg"
|
||||
target_path = f"{target_path}.{ext}"
|
||||
if uploads_root is not None:
|
||||
target_path = ensure_path_within_root(target_path, uploads_root)
|
||||
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
|
||||
@@ -239,7 +239,7 @@ def test_sana_wm_realtime_camera_state_uses_sana_normalizer():
|
||||
|
||||
|
||||
def test_sana_wm_realtime_adapter_preserves_requested_size():
|
||||
async def fake_save_image_to_path(image, target_path):
|
||||
async def fake_save_image_to_path(image, target_path, *, uploads_root):
|
||||
return target_path
|
||||
|
||||
old_save_image_to_path = realtime_adapter.save_image_to_path
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import asyncio
|
||||
import io
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.testclient import TestClient
|
||||
from starlette.datastructures import UploadFile
|
||||
|
||||
from sglang.multimodal_gen.configs.sample.sampling_params import SamplingParams
|
||||
from sglang.multimodal_gen.runtime.entrypoints.openai import (
|
||||
image_api,
|
||||
mesh_api,
|
||||
utils,
|
||||
video_api,
|
||||
)
|
||||
from sglang.multimodal_gen.runtime.utils.image_io import ensure_path_within_root
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"filename,expected",
|
||||
[
|
||||
("../../evil.txt", "evil.txt"),
|
||||
(r"..\..\evil.txt", "evil.txt"),
|
||||
("/tmp/evil.txt", "evil.txt"),
|
||||
("..", "fallback"),
|
||||
("", "fallback"),
|
||||
("unsafe\x00 name.png", "unsafe_name.png"),
|
||||
],
|
||||
)
|
||||
def test_sanitize_upload_filename(filename, expected):
|
||||
assert utils.sanitize_upload_filename(filename, "fallback") == expected
|
||||
|
||||
|
||||
def test_ensure_path_within_root_rejects_escape(tmp_path):
|
||||
root = tmp_path / "uploads"
|
||||
with pytest.raises(ValueError, match="escapes"):
|
||||
ensure_path_within_root(root / ".." / "evil.txt", root)
|
||||
|
||||
|
||||
@pytest.fixture(params=["upload", "bytes", "base64", "url"])
|
||||
def image_source(request, monkeypatch):
|
||||
if request.param == "upload":
|
||||
return UploadFile(io.BytesIO(b"image"), filename="image.png"), ""
|
||||
if request.param == "bytes":
|
||||
return b"image", ""
|
||||
if request.param == "base64":
|
||||
return "data:image/png;base64,aW1hZ2U=", ".png"
|
||||
|
||||
client_cls = httpx.AsyncClient
|
||||
transport = httpx.MockTransport(
|
||||
lambda req: httpx.Response(
|
||||
200, headers={"content-type": "image/png"}, content=b"image"
|
||||
)
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
utils.httpx,
|
||||
"AsyncClient",
|
||||
lambda **kwargs: client_cls(transport=transport, **kwargs),
|
||||
)
|
||||
return "https://example.invalid/image.png", ".png"
|
||||
|
||||
|
||||
def test_save_image_sources_within_uploads_root(tmp_path, image_source):
|
||||
source, suffix = image_source
|
||||
root = tmp_path / "uploads"
|
||||
saved = asyncio.run(
|
||||
utils.save_image_to_path(source, str(root / "input"), uploads_root=str(root))
|
||||
)
|
||||
assert Path(saved) == root / f"input{suffix}"
|
||||
assert Path(saved).read_bytes() == b"image"
|
||||
|
||||
|
||||
def test_save_image_sources_reject_escape_before_writing(tmp_path, image_source):
|
||||
source, _ = image_source
|
||||
root = tmp_path / "uploads"
|
||||
with pytest.raises(ValueError, match="escapes"):
|
||||
asyncio.run(
|
||||
utils.save_image_to_path(
|
||||
source, str(root / ".." / "outside"), uploads_root=str(root)
|
||||
)
|
||||
)
|
||||
assert not list(tmp_path.iterdir())
|
||||
|
||||
|
||||
def test_final_upload_path_cannot_follow_symlink_outside_root(tmp_path, image_source):
|
||||
source, suffix = image_source
|
||||
root = tmp_path / "uploads"
|
||||
root.mkdir()
|
||||
outside = tmp_path / "outside"
|
||||
outside.write_bytes(b"unchanged")
|
||||
(root / f"input{suffix}").symlink_to(outside)
|
||||
with pytest.raises(Exception, match="escapes"):
|
||||
asyncio.run(
|
||||
utils.save_image_to_path(
|
||||
source, str(root / "input"), uploads_root=str(root)
|
||||
)
|
||||
)
|
||||
assert outside.read_bytes() == b"unchanged"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source", ["https://example.invalid/image.png", "data:image/png;base64,aW1hZ2U="]
|
||||
)
|
||||
def test_prefer_remote_source_still_avoids_disk_io(tmp_path, source):
|
||||
root = tmp_path / "uploads"
|
||||
saved = asyncio.run(
|
||||
utils.save_image_to_path(
|
||||
source,
|
||||
str(root / "input"),
|
||||
uploads_root=str(root),
|
||||
prefer_remote_source=True,
|
||||
)
|
||||
)
|
||||
assert saved == source
|
||||
assert not root.exists()
|
||||
|
||||
|
||||
def test_video_upload_sanitizes_filename_and_preserves_metadata(tmp_path):
|
||||
filename = "../../../evil.png"
|
||||
upload = UploadFile(io.BytesIO(b"image"), filename=filename)
|
||||
saved = asyncio.run(
|
||||
video_api._save_first_input_image([upload], "request", str(tmp_path))
|
||||
)
|
||||
assert Path(saved) == tmp_path / "request_evil.png"
|
||||
assert Path(saved).read_bytes() == b"image"
|
||||
assert upload.filename == filename
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"api,endpoint,stop_at,relative_path",
|
||||
[
|
||||
(
|
||||
image_api,
|
||||
"/v1/images/edits",
|
||||
"build_sampling_params",
|
||||
"uploads/request_0_evil.png",
|
||||
),
|
||||
(
|
||||
mesh_api,
|
||||
"/v1/meshes",
|
||||
"_build_sampling_params_from_request",
|
||||
"outputs/uploads/request_evil.png",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_multipart_upload_is_sanitized_before_generation(
|
||||
monkeypatch, tmp_path, api, endpoint, stop_at, relative_path
|
||||
):
|
||||
monkeypatch.chdir(tmp_path)
|
||||
args = SimpleNamespace(
|
||||
input_save_path=str(tmp_path / "uploads"),
|
||||
output_path=str(tmp_path / "outputs"),
|
||||
)
|
||||
monkeypatch.setattr(api, "get_global_server_args", lambda: args)
|
||||
monkeypatch.setattr(api, "generate_request_id", lambda: "request")
|
||||
monkeypatch.setattr(
|
||||
image_api, "resolve_sampling_params_cls", lambda args: SamplingParams
|
||||
)
|
||||
|
||||
def stop_before_generation(*args, **kwargs):
|
||||
assert (tmp_path / relative_path).read_bytes() == b"image"
|
||||
raise HTTPException(status_code=418, detail="upload verified")
|
||||
|
||||
monkeypatch.setattr(api, stop_at, stop_before_generation)
|
||||
app = FastAPI()
|
||||
app.include_router(api.router)
|
||||
with TestClient(app) as client:
|
||||
response = client.post(
|
||||
endpoint,
|
||||
data={"prompt": "test"},
|
||||
files={"image": ("../../../evil.png", b"image", "image/png")},
|
||||
)
|
||||
assert response.status_code == 418, response.text
|
||||
assert not (tmp_path / "evil.png").exists()
|
||||
Reference in New Issue
Block a user