Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(attachments): keep nested declared-root parents in the no-follow walk

qiqi2442beeaa1e4
diff --git a/core/handlers/inbound_attachments.py b/core/handlers/inbound_attachments.py
index 1a2293bdb..3e2804fa3 100644
--- a/core/handlers/inbound_attachments.py
+++ b/core/handlers/inbound_attachments.py
@@ -130,15 +130,28 @@ class InboundAttachmentMaterializer:
self._home = Path(os.path.abspath(os.path.expanduser(os.fspath(home))))
# The anchor is trusted and resolved once; every component below it is
# Avibe-owned and stays under a per-component no-follow walk.
+ anchor = paths.physical_home(self._home)
if attachments_root is None:
- anchor = paths.physical_home(self._home)
- self._owned_parts = ("attachments", "im")
+ owned_parts: tuple[str, ...] = ("attachments", "im")
else:
declared_root = Path(os.path.abspath(os.path.expanduser(os.fspath(attachments_root))))
- anchor = paths.physical_home(declared_root.parent)
- self._owned_parts = (declared_root.name, "im")
+ relative_parts = _owned_parts_below(anchor, self._home, declared_root)
+ if relative_parts is None:
+ # A declared root outside the home is its own anchor, on the same
+ # rule as the home: the path reaching it is operator config, and
+ # Avibe only owns what it creates underneath.
+ anchor = paths.physical_home(declared_root)
+ owned_parts = ("im",)
+ else:
+ # Every component between the home and a declared root inside it
+ # is Avibe-owned space, so all of them stay in the no-follow walk
+ # instead of being resolved: a symlink planted at an intermediate
+ # component such as ``<home>/custom`` must not redirect leases out
+ # of the tree.
+ owned_parts = (*relative_parts, "im")
self._anchor = anchor
- self._root = anchor.joinpath(*self._owned_parts)
+ self._owned_parts = owned_parts
+ self._root = anchor.joinpath(*owned_parts)
async def materialize(
self,
@@ -577,14 +590,34 @@ def _remove_lease_directory(state: _LeaseState) -> None:
os.close(state.root_fd)
+def _owned_parts_below(
+ anchor: Path,
+ home: Path,
+ declared_root: Path,
+) -> tuple[str, ...] | None:
+ """Components from the home down to ``declared_root``, or ``None`` if outside.
+
+ A declared root under the home is accepted in either spelling: as written
+ against the logical home, or already resolved against the physical one. The
+ returned components are relative to the anchor either way, so the caller can
+ keep every one of them inside the ``O_NOFOLLOW`` walk.
+ """
+
+ for base in (home, anchor):
+ if declared_root.is_relative_to(base):
+ return declared_root.relative_to(base).parts
+ return None
+
+
def _open_or_create_private_directory(anchor: Path, owned_parts: tuple[str, ...]) -> int:
"""Create a private root while refusing symlinks in every owned component.
- ``anchor`` is the already-resolved Avibe home: the operator owns the path
- that reaches it, so it is opened in one step and its own symlinked parents
- stay legal. ``owned_parts`` are the components Avibe creates underneath,
- and each one is opened with ``O_NOFOLLOW`` so a planted symlink cannot
- redirect a lease outside Avibe-owned storage.
+ ``anchor`` is the already-resolved trust anchor -- the Avibe home, or a
+ declared root the operator put outside it: the operator owns the path that
+ reaches it, so it is opened in one step and its own symlinked parents stay
+ legal. ``owned_parts`` are every component Avibe creates underneath, and
+ each one is opened with ``O_NOFOLLOW`` so a planted symlink cannot redirect
+ a lease outside Avibe-owned storage.
"""
if not anchor.is_absolute():
diff --git a/tests/test_inbound_attachments.py b/tests/test_inbound_attachments.py
index 0016dc5a9..db92900ac 100644
--- a/tests/test_inbound_attachments.py
+++ b/tests/test_inbound_attachments.py
@@ -314,6 +314,68 @@ async def test_materializer_traverses_a_symlinked_parent_for_a_declared_root(
batch.lease.release()
+async def test_materializer_rejects_symlinked_parent_inside_a_nested_declared_root(
+ tmp_path: Path,
+) -> None:
+ """Intermediate components of a nested declared root stay no-follow.
+
+ A declared root such as ``<home>/custom/downloads`` reaches it through
+ ``custom``, which is Avibe-owned space below the trusted home. Resolving the
+ declared root's parents would follow a symlink planted there before the
+ protected walk starts, redirecting leases outside the declared tree.
+ """
+
+ home = tmp_path / "avibe-home"
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ home.mkdir()
+ (home / "custom").symlink_to(outside, target_is_directory=True)
+ context = MessageContext(
+ user_id="U1",
+ channel_id="D1",
+ platform="slack",
+ files=[FileAttachment("notes.txt", "text/plain", url="ref", size=5)],
+ )
+
+ with pytest.raises(OSError):
+ await InboundAttachmentMaterializer(
+ effective_home=home,
+ attachments_root=home / "custom" / "downloads",
+ ).materialize(context, _StubClient({"notes.txt": b"notes"}))
+
+ assert list(outside.iterdir()) == []
+
+
+async def test_materializer_keeps_a_nested_declared_root_under_the_home(
+ tmp_path: Path,
+) -> None:
+ """A nested declared root still materializes below the resolved home."""
+
+ physical = tmp_path / "volume" / "user"
+ physical.mkdir(parents=True)
+ (tmp_path / "home").symlink_to(tmp_path / "volume", target_is_directory=True)
+ home = tmp_path / "home" / "user" / ".avibe"
+ context = MessageContext(
+ user_id="U1",
+ channel_id="D1",
+ platform="slack",
+ files=[FileAttachment("notes.txt", "text/plain", url="ref", size=5)],
+ )
+
+ batch = await InboundAttachmentMaterializer(
+ effective_home=home,
+ attachments_root=home / "custom" / "downloads",
+ ).materialize(context, _StubClient({"notes.txt": b"notes"}))
+
+ assert batch.errors == ()
+ path = Path(batch.attachments[0].local_path or "")
+ assert path.read_bytes() == b"notes"
+ assert path.is_relative_to(physical / ".avibe" / "custom" / "downloads" / "im")
+ batch.lease.release()
+
+
@pytest.mark.asyncio
async def test_materializer_keeps_download_writes_on_anchored_descriptor(
tmp_path: Path,