fix(profiles): reject traversal-shaped profile names in get_profile_dir

A WS 'profile' param like '../../foo' normalized to a path component that
escaped the profiles root, letting a connected client bind an arbitrary
existing directory as a profile home (state.db opened there, and session
delete chains into per-id file cleanup under <dir>/sessions/).

get_profile_dir now validates the canonical name against the profile id
regex before joining it under profiles/. The regex only, not the reserved
list, so pre-reserved-list dirs like profiles/hermes keep resolving.
Callers that probe existence (profile_exists, _profile_home, the 4064
resolvers) treat ValueError as 'not found'.
This commit is contained in:
Adolanium
2026-09-11 18:39:41 +03:00
committed by Teknium
parent 6636b0896c
commit 2770f93064
6 changed files with 63 additions and 7 deletions
+14
View File
@@ -105,6 +105,20 @@ class TestGetProfileDir:
result = get_profile_dir("default")
assert result == tmp_path / ".hermes"
def test_valid_name_resolves_under_profiles_root(self, profile_env):
assert get_profile_dir("coder") == _get_profiles_root() / "coder"
@pytest.mark.parametrize("name", ["..", "../outside", "../../tmp", "a/b", "a\\b", ".hidden", "has space"])
def test_traversal_and_invalid_names_rejected(self, name, profile_env):
# The name becomes a path component under profiles/; invalid ids must
# raise instead of escaping the root.
with pytest.raises(ValueError):
get_profile_dir(name)
@pytest.mark.parametrize("name", ["..", "../outside", "a/b"])
def test_profile_exists_false_for_invalid_names(self, name, profile_env):
assert profiles.profile_exists(name) is False
# ===================================================================
# TestCreateProfile
@@ -57,3 +57,24 @@ def test_custom_root_basename_target_fails_closed_when_unavailable(tmp_path, mon
with pytest.raises(FileNotFoundError):
with server._profile_db({"profile": "customer-data"}):
pass
@pytest.mark.parametrize("name", ["..", "../outside", "../../tmp", "a/b", "a\\b", ".hidden"])
def test_profile_param_traversal_fails_closed(tmp_path, monkeypatch, name):
"""A traversal-shaped ``profile`` param must never resolve outside profiles/."""
from tui_gateway import server
home = tmp_path / ".hermes"
outside = tmp_path / "outside"
outside.mkdir(parents=True) # a real directory the traversal could land on
home.mkdir()
(home / "config.yaml").write_text("terminal:\n cwd: /launch\n")
monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setattr(server, "_hermes_home", home)
with pytest.raises(FileNotFoundError):
server._profile_home(name)
with pytest.raises(FileNotFoundError):
with server._profile_db({"profile": name}):
pass