Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep an uploaded document's own name instead of replacing it with underscores (#10667)

Nilay89b7ff009d93
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 4162b2eff63..a4b872d202e 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -4851,8 +4851,8 @@ _roster_failure_logged = False
# character after them, so one file name can rewrite how the rest of the sentence renders
# (CVE-2021-42574, "Trojan Source"); U+200B and the joiners split a name into pieces that
# read as one; ESC is a terminal control sequence in any console or log that echoes the
-# prompt. Only linked folders can carry them -- uploads pass an allowlist at
-# routes/rag.py:_sanitize_filename -- but a linked folder indexes a relative path exactly
+# prompt. Only linked folders can carry them -- routes/rag.py:_document_label strips
+# them out of an uploaded name -- but a linked folder indexes a relative path exactly
# as it came off disk, and every byte except "/" and NUL is legal in one.
#
# The whitespace-like controls map to a space instead of being dropped, so that a name
diff --git a/studio/backend/routes/rag.py b/studio/backend/routes/rag.py
index aac6ec0658a..e6cc8cd6ff3 100644
--- a/studio/backend/routes/rag.py
+++ b/studio/backend/routes/rag.py
@@ -24,6 +24,7 @@ import re
import secrets
import sqlite3
import time
+import unicodedata
import uuid
from contextlib import contextmanager
from typing import Annotated, Iterator
@@ -94,12 +95,16 @@ def _availability(available: bool) -> dict:
}
-_SAFE = re.compile(r"[^A-Za-z0-9._-]+")
-
-
-def _sanitize_filename(name: str) -> str:
- base = os.path.basename(name or "").strip() or "document"
- base = _SAFE.sub("_", base)
+def _document_label(name: str) -> str:
+ # A display label, never a path: the bytes are stored at uploads/<uuid><ext>.
+ base = "".join(
+ (" " if ch.isspace() else "")
+ if unicodedata.category(ch) in ("Cc", "Cf", "Zl", "Zp")
+ else ch
+ for ch in name or ""
+ )
+ # Not \s+: U+3000 and the other Zs spaces belong to the name.
+ base = re.sub(r" +", " ", base).strip() or "document"
if len(base) <= 200:
return base
# Trim the stem, not the extension: _save_upload gates on the extension, so
@@ -110,6 +115,21 @@ def _sanitize_filename(name: str) -> str:
return stem[: 200 - len(ext)] + ext
+# Names treated as Windows paths. Each is also a legal POSIX filename; the list stays
+# narrow because splitting a real name loses part of it. One leading backslash counts as
+# UNC because multipart parsing unescapes "\\" to "\".
+_LOOKS_LIKE_WINDOWS_PATH = re.compile(r"^(?:[A-Za-z]:[\\/]|\\\\?|\.{1,2}\\)")
+
+
+def _sanitize_filename(name: str) -> str:
+ """Label a browser upload, whose client-supplied name may carry a path."""
+ # Not ntpath.basename: it reads "P:L statement.pdf", how macOS stores a Finder "/",
+ # as drive "P:". Classify first: splitting on "/" would strip the drive from "C:/a\b".
+ raw = name or ""
+ parts = re.split(r"[\\/]", raw) if _LOOKS_LIKE_WINDOWS_PATH.match(raw) else raw.split("/")
+ return _document_label(parts[-1])
+
+
def _persist_upload_stream(source, filename: str, *, empty_detail: str) -> tuple[str, str, str]:
"""Copy a validated document stream into the managed uploads root.
@@ -182,7 +202,8 @@ def _save_native_path_upload(lease: str) -> tuple[str, str, str]:
except NativePathLeaseError as exc:
raise HTTPException(status_code = 400, detail = str(exc)) from exc
- filename = _sanitize_filename(grant.canonical_path.name)
+ # Path.name is one component, so a "\" in it is part of the name, as in "AC\DC.pdf".
+ filename = _document_label(grant.canonical_path.name)
try:
with open(grant.canonical_path, "rb") as source:
return _persist_upload_stream(
diff --git a/studio/backend/tests/test_rag_project_source_upload.py b/studio/backend/tests/test_rag_project_source_upload.py
index fd20816b569..55aca40dbd8 100644
--- a/studio/backend/tests/test_rag_project_source_upload.py
+++ b/studio/backend/tests/test_rag_project_source_upload.py
@@ -3,12 +3,14 @@
"""Project sources upload: the path the create-project dialog drives."""
+import io
import os
import pytest
+from fastapi import UploadFile
from core.rag import ingestion, store
-from routes.rag import _sanitize_filename
+from routes.rag import _sanitize_filename, _save_upload
from storage import rag_db
@@ -79,3 +81,57 @@ def test_sanitized_filenames_carry_no_path(raw):
@pytest.mark.parametrize("raw", ["." * 300, "noext" * 100, "a" * 100 + "." + "e" * 250])
def test_sanitizer_degrades_safely(raw):
assert 0 < len(_sanitize_filename(raw)) <= 200
+
+
+ "raw",
+ [
+ "报告.pdf",
+ "日本語ドキュメント.txt",
+ "Отчёт 2026.pdf",
+ "résumé.docx",
+ "My Report.pdf",
+ "Q3: Revenue.pdf",
+ # macOS keeps a Finder "/" on disk as ":", so this is how a dropped
+ # "P/L statement.pdf" reaches the sanitizer. Its first component is the name.
+ "P:L statement.pdf",
+ "C:notes.txt",
+ ],
+)
+def test_sanitizer_keeps_the_name_the_user_gave(raw):
+ assert _sanitize_filename(raw) == raw
+
+
+ "raw,expected",
+ [
+ ("a\x00b.pdf", "ab.pdf"),
+ ("a\u202eb.pdf", "ab.pdf"),
+ ("a\u200bb.pdf", "ab.pdf"),
+ ("\ufeff报告.pdf", "报告.pdf"),
+ ("a\nb.pdf", "a b.pdf"),
+ ("a\u2028b.pdf", "a b.pdf"),
+ # What a browser actually sends for a file picked on Windows.
+ ("C:\\fakepath\\notes.txt", "notes.txt"),
+ ],
+)
+def test_sanitizer_normalizes_what_a_label_cannot_carry(raw, expected):
+ assert _sanitize_filename(raw) == expected
+
+
+def test_uploaded_unicode_name_is_persisted_verbatim(rag_home, stub_embeddings):
+ payload = b"alpha bravo charlie " * 50
+ stored_path, filename, _ = _save_upload(
+ UploadFile(file = io.BytesIO(payload), filename = "报告 2026.txt")
+ )
+ assert filename == "报告 2026.txt"
+
+ _, job_id = _ingest("P9", filename, stored_path)
+ assert _wait(job_id)["status"] == "completed"
+
+ conn = rag_db.get_connection()
+ try:
+ docs = store.list_documents(conn, store.project_scope("P9"))
+ assert [d["filename"] for d in docs] == ["报告 2026.txt"]
+ finally:
+ conn.close()