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): anchor inbound lease root at the resolved Avibe home

qiqi7557a1e81be1
diff --git a/config/paths.py b/config/paths.py
index ebc8cf51c..255cc01b9 100644
--- a/config/paths.py
+++ b/config/paths.py
@@ -13,6 +13,22 @@ def _expand_path(value: str | Path) -> Path:
return Path(value).expanduser().resolve()
+def physical_home(home: Path | str) -> Path:
+ """Resolve an Avibe home to the path a no-follow walk can traverse.
+
+ The home is a trust anchor, not an attack surface. Supported layouts reach
+ it through symlinks that Avibe or the operating system created: a
+ ``/home/<user>`` entry pointing at another volume, or the legacy
+ ``~/.vibe_remote`` back-symlink. A per-component ``O_NOFOLLOW`` walk that
+ starts at ``/`` rejects exactly those layouts, because Linux reports
+ ``ENOTDIR`` when ``O_NOFOLLOW | O_DIRECTORY`` meets a symlink. Callers
+ resolve the home once here and keep ``O_NOFOLLOW`` for every component
+ Avibe owns below it, where a planted symlink would be an actual escape.
+ """
+
+ return Path(os.path.realpath(os.path.expanduser(os.fspath(home))))
+
+
def _default_avibe_dir(home: Path | None = None) -> Path:
return (home or Path.home()) / AVIBE_HOME_DIRNAME
diff --git a/core/handlers/inbound_attachments.py b/core/handlers/inbound_attachments.py
index 132461206..1a2293bdb 100644
--- a/core/handlers/inbound_attachments.py
+++ b/core/handlers/inbound_attachments.py
@@ -128,11 +128,17 @@ class InboundAttachmentMaterializer:
) -> None:
home = paths.get_vibe_remote_dir() if effective_home is None else effective_home
self._home = Path(os.path.abspath(os.path.expanduser(os.fspath(home))))
- self._root = (
- self._home / "attachments" / "im"
- if attachments_root is None
- else Path(attachments_root) / "im"
- )
+ # The anchor is trusted and resolved once; every component below it is
+ # Avibe-owned and stays under a per-component no-follow walk.
+ if attachments_root is None:
+ anchor = paths.physical_home(self._home)
+ self._owned_parts = ("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")
+ self._anchor = anchor
+ self._root = anchor.joinpath(*self._owned_parts)
async def materialize(
self,
@@ -144,7 +150,7 @@ class InboundAttachmentMaterializer:
max_concurrency: int = 1,
language: str = "en",
) -> MaterializedAttachmentBatch:
- root_fd = _open_or_create_private_directory(self._root)
+ root_fd = _open_or_create_private_directory(self._anchor, self._owned_parts)
lease_id = secrets.token_hex(16)
lease_dir = self._root / lease_id
lease_fd: int | None = None
@@ -571,14 +577,27 @@ def _remove_lease_directory(state: _LeaseState) -> None:
os.close(state.root_fd)
-def _open_or_create_private_directory(directory: Path) -> int:
- """Create a private root while refusing symlinks in every path component."""
+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.
+ """
- if not directory.is_absolute():
+ if not anchor.is_absolute():
raise RuntimeError("attachment lease root must be absolute")
- descriptor = os.open(directory.anchor, _directory_open_flags())
+ if not owned_parts or any(part in {"", ".", ".."} for part in owned_parts):
+ raise RuntimeError("attachment lease root must name owned components")
+ try:
+ descriptor = os.open(anchor, _directory_open_flags())
+ except FileNotFoundError:
+ os.makedirs(anchor, mode=0o700, exist_ok=True)
+ descriptor = os.open(anchor, _directory_open_flags())
try:
- for component in directory.parts[1:]:
+ for component in owned_parts:
try:
next_descriptor = os.open(
component,
diff --git a/tests/test_inbound_attachments.py b/tests/test_inbound_attachments.py
index 809bfb34a..0016dc5a9 100644
--- a/tests/test_inbound_attachments.py
+++ b/tests/test_inbound_attachments.py
@@ -221,13 +221,19 @@ async def test_materializer_preserves_legacy_two_argument_path_clients(tmp_path:
@pytest.mark.asyncio
-async def test_materializer_rejects_symlinked_attachment_root(tmp_path: Path) -> None:
[email protected]("owned_component", ["attachments", "im"])
+async def test_materializer_rejects_symlinked_attachment_root(
+ tmp_path: Path,
+ owned_component: str,
+) -> None:
+ """Every component Avibe owns below the home stays a no-follow boundary."""
+
home = tmp_path / "avibe-home"
- attachments = home / "attachments"
outside = tmp_path / "outside"
- attachments.mkdir(parents=True)
outside.mkdir()
- (attachments / "im").symlink_to(outside, target_is_directory=True)
+ planted = home / "attachments" if owned_component == "attachments" else home / "attachments" / "im"
+ planted.parent.mkdir(parents=True, exist_ok=True)
+ planted.symlink_to(outside, target_is_directory=True)
context = MessageContext(
user_id="U1",
channel_id="D1",
@@ -244,6 +250,70 @@ async def test_materializer_rejects_symlinked_attachment_root(tmp_path: Path) ->
assert list(outside.iterdir()) == []
+async def test_materializer_traverses_a_symlinked_parent_above_the_home(
+ tmp_path: Path,
+) -> None:
+ """A home reached through symlinked parents is a layout, not an escape.
+
+ Installs where ``/home/<user>`` points at another volume, and the legacy
+ ``~/.vibe_remote`` back-symlink, both reach the Avibe home through a
+ symlink the operator owns. Linux answers ``O_NOFOLLOW | O_DIRECTORY`` on a
+ symlink with ``ENOTDIR``, so a walk that starts at ``/`` used to fail on
+ the symlinked component before reaching anything Avibe owns.
+ """
+
+ 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"
+ client = _StubClient({"notes.txt": b"notes"})
+ 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).materialize(
+ context,
+ client,
+ )
+
+ assert batch.errors == ()
+ path = Path(batch.attachments[0].local_path or "")
+ assert path.read_bytes() == b"notes"
+ assert path.is_relative_to(physical / ".avibe" / "attachments" / "im")
+ batch.lease.release()
+
+
+async def test_materializer_traverses_a_symlinked_parent_for_a_declared_root(
+ tmp_path: Path,
+) -> None:
+ """The declared ``attachments_root`` carries the same anchor rule."""
+
+ physical = tmp_path / "volume" / "user"
+ physical.mkdir(parents=True)
+ (tmp_path / "home").symlink_to(tmp_path / "volume", target_is_directory=True)
+ attachments_root = tmp_path / "home" / "user" / ".avibe" / "attachments"
+ context = MessageContext(
+ user_id="U1",
+ channel_id="D1",
+ platform="slack",
+ files=[FileAttachment("notes.txt", "text/plain", url="ref", size=5)],
+ )
+
+ batch = await InboundAttachmentMaterializer(
+ attachments_root=attachments_root,
+ ).materialize(context, _StubClient({"notes.txt": b"notes"}))
+
+ assert batch.errors == ()
+ path = Path(batch.attachments[0].local_path or "")
+ assert path.is_relative_to(physical / ".avibe" / "attachments" / "im")
+ batch.lease.release()
+
+
@pytest.mark.asyncio
async def test_materializer_keeps_download_writes_on_anchored_descriptor(
tmp_path: Path,