Skip to content

Commit a78f6e6

Browse files
committed
fix(security): a name that is itself a project's domain is that domain; the collision is tested on real repositories (GHSA-7848-qh48-mx2w)
Review 2 of #969, B2. foo-lib-a and foo-lib-b form the family foo-lib while the repository zed lives in a directory named foo-lib, so the registry maps foo-lib to zed. remember(directory=foo-lib-a) from a zed session was checked as zed and stored as foo-lib. The tests build that layout with git repositories and the real registry and fail on 7eb8a39. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_018kyZYsWnKSjpX4oNV19PCF Signed-off-by: Claude <[email protected]>
1 parent 68fb71a commit a78f6e6

3 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ adheres to [Semantic Versioning](https://semver.org/).
2020
A domain that is an identity already (resolved from a path, stored on a row) is
2121
never resolved through the name aliases a second time: where a directory name is
2222
also another family's domain, that family keeps its own writes and the aliased
23-
project cannot change its rows. A memory-replica refresh reads every replica of
23+
project cannot change its rows. A name given to `ingest_prd` or `ingest_document`
24+
that is itself another project's domain is refused, never redirected to the
25+
session's project. A memory-replica refresh reads every replica of
2426
the block, so another project's row no longer hides this session's own.
2527
A global memory reaches every project, so an explicit `is_global=true` (in
2628
`remember` or `anchor`) needs `CORTEX_CROSS_PROJECT_WRITES=*`; a global scope the

‎mcp_server/handlers/_write_scope.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ def require_writable_domain(
7373
The check reads the resolved domain, so the caller stores that one, never the
7474
raw argument: a name that resolves to the session's own project (a directory
7575
name, a family member) can also be another family's canonical, and rows stored
76-
under the raw name would reach that family's hot memories. A name that resolves
76+
under the raw name would reach that family's hot memories. A name that is itself
77+
a project's domain is that domain, whatever alias it also carries: it is refused
78+
from another project, never redirected to the session's. A name that resolves
7779
to no known project is a shared domain and is returned as given.
7880
"""
79-
target = resolve_explicit_domain(domain or "")
81+
name = (domain or "").strip().lower()
82+
target = name if name in project_domains() else resolve_explicit_domain(name)
8083
if _refused_domain(target, environ) is not None:
8184
_refuse(target, tool, environ)
8285
return target if target in project_domains() else (domain or "")

‎tests_py/handlers/test_ghsa_7848_resolved_domain.py‎

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
from __future__ import annotations
1010

11+
import subprocess
12+
1113
import pytest
1214

1315
from mcp_server.errors import ValidationError
14-
from mcp_server.handlers import _write_scope
16+
from mcp_server.handlers import _write_scope, query_methodology
17+
from mcp_server.handlers.remember import handler as remember_handler
1518
from mcp_server.shared import domain_mapping
1619
from tests_py.handlers.test_ghsa_7848_cross_project_write import (
1720
projects as _projects,
@@ -105,12 +108,90 @@ def test_the_aliased_project_cannot_change_the_familys_rows(collision):
105108
_write_scope.require_writable_canonical(" Family ", "remember")
106109

107110

108-
def test_the_alias_given_as_a_name_still_resolves_to_the_sessions_project(collision):
109-
assert _write_scope.require_writable_domain("family", "ingest_prd") == "beta"
111+
def test_a_name_that_is_another_familys_domain_is_refused_never_redirected(collision):
112+
with pytest.raises(ValidationError, match=r"^ingest_prd: domain 'family' belongs"):
113+
_write_scope.require_writable_domain(" Family ", "ingest_prd")
110114

111115

112116
def test_a_resolved_domain_of_no_known_project_stays_writable(collision):
113117
_write_scope.require_writable_canonical("team-notes", "remember")
114118
_write_scope.require_writable_canonical("", "remember")
115119
_write_scope.require_writable_canonical(None, "remember")
116120
_write_scope.require_writable_canonical("beta", "remember")
121+
122+
123+
# Review 2 of PR #969, B2: the same collision built with real git repositories and
124+
# the real registry (`_group_repos`), on the real handlers and store.
125+
@pytest.fixture
126+
def family(tmp_path, monkeypatch):
127+
"""`foo-lib-a` and `foo-lib-b` form the family `foo-lib`; the repository `zed`
128+
lives in a directory named `foo-lib`, so the registry maps `foo-lib` to `zed`."""
129+
dev = tmp_path.resolve() / "dev"
130+
for name in ("foo-lib-a", "foo-lib-b", "foo-lib"):
131+
(dev / name / "src").mkdir(parents=True)
132+
subprocess.run(["git", "init", "-q", str(dev / name)], check=True)
133+
subprocess.run(
134+
[
135+
"git",
136+
"-C",
137+
str(dev / "foo-lib"),
138+
"remote",
139+
"add",
140+
"origin",
141+
"https://example.invalid/owner/zed.git",
142+
],
143+
check=True,
144+
)
145+
monkeypatch.setenv("HOME", str(tmp_path / "home"))
146+
monkeypatch.setenv("CORTEX_DEV_ROOT", str(dev))
147+
monkeypatch.delenv(_write_scope.ALLOW_ENV, raising=False)
148+
monkeypatch.setattr(domain_mapping, "_candidate_dev_roots", lambda: [dev])
149+
domain_mapping._build_registry.cache_clear()
150+
yield dev
151+
domain_mapping._build_registry.cache_clear()
152+
153+
154+
def _hot(domain):
155+
return [m["content"] for m in query_methodology._get_hot_memories(domain, "")]
156+
157+
158+
def test_the_registry_holds_the_collision(family):
159+
assert domain_mapping.resolve_cwd(str(family / "foo-lib-a")) == "foo-lib"
160+
assert domain_mapping.resolve_cwd(str(family / "foo-lib")) == "zed"
161+
assert domain_mapping.resolve_explicit_domain("foo-lib") == "zed"
162+
assert {"foo-lib", "zed"} <= domain_mapping.project_domains()
163+
164+
165+
@pytest.mark.asyncio
166+
async def test_a_session_in_the_aliased_project_cannot_write_into_the_family(
167+
family, monkeypatch
168+
):
169+
monkeypatch.setenv(_write_scope.SESSION_ENV, str(family / "foo-lib"))
170+
marker = "GHSA-7848 collision marker"
171+
with pytest.raises(ValidationError, match=r"^remember: domain 'foo-lib' belongs"):
172+
await remember_handler(
173+
{
174+
"content": marker,
175+
"directory": str(family / "foo-lib-a" / "src"),
176+
"force": True,
177+
}
178+
)
179+
with pytest.raises(ValidationError, match=r"^ingest_prd: domain 'foo-lib' belongs"):
180+
_write_scope.require_writable_domain("foo-lib", "ingest_prd")
181+
assert _write_scope.memory_writable({"domain": "foo-lib"}) is False
182+
assert marker not in _hot("foo-lib")
183+
184+
185+
@pytest.mark.asyncio
186+
async def test_the_family_writes_in_its_own_domain(family, monkeypatch):
187+
monkeypatch.setenv(_write_scope.SESSION_ENV, str(family / "foo-lib-a"))
188+
stored = await remember_handler(
189+
{
190+
"content": "family note",
191+
"directory": str(family / "foo-lib-a"),
192+
"force": True,
193+
}
194+
)
195+
assert stored["stored"] is True
196+
assert "family note" in _hot("foo-lib")
197+
assert _write_scope.require_writable_domain("foo-lib", "ingest_prd") == "foo-lib"

0 commit comments

Comments
 (0)