Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/backend/api/routes/v1/file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ def upload_user_file(

user_id = str(user.user_id)

public_meta = {}
if user.public_chat_visitor:
from core.chat.public import prepare_visitor_upload
public_meta = prepare_visitor_upload(db, user, chat_id, folder_id)

# Verify the chat_id already exists in the database (the session may not yet be created at upload time)
# A scoped key's chat id names its own session; create it on the first-turn upload.
upload_meta = {}
db_chat_id: Optional[str] = None
if chat_id:
exists = db.query(ChatSession.chat_id).filter(ChatSession.chat_id == chat_id).first()
if exists:
db_chat_id = chat_id
if user.public_chat_visitor or (chat_id and (user.api_key_agent_id or user.api_key_project_id)):
from core.chat.public import prepare_scoped_upload
upload_meta = prepare_scoped_upload(db, user, chat_id, folder_id)
db_chat_id = chat_id
elif chat_id and db.query(ChatSession.chat_id).filter(ChatSession.chat_id == chat_id).first():
db_chat_id = chat_id

artifact_id, retry_meta, existing = prepare_upload(
db,
Expand Down Expand Up @@ -140,7 +138,7 @@ def space_path(name: str) -> str:
user_folder_id=db_folder_id,
source="user_upload",
artifact_id=artifact_id,
extra={**retry_meta, **public_meta},
extra={**retry_meta, **upload_meta},
)
except HTTPException:
db.rollback()
Expand Down
39 changes: 32 additions & 7 deletions src/backend/core/capabilities/skills/publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ def read_tree(root: Path) -> dict[str, bytes]:
return files


def ownership_hint(paths) -> str:
"""Say who owns what and how to fix it, so EACCES is actionable from the log."""
me = f"{os.geteuid()}:{os.getegid()}" if hasattr(os, "geteuid") else "?"
parts = []
for path in paths:
try:
st = os.lstat(path)
parts.append(f"{path} owner={st.st_uid}:{st.st_gid} mode={st.st_mode & 0o7777:o}")
except OSError as exc:
parts.append(f"{path} ({exc.strerror})")
return (
f"backend runs as {me}; " + "; ".join(parts)
+ f"; fix on the storage host: chown -R {me} <path> (paths are under STORAGE_PATH)"
)


def remove_entry(path: Path) -> None:
if path.is_symlink() or path.is_file():
path.unlink()
Expand Down Expand Up @@ -135,7 +151,10 @@ def publish_tree(
area = get_sandbox_skills_dir().parent / ".skill-publication"
area.mkdir(parents=True, exist_ok=True)
target.parent.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix="publish-", dir=area) as tmp:
# The retired tree may hold files this account cannot delete; the new
# tree is already live by then, so a leftover in the staging area
# (outside every mount) must not fail the publish.
with tempfile.TemporaryDirectory(prefix="publish-", dir=area, ignore_cleanup_errors=True) as tmp:
stage = Path(tmp) / "new"
stage.mkdir()
for name, data in files.items():
Expand All @@ -145,14 +164,20 @@ def publish_tree(
path.chmod(modes.get(name, 0o644))
backup = Path(tmp) / "old"
had_old = target.exists() or target.is_symlink()
if had_old:
os.replace(target, backup)
try:
os.replace(stage, target)
except BaseException:
if had_old:
os.replace(backup, target)
raise
os.replace(target, backup)
try:
os.replace(stage, target)
except BaseException:
if had_old:
os.replace(backup, target)
raise
except PermissionError as exc:
raise PermissionError(
exc.errno, f"{exc.strerror}; {ownership_hint([target.parent, target])}",
str(target),
) from exc
return target


Expand Down
11 changes: 7 additions & 4 deletions src/backend/core/chat/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,22 @@ def set_visitor_approval_mode(db, user, mode):
db.commit()


def prepare_visitor_upload(db, user, chat_id, folder_id=None):
def prepare_scoped_upload(db, user, chat_id, folder_id=None):
"""Bind a scoped key's upload to its own session, creating it on the first turn."""
from core.chat.schemas import ChatRequest
from core.auth.agent_api import prepare_agent_api_request
from core.projects.api import prepare_project_api_request

if not chat_id or folder_id:
if user.public_chat_visitor and (not chat_id or folder_id):
raise HTTPException(422, detail="A public upload requires its conversation")
body = ChatRequest(chat_id=chat_id, message="Upload", agent_id=user.api_key_agent_id,
project_id=user.api_key_project_id)
prepare_project_api_request(db, user, body)
prepare_agent_api_request(db, user, body)
return {"public_chat_visitor": user.public_chat_visitor, "public_chat_key_id": user.api_key_id,
"agent_api_key_id": user.api_key_id}
meta = {"agent_api_key_id": user.api_key_id}
if user.public_chat_visitor:
meta.update(public_chat_visitor=user.public_chat_visitor, public_chat_key_id=user.api_key_id)
return meta


def validate_visitor_attachments(db, user, attachments):
Expand Down
2 changes: 2 additions & 0 deletions src/backend/core/sandbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"ProcessRequest": "protocol",
"ProcessResult": "protocol",
"SandboxProvider": "protocol",
"SandboxAdmin": "protocol",
"OrdinarySandboxProvider": "protocol",
"SandboxCapacityExhausted": "errors",
"SandboxConnectError": "errors",
"SandboxError": "errors",
Expand Down
8 changes: 4 additions & 4 deletions src/backend/core/sandbox/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

from core.config.settings import settings

from .protocol import SandboxProvider
from .protocol import OrdinarySandboxProvider

logger = logging.getLogger(__name__)


def _ordinary_provider() -> SandboxProvider:
def _ordinary_provider() -> OrdinarySandboxProvider:
if process_state().provider is not None:
return process_state().provider

Expand All @@ -43,7 +43,7 @@ def _ordinary_provider() -> SandboxProvider:
return process_state().provider


def get_sandbox_provider() -> SandboxProvider:
def get_sandbox_provider() -> OrdinarySandboxProvider:
from core.sandbox.sessions.router import SessionSandboxRouter

return SessionSandboxRouter(_ordinary_provider())
Expand All @@ -56,7 +56,7 @@ def reset_provider_cache() -> None:

@dataclass
class FactoryState:
provider: SandboxProvider | None = None
provider: OrdinarySandboxProvider | None = None


_STATE = FactoryState()
Expand Down
3 changes: 1 addition & 2 deletions src/backend/core/sandbox/processes/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ async def resolve():
async def launch(provider, installation, module, user_id, chat_id, config):
config = {
**config,
"_container_isolated": not getattr(provider, "runs_on_host", True)
and getattr(provider, "name", "") in {"opensandbox", "script_runner", "cube"},
"_container_isolated": not provider.runs_on_host,
}
runtime = module["resource"]
source = (installation.package / runtime["entry"]).resolve()
Expand Down
31 changes: 24 additions & 7 deletions src/backend/core/sandbox/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ class SandboxInfo:


class SandboxProvider(Protocol):
"""Sandbox driver interface. Each provider implementation must declare
``name`` for logging and health reporting."""
"""Execution contract every sandbox provider satisfies, including the
providers a session route (``register_sandbox_route``) hands out.

Providers do not subclass this Protocol, so it gives them nothing at
runtime: each provider declares every member itself, and members carry no
defaults here, because a default would only document a value no provider
has. ``tests/sandbox/test_provider_contract.py`` checks the real classes.
"""

name: str

# 这个 provider 是不是直接在用户宿主机上起进程。True 表示除了 OS 沙箱之外
# 没有别的隔离边界,命令必须带着 ``ProcessRequest.sandbox_launch`` 执行;
# False 表示隔离由容器提供,OS 沙箱在那里既无处施加也无意义——调用方据此
# 决定要不要构造 launch,避免出现"构造了但没人施加"的静默失效。
runs_on_host: bool = False
# 每个实现都要显式声明;调用方直接读,不用 getattr 兜底。
runs_on_host: bool

async def run_to_completion(self, req: ProcessRequest) -> ProcessResult:
"""Run once and await the final result; internal callers never manage handles."""
Expand Down Expand Up @@ -237,10 +244,16 @@ async def touch_session(self, session_id: str) -> bool:

async def health(self) -> bool: ...

# ── Read-only admin interface (for the security admin console; all pure
# queries with zero side effects) ──────────────
# Unsupported capabilities should raise SandboxAdminNotSupported, and the
# caller degrades the display accordingly.

class SandboxAdmin(Protocol):
"""Read-only admin view for the security console; all pure queries with
zero side effects. Only the ordinary provider serves it (the console reads
``get_sandbox_provider()``, whose router forwards these to the ordinary
provider), so session-route providers need not implement it.

Unsupported capabilities should raise SandboxAdminNotSupported, and the
caller degrades the display accordingly.
"""

def admin_capabilities(self) -> "SandboxAdminCapabilities":
"""Declare how far this provider supports the read-only sandbox
Expand All @@ -262,3 +275,7 @@ async def admin_get_sandbox(self, sandbox_id: str) -> Optional["SandboxInfo"]:
...

def admin_pool_stats(self) -> dict: ...


class OrdinarySandboxProvider(SandboxProvider, SandboxAdmin, Protocol):
"""What ``SANDBOX_PROVIDER`` selects: execution plus the admin view."""
3 changes: 3 additions & 0 deletions src/backend/core/sandbox/providers/opensandbox/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class OpenSandboxProvider(
_OpenSandboxConnectionMixin,
):
name = "opensandbox"
# Commands run inside a separate container; the container is the isolation
# boundary, so there is no host OS sandbox to apply.
runs_on_host = False

# 类属性兜底:有测试绕过 __init__ 直接造实例,亲和层读不到就会 AttributeError。
# None 表示未锚定服务循环,装饰器退化为就地执行。
Expand Down
11 changes: 11 additions & 0 deletions src/backend/core/sandbox/sessions/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ def __init__(self, ordinary):
def __getattr__(self, name):
return getattr(self._ordinary, name)

# Contract data members are named here rather than left to __getattr__,
# so the router visibly carries them and a provider that lacks one fails
# with that provider's name.
@property
def name(self):
return self._ordinary.name

@property
def runs_on_host(self):
return self._ordinary.runs_on_host

def _for(self, session_id):
from core.harness.registry import sandbox_route

Expand Down
70 changes: 69 additions & 1 deletion src/backend/tests/api/test_agent_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from core.auth.agent_api import begin_agent_api_call, bind_agent_api_run, list_agent_api_calls, prepare_agent_api_request
from tests.api.test_agent_api_scope import db, _key, _user
from core.chat.schemas import ChatRequest
from tests.db_support import share_session
from tests.db_support import share_bind, share_session


def _app(db, monkeypatch, *, user_id="owner", middleware=False):
Expand Down Expand Up @@ -333,3 +333,71 @@ async def cancel(run_id: str):
)
assert response.status_code == 409
assert db.query(AgentApiCallLog).count() == 0


def _upload_client(db, monkeypatch, tmp_path, user):
import core.files.storage
from fastapi.testclient import TestClient
from api.routes.v1 import file_upload
from core.files.storage.factory import process_state
from core.files.storage.local import LocalStorageBackend

monkeypatch.setenv("STORAGE_PATH", str(tmp_path / "storage"))
storage = LocalStorageBackend()
monkeypatch.setattr(process_state(), "storage_instance", storage)
monkeypatch.setattr(core.files.storage, "get_storage", lambda: storage)
app = FastAPI()
app.include_router(file_upload.router)
app.dependency_overrides[get_current_user] = lambda: user
app.dependency_overrides[get_db] = lambda: db
return TestClient(app)


def test_first_turn_upload_creates_the_keys_session_and_is_readable(db, monkeypatch, tmp_path):
from core.auth.agent_api import make_agent_api_scope
from core.db.models import Artifact, ChatSession
from core.llm.api_runtime.agent import artifact_in_scope, parse_api_scope

key, _ = _key(db)
user = _user(key)
client = _upload_client(db, monkeypatch, tmp_path, user)
response = client.post(
"/v1/file/upload", data={"chat_id": "cn-first"},
files={"file": ("table.xlsx", io.BytesIO(b"bytes"), "application/octet-stream")},
)
assert response.status_code == 200, response.text
file_id = response.json()["file_id"]
db.expire_all()
assert db.get(ChatSession, "cn-first").extra_data["agent_api_scope"]["api_key_id"] == key.id
artifact = db.get(Artifact, file_id)
assert artifact.chat_id == "cn-first"
assert artifact.extra_data["agent_api_key_id"] == key.id
share_bind(monkeypatch, db.bind)
scope = parse_api_scope(
make_agent_api_scope(user, "cn-first"), owner_user_id="owner",
chat_id="cn-first", agent_id=key.agent_id,
)
assert artifact_in_scope(file_id, scope)
# The first message reuses the session the upload created.
prepare_agent_api_request(
db, user, ChatRequest(chat_id="cn-first", message="Read it", agent_id=key.agent_id)
)


def test_upload_cannot_attach_to_another_scopes_chat(db, monkeypatch, tmp_path):
from core.db.models import Artifact, ChatSession

db.add(ChatSession(chat_id="owner-chat", user_id="owner", title="Private"))
db.commit()
other, _ = _key(db, agent_id="ua_two")
prepare_agent_api_request(
db, _user(other), ChatRequest(chat_id="other-chat", message="Hi", agent_id="ua_two")
)
client = _upload_client(db, monkeypatch, tmp_path, _user(_key(db)[0]))
for chat_id in ("owner-chat", "other-chat"):
response = client.post(
"/v1/file/upload", data={"chat_id": chat_id},
files={"file": ("a.txt", io.BytesIO(b"x"), "text/plain")},
)
assert response.status_code == 403
assert db.query(Artifact).count() == 0
6 changes: 3 additions & 3 deletions src/backend/tests/api/test_public_chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ def test_public_composer_capabilities_and_uploads(db, kind, resource):
("/v1/tool-approval", "GET"), ("/v1/tool-approval", "PUT"),
("/v1/file/upload", "POST")):
assert authenticate_public_chat(request(link["token"], "a" * 48, path, method), db)
metadata = service.prepare_visitor_upload(db, user, "upload-chat")
metadata = service.prepare_scoped_upload(db, user, "upload-chat")
assert metadata["public_chat_visitor"] == user.public_chat_visitor
assert public_session_matches(db.get(ChatSession, "upload-chat"), user)
with pytest.raises(HTTPException):
service.prepare_visitor_upload(db, other, "upload-chat")
service.prepare_scoped_upload(db, other, "upload-chat")
with pytest.raises(HTTPException):
service.prepare_visitor_upload(db, user, "another", folder_id="folder")
service.prepare_scoped_upload(db, user, "another", folder_id="folder")
file = Artifact(artifact_id="uploaded", user_id="owner", chat_id="upload-chat",
filename="hello.txt", mime_type="text/plain", storage_key="hello",
type="document", title="Hello", size_bytes=5, extra_data=metadata)
Expand Down
Loading
Loading