Files
hermes-agent/tests/plugins/test_plugin_storage.py
kshitijk4poor 8f239226bc fix(plugins): plugin writes refuse a deleted or renamed profile home
A session still bound to profile b survives `hermes profile delete b` and
`hermes profile rename b c`. Its next plugin write recreated profiles/b
through a raw mkdir, and nothing raised.

Seven plugin state writers now create their directories through
mkdir_under_hermes_home, which refuses a missing or tombstoned named home
with its existing FileNotFoundError: plugin_data_dir (and the
hermes-achievements fallback that duplicates it), the CLI plugins dir,
the update-check markers, disk-cleanup state, the Discord recovery ledger,
the Weixin account dir and the A2A audit log.

Callers that already catch keep skipping the write (Discord recovery,
the update-check writers, the disk-cleanup session-end hook and audit log,
the A2A audit). The disk-cleanup slash commands reply that the profile no
longer exists instead of raising. plugin_data_dir documents the refusal.
2026-11-22 02:17:23 +05:30

79 lines
2.7 KiB
Python

"""Tests for the per-plugin durable storage convention (plugins/plugin_storage).
The contract under test: data lives under ``<hermes home>/plugin-data/<name>/``
(NOT the ``plugins/<name>/`` install tree), names that could escape the root
are rejected, and the sqlite helper opens a WAL-mode connection inside the
data dir.
"""
from __future__ import annotations
import pytest
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from plugins.plugin_storage import plugin_data_dir, plugin_db
from tests.fixtures.dead_profile_home import MOVES, dead_named_home
@pytest.fixture
def hermes_home(tmp_path):
token = set_hermes_home_override(str(tmp_path))
try:
yield tmp_path
finally:
reset_hermes_home_override(token)
def test_data_dir_lives_outside_the_install_tree(hermes_home):
root = plugin_data_dir("my-plugin")
assert root == hermes_home / "plugin-data" / "my-plugin"
assert root.is_dir()
# The invariant that motivated the module: data must not live under the
# install tree that `hermes plugins remove` deletes.
assert (hermes_home / "plugins") not in root.parents
@pytest.mark.parametrize("bad", ["", ".", "..", "../escape", "a/b", "a\\b", "x" * 65])
def test_hostile_names_are_rejected(hermes_home, bad):
with pytest.raises(ValueError):
plugin_data_dir(bad)
def test_plugin_db_journal_mode_is_the_shared_fallback_verdict(hermes_home):
"""Plugin DBs take the journal mode the core WAL helper decides for this SQLite build and
filesystem (WAL normally; DELETE on WAL-reset-bug builds or network FS) — never a raw PRAGMA."""
from sqlite_support import is_sqlite_wal_reset_vulnerable
conn = plugin_db("board")
try:
conn.execute("CREATE TABLE t (x)")
conn.execute("INSERT INTO t VALUES (1)")
conn.commit()
mode = conn.execute("PRAGMA journal_mode").fetchone()[0]
assert mode == ("delete" if is_sqlite_wal_reset_vulnerable() else "wal")
finally:
conn.close()
assert (hermes_home / "plugin-data" / "board" / "data.db").exists()
def test_plugin_db_rejects_path_shaped_filenames(hermes_home):
with pytest.raises(ValueError):
plugin_db("board", filename="../outside.db")
with pytest.raises(ValueError):
plugin_db("board", filename="")
@pytest.mark.parametrize("move", MOVES)
def test_data_dir_refuses_a_deleted_or_renamed_profile_home(tmp_path, move):
home = dead_named_home(tmp_path, move)
token = set_hermes_home_override(str(home))
try:
with pytest.raises(FileNotFoundError, match="Named profile home does not exist"):
plugin_data_dir("my-plugin")
finally:
reset_hermes_home_override(token)
assert not home.exists()