From 0ebf8ce7b25eabb9c87e72627ffc69ff8ba01e05 Mon Sep 17 00:00:00 2001 From: Aling33 <152494711+Aling33@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:45:33 +0800 Subject: [PATCH] fix(gateway): refuse artifact share imports where the writer is unavailable (#8402) * fix(gateway): refuse artifact share imports where the writer is unavailable The descriptor-confined import writer needs O_DIRECTORY, O_NOFOLLOW and dir_fd, so a non-POSIX Gateway answered POST /api/shares/{token}/fork with a 500 AttributeError after it had already created the destination thread and rolled it back. Capture already refuses the same artifacts with 422, so the import path now returns that contract before any workspace state exists. * fix(gateway): refuse artifact share imports where the writer is unavailable The descriptor-confined import writer needs O_DIRECTORY, O_NOFOLLOW and dir_fd, so a non-POSIX Gateway answered POST /api/shares/{token}/fork with a 500 AttributeError after it had already created the destination thread and rolled it back. Capture already refuses the same artifacts with 422, so the import path now returns that contract before any workspace state exists. * fix(gateway): refuse artifact share imports where the writer is unavailable The descriptor-confined import writer needs O_DIRECTORY, O_NOFOLLOW and dir_fd, so a non-POSIX Gateway answered POST /api/shares/{token}/fork with a 500 AttributeError after it had already created the destination thread and rolled it back. Capture already refuses the same artifacts with 422, so the import path now returns that contract before any workspace state exists, and the sharing guide states the requirement next to the endpoint. --- backend/app/gateway/share_fork.py | 8 ++++++++ backend/tests/test_shares_router.py | 18 ++++++++++++++++++ docs/guide/sharing.md | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/backend/app/gateway/share_fork.py b/backend/app/gateway/share_fork.py index 9d57cd218c..0ee5148c01 100644 --- a/backend/app/gateway/share_fork.py +++ b/backend/app/gateway/share_fork.py @@ -6,6 +6,7 @@ import os import uuid from contextlib import asynccontextmanager +from deerflow_extension_api import AgentRunError from fastapi import HTTPException, Request from langchain_core.messages import AIMessage, HumanMessage from langgraph.types import Overwrite @@ -38,6 +39,11 @@ logger = logging.getLogger(__name__) SHARE_IMPORT_COMPLETE_KEY = "share_import_complete" SHARE_IMPORT_HISTORY_MARKED_KEY = "share_import_history_marked" +# The descriptor-confined writer below is POSIX-only. Capture already refuses +# the same artifacts off POSIX, so import answers with that contract instead of +# surfacing a 500 from a partial write. +_ARTIFACT_IMPORT_SUPPORTED = os.name == "posix" + def _write_fork_files(owner: str, thread_id: str, artifacts: list[dict]) -> None: """A fresh UUID directory, descriptor-confined descendants and exclusive files.""" @@ -88,6 +94,8 @@ async def fork_share(request: Request, repo: ThreadShareRepository, token: str, share = await repo.get_public(token) if share is None: raise HTTPException(404, "Share unavailable") + if share["artifacts"] and not _ARTIFACT_IMPORT_SUPPORTED: + raise AgentRunError(422, "Importing a share with artifacts requires POSIX") owner = get_effective_user_id() thread_id = str(uuid.uuid5(uuid.NAMESPACE_URL, f"deerflow-share:{owner}:{share['share_id']}:{request_id}")) threads = get_thread_store(request) diff --git a/backend/tests/test_shares_router.py b/backend/tests/test_shares_router.py index b5f28fc9f0..db521c183d 100644 --- a/backend/tests/test_shares_router.py +++ b/backend/tests/test_shares_router.py @@ -612,6 +612,24 @@ async def test_imported_nested_files_have_sandbox_write_permissions(share_app): assert stat.S_IMODE(directory.stat().st_mode) == 0o777 +async def test_artifact_import_off_posix_is_refused_before_any_state(share_app, monkeypatch): + ctx = share_app + share = await make_share(ctx) + snapshot = await ctx.client.get(f"/api/shares/{share['token']}") + assert snapshot.json()["artifacts"] + url = f"/api/shares/{share['token']}/fork" + request_id = str(uuid4()) + monkeypatch.setattr(share_fork, "_ARTIFACT_IMPORT_SUPPORTED", False) + refused = await ctx.client.post(url, json={"request_id": request_id}, headers=headers("bob")) + assert refused.status_code == 422, refused.text + assert refused.json()["detail"] == "Importing a share with artifacts requires POSIX" + # The refusal precedes any workspace state, so the same request can succeed + # once the descriptor-confined writer is available again. + monkeypatch.setattr(share_fork, "_ARTIFACT_IMPORT_SUPPORTED", True) + retried = await ctx.client.post(url, json={"request_id": request_id}, headers=headers("bob")) + assert retried.status_code == 200, retried.text + + async def test_unsafe_artifact_fails_without_publishing_partial_share(share_app): ctx = share_app ctx.output.unlink() diff --git a/docs/guide/sharing.md b/docs/guide/sharing.md index 29ab57f74b..d3e7fe0467 100644 --- a/docs/guide/sharing.md +++ b/docs/guide/sharing.md @@ -68,7 +68,7 @@ All paths below are relative to `/api`. | `DELETE /threads/{id}/shares/{share_id}` | Owner with `threads:write`; revokes access and removes stored snapshot bytes. | | `GET /shares/{token}` | Anonymous capability read; returns visible messages, artifact manifest, checkpoint and sequence. | | `GET`, `HEAD /shares/{token}/artifacts/{path}` | Reads only a copied manifest entry. Supports byte ranges and `?download=true`. | -| `POST /shares/{token}/fork` | Signed-in visitor with `threads:write`; body contains a UUID `request_id`. Returns the new `thread_id`. Retrying the same request reuses a completed import. | +| `POST /shares/{token}/fork` | Signed-in visitor with `threads:write`; body contains a UUID `request_id`. Returns the new `thread_id`. Retrying the same request reuses a completed import. Importing a share that carries artifacts requires a POSIX host; other hosts answer `422` before creating any state. | Creation captures the latest idle root checkpoint and its persisted visible history under exclusive thread admission. `checkpoint_id`, when supplied, is