Skip to content

Commit 7eb8a39

Browse files
committed
fix(security): a write stores the domain its scope check resolved (GHSA-7848-qh48-mx2w)
Review 1 of #969, B1: ingest_prd and ingest_document checked the resolved domain and stored the raw argument. require_writable_domain now returns the domain to store and both handlers keep it. 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 3b7f417 commit 7eb8a39

5 files changed

Lines changed: 94 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ adheres to [Semantic Versioning](https://semver.org/).
1414
the session's (`CLAUDE_PROJECT_DIR`, else the working directory), unless the user
1515
allows it locally with `CORTEX_CROSS_PROJECT_WRITES=<domain>,…` or `*`. Shared
1616
domains that name no repository stay writable. No tool argument can reopen it.
17+
A write stores the domain the check resolved, never the raw argument: `ingest_prd`
18+
and `ingest_document` kept a name that resolves to the session's project but can
19+
also be another repository family's domain.
1720
A global memory reaches every project, so an explicit `is_global=true` (in
1821
`remember` or `anchor`) needs `CORTEX_CROSS_PROJECT_WRITES=*`; a global scope the
1922
detector infers without it is kept local and reported (`global_reason`). `anchor`,

‎mcp_server/handlers/_write_scope.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,19 @@ def _refused_domain(domain: str, environ: Mapping[str, str]) -> tuple[str, str]
5959

6060
def require_writable_domain(
6161
domain: str, tool: str, environ: Mapping[str, str] = os.environ
62-
) -> None:
63-
"""Raise ValidationError when `domain` is another project's than the session's."""
62+
) -> str:
63+
"""The domain the write must store; ValidationError when it is another project's.
64+
65+
The check reads the resolved domain, so the caller stores that one, never the
66+
raw argument: a name that resolves to the session's own project (a directory
67+
name, a family member) can also be another family's canonical, and rows stored
68+
under the raw name would reach that family's hot memories. A name that resolves
69+
to no known project is a shared domain and is returned as given.
70+
"""
6471
refused = _refused_domain(domain, environ)
6572
if refused is None:
66-
return
73+
target = resolve_explicit_domain(domain or "")
74+
return target if target in project_domains() else (domain or "")
6775
target, session = refused
6876
where = f"this session's project ({session!r})" if session else "no known project"
6977
raise ValidationError(

‎mcp_server/handlers/ingest_document.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ async def handler(args: dict[str, Any] | None = None) -> dict[str, Any]:
193193
source=str(path), version=content_version(raw), source_kind=kind
194194
)
195195
domain = (str(args.get("domain") or "").strip()) or "documents"
196-
require_writable_domain(domain, "ingest_document") # GHSA-7848-qh48-mx2w
196+
# GHSA-7848-qh48-mx2w: the rows keep the domain the check resolved.
197+
domain = require_writable_domain(domain, "ingest_document")
197198
return await _persist(path, parsed, prov, domain)
198199

199200

‎mcp_server/handlers/ingest_prd.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ async def handler(args: dict[str, Any] | None = None) -> dict[str, Any]:
323323

324324
title = _extract_title(text, args.get("title"))
325325
domain = (args.get("domain") or "prd").strip() or "prd"
326-
require_writable_domain(domain, "ingest_prd") # GHSA-7848-qh48-mx2w
326+
# GHSA-7848-qh48-mx2w: the rows keep the domain the check resolved.
327+
domain = require_writable_domain(domain, "ingest_prd")
327328
directory = str(Path(args.get("path") or ".").expanduser().resolve().parent)
328329

329330
# 1. Write the spec page.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""A write stores the domain its scope check resolved (GHSA-7848-qh48-mx2w).
2+
3+
Review 1 of PR #969, B1: `ingest_prd` and `ingest_document` checked the resolved
4+
domain and stored the raw argument. `_group_repos` can register a directory name as
5+
an alias of a project whose canonical differs while that same name is another
6+
family's canonical: rows stored under the raw name reach that family's hot memories.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import pytest
12+
13+
from mcp_server.handlers import _write_scope
14+
from mcp_server.shared import domain_mapping
15+
from tests_py.handlers.test_ghsa_7848_cross_project_write import (
16+
projects as _projects,
17+
)
18+
19+
projects = _projects # the fixture, registered in this module under its name
20+
21+
22+
@pytest.fixture
23+
def aliased(projects, monkeypatch):
24+
"""`family` is a name that resolves to the session's project `beta`."""
25+
real = domain_mapping.resolve_explicit_domain
26+
alias = lambda name: "beta" if name.strip().lower() == "family" else real(name) # noqa: E731
27+
monkeypatch.setattr(_write_scope, "resolve_explicit_domain", alias)
28+
return projects
29+
30+
31+
def test_the_check_returns_the_domain_it_resolved(aliased):
32+
assert _write_scope.require_writable_domain("family", "t") == "beta"
33+
assert _write_scope.require_writable_domain(" Beta ", "t") == "beta"
34+
# A shared domain names no project: it is stored as given.
35+
assert _write_scope.require_writable_domain("Team-Notes", "t") == "Team-Notes"
36+
assert _write_scope.require_writable_domain("", "t") == ""
37+
assert _write_scope.require_writable_domain(None, "t") == ""
38+
39+
40+
@pytest.mark.asyncio
41+
async def test_ingest_prd_stores_the_resolved_domain_never_the_alias(
42+
aliased, monkeypatch
43+
):
44+
from mcp_server.handlers import ingest_prd
45+
46+
rows = []
47+
48+
class Store:
49+
def insert_memory(self, record):
50+
rows.append(record)
51+
return len(rows)
52+
53+
monkeypatch.setattr(ingest_prd, "write_page", lambda *a, **k: None)
54+
monkeypatch.setattr(ingest_prd, "_get_store", lambda: Store())
55+
text = "# Plan\n\n## Decisions\n\n- keep it\n\n## Requirements\n\n- ship it\n"
56+
await ingest_prd.handler({"content": text, "domain": "family"})
57+
assert rows and {row["domain"] for row in rows} == {"beta"}
58+
59+
60+
@pytest.mark.asyncio
61+
async def test_ingest_document_stores_the_resolved_domain_never_the_alias(
62+
aliased, tmp_path, monkeypatch
63+
):
64+
from mcp_server.handlers import ingest_document
65+
66+
seen = []
67+
68+
async def persist(path, parsed, prov, domain):
69+
seen.append(domain)
70+
return {"ingested": True}
71+
72+
monkeypatch.setattr(ingest_document, "_persist", persist)
73+
page = tmp_path / "export.html"
74+
page.write_text("<h1>Onboarding</h1><p>welcome</p>", encoding="utf-8")
75+
await ingest_document.handler({"path": str(page), "domain": "family"})
76+
assert seen == ["beta"]

0 commit comments

Comments
 (0)